📄 1.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import javax.microedition.lcdui.game.*;
/**
* Sample displaying a cube defined by eight vertices which are connected
* by triangles.
*
* @author Redhair
*/
class M3GCanvas extends GameCanvas implements Runnable
{
private Graphics3D g3d; // Graphics object used to render the 3d world.
private Graphics g2d;
private World world; // This world contains the camera and the pyramidMesh.
private Camera camera;
private final int WIDTH, HEIGHT;
public M3GCanvas(){
super(false);
WIDTH = getWidth();
HEIGHT = getHeight();
camera = new Camera();
float aspectRatio = ((float) WIDTH) / ((float) HEIGHT);
camera.setPerspective(70.0f, aspectRatio, 0.1f, 50.0f);
camera.translate(0,0,6);
world = new World();
setFullScreenMode(false);
// world.addChild( mobileCamera.getCameraGroup() );
// world.setActiveCamera( mobileCamera.getCamera() );
world.addChild(camera );
world.setActiveCamera(camera );
g3d = Graphics3D.getInstance();
Group groupRed = new Group();
// groupRed.setUserID(USER_ID_RED_MESHES);
Group groupBlue = new Group();
// groupBlue.setUserID(USER_ID_BLUE_MESHES);
Group groupAll = new Group();
// groupAll.setUserID(USER_ID_ALL_MESHES);
groupAll.addChild(groupRed);
groupAll.addChild(groupBlue );
world.addChild(groupAll );
for (int i=0; i<8; i++)
{
if ((i%2) == 0)
{
cube = createCube(0xFFFF0000);
groupRed.addChild(cube);
}
else
{
cube = createCube(0xFF0000FF);
groupBlue.addChild(cube);
}
cube.setTranslation(1.5f * (float) Math.cos(i*Math.PI/4), 1.5f * (float) Math.sin(i*Math.PI/4), 0.0f);
// cube.setScale(0.4f, 0.4f, 0.4f);
}
try{
pointerImage = Image.createImage("/pointer.png");
}catch(Exception e){
System.out.println("Failed to load image");
}
ri = new RayIntersection();
// world.pick(-1, POINTER_X,POINTER_Y, world.getActiveCamera(), ray);
oldAppearance = null;
Fog fog = new Fog();
fog.setColor(0xFFFF0000);
fog.setMode(Fog.LINEAR);
fog.setDensity(100);
fog.setLinear(-1.0f, 1.0f);
newAppearance = new Appearance();
newAppearance.setFog(fog);
Thread t = new Thread(this);
t.start();
}
public void run() {
g2d = getGraphics();
while(true){
try{
g3d.bindTarget(g2d); // Binds the given Graphics or mutable Image2D as the rendering target of this Graphics3D
//g3d.clear(null);
p1 = (float)POINTER_X / (float)WIDTH;
p2 = (float)POINTER_Y / (float)HEIGHT;
world.pick(-1, p1,p2, camera, ri);
// boolean a = world.pick(-1, POINTER_X,POINTER_Y, world.getActiveCamera(), ray);
if (ri.getIntersected() != null)
{
pickedNode = ri.getIntersected();
distance = ri.getDistance();
// distance = ((int)((distance+0.005)*100.0f))/100.0f;
index = ri.getSubmeshIndex();
ri.getRay(ray);
px = ray[0] + ray[3] * ri.getDistance();
py = ray[1] + ray[4] * ri.getDistance();
pz = ray[2] + ray[5] * ri.getDistance();
// px = ((int)((px+0.005)*100.0f))/100.0f;
// py = ((int)((px+0.005)*100.0f))/100.0f;
// pz = ((int)((px+0.005)*100.0f))/100.0f;
nx = ri.getNormalX();
ny = ri.getNormalY();
nz = ri.getNormalZ();
// nx = ((int)((nx+0.005)*100.0f))/100.0f;
// ny = ((int)((nx+0.005)*100.0f))/100.0f;
// nz = ((int)((nx+0.005)*100.0f))/100.0f;
if (pickedNode instanceof Mesh)
{
oldAppearance = ((Mesh)pickedNode).getAppearance(0);
((Mesh)pickedNode).setAppearance(0, newAppearance);
}
}
g3d.render(world);
}finally{
g3d.releaseTarget();
}
g2d.setColor(255,255,255);
g2d.drawString( "Distance: "+distance , 5,5, Graphics.TOP|Graphics.LEFT);
g2d.drawString( "SubMeshindex: "+index, 5,18, Graphics.TOP|Graphics.LEFT);
g2d.drawString( "Pos: "+"("+px+","+py+","+pz+")", 5,31, Graphics.TOP|Graphics.LEFT);
g2d.drawString( "Normal: "+"("+nx+","+ny+","+nz+")", 5,44, Graphics.TOP|Graphics.LEFT);
g2d.drawImage(pointerImage, POINTER_X, POINTER_Y, Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
}
private Mesh createCube(int RGBA){
IndexBuffer indexBuffer;
Appearance appearance = new Appearance();
VertexBuffer vertexBuffer = new VertexBuffer();
/** The cube's vertex positions (x, y, z). */
byte []POINTS = new byte[] {
-1, -1, 1, 1, -1, 1, -1, 1, 1, 1, 1, 1,
-1, -1, -1, 1, -1, -1, -1, 1, -1, 1, 1, -1};
/** Indices that define how to connect the vertices to build
* triangles. */
short[] TEXCOORDS = {
0, 1, 1, 1, 0, 0, 1, 0, // front
0, 1, 1, 1, 0, 0, 1, 0, // back
};
int []INDICES = new int[]{
0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1 };
// The length of each sequence in the indices array.
// int []LENGTH = new int[] {3, 3, 3, 3, 3, 3,3, 3, 3, 3, 3, 3}; // the pyramid is built by 12 triangles
VertexArray POSITION_ARRAY, COLOR_ARRAY,TEXCOORD_ARRAY;
// Create a VertexArray to be used by the VertexBuffer
POSITION_ARRAY = new VertexArray(POINTS.length / 3, 3, 1);
POSITION_ARRAY.set(0, POINTS.length / 3, POINTS);
TEXCOORD_ARRAY = new VertexArray( TEXCOORDS.length / 2, 2, 2 );
TEXCOORD_ARRAY.set( 0, TEXCOORDS.length/2,TEXCOORDS );
indexBuffer = new TriangleStripArray(INDICES, new int[] {INDICES.length});
// VertexBuffer holds references to VertexArrays that contain the positions, colors, normals,
// and texture coordinates for a set of vertices
vertexBuffer.setPositions(POSITION_ARRAY, 1, null);
vertexBuffer.setDefaultColor(RGBA);
vertexBuffer.setTexCoords( 0, TEXCOORD_ARRAY, 1.0f, null );
Mesh mesh = new Mesh(vertexBuffer, indexBuffer,null);
mesh.setAppearance (0,appearance);
return mesh;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -