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

📄 my3d001_canvas.java

📁 实现了一个简单而经典的3D游戏
💻 JAVA
字号:
package packet3d004;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.*;//use 3g
import javax.microedition.lcdui.*;//use g
public class My3D001_Canvas extends GameCanvas implements Runnable,CommandListener {
	private My3D_MIDlet mm;
	private Graphics3D g3d=Graphics3D.getInstance();
	private Graphics g=this.getGraphics();
    private World world;
    private Camera camera;
    private Mesh mesh;
    private boolean running=false;
    private LoadImage li;
    
    private Light light;
    private Mesh mesh1;
    private Mesh mesh2;
    private Mesh mesh3;
    private Mesh mesh4;
    private Group group;
    private int startTime=0;
    private int currentTime=0;
    //private Transform tf;
    private Sprite3D sp;
    public My3D001_Canvas(My3D_MIDlet mm)
    {
    	super(true);//不响应键盘事件
    	this.mm=mm;
    	this.startTime=(int)System.currentTimeMillis();
    	
    	
    	world=new World();
    	
    	camera=new Camera();
    	
    	world.addChild(camera);
    	
    	float w=this.getWidth();
    	float h=this.getHeight();
    	
    	camera.setPerspective(60.0f,w/h,0.1f,100.0f);
    	camera.setTranslation(0.0f,0.0f,10.0f);
    	
    	mesh=createdMesh();
    	mesh.setTranslation(0.0f,0.0f,-30.0f);//进行z轴平移
    	
		
		world.addChild(mesh);
    	
    
    	world.setActiveCamera(camera);//根节点(World)是任何程序比备的,他就是一个观察者
    	
    	
    	
    	
    	mesh1=createdMesh();
    	mesh1.setTranslation(0.0f,0.0f,-40.0f);
    	mesh1.setScale(0.8f,0.8f,0.8f);
    	
    	
    	world.addChild(mesh1);
    	
    	//tf=new Transform();
    	mesh2=createdMesh();
    	//mesh2.setTranslation(0.0f,0.0f,-45.0f);
    	//mesh2.setScale(0.8f,0.8f,0.8f);
    	
    	mesh3=createdMesh();
    	//mesh3.setTranslation(0.0f,0.0f,-50.0f);
    	//mesh3.setScale(0.4f,0.4f,0.4f);
    	mesh4=createdMesh();
    	
    	group=new Group();
    	group.addChild(mesh2);
    	group.addChild(mesh3);
    	group.addChild(mesh4);
    	group.setTranslation(0.0f,0.0f,-40.0f);//节点组封装了节点的变换属性,使其一致//
    	
    	world.addChild(group);
    	
    	
    	//sp.setTranslation(0.0f,0.0f,-35.0f);
    	CompositingMode cm=new CompositingMode();
    	cm.setAlphaThreshold(0.0f);
    	cm.setAlphaWriteEnable(true);
    	cm.setColorWriteEnable(true);
    	cm.setDepthWriteEnable(true);
    	
    	Fog fog=new Fog();
    	fog.setMode(Fog.LINEAR);
    	fog.setLinear(1.0f,50f);//注意要想有理想的雾化效果(物体句Z轴的距离在此范围之内)......
    	fog.setColor(0xffffff);
    	
    	Appearance app=new Appearance();
    	app.setCompositingMode(cm);
    	app.setFog(fog);
    	
    	li=new LoadImage("/22.PNG");
    	Image image=LoadImage.getImage();
    	Image2D img2d=new Image2D(Image2D.RGB,image);
    	sp=new Sprite3D(true,img2d,app);
    	sp.setScale(1.5f,1.5f,1.5f);
    	sp.setTranslation(0.0f,0.0f,-5.0f);
    	world.addChild(sp);
    	
    	
    }
    //startRun()
    public void startRun()
    {
      running=true;
      Thread tt=new Thread(this);
      tt.start();
    }
    //stopRun()
    public void stopRun()
    {
    	running=false;
    }
    public void run()
    {
    	while(running==true)
    	{
    		
    		double temp=Math.tan((1.0f-(float)Math.cos(((System.currentTimeMillis()-startTime)/1000)*0.0005f))*0.5f);
    		
    		//sp.setTranslation(0.0f,0.0f,(float)(temp*temp*temp)*-100.0f+30.0f);
            // sp.postRotate(15.0f, 0.0f, 1.0f,0.0f); 
    		//精灵类只是一个拥有3D位置的2D图象,股旋转意义不大
    		sp.translate(0.0f,0.0f,-0.03f);
           
    		
    		group.postRotate(6.0f,0.5f,1.0f,0.3f);
    		group.translate(0.04f,-0.06f,0.08f);
    		//
    		mesh1.postRotate(6.0f,0.0f,1.0f,0.0f);
    		mesh1.translate(-0.05f,0.05f,-0.05f);
    		//mesh.setScale(0.5f,0.5f,0.0f);
    		mesh.postRotate(5.0f,1.0f,1.0f, 1.0f);
    		mesh.translate(0.05f, 0.05f,0.05f);
    		
    		/*tf.postRotate(6.0f,0.0f,1.0f,0.0f);
    		tf.postTranslate(0.05f,0.05f,0.05f);
    		g3d.setCamera(camera, tf);
    		world.setActiveCamera(camera);*///行不通的????
    		this.draw3D();
    		//this.draw2D();
    		this.flushGraphics();
    		System.out.println("run()");
    			
    	}
    	
    }
    //draw3D()
    public void draw3D()
    {
    	try
    	{
    		
    		g3d.bindTarget(g);
    		
    		g3d.render(world);
    		
    		
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    	finally
    	{
    		g3d.releaseTarget();
    	}
    }
    //draw2D()
    public void draw2D()
    {
    	g.setColor(0x000000);
    	g.fillRect(0,0,this.getWidth(),this.getHeight());
    }
    //cretedMesh()
    public Mesh createdMesh()
    {
    	short []positions=new short[]{
    			-1,-1,1,  1,-1,1,  -1,1,1,  1,1,1,//front
    			1,-1,-1, -1,-1,-1,  1,1,-1, -1,1,-1,//back
    			1,-1,1,   1,-1,-1,  1,1,1,   1,1,-1,//right
    			-1,-1,-1, -1,-1,1,  -1,1,-1, -1,1,1,//left
    			-1,1,1,  1,1,1,   -1,1,-1,   1,1,-1,//top
    			1,-1,1,  -1,-1,1,  1,-1,-1,  -1,-1,-1//button
    			
    	};
    	int []dices=new int[]{
    	        0,1,2,3,
    	        4,5,6,7,
    	        8,9,10,11,
    	        12,13,14,15,
    	        16,17,18,19,
    	        20,21,22,23
    	};
    	short []textures=new short[]{
    	
    			0,1, 1,1, 0,0, 1,0,//front
    			0,1, 1,1, 0,0, 1,1,//back
    			0,1, 1,1, 0,0, 1,0,//right
    			0,1, 1,1, 0,0, 1,0,//left
    			0,1, 1,1, 0,0, 1,0,//top
    			0,1, 1,1, 0,0, 1,0,//botton
    	};
    	int []length=new int[]{4,4,4,4,4,4};
    	VertexArray position_array=new VertexArray(positions.length/3,3,2);
    	position_array.set(0,positions.length/3,positions);
    	VertexArray texture_array=new VertexArray(textures.length/2,2,2);
    	texture_array.set(0,textures.length/2,textures);
    	
    	VertexBuffer vb=new VertexBuffer();
    	vb.setPositions(position_array,3.0f,null);
    	vb.setTexCoords(0,texture_array, 1.0f,null);
    	vb.setTexCoords(1,texture_array,1.0f,null);
    	
    	IndexBuffer ib=new TriangleStripArray(dices,length);
    	
    	
    	Appearance app=new Appearance();

    	 PolygonMode pm=new PolygonMode();
    	 pm.setCulling(PolygonMode.CULL_NONE);//两面都显示
    	 pm.setWinding(PolygonMode.WINDING_CCW);//设定逆时针为正面
    	 app.setPolygonMode(pm);
    	 
    	 li=new LoadImage("/texture.png");
    	 Image image=LoadImage.getImage();
    	 System.out.println("image::"+image);
    	 Image2D image2d=new Image2D(Image2D.RGB,image);
    	 Texture2D tt=new Texture2D(image2d);
    	 tt.setFiltering(Texture2D.FILTER_BASE_LEVEL,Texture2D.FILTER_NEAREST);//目前都采取这种采样
    	 tt.setWrapping(Texture2D.WRAP_CLAMP,Texture2D.WRAP_CLAMP);//重复一次
    	 tt.setBlending(Texture2D.FUNC_MODULATE);//采取修正的贴图方式
          li=new LoadImage("/22.PNG");
         image=LoadImage.getImage();
         image2d=new Image2D(Image2D.RGB,image);
    	 Texture2D ttt=new Texture2D(image2d);
    	 ttt.setFiltering(Texture2D.FILTER_BASE_LEVEL,Texture2D.FILTER_NEAREST);//目前都采取这种采样
    	 ttt.setWrapping(Texture2D.WRAP_CLAMP,Texture2D.WRAP_CLAMP);//重复一次
    	 ttt.setBlending(Texture2D.FUNC_MODULATE);//采取修正的贴图方式
    	 app.setTexture(0,tt);
    	 app.setTexture(1,ttt);
    	 
    	 Mesh mesh=new Mesh(vb,ib,app);
    	 return mesh;
    	
    	
    	
    }
    ////
    public void commandAction(Command c,Displayable d)
    {
    	if(c==mm.exitCommand)
    	{
    		mm.quitApp();
    	}
     }
}

⌨️ 快捷键说明

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