m3gcanvas.java
来自「3D手机游戏开发实例源代码」· Java 代码 · 共 679 行
JAVA
679 行
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import javax.microedition.lcdui.game.*;
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; // the camera in the scene
private int CurrentFrame=0;
private float[] Weights = new float[3];;
private IndexBuffer indexBuffer;
private Transform cubeTransform = new Transform();
private VertexBuffer base = new VertexBuffer();
private VertexBuffer[] targets = new VertexBuffer[2];
private Appearance appearance = new Appearance();
private Light light = new Light();
private Background bg = new Background();
private Transform modelTrans = new Transform();
private MorphingMesh hand;
// distance to move the model up/down/left/right/fwd/back for a key press
// angle to rotate the model left/right for a key press
public M3GCanvas(){
super(false);
setFullScreenMode(false);
camera = new Camera();
float aspect = (float) getWidth() / (float) getHeight();
camera.setPerspective(45.0f, aspect, 1.0f, 1000.0f);
Transform cameraTransform = new Transform();
cameraTransform.postTranslate(0.0f,100.0f, 0);
bg.setColor(0x9EFEFE); // light blue
// create a light
light.setColor(0xffffff); // white light
light.setIntensity(1.25f); // over bright
g3d = Graphics3D.getInstance();
g3d.setCamera(camera, cameraTransform);
// g3d.setCamera(camera, camTrans); // position the camera
// set up a "headlight": a directional light shining
// from the direction of the camera
g3d.resetLights();
g3d.addLight(light, cameraTransform);
// initialise the model's transform vars
hand = makeMorph();
// hand.setOrientation(110.0f, 2.0f,0.0f,1.0f) ;
modelTrans.postRotate(10.0f, 1.0f,0.0f,0.0f) ;
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(bg);
for (int i = 0; i < 3; i++)
{
Weights[i] = (float) Math.sin(((float) (CurrentFrame % (60*(i+1)))) / ((float) (60*(i+1))) * Math.PI);
}
CurrentFrame++;
hand.setWeights(Weights);
g3d.render(hand,modelTrans);
}finally{
g3d.releaseTarget();
}
flushGraphics();
}
}
private IndexBuffer getIndexBuffer(){
/** The cube's vertex positions (x, y, z). */
int[] INDICES= new int[]{
1,3,0,
4,3,1,
2,4,1,
4,5,3,
0,9,8,
0,6,9,
6,10,9,
6,7,10,
9,11,8,
11,9,12,
9,13,12,
9,10,13,
11,16,8,
11,14,16,
17,14,15,
14,17,16,
8,1,0,
8,16,1,
17,1,16,
1,17,2,
18,3,5,
6,3,18,
7,6,18,
6,0,3,
19,21,11,
22,21,19,
20,22,19,
22,23,21,
15,25,24,
15,14,25,
14,21,25,
14,11,21,
24,27,26,
24,25,27,
21,27,25,
27,21,23,
29,30,28,
31,30,29,
5,31,29,
31,32,30,
4,31,5,
31,4,33,
4,34,33,
4,2,34,
33,32,31,
32,33,35,
34,35,33,
35,34,36,
38,39,37,
30,39,38,
28,30,38,
30,32,39,
41,42,40,
39,42,41,
37,39,41,
39,32,42,
44,45,43,
45,44,46,
37,46,44,
46,37,41,
45,48,47,
45,46,48,
46,40,48,
46,41,40,
43,51,44,
43,49,51,
49,52,51,
49,50,52,
51,37,44,
37,51,38,
51,28,38,
51,52,28,
35,54,32,
54,35,55,
36,55,35,
55,36,53,
55,23,54,
23,55,27,
26,55,53,
55,26,27,
32,56,42,
32,54,56,
54,57,56,
54,23,57,
56,40,42,
40,56,58,
57,58,56,
58,57,59,
61,63,60,
63,61,64,
62,64,61,
64,62,65,
64,20,63,
20,64,22,
65,22,64,
22,65,23,
65,57,23,
57,65,66,
62,66,65,
66,62,67,
66,59,57,
59,66,68,
67,68,66,
68,67,69,
60,71,70,
60,63,71,
63,72,71,
63,20,72,
70,74,73,
70,71,74,
71,75,74,
71,72,75,
52,76,28,
76,52,77,
50,77,52,
77,50,78,
77,79,76,
79,77,80,
78,80,77,
80,78,81,
82,76,79,
76,82,84,
82,85,84,
82,83,85,
84,28,76,
28,84,29,
85,29,84,
29,85,5,
72,86,75,
86,72,87,
20,87,72,
87,20,19,
87,88,86,
88,87,89,
19,89,87,
89,19,11,
89,90,88,
90,89,91,
89,12,91,
89,11,12,
90,93,92,
90,91,93,
91,13,93,
91,12,13,
94,97,96,
94,95,97,
7,97,95,
97,7,18,
96,85,83,
96,97,85,
18,85,97,
85,18,5,
58,48,40,
48,58,98,
58,68,98,
58,59,68,
98,47,48,
47,98,99,
98,69,99,
98,68,69,
7,100,10,
7,95,100,
95,101,100,
95,94,101,
100,13,10,
13,100,93,
100,92,93,
100,101,92,
102,106,105,
102,103,106,
104,106,103,
106,104,107,
106,108,105,
108,106,109,
107,109,106,
109,107,110,
62,112,67,
62,111,112,
111,103,112,
111,104,103,
67,113,69,
67,112,113,
112,102,113,
112,103,102,
114,117,116,
114,115,117,
104,117,115,
117,104,111,
116,61,60,
116,117,61,
111,61,117,
61,111,62,
119,120,118,
120,119,121,
110,121,119,
121,110,107,
121,114,120,
114,121,115,
107,115,121,
115,107,104,
122,126,125,
122,123,126,
123,127,126,
123,124,127,
125,129,128,
125,126,129,
126,130,129,
126,127,130,
43,132,131,
43,45,132,
47,132,45,
132,47,133,
131,125,128,
131,132,125,
133,125,132,
125,133,122,
134,137,136,
134,135,137,
135,129,137,
135,128,129,
136,139,138,
136,137,139,
137,130,139,
137,129,130,
50,141,140,
50,49,141,
49,131,141,
49,43,131,
140,135,134,
140,141,135,
141,128,135,
141,131,128,
108,143,105,
108,142,143,
124,143,142,
143,124,123,
105,144,102,
105,143,144,
123,144,143,
144,123,122,
99,133,47,
133,99,145,
69,145,99,
145,69,113,
145,122,133,
122,145,144,
113,144,145,
144,113,102,
134,147,140,
134,146,147,
146,116,147,
146,114,116,
140,148,50,
140,147,148,
147,60,148,
147,116,60,
114,149,120,
114,146,149,
134,149,146,
149,134,136,
120,150,118,
120,149,150,
136,150,149,
150,136,138,
119,151,110,
151,119,152,
118,152,119,
152,118,153,
151,155,154,
151,152,155,
152,156,155,
152,153,156,
157,160,159,
157,158,160,
158,109,160,
158,108,109,
159,151,154,
159,160,151,
160,110,151,
160,109,110,
161,139,130,
139,161,163,
161,164,163,
161,162,164,
163,138,139,
138,163,165,
163,166,165,
163,164,166,
167,170,169,
167,168,170,
162,170,168,
170,162,161,
170,124,169,
124,170,127,
161,127,170,
127,161,130,
171,168,167,
168,171,172,
157,172,171,
172,157,159,
168,173,162,
168,172,173,
159,173,172,
173,159,154,
156,175,155,
156,174,175,
166,175,174,
175,166,164,
175,154,155,
154,175,173,
175,162,173,
175,164,162,
176,180,179,
176,177,180,
177,181,180,
177,178,181,
180,182,179,
182,180,183,
180,184,183,
180,181,184,
185,74,75,
74,185,186,
178,186,185,
186,178,177,
186,73,74,
73,186,187,
177,187,186,
187,177,176,
188,192,191,
188,189,192,
190,192,189,
192,190,193,
191,195,194,
191,192,195,
192,196,195,
192,193,196,
80,197,79,
197,80,198,
81,198,80,
198,81,199,
198,194,197,
194,198,191,
199,191,198,
191,199,188,
199,200,188,
200,199,201,
199,202,201,
199,81,202,
201,176,200,
176,201,187,
201,73,187,
201,202,73,
176,203,200,
176,179,203,
182,203,179,
203,182,204,
203,188,200,
188,203,189,
203,190,189,
203,204,190,
194,206,197,
194,205,206,
178,206,205,
206,178,185,
197,207,79,
197,206,207,
185,207,206,
207,185,75,
205,181,178,
181,205,208,
205,195,208,
205,194,195,
208,184,181,
184,208,209,
208,196,209,
208,195,196,
210,214,213,
210,211,214,
212,214,211,
214,212,215,
214,216,213,
216,214,217,
215,217,214,
217,215,218,
219,215,212,
215,219,220,
75,220,219,
220,75,86,
220,218,215,
218,220,221,
86,221,220,
221,86,88,
222,226,225,
222,223,226,
224,226,223,
226,224,227,
226,228,225,
228,226,229,
227,229,226,
229,227,230,
229,231,228,
231,229,232,
230,232,229,
232,230,233,
232,79,231,
79,232,82,
232,83,82,
232,233,83,
221,234,218,
234,221,235,
221,236,235,
221,88,236,
235,230,234,
230,235,233,
236,233,235,
233,236,83,
216,238,237,
216,217,238,
218,238,217,
238,218,234,
238,224,237,
224,238,227,
238,230,227,
238,234,230,
219,207,75,
207,219,239,
212,239,219,
239,212,240,
207,231,79,
207,239,231,
239,228,231,
239,240,228,
240,225,228,
225,240,241,
240,211,241,
240,212,211,
241,222,225,
222,241,242,
241,210,242,
241,211,210,
243,247,246,
243,244,247,
245,247,244,
247,245,248,
247,249,246,
249,247,250,
248,250,247,
250,248,251,
249,253,252,
249,250,253,
251,253,250,
253,251,254,
252,90,92,
252,253,90,
253,88,90,
253,254,88,
255,259,258,
255,256,259,
257,259,256,
259,257,260,
259,261,258,
261,259,262,
259,263,262,
259,260,263,
96,264,94,
264,96,265,
83,265,96,
265,83,266,
264,256,255,
264,265,256,
266,256,265,
256,266,257,
248,267,251,
267,248,268,
248,269,268,
248,245,269,
268,257,267,
257,268,260,
268,263,260,
268,269,263,
254,236,88,
236,254,270,
251,270,254,
270,251,267,
270,83,236,
83,270,266,
267,266,270,
266,267,257,
101,252,92,
252,101,271,
101,264,271,
101,94,264,
271,249,252,
249,271,272,
271,255,272,
271,264,255,
258,272,255,
272,258,273,
261,273,258,
273,261,274,
272,246,249,
272,273,246,
273,243,246,
273,274,243,
171,158,157,
158,171,275,
171,169,275,
171,167,169,
275,108,158,
108,275,142,
169,142,275,
142,169,124,
150,153,118,
153,150,276,
150,165,276,
150,138,165,
153,174,156,
153,276,174,
276,166,174,
276,165,166,
196,277,209,
196,193,277,
190,277,193,
277,190,204,
277,184,209,
184,277,183,
277,182,183,
277,204,182,
222,278,223,
222,242,278,
210,278,242,
278,210,213,
278,224,223,
224,278,237,
278,216,237,
278,213,216,
261,279,274,
261,262,279,
263,279,262,
279,263,269,
279,243,274,
243,279,244,
269,244,279,
244,269,245,
73,280,70,
73,202,280,
81,280,202,
280,81,78,
70,148,60,
70,280,148,
78,148,280,
148,78,50
};
// The length of each sequence in the indices array.
int []LENGTH = new int[552] ; // the pyramid is built by 12 triangles
for(int i =0;i<552;i++){
LENGTH[i]=3;
}
indexBuffer = new TriangleStripArray(INDICES, LENGTH);
return indexBuffer;
}
private MorphingMesh makeMorph()
/* The model base is PenguinBase, and its two targets
are PenguinUp (flippers up) and PenguinDown (flippers down).
*/
{
VertexBuffer base = (new HandBase()).getVertexBuffer();
VertexBuffer[] targets = new VertexBuffer[2]; // 2 targets
targets[0] = (new HandTarget1()).getVertexBuffer(); // PenguinUp
targets[1] = (new HandTarget2()).getVertexBuffer(); // PenguinDown
IndexBuffer indBuf = getIndexBuffer();
Appearance app = new Appearance();
Material mat = new Material();
mat.setColor(Material.AMBIENT, 0x00765D4D);
mat.setColor(Material.EMISSIVE, 0x00000000);
mat.setColor(Material.DIFFUSE, 0xFF876A56);
mat.setColor(Material.SPECULAR, 0x004D4D4D);
mat.setShininess(60.0f);
appearance.setMaterial(mat);
MorphingMesh m = new MorphingMesh(base, targets, indBuf,appearance);
return m;
} // end of makeMorph()
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?