📄 vertexcanvas.java
字号:
package testVertex;
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
public class VertexCanvas extends Canvas implements Runnable {
private Graphics3D _graphics3d = null;
private VertexArray vertexArray, colorArray;
private VertexBuffer vertexBuffer;
private IndexBuffer triangles;
private Appearance appearance;
private Light light;
private Camera camera = null;
public VertexCanvas() {
_graphics3d = Graphics3D.getInstance();
byte vertrices[] = new byte[] { -1, 0, 0, 1, 0, 0, 1, 1, 0, -1, 1, 0 };
byte vertex_colors[] = { (byte) 255, 0, 0, 0, (byte) 255, 0, 0, 0,
(byte) 255, 0, (byte) 255, (byte) 255 };
vertexArray = new VertexArray(vertrices.length / 3, 3, 1);
vertexArray.set(0, vertrices.length / 3, vertrices);
colorArray = new VertexArray(vertex_colors.length / 3, 3, 1);
colorArray.set(0, vertex_colors.length / 3, vertex_colors);
vertexBuffer = new VertexBuffer();
vertexBuffer.setPositions(vertexArray, 1.0f, null);
vertexBuffer.setColors(colorArray);
int indices[] = new int[] { 0, 1, 3, 2 };
int[] stripLengths = new int[] { 4 };
triangles = new TriangleStripArray(indices, stripLengths);
appearance = new Appearance();
PolygonMode pm = new PolygonMode();
pm.setCulling(PolygonMode.CULL_NONE);
pm.setShading(PolygonMode.SHADE_FLAT);
appearance.setPolygonMode(pm);
camera = new Camera();
camera.setTranslation(0, 0, 5);
float aspect = (float) getWidth() / (float) getHeight();
camera.setPerspective(30.0f, aspect, 1.0f, 1000.0f);
Transform camTransform = new Transform();
camTransform.postTranslate(0.0f, 0.0f, 5.0f);
_graphics3d.setCamera(camera, camTransform);
light = new Light();
light.setColor(0xffffff); // 设置为白色光线t
light.setIntensity(1.25f);
Thread thread = new Thread(this);
thread.start();
}
protected void paint(Graphics g) {
_graphics3d.bindTarget(g);
_graphics3d.clear(null);
_graphics3d.render(vertexBuffer, triangles, appearance, null);
_graphics3d.releaseTarget();
}
public void run() {
while (true) {
try {
Thread.sleep(40);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -