Doja平台上实现多个纹理

 
Doja平台上实现多个纹理
作者:飘飘白云
这个程序在imode平台上实现了原生模型上使用多个纹理的效果,这一次光照效果比较明显。程序中使用的图片image2.bmp来自j2medev网站。
源程序和资源下载: 点击这里
 
import com.nttdocomo.ui.*;
import com.nttdocomo.opt.ui.j3d.*;
import javax.microedition.io.*;
//
/**
 * @author ppby 2006-01-26
 * Doja3D texture effect on primitive object
 */
public class DojaTexture extends IApplication {
              private Canvas3D              canv;
 
              public void start() {
                            try {
                                          canv = new Canvas3D();
                                          Display.setCurrent(canv);
                                          Thread thread = new Thread(canv);
                                          thread.start();
                            } catch (IllegalThreadStateException itse) {
                                          itse.printStackTrace();
                            } catch (Exception e) {
                                          e.printStackTrace();
                            }
              }
}
 
/**
 * Canvas class
 */
class Canvas3D extends Canvas implements Runnable {
             
              //quad vertexex array
              private int[] quadV =
              {
                                          -120,-120, 120,     120, -120, 120,    120, 120, 120,     -120, 120, 120, // front
                                          120, -120, -120,    -120, -120, -120, -120, 120, -120,    120, 120, -120, // back
                                -120, -120, -120,   -120, -120, 120,   -120, 120, 120,     -120, 120, -120, // left
                                120, -120, 120,    120, -120, -120,   120, 120, -120,     120, 120, 120, // right
                                -120, 120, 120,    120, 120, 120,     120, 120, -120,     -120, 120, -120, // top
                               120, -120, -120,   -120, -120, -120, -120, -120, 120,    120, -120, 120, // bottom             
              };
             
              //normals array for each vertex of quad
              private int[] quadN =
              {
                                0, 0, 4096,    0, 0, 4096,     0, 0, 4096,    0, 0, 4096,  
                      0, 0, -4096,    0, 0, -4096,   0, 0, -4096,    0, 0, -4096,   
                      -4096, 0, 0,   -4096, 0, 0,    -4096, 0, 0,   -4096, 0, 0, 
                      4096, 0, 0,    4096, 0, 0,     4096, 0, 0,    4096, 0, 0,  
                      0, 4096, 0,    0, 4096, 0,     0, 4096, 0,    0, 4096, 0,   
                      0, -4096, 0,    0, -4096, 0,   0, -4096, 0,    0, -4096, 0,
             
              };
             
              //texture array for each vertex of quad primitive
              // One image containing two textures of 96x96, the whole image is 192x96
    // the values given is the pixel values of the image.
              private int[]              quadT =
              {      
            96, 96,      192, 96,   192, 0,      96, 0,
            0, 96,      96, 96,   96, 0,      0, 0,
            0, 96,      96, 96,   95, 0,      0, 0,
            0, 96,      96, 96,   95, 0,      0, 0,
            0, 96,      96, 96,   95, 0,      0, 0,
            0, 96,      96, 96,   95, 0,      0, 0,      
              };
             
              //color array for each vertex of quad primitive
              private int[] quadC =
              {
                                          0xff << 16, //red
                                          0xff << 8, //green
                                          0xff << 4, //blue
                                          0xff << 8 | 0xff << 16, //yellow
                                          0xff << 16 | 0xff << 4,
                                          0xff << 8 | 0xff << 4,
              };
              //quad primitive array
              private PrimitiveArray quadPrim = null;
             
              Graphics g = null;
             
              // 3D affine transformation matrix
              private AffineTrans              viewT                            = new AffineTrans();
              private AffineTrans affineT               = new AffineTrans();
              private AffineTrans              rotateT                          = new AffineTrans();
              private int                     rotX, rotY;                    // Variable for rotation
    private int               nAmLight = 2048 ;//ambient light intensity
   
    private Texture quadTexture;   // texture to use for the primitive figure.
 
              public Canvas3D() {
                            g = this.getGraphics();
                           
                            try {
                                          // Set a camera:(eye position,look at position,up direction)
                                          viewT.lookAt(
                                                                      new Vector3D(0, 0, 800),
                                                                      new Vector3D(0, 0, 200),
                                                                      new Vector3D(0, 4096, 0)
                                          );
                                         
                                          //setup quad primitive data
            setupPrim();
 
            // load the texture to use for the primitive.
            quadTexture = new Texture(Connector.openDataInputStream("resource:///image2.bmp"), false);
                                         
                            } catch (Exception e) {
                                          System.out.println("Init data failed");
                                          e.printStackTrace();
                            }
              }
 
             
              public void run() {
                            while (true) {
                                          updateAffineTrans();
                                          render();
                                          try {
                                                        Thread.sleep(100); // wait 100ms
                                          } catch (InterruptedException ie) {
                                                        System.out.println("Run error~~");
                                          }
                            }
              }
             
              public void setupPrim()
              {
                  quadPrim = new PrimitiveArray(
                                    Graphics3D.PRIMITIVE_QUADS,
                                    Graphics3D.NORMAL_PER_VERTEX
                                    |Graphics3D.TEXTURE_COORD_PER_VERTEX
                                    |Graphics3D.COLOR_NONE                                          //no color for each face
                                    |Graphics3D.ATTR_LIGHT,                                          //enable light
                                    6
        );
                 
                          //set quad primitive data
                          int[] primVer = quadPrim.getVertexArray();
                          int[] primTex = quadPrim.getTextureCoordArray();
                          int[] primNor = quadPrim.getNormalArray();
                                       
                           arrayCopy(primVer,quadV);
                          arrayCopy(primNor,quadN);
                          arrayCopy(primTex,quadT);
             
                          System.out.println("ver " + primVer.length + " tex " + primTex.length + " Nor " + primNor.length);
              }
 
              public void updateAffineTrans()
              {
                            affineT.setIdentity();
 
                            //rotate y axis
                            rotateT.setIdentity();
                            rotateT.setRotateY(rotY);
                            affineT.mul(rotateT);
                           
                            //rotate x axis
                            rotateT.setIdentity();
                            rotateT.setRotateX(rotX);
                            affineT.mul(rotateT,affineT);
              }
             
              public void arrayCopy(int[] dist,int[] src)
              {
                            for(int i = 0;i < dist.length;i++)
                            {
                                          dist[i] = src[i];
                            }
              }
             
              public void render()
              {
                            try {
                                          g.lock();
                                          // Background Clear
                                          g.setColor(Graphics.getColorOfRGB(0, 0, 0));
                                          g.fillRect(0, 0, getWidth(), getHeight());
 
                                          Graphics3D g3d = (Graphics3D) g;
                                         
                                          g3d.setClipRect3D(0, 0, getWidth(),getHeight());
                                          g3d.setScreenScale(4096/2, 4096/2);              
                                          g3d.setScreenCenter(getWidth()>>1, getHeight()>>1);
                                          g3d.setPerspective(200, 10000, 4096*50/360);
                                         
                                          //set light
                                          g3d.setAmbientLight(nAmLight);   
                                          g3d.setDirectionLight(new Vector3D(1,1,0),4096/2);
                      g3d.enableLight(true); 
                                         
                                          //set viewport
                                          affineT.mul(viewT,affineT);
                                          g3d.setViewTrans(affineT);                                           // Set view coordinate matrix
                           
                                          //set texture for quad primitive
                                          g3d.setPrimitiveTextureArray(quadTexture);
                                          g3d.setPrimitiveTexture(0);
                                         
                                          //render quad
                                          g3d.renderPrimitives(quadPrim, 0);
                                         
                                          g3d.flush();
                                         
                                          g.setColor(Graphics.getColorOfRGB(0, 255, 0xff));
                                          g.drawString("Rotate:up,down,left,right key.", 5, 5+ Font.getDefaultFont().getHeight());
                                          g.drawString("Light :KEY_7,KEY_9.", 5, 10+2*Font.getDefaultFont().getHeight());
 
                                          g.unlock(true);
                            } catch (Exception e) {
                                          System.out.println("Paint() err");
                                          e.printStackTrace();
                            }
              }
 
              /**
               * Key event process.
               */
              public void processEvent(int type, int param) {
                            try {
                                          if (type == Display.KEY_PRESSED_EVENT) {
                                                        switch (param) {
                                                                      //rotate
                                                        case Display.KEY_UP:
                                                                      rotX -= 128;
                                                                      rotX %= 4096;
                                                                      break;
                                                        case Display.KEY_DOWN:
                                                                      rotX += 128;
                                                                      rotX %= 4096;
                                                                      break;
                                                        case Display.KEY_LEFT:
                                                                      rotY -= 128;
                                                                      rotY %= 4096;
                                                                      break;
                                                        case Display.KEY_RIGHT:
                                                                      rotY += 128;
                                                                      rotY %= 4096;
                                                                      break;
                                                                     
                                                                      //light
                                                        case Display.KEY_7:
                                                                      nAmLight -= 512;
                                                                      nAmLight %= 4096;
                                                                      nAmLight = (nAmLight<0? 1:nAmLight);
                                                                      break;
                                                        case Display.KEY_9:
                                                                      nAmLight += 512;
                                                                      nAmLight = (nAmLight>=4096 ? 4095:nAmLight);
                                                                      nAmLight %= 4096;
                                                                      break;
                                                        }
                                          }
                            } catch (Exception e) {
                                          System.out.println("Set attribute value err");
                                          e.printStackTrace();
                            }
              }
             
              public void paint(Graphics _g) {
              }
}

你可能感兴趣的:(Doja平台上实现多个纹理)