⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 samplecanvas.java

📁 SEMC_Java3D_041103_final.pdf主要描述java在3d方面的开发
💻 JAVA
字号:
/*************************************************************************
 SampleApp is a demo application that shows the use of various Mascot Capsule
 Version 3 API calls to generate a 3D scene with animation. This file has
 the code for rendering a cube, applying an image as a texture map on its
 faces, and then rotating the cube. Repeated calls to SampleCanvas's
 paint() method slowly rotate the cube's position, producing an animation
 effect.Another image is drawn as a background. Note that drawImage must
 execute to produce the background before any 3D API render methods execute.
 This is because you can't mix 2D and 3D API calls.
 Based on example code from Sony Ericsson.

 IMPORTANT: This code requires use of the Mascot Capulse version 3 API
            to function.
  
 File: SampleCanvas.java
 Needs: SampleApp.java
 Resources: cubeface.bmp, backdrop.png

 Created by: Tom Thompson
 Edited By: Oscar Vivall
 
 COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2004.

 The software is the copyrighted work of Sony Ericsson Mobile Communications AB.
 The use of the software is subject to the terms of the end-user license
 agreement which accompanies or is included with the software. The software is
 provided "as is" and Sony Ericsson specifically disclaim any warranty or
 condition whatsoever regarding merchantability or fitness for a specific
 purpose, title or non-infringement. No warranty of any kind is made in
 relation to the condition, suitability, availability, accuracy, reliability,
 merchantability and/or non-infringement of the software provided herein.
 *************************************************************************/

import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;
import java.io.*;
import java.lang.*;

import com.mascotcapsule.micro3d.v3.*;

// SampleCanvas class
public class SampleCanvas extends Canvas {
	
    // Constants for graphics commands
    private static final int COMMAND = Graphics3D.PRIMITVE_TRIANGLES | 
                                       Graphics3D.PDATA_NORMAL_PER_VERTEX |
                                       Graphics3D.PDATA_TEXURE_COORD | 
                                       Graphics3D.PDATA_COLOR_NONE | 
                                       Graphics3D.ENV_ATTR_LIGHTING;
	
    // 3D and 2D elements
    private static Texture d1_texture;
    private static Image iImage;

    // Vectors for point of camera point of view
    private Vector3D D1_POS = new Vector3D(0,50,50);
    private Vector3D D1_LOOK = new Vector3D(0,-2048,-2048);
    private Vector3D D1_UP = new Vector3D(0,4096,0);
    private int D1_pers = 512;

    // AffineTrans matrices declarations
    private AffineTrans rotationXTrans;
    private AffineTrans rotationYTrans;
    private AffineTrans lookAtTrans;
    private AffineTrans trans;
   
    // Triangles that make up the cube's faces -- these are vertices, not triangle strips
    private int[] vert = { 
                10, 10, 10,   -10, 10, 10,    10,-10, 10,   -10, 10, 10,    10,-10, 10,   -10,-10, 10,   // front
                -10, 10,-10,    10, 10,-10,   -10,-10,-10,    10, 10,-10,   -10,-10,-10,    10,-10,-10,   // back
                -10, 10, 10,   -10, 10,-10,   -10,-10, 10,  -10, 10,-10,    -10,-10, 10,   -10,-10,-10,   // left
                10, 10,-10,    10, 10, 10,    10,-10,-10,    10, 10, 10,    10,-10,-10,    10,-10, 10,   // right
                10, 10,-10,   -10, 10,-10,    10, 10, 10,   -10, 10,-10,    10, 10, 10,   -10, 10, 10,   // top
                10,-10, 10,   -10,-10, 10,    10,-10,-10,   -10,-10, 10,    10,-10,-10,   -10,-10,-10 }; // bottom

    // Cube's normals -- Used in lighting calculations.
    private int[] norm = {  
                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, 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,      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,    0, -4096, 0,    0, -4096, 0,    0, -4096, 0,    0, -4096, 0};

    // Coordinates on 2D BMP image to be mapped onto the cube's faces.
    private int[] tex = {  
                96, 32,      64, 32,      96, 64,       64, 32,      96, 64,       64, 64,    // cyan
                64, 32,      32, 32,      64, 64,       32, 32,      64, 64,       32, 64,    // blue
                64, 0,       32, 0,       64, 32,       32, 0,       64, 32,       32, 32,    // yellow
                32, 0,        0, 0,       32, 32,        0, 0,       32, 32,        0, 32,    // red
                32, 32,       0, 32,      32, 64,        0, 32,      32, 64,        0, 64,    // magenta
                96, 0,       64, 0,       96, 32,       64, 0,       96, 32,       64, 32 }; // green

    private int [] kolor = {  0xFF00FF }; // Placeholder, because we're using textures to color the cube

    // Lights settings. Mascot v3 only has two lights: one directional and one ambient
    // Directional light, vector and intesity
    private Vector3D lightDirVec = new Vector3D(0,0,4096); // Light position
    private int lightDir = 3730;

    // Ambient light intensity
    private int lightAmbi = 1626;
    private Light d1_light = new Light(lightDirVec,lightDir,lightAmbi);
    private Graphics3D g3d;

    // Declare FigureLayout and Effect3D objects	
    FigureLayout d1_layout;
    Effect3D effect;
	
    // SampleCanvas constructor
    public SampleCanvas() {

        // Set up this Displayable to listen to command events
        setCommandListener(new CommandListener(){
            public void commandAction(Command c, Displayable d) {
                    if (c.getCommandType() == Command.EXIT) {
                        // exit the MIDlet
                        SampleApp.quitApp();
                    } // end if
                } // end commandAction
            }
        ); // end setCommandListener;
        
        init();    // Initialize the 3D objects
        
    } // end constructor

    public void init() {

        // Load Texture and background
        try{
            d1_texture = new Texture("/res/cubeface.bmp",true);
            iImage = Image.createImage("/res/backdrop.png");
        } catch (Exception e) {e.printStackTrace();}

        //  Effects set up
        effect = new Effect3D();
        effect.setShadingType(Effect3D.NORMAL_SHADING);
        effect.setLight(d1_light);

        // Instantiate 3D graphics object		
        g3d = new Graphics3D();

        // Set up Figure's (cube) layout
        d1_layout = new FigureLayout();
        d1_layout.setPerspective(10,4096,D1_pers);
        d1_layout.setCenter(getWidth()/2, getHeight()/2);

        // Set up camera and rotation transform
        trans = new AffineTrans();
        trans.setIdentity();

        lookAtTrans = new AffineTrans();
        lookAtTrans.setIdentity();
        lookAtTrans.lookAt(D1_POS, D1_LOOK, D1_UP);
        trans.mul(lookAtTrans);

        rotationXTrans = new AffineTrans();
        rotationXTrans.setIdentity();
        rotationYTrans = new AffineTrans();
        rotationYTrans.setIdentity();

        // Add the Exit command
        addCommand(new Command("Exit", Command.EXIT, 1));
	} // end init

    // Paint the screen
    public void paint( Graphics g ) {
        // Set up background and draw it. Do it now and not with the 3D calls!
        g.setColor(0,0,100);
        g.fillRect(0,0,getWidth(),getHeight());

        g.drawImage(iImage, getWidth()/2, 0, Graphics.TOP | Graphics.HCENTER);
        System.gc();

        // Do the rotation, 4096 = 360 degrees
        rotationXTrans.rotationX(32);
        rotationYTrans.rotationY(32);

        trans.mul(rotationYTrans);
        trans.mul(rotationXTrans);

        d1_layout.setAffineTrans(trans);

        // Draw the scene           
        try{
            g3d.bind(g);
            g3d.renderPrimitives(d1_texture, 0, 0, d1_layout, effect, COMMAND, 12, vert, norm, tex, kolor);
            g3d.flush();
        } catch (Throwable h){
            h.printStackTrace();
        }finally{
            g3d.release( g );			
        } // end catch

    } // end paint()
        
} // end SampleCanvas class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -