📄 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 java.io.*;
import com.mascotcapsule.micro3d.v3.*;
import javax.microedition.lcdui.Graphics;
class CRender3D {
//final definitions
static final boolean DEBUG = true;
static final int WIDTH = 176;
static final int HEIGHT = 176;
//3d related objects
public final int m_ModelTotalCount = 5;
public final int m_SegmentTotalCount = 0;
Figure[] m_Models;
int[][] m_SegmentVectors;
int[][] m_SegmentTexCoords;
Texture[][] m_ModelTextures;
Texture[] m_SegmentTextures;
ActionTable[] m_ModelAction;
FigureLayout m_Layout;
Effect3D m_Effect;
AffineTrans m_Trans;
AffineTrans m_ViewTrans;
int m_FineFaces;
int[] m_FineVectors;
int[] m_FineTexCoords;
// independent resources
Texture m_EnvMap;
//3d rendering
Graphics3D m_g3d;
//we could use resource byte array to instead of this
String[] m_ModelNames;
String[] m_SegmentNames;
String[] m_ModelActionNames;
String[][] m_ModelTexNames;
String[] m_SegmentTexNames;
public String m_Msg = new String("");
//camera ref
public int[] m_eye = { 0, 0, 0 };
public int[] m_lookat = { 0, 1, 0 };
public int[] m_up = { 0, 0, 1 };
// int[] m_eye = { 0, 0, 0 };
// int[] m_lookat = { 0, 0, 1024 };
// int[] m_up = { 0, -4096, 0 };
/**
* 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_ModelNames = new String[m_ModelTotalCount];
m_SegmentNames = new String[m_SegmentTotalCount];
m_ModelActionNames = new String[m_ModelTotalCount];
m_ModelTexNames = new String[m_ModelTotalCount][];
m_SegmentTexNames = new String[m_SegmentTotalCount];
m_Models = new Figure[m_ModelTotalCount];
m_SegmentVectors = new int[m_SegmentTotalCount][];
m_SegmentTexCoords = new int[m_SegmentTotalCount][];
m_ModelTextures = new Texture[m_ModelTotalCount][];
m_SegmentTextures = new Texture[m_SegmentTotalCount];
m_ModelAction = new ActionTable[m_ModelTotalCount];
m_FineVectors = new int[255*3*3];
m_FineTexCoords = new int[255*3*2];
m_ModelNames[0] = "/stage.mbac";
m_ModelNames[1] = "/bottle.mbac";
m_ModelNames[2] = "/ball.mbac";
m_ModelNames[3] = "/player.mbac";
m_ModelNames[4] = "/arrow.mbac";
// m_SegmentNames[0] = "/floor.seg";
for (int i = 0; i < m_ModelTotalCount; i++) {
m_ModelTexNames[i] = null;
m_ModelActionNames[i] = null;
m_ModelAction[i] = null;
}
m_ModelActionNames[3] = "/Player.mtra";
m_ModelTexNames[0] = new String[8];
m_ModelTexNames[0][0] = "/w2.bmp";
m_ModelTexNames[0][1] = "/f5.bmp";
m_ModelTexNames[0][2] = "/w1.bmp";
m_ModelTexNames[0][3] = "/o1.bmp";
m_ModelTexNames[0][4] = "/f3.bmp";
m_ModelTexNames[0][5] = "/f2.bmp";
m_ModelTexNames[0][6] = "/f1.bmp";
m_ModelTexNames[0][7] = "/w4.bmp";
m_ModelTexNames[1] = new String[1];
m_ModelTexNames[1][0] = "/ball.bmp";
m_ModelTexNames[2] = new String[1];
m_ModelTexNames[2][0] = "/ball.bmp";
m_ModelTexNames[3] = new String[2];
m_ModelTexNames[3][0] = "/player.bmp";
m_ModelTexNames[3][1] = "/ball.bmp";
m_ModelTexNames[4] = new String[1];
m_ModelTexNames[4][0] = "/o1.bmp";
// m_SegmentTexNames[0] = "/floor1.bmp";
//create obj according to resource.
for (int i = 0; i < m_ModelTotalCount; i++)
if (m_ModelTexNames[i] != null) {
m_ModelTextures[i] = new Texture[m_ModelTexNames[i].length];
}
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_ModelNames == null) {
return false;
}
}
try {
m_Models[id] = new Figure(m_ModelNames[id]);
} catch (Exception e) {
if (DEBUG) {
m_Msg = "Figure: " + id + e.toString();
}
}
try {
if (m_ModelTexNames[id] != null)
for (int i = 0; i < m_ModelTexNames[id].length; i++)
if (m_ModelTexNames[id][i] != null)
m_ModelTextures[id][i] = new Texture(
m_ModelTexNames[id][i], true);
} catch (Exception e) {
if (DEBUG) {
m_Msg = "Texture: " + id + m_Msg;
}
}
try {
if (m_ModelTexNames[id] != null) {
m_Models[id].setTexture(m_ModelTextures[id]);
}
if (m_ModelActionNames[id] != null) {
m_ModelAction[id] = new ActionTable(m_ModelActionNames[id]);
}
} catch (Exception e) {
m_Msg = "setTexture&Actions" + id + m_Msg;
}
return true;
}
/**
* Release the model specified by the id
*
* @param id
* int: model id
*/
public void ReleaseModel(int id) {
if (DEBUG) {
if (id > m_ModelTotalCount || id < 0 || m_Models == null) {
return;
}
}
m_Models[id] = null;
m_ModelActionNames[id] = null;
for (int i = 0; i < m_ModelTexNames[id].length; i++) {
m_ModelTextures[id][i] = null;
}
}
/**
* 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) {
if (modelid == 0) {
m_Layout.setAffineTrans(m_ViewTrans);
m_g3d.renderFigure(m_Models[modelid], 0, 0, m_Layout, m_Effect);
m_g3d.flush();
return;
}
AffineTrans trans = new AffineTrans();
AffineTrans trans2 = new AffineTrans();
trans.setIdentity();
trans2.setIdentity();
if (rot != null) {
if (rot[0] != 0) {
trans.setRotationX(rot[0]);
}
if (rot[1] != 0) {
trans2.setRotationY(rot[1]);
trans.mul(trans2);
}
if (rot[2] != 0) {
trans2.setRotationZ(rot[2]);
trans.mul(trans2);
}
}
if (pos != null) {
trans.m03 = pos[0];
trans.m13 = pos[1];
trans.m23 = pos[2];
}
m_Trans.mul(m_ViewTrans, trans);
m_Layout.setAffineTrans(m_Trans);
m_Effect.setSphereTexture(null);
switch (modelid) {
case 2:
m_Effect.setSphereTexture(m_EnvMap);
break;
case 3:
m_Models[3].setPosture(m_ModelAction[3], actionid, frameid * 65536);
break;
}
m_g3d.renderFigure(m_Models[modelid], 0, 0, m_Layout, m_Effect);
}
/**
* set initial parameters for 3d render
*/
public void SetupEnv() {
m_g3d = new Graphics3D();
m_Layout = new FigureLayout();
m_Effect = new Effect3D(null, Effect3D.NORMAL_SHADING, false, null);
m_Trans = new AffineTrans();
m_ViewTrans = new AffineTrans();
m_Layout.setCenter(WIDTH / 2, HEIGHT / 2);
m_Trans.setIdentity();
m_ViewTrans.lookAt(new Vector3D(m_eye[0], m_eye[1], m_eye[2]),
new Vector3D(m_lookat[0], m_lookat[1], m_lookat[2]),
new Vector3D(m_up[0], m_up[1], m_up[2]));
m_Layout.setAffineTrans(m_Trans);
m_Layout.setPerspective(10, 5000, 50 * 4096 / 360);
// Light iLight = new Light();
// iLight.setAmbientIntensity(2000);
// m_Effect.setLight(iLight);
}
/**
* call this function before you render any 3D models
*
* @param g
* Graphics
*/
public void StartRender3D(Graphics g) {
m_g3d.bind(g);
}
/**
* call this function when you finish all 3d rendering
*
* @param g
* Graphics
*/
public void EndRender3D(Graphics g) {
m_g3d.flush();
m_g3d.release(g);
}
public void LoadAllModels() {
for (int i = 0; i < m_ModelTotalCount; i++) {
LoadModel(i);
}
for (int i = 0; i < m_SegmentTotalCount; i++)
LoadSegment(i);
try {
m_EnvMap = new Texture("/e_color.bmp", false);
} catch (Exception e) {
m_Msg = "env map" + e.toString();
}
}
public void UpdateCamera() {
//m_ViewTrans.setRotationY(m_rotationY);
m_ViewTrans.lookAt(new Vector3D(m_eye[0], m_eye[1], m_eye[2]),
new Vector3D(m_lookat[0], m_lookat[1], m_lookat[2]),
new Vector3D(m_up[0], m_up[1], m_up[2]));
}
public void LoadSegment(int id) {
int i;
try {
InputStream is = this.getClass().getResourceAsStream(
m_SegmentNames[id]);
DataInputStream dis = new DataInputStream(is);
int faces = dis.readShort();
m_SegmentVectors[id] = new int[faces*4*3];
m_SegmentTexCoords[id] = new int[faces*4*2];
for (i = 0; i < faces * 4 * 3; i++)
m_SegmentVectors[id][i] = dis.readShort();
for (i = 0; i < faces * 4 * 2; i++)
m_SegmentTexCoords[id][i] = dis.readShort();
} catch (Exception e) {
m_Msg = "Segment" + e.toString();
}
try {
m_SegmentTextures[id] = new Texture(m_SegmentTexNames[id], true);
} catch (IOException e) {
m_Msg = "segtex" + e.toString();
}
// int[] vec = { -260, 1000, -250, -140, 1000, -250, -140, 500, -250,
// -260, 500, -250, -260, 2000, -250, -140, 2000, -250, -140,
// 1500, -250, -260, 1500, -250, -260, 500, -250, -140, 500, -250,
// -140, 0, -250, -260, 0, -250, -260, 1500, -250, -140, 1500,
// -250, -140, 1000, -250, -260, 1000, -250, -60, 1000, -250, 60,
// 1000, -250, 60, 500, -250, -60, 500, -250, -60, 2000, -250, 60,
// 2000, -250, 60, 1500, -250, -60, 1500, -250, -60, 500, -250,
// 60, 500, -250, 60, 0, -250, -60, 0, -250, -60, 1500, -250, 60,
// 1500, -250, 60, 1000, -250, -60, 1000, -250 };
// int[] texture = { 0, 0, 63, 0, 63, 63, 0, 63, 0, 0, 63, 0, 63, 63, 0,
// 63, 0, 0, 63, 0, 63, 63, 0, 63, 0, 0, 63, 0, 63, 63, 0, 63, 0,
// 0, 63, 0, 63, 63, 0, 63, 0, 0, 63, 0, 63, 63, 0, 63, 0, 0, 63,
// 0, 63, 63, 0, 63, 0, 0, 63, 0, 63, 63, 0, 63 };
// m_SegmentVectors[id] = new int[vec.length];
// for (i = 0; i < vec.length; i++)
// m_SegmentVectors[id][i] = vec[i];
// m_SegmentTexCoords[id] = new int[texture.length];
// for (i = 0; i < texture.length; i++)
// m_SegmentTexCoords[id][i] = texture[i];
}
public void RenderSegment(int id) {
int[] norm = { 0, 0, 4096 };
int[] color = { 0 };
try {
FineSegment(id);
}catch(Exception e){m_Msg = "fs" + e.toString();}
m_Layout.setAffineTrans(m_ViewTrans);
try {
m_g3d.renderPrimitives(m_SegmentTextures[id], 0, 0, m_Layout, m_Effect,
Graphics3D.PRIMITVE_QUADS | Graphics3D.PDATA_NORMAL_NONE |
Graphics3D.PDATA_COLOR_NONE | Graphics3D.PATTR_COLORKEY |
Graphics3D.PDATA_TEXURE_COORD,
m_FineFaces, m_FineVectors, norm, m_FineTexCoords, color);
// m_FineFaces, m_FineVectors, norm, m_FineTexCoords, color);
m_Msg = "" + m_FineFaces;
} catch (Exception e) {
m_Msg = "rp" + e.toString();
}
}
public void FineSegment(int id) {
int i, j, segs, midx, midy, midz, tempx, tempy, tempz, dist_eye, dist_diag;
int pntx, pnty, pntz, dir1x, dir1y, dir1z, dir2x, dir2y, dir2z;
int texx, texy, texdir1x, texdir1y, texdir2x, texdir2y;
m_FineFaces = 0;
segs = m_SegmentVectors[id].length /4 /3;
for (i=0; i<segs; i++) {
tempx = i*4*3;
pntx = m_SegmentVectors[id][tempx];
pnty = m_SegmentVectors[id][tempx+1];
pntz = m_SegmentVectors[id][tempx+2];
dir1x = m_SegmentVectors[id][tempx+3]-pntx;
dir1y = m_SegmentVectors[id][tempx+4]-pnty;
dir1z = m_SegmentVectors[id][tempx+5]-pntz;
dir2x = m_SegmentVectors[id][tempx+9]-pntx;
dir2y = m_SegmentVectors[id][tempx+10]-pnty;
dir2z = m_SegmentVectors[id][tempx+11]-pntz;
midx = pntx + (dir1x+dir2x) /2;
midy = pnty + (dir1y+dir2y) /2;
midz = pntz + (dir1z+dir2z) /2;
// dist_eye = (midx-m_eye[0])*(midx-m_eye[0]) +
// (midy-m_eye[1])*(midy-m_eye[1]) +
// (midz-m_eye[2])*(midz-m_eye[2]);
dist_eye = midy - m_eye[1];
if (midy > pnty)
dist_diag = midy - pnty;
else
dist_diag = pnty - midy;
if (dist_eye < 0)
if (dist_eye + dist_diag < 0)
continue;
else
dist_eye = -dist_eye;
if (dist_diag > dist_eye/3) {
dir1x /= 8;
dir1y /= 8;
dir1z /= 8;
tempx = m_FineFaces*4*3;
for (j=0; j<8; j++) {
m_FineVectors[tempx++] = pntx;
m_FineVectors[tempx++] = pnty;
m_FineVectors[tempx++] = pntz;
m_FineVectors[tempx++] = pntx+dir2x;
m_FineVectors[tempx++] = pnty+dir2y;
m_FineVectors[tempx++] = pntz+dir2z;
pntx += dir1x;
pnty += dir1y;
pntz += dir1z;
m_FineVectors[tempx++] = pntx+dir2x;
m_FineVectors[tempx++] = pnty+dir2y;
m_FineVectors[tempx++] = pntz+dir2z;
m_FineVectors[tempx++] = pntx;
m_FineVectors[tempx++] = pnty;
m_FineVectors[tempx++] = pntz;
}
tempx = i*4*2;
texx = m_SegmentTexCoords[id][tempx];
texy = m_SegmentTexCoords[id][tempx+1];
texdir1x = m_SegmentTexCoords[id][tempx+2]-texx;
texdir1y = m_SegmentTexCoords[id][tempx+3]-texy;
texdir2x = m_SegmentTexCoords[id][tempx+6]-texx;
texdir2y = m_SegmentTexCoords[id][tempx+7]-texy;
texdir1x /= 8;
texdir1y /= 8;
tempx = m_FineFaces*4*2;
for (j=0; j<8; j++) {
m_FineTexCoords[tempx++] = texx;
m_FineTexCoords[tempx++] = texy;
m_FineTexCoords[tempx++] = texx+texdir2x;
m_FineTexCoords[tempx++] = texy+texdir2y;
texx += texdir1x;
texy += texdir1y;
m_FineTexCoords[tempx++] = texx+texdir2x;
m_FineTexCoords[tempx++] = texy+texdir2y;
m_FineTexCoords[tempx++] = texx;
m_FineTexCoords[tempx++] = texy;
}
m_FineFaces += 8;
} else {
tempx = m_FineFaces*4*3;
m_FineVectors[tempx++] = pntx;
m_FineVectors[tempx++] = pnty;
m_FineVectors[tempx++] = pntz;
m_FineVectors[tempx++] = pntx+dir1x;
m_FineVectors[tempx++] = pnty+dir1y;
m_FineVectors[tempx++] = pntz+dir1z;
m_FineVectors[tempx++] = pntx+dir1x+dir2x;
m_FineVectors[tempx++] = pnty+dir1y+dir2y;
m_FineVectors[tempx++] = pntz+dir1z+dir2z;
m_FineVectors[tempx++] = pntx+dir2x;
m_FineVectors[tempx++] = pnty+dir2y;
m_FineVectors[tempx++] = pntz+dir2z;
tempx = m_FineFaces*4*2;
tempy = i*4*2;
m_FineTexCoords[tempx++] = m_SegmentTexCoords[id][tempy++];
m_FineTexCoords[tempx++] = m_SegmentTexCoords[id][tempy++];
m_FineTexCoords[tempx++] = m_SegmentTexCoords[id][tempy++];
m_FineTexCoords[tempx++] = m_SegmentTexCoords[id][tempy++];
m_FineTexCoords[tempx++] = m_SegmentTexCoords[id][tempy++];
m_FineTexCoords[tempx++] = m_SegmentTexCoords[id][tempy++];
m_FineTexCoords[tempx++] = m_SegmentTexCoords[id][tempy++];
m_FineTexCoords[tempx++] = m_SegmentTexCoords[id][tempy++];
m_FineFaces++;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -