📄 crender3d.java
字号:
/**
* <p>Title: class CRender3D</p>
* <p>Description: Render/draw 3D models; control all the 3D resources,
* including models, textures and animations.</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: Gameloft ShangHai</p>
* @author Tao Qing
* @version 1.0
*/
import javax.microedition.m3g.*;
import javax.microedition.lcdui.Graphics;
class CRender3D {
//final definitions
static final boolean DEBUG = true;
static final int ID_WORLD = 75;
//3d related objects
int m_ModelTotalCount;
Node m_world = null;
Node[] m_Models;
Texture2D[][] m_ModelTextures;
Camera m_Camera;
Light m_Light;
Light m_LightForTenpin;
//3d rendering
Graphics3D m_g3d;
//we could use resource byte array to instead of this
String m_ModelName;
int [] m_ModelIDs;
String[][] m_ModelTexNames;
//camera ref
int[] m_eye = { 0, 600, 100}; //cm
int[] m_lookat = { 0, 1100, 20 }; //m
int [] m_dir = new int[3];
int[] m_up = { 0, 0, 10 };
Transform m_tCamera;
Transform m_tLight;
/**
* load all models' information used in this program, include model, texture, action.
* @return boolean
*/
public boolean LoadModelsInfo() {
//NOTE: we could read all models and textures data arrays from a packed file instead of
//write constant codes.
m_ModelTotalCount = 4;
m_ModelName = "/all.m3g";
m_ModelIDs = new int[m_ModelTotalCount];
m_ModelIDs[0] = 77;
m_ModelIDs[1] = 78;
m_ModelIDs[2] = 79;
m_ModelIDs[3] = 80;
m_ModelTexNames = new String[m_ModelTotalCount][];
//create obj according to resource.
m_Models = new Mesh[m_ModelTotalCount];
m_ModelTextures = new Texture2D[m_ModelTotalCount][];
for (int i = 0; i < m_ModelTotalCount; i++) {
if (m_ModelTexNames[i] != null)
m_ModelTextures[i] = new Texture2D[m_ModelTexNames[i].length];
}
try {
Object3D [] temp = Loader.load(m_ModelName);
int i=0;
while (temp[i].getUserID() != ID_WORLD)
i++;
m_world = (World)temp[i];
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* load the model specified by the id
* @param id int: model id
* @return boolean
*/
public boolean LoadModel(int id) {
if (DEBUG) {
if (id >= m_ModelTotalCount || id < 0 ||
m_Models == null || m_ModelName == null)
return false;
}
try {
m_Models[id] = (Node) (m_world.find(m_ModelIDs[id]));
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
/**
* Release the model specified by the id
* @param id int: model id
*/
public void ReleaseModel(int id) {}
/**
* Render specified model with specified frame
* @param modelid int: model id
* @param frameid int: frame id
* @param pos int[]: position of the model
* @param rot int[]: rotation of the model
*/
public void RenderModel(int modelid, int actionid, int frameid, int[] pos, int[] rot) {
//the translation should be TRANSLATE*ROT*TRNASFORM, it's reverse proceduce
Transform tr1 = new Transform();
//tr1.postRotate(-90, 1, 0, 0);
tr1.postTranslate(pos[0], pos[2], -pos[1]);
if (rot != null) {
//rotate x, then y, z
if (rot[1] != 0) //z
tr1.postRotate(rot[1]*360/4096.0f, 0, 1, 0);
if (rot[2] != 0) //y
tr1.postRotate(rot[2]*360/4096.0f, 0, 0, 1);
if (rot[0] != 0) //x
tr1.postRotate(rot[0]*360/4096.0f, 1, 0, 0);
}
Transform trans = new Transform();
m_Models[modelid].getTransformTo(m_world, trans);
float [] mat = new float [16];
trans.get(mat);
tr1.postMultiply(trans);
trans.get(mat);
m_g3d.render(m_Models[modelid], tr1);
}
/**
* set initial parameters for 3d render
*/
public void SetupEnv() {
m_dir[0] = m_lookat[0]-m_eye[0];
m_dir[1] = m_lookat[1]-m_eye[1];
m_dir[2] = m_lookat[2]-m_eye[2];
m_g3d = Graphics3D.getInstance();
m_tCamera = new Transform();
m_tLight = new Transform();
m_tCamera.postTranslate(0, 0, 30);
//m_tLight.postTranslate(0,100,30); //added by Milo 09-22
m_Camera = new Camera();
m_Light = new Light();
m_LightForTenpin = new Light();
m_Camera.setPerspective(30,
// (float) getWidth() / (float) getHeight(),
1,
1, 3000);
m_Light.setColor(0xffffff);
m_Light.setIntensity(0.7f);
m_Light.setMode(Light.AMBIENT);
m_Light.setOrientation(15, 1, 0, 0);
UpdateCamera();
}
/**
* call this function before you render any 3D models
* @param g Graphics
*/
public void StartRender3D(Graphics g) {
m_g3d.bindTarget(g);
Background ib = new Background();
ib.setColor(0xB8D5F3);
m_g3d.clear(ib);
m_g3d.setCamera(m_Camera, m_tCamera);
m_g3d.resetLights();
m_g3d.addLight(m_Light, m_tLight);
/* short[] vert = { 10, 10, 10, -10, 10, 10, 10, -10, 10, -10, -10, 10};
VertexArray vertArray = new VertexArray(vert.length / 3, 3, 2);
vertArray.set(0, vert.length / 3, vert);
byte[] norm = { 100, 0, 127, 0, 0, 127, 0, 0, 127, 0, 0, 127};
VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);
normArray.set(0, norm.length / 3, norm);
int[] stripLen = {4};
VertexBuffer vb = new VertexBuffer();
vb.setPositions(vertArray, 1.0f, null); // unit scale, zero bias
vb.setNormals(normArray);
TriangleStripArray iIb = new TriangleStripArray(0, stripLen);
Appearance iAppearance = new Appearance();
Material iMaterial = new Material();
iAppearance.setMaterial(iMaterial);
iMaterial.setColor(Material.DIFFUSE, 0xFFFFFFFF); // white
iMaterial.setColor(Material.SPECULAR, 0xFFFFFFFF); // white
iMaterial.setShininess(100.0f);
m_g3d.render(vb, iIb, iAppearance, m_tLight);
*/
}
/**
* call this function when you finish all 3d rendering
* @param g Graphics
*/
public void EndRender3D(Graphics g) {
m_g3d.releaseTarget();
}
public void LoadAllModels() {
for (int i = 0; i < m_ModelTotalCount; i++)
LoadModel(i);
}
public void Normal(float []vector)
{
float i = vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2];
float j = (float)Math.sqrt(i);
if (j > 0.00000001f) {
vector[0] /= j;
vector[1] /= j;
vector[2] /= j;
}
}
public void UpdateCamera() {
int[] eye = {m_eye[0],m_eye[1],m_eye[2]};
m_lookat[0] = m_dir[0]+eye[0];
m_lookat[1] = m_dir[1]+eye[1];
m_lookat[2] = m_dir[2]+eye[2];
float[] a = new float[16];
float[] xaxis = new float[3];
float[] yaxis = new float[3];
float[] zaxis = new float[3];
zaxis[0] = eye[0] - m_lookat[0];
zaxis[1] = eye[1] - m_lookat[1];
zaxis[2] = eye[2] - m_lookat[2];
Normal(zaxis);
xaxis[0] = m_up[1] * zaxis[2] - zaxis[1] * m_up[2];
xaxis[1] = m_up[2] * zaxis[0] - zaxis[2] * m_up[0];
xaxis[2] = m_up[0] * zaxis[1] - zaxis[0] * m_up[1];
Normal(xaxis);
yaxis[0] = zaxis[1] * xaxis[2] - xaxis[1] * zaxis[2];
yaxis[1] = zaxis[2] * xaxis[0] - xaxis[2] * zaxis[0];
yaxis[2] = zaxis[0] * xaxis[1] - xaxis[0] * zaxis[1];
a[0] = xaxis[0];
a[1] = yaxis[0];
a[2] = zaxis[0];
a[3] = 0;
a[4] = xaxis[1];
a[5] = yaxis[1];
a[6] = zaxis[1];
a[7] = 0;
a[8] = xaxis[2];
a[9] = yaxis[2];
a[10] = zaxis[2];
a[11] = 0;
a[12] = -xaxis[0]*eye[0]-xaxis[1]*eye[1]-xaxis[2]*eye[2];
a[13] = -yaxis[0]*eye[0]-yaxis[1]*eye[1]-yaxis[2]*eye[2];
a[14] = -zaxis[0]*eye[0]-zaxis[1]*eye[1]-zaxis[2]*eye[2];
a[15] = 1;
/* zaxis = normal(Eye - At)
xaxis = normal(cross(Up, zaxis))
yaxis = cross(zaxis, xaxis)
xaxis.x yaxis.x zaxis.x 0
xaxis.y yaxis.y zaxis.y 0
xaxis.z yaxis.z zaxis.z 0
-dot(xaxis, eye) -dot(yaxis, eye) -dot(zaxis, eye) 1
*/
m_tCamera.set(a);
m_tCamera.transpose();
m_tCamera.postRotate(90, 1, 0, 0);
m_tCamera.invert();
//m_tCamera.postRotate(180, 1, 0, 0);
// m_tCamera.postRotate(180, 0, 1, 0);
//m_tCamera.invert();
//m_tCamera.setIdentity();
//m_tCamera.postTranslate(0, 3, 0);
}
public void setLightResource(){
//m_tLight.postTranslate(90,1000,30); //added by Milo 09-22
m_LightForTenpin.setColor(0xffff00);
m_LightForTenpin.setIntensity(1.0f);
m_LightForTenpin.setMode(Light.DIRECTIONAL);
//m_LightForTenpin.setOrientation(10, -1, 0, 0);
m_LightForTenpin.setAttenuation(1,0,0);
m_LightForTenpin.setSpotAngle(0);
//m_g3d.resetLights();
m_g3d.addLight(m_LightForTenpin, m_tLight);
UpdateCamera();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -