📄 context3d.java
字号:
short mH = (short)(-H);
// Define vertices for the piece. Number of vertices for a piece is
// nbrOfSlices * (nbrOfPointsTopSlice + nbrOfPointsBottomSlice + nbrOfPointsSide)
short[] vert = new short[slices * 3 * (3+3+4)];
double angle = (2d*Math.PI/(double)slices)/2d;
for (int i = 0; i < slices; i++)
{
// Define one slice of the piece
int TOP = i*3*3;
int BOT = i*3*3 + (slices*3*3);
int SID = i*3*4 + 2*(slices*3*3);
short sin = (short)((double)I*Math.sin(angle));
short cos = (short)((double)I*Math.cos(angle));
angle += (2d*Math.PI / (double)slices);
short nsin = (short)((double)I*Math.sin(angle));
short ncos = (short)((double)I*Math.cos(angle));
// TOP
vert[TOP++] = 0; // center pt
vert[TOP++] = 0;
vert[TOP++] = H;
vert[TOP++] = cos; // edge pt
vert[TOP++] = sin;
vert[TOP++] = H;
vert[TOP++] = ncos; // next edge pt
vert[TOP++] = nsin;
vert[TOP++] = H;
// BOTTOM
vert[BOT++] = 0; // center pt
vert[BOT++] = 0;
vert[BOT++] = mH;
vert[BOT++] = ncos; // edge pt
vert[BOT++] = nsin;
vert[BOT++] = mH;
vert[BOT++] = cos; // next edge pt
vert[BOT++] = sin;
vert[BOT++] = mH;
// SIDE
vert[SID++] = cos; // top edge pt
vert[SID++] = sin;
vert[SID++] = H;
vert[SID++] = cos; // bottom next edge pt
vert[SID++] = sin;
vert[SID++] = mH;
vert[SID++] = ncos; // top next edge pt
vert[SID++] = nsin;
vert[SID++] = H;
vert[SID++] = ncos; // bottom edge pt
vert[SID++] = nsin;
vert[SID++] = mH;
}
// create VertexArray to hold the vertices
VertexArray vertArray = new VertexArray(vert.length / 3, 3, 2);
vertArray.set(0, vert.length / 3, vert);
// Setup per-vertex normals for the piece; these match with the vertices
// above. Each top/bottom-vertex-normal is perpendicular to the face.
// Fake round corners by giving sphere-normals for each vertex of a side.
// This enables a Goraudshading effect of round sides.
byte[] vertexNormals = new byte[slices * 3 * (3+3+4)];
angle = (2d*Math.PI/(double)slices)/2d;
for (int i = 0; i < slices; i++)
{
int TOP = i*3*3;
int BOT = i*3*3 + (slices*3*3);
int SID = i*3*4 + 2*(slices*3*3);
byte sin = (byte)((double)127*Math.sin(angle));
byte cos = (byte)((double)127*Math.cos(angle));
angle += (2d*Math.PI / (double)slices);
byte nsin = (byte)((double)127*Math.sin(angle));
byte ncos = (byte)((double)127*Math.cos(angle));
// TOP
vertexNormals[TOP++] = 0; // center
vertexNormals[TOP++] = 0;
vertexNormals[TOP++] = 127;
vertexNormals[TOP++] = 0; // edge
vertexNormals[TOP++] = 0;
vertexNormals[TOP++] = 127;
vertexNormals[TOP++] = 0; // next edge
vertexNormals[TOP++] = 0;
vertexNormals[TOP++] = 127;
// BOTTOM
vertexNormals[BOT++] = 0; // center
vertexNormals[BOT++] = 0;
vertexNormals[BOT++] = -127;
vertexNormals[BOT++] = 0; // edge
vertexNormals[BOT++] = 0;
vertexNormals[BOT++] = -127;
vertexNormals[BOT++] = 0; // next edge
vertexNormals[BOT++] = 0;
vertexNormals[BOT++] = -127;
// SIDES
vertexNormals[SID++] = cos; // top edge
vertexNormals[SID++] = sin;
vertexNormals[SID++] = 0;
vertexNormals[SID++] = cos; // bottom edge
vertexNormals[SID++] = sin;
vertexNormals[SID++] = 0;
vertexNormals[SID++] = ncos; // top next edge
vertexNormals[SID++] = nsin;
vertexNormals[SID++] = 0;
vertexNormals[SID++] = ncos; // bottom next edge
vertexNormals[SID++] = nsin;
vertexNormals[SID++] = 0;
}
// create a vertex array for the normals of the object
VertexArray normArray = new VertexArray(vertexNormals.length / 3, 3, 1);
normArray.set(0, vertexNormals.length / 3, vertexNormals);
// per vertex texture coordinates
// texsize = 256x32 => 32*(8x1)
short[] tex = new short[slices * 2 * (3+3+4)];
angle = (2d*Math.PI/(double)slices)/2d;
int offsX = 14 * I / 8;
for (int i = 0; i < slices; i++)
{
int TOP = i*3*2;
int BOT = i*3*2 + (slices*3*2);
int SID = i*4*2 + 2*(slices*3*2);
short sin = (short)((double)I*Math.sin(angle));
short cos = (short)((double)I*Math.cos(angle));
angle += (2d*Math.PI / (double)slices);
short nsin = (short)((double)I*Math.sin(angle));
short ncos = (short)((double)I*Math.cos(angle));
// TOP
tex[TOP++] = (short)(I / 8 + offsX); // center
tex[TOP++] = I;
tex[TOP++] = (short)((I + cos) / 8 + offsX); // edge
tex[TOP++] = (short)(I + sin);
tex[TOP++] = (short)((I + ncos) / 8 + offsX); // next edge
tex[TOP++] = (short)(I + nsin);
// BOTTOM
tex[BOT++] = (short)(I / 8 + offsX); // simile
tex[BOT++] = I;
tex[BOT++] = (short)((I + ncos) / 8 + offsX);
tex[BOT++] = (short)(I + nsin);
tex[BOT++] = (short)((I + cos) / 8 + offsX);
tex[BOT++] = (short)(I + sin);
// SIDE
tex[SID++] = 0; // top edge
tex[SID++] = 0;
tex[SID++] = (short)(2*I / 8); // bottom edge
tex[SID++] = 0;
tex[SID++] = 0; // top next edge
tex[SID++] = (short)(2*I);
tex[SID++] = (short)(2*I / 8); // bottom next edge
tex[SID++] = (short)(2*I);
}
// create a vertex array for the texture coordinates
VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);
texArray.set(0, tex.length / 2, tex);
// create the VertexBuffer
float posScale = 1.0f / (float)(I);
float texScale = 1.0f / (float)(2f * I);
m_pieceVertBuffer = new VertexBuffer();
m_pieceVertBuffer.setPositions(vertArray, posScale, null); // 1/UNITY scale, no bias
m_pieceVertBuffer.setNormals(normArray);
m_pieceVertBuffer.setTexCoords(0, texArray, texScale, null); // 1/(2*UNITY) scale, no bias
// the length of each triangle strip
int[] stripLen = new int[slices*(1+1+1)];
for (int i = 0; i < slices; i++)
{
int TOP = i;
int BOT = i + slices;
int SID = i + 2*slices;
stripLen[TOP] = 3; // Top pie slice
stripLen[BOT] = 3; // Bottom pie slice
stripLen[SID] = 4; // Side
}
// create the index buffer for our object (this tells how to
// create triangle strips from the contents of the vertex buffer).
m_pieceIndexBuffer = new TriangleStripArray(0, stripLen);
}
/**
* Calculates dice vertex- and index buffers.
*/
protected void calculateDiceBuffers()
{
// Define values for dice, these are used throughout the
// definitions of vertices, normals, and texture coordinates
short I = UNITY; // Unit value
short s = (short)(I/2); // Size of corner (0 - I)
short D = (short)(I-s); // Position of corner
short mI = (short)(-I); // Negative unit value
short mD = (short)(mI+s); // Negative position of corner
// Define vertices for the dices
short[] vertices = {
// FACES
// front
mI,mD, I, mD,mI, I, I,mD, I, D,mI, I,
mI, D, I, mI,mD, I, I, D, I, I,mD, I,
mD, I, I, mI, D, I, D, I, I, I, D, I,
// back
I,mD,mI, D,mI,mI, mI,mD,mI, mD,mI,mI,
I, D,mI, I,mD,mI, mI, D,mI, mI,mD,mI,
D, I,mI, I, D,mI, mD, I,mI, mI, D,mI,
// right
I,mI,mD, I,mD,mI, I, I,mD, I, D,mI,
I,mI, D, I,mI,mD, I, I, D, I, I,mD,
I,mD, I, I,mI, D, I, D, I, I, I, D,
// left
mI, I,mD, mI,D,mI, mI,mI,mD, mI,mD,mI,
mI, I, D, mI,I,mD, mI,mI, D, mI,mI,mD,
mI, D, I, mI,I, D, mI,mD, I, mI,mI, D,
// bottom
mI,mI,mD, mD,mI,mI, I,mI,mD, D,mI,mI,
mI,mI, D, mI,mI,mD, I,mI, D, I,mI,mD,
mD,mI, I, mI,mI, D, D,mI, I, I,mI, D,
// top
I, I,mD, D, I,mI, mI, I,mD, mD, I,mI,
I, I, D, I, I,mD, mI, I, D, mI, I,mD,
D, I, I, I, I, D, mD, I, I, mI, I, D,
// CORNERS
// near, top
// left
mI, D, I, mD, I, I, mI, I, D,
// right
I, D, I, I, I, D, D, I, I,
// near, bottom
// left
mI,mD, I, mI,mI, D, mD,mI, I,
// right
I,mD, I, D,mI, I, I,mI, D,
// far, top
// left
mI, D,mI, mI, I,mD, mD, I,mI,
// right
I, D,mI, D, I,mI, I, I,mD,
// far, bottom
// left
mI,mD,mI, mD,mI,mI, mI,mI,mD,
// right
I,mD,mI, I,mI,mD, D,mI,mI
};
// create a VertexArray to hold the vertices for the object
VertexArray vertArray = new VertexArray(vertices.length / 3, 3, 2);
vertArray.set(0, vertices.length / 3, vertices);
double n = Math.sqrt(I*I + I*I + D*D); // Length of any corner vector
byte nI = (byte)(127d*(double)I/n);
byte mnI = (byte)(-nI);
byte nD = (byte)(127d*(double)D/n);
byte mnD = (byte)(-nD);
// Setup per-vertex normals for the dice; these match with the vertices
// above. Each face-vertex-normal is perpendicular to each face.
// Fake round corners by giving sphere-normals for each vertex of a corner.
// This enables a Goraudshading effect of round corners.
byte[] vertexNormals = {
// FACES
// front
0,0,127, 0,0,127, 0,0,127, 0,0,127,
0,0,127, 0,0,127, 0,0,127, 0,0,127,
0,0,127, 0,0,127, 0,0,127, 0,0,127,
// back
0,0,-127, 0,0,-127, 0,0,-127, 0,0,-127,
0,0,-127, 0,0,-127, 0,0,-127, 0,0,-127,
0,0,-127, 0,0,-127, 0,0,-127, 0,0,-127,
// right
127,0,0, 127,0,0, 127,0,0, 127,0,0,
127,0,0, 127,0,0, 127,0,0, 127,0,0,
127,0,0, 127,0,0, 127,0,0, 127,0,0,
// left
-127,0,0, -127,0,0, -127,0,0, -127,0,0,
-127,0,0, -127,0,0, -127,0,0, -127,0,0,
-127,0,0, -127,0,0, -127,0,0, -127,0,0,
// top
0,-127,0, 0,-127,0, 0,-127,0, 0,-127,0,
0,-127,0, 0,-127,0, 0,-127,0, 0,-127,0,
0,-127,0, 0,-127,0, 0,-127,0, 0,-127,0,
// bottom
0,127,0, 0,127,0, 0,127,0, 0,127,0,
0,127,0, 0,127,0, 0,127,0, 0,127,0,
0,127,0, 0,127,0, 0,127,0, 0,127,0,
// CORNERS
// near, top
// left
mnI, nD, nI, mnD, nI, nI, mnI, nI, nD,
// right
nI, nD, nI, nI, nI, nD, nD, nI, nI,
// near, bottom
// left
mnI,mnD, nI, mnI,mnI, nD, mnD,mnI, nI,
// right
nI,mnD, nI, nD,mnI, nI, nI,mnI, nD,
// far, top
// left
mnI, nD,mnI, mnI, nI,mnD, mnD, nI,mnI,
// right
nI, nD,mnI, nD, nI,mnI, nI, nI,mnD,
// far, bottom
// left
mnI,mnD,mnI, mnD,mnI,mnI, mnI,mnI,mnD,
// right
nI,mnD,mnI, nI,mnI,mnD, nD,mnI,mnI
};
// create a vertex array for the normals of the object
VertexArray normArray = new VertexArray(vertexNormals.length / 3, 3, 1);
normArray.set(0, vertexNormals.length / 3, vertexNormals);
// per vertex texture coordinates
// texsize = 256x32 => 32*(8x1)
short txIbase = (short)((I + I)/8);
short mtxIbase = (short)((mI + I)/8);
short txDbase = (short)((D + I)/8);
short mtxDbase = (short)((mD + I)/8);
short tyI = (short)(I + (7*I)/8); // Mathematically, tyI should be 2*I -
// however, this causes the 3d library to
// crash in some cases, probably due to
// that (2*I) * (1.0f / (float)(2*I)) becomes
// somewhat greater than 1.000000f, making it
// read texture beyond the image and thus crash
short mtyI = (short)(mI + I);
short tyD = (short)(D + I);
short mtyD = (short)(mD + I);
short[] txI = new short[8];
short[] mtxI = new short[8];
short[] txD = new short[8];
short[] mtxD = new short[8];
for (int i = 0; i < 8; i++)
{
txI[i] = (short)(txIbase + txIbase * i);
mtxI[i] = (short)(mtxIbase + txIbase * i);
txD[i] = (short)(txDbase + txIbase * i);
mtxD[i] = (short)(mtxDbase + txIbase * i);
}
short[] tex = {
// FACES
// front - 1
mtxI[1],mtyD, mtxD[1],mtyI, txI[1],mtyD, txD[1],mtyI,
mtxI[1], tyD, mtxI[1],mtyD, txI[1], tyD, txI[1],mtyD,
mtxD[1], tyI, mtxI[1], tyD, txD[1], tyI, txI[1], tyD,
// back - 6
mtxI[6],mtyD, mtxD[6],mtyI, txI[6],mtyD, txD[6],mtyI,
mtxI[6], tyD, mtxI[6],mtyD, txI[6], tyD, txI[6],mtyD,
mtxD[6], tyI, mtxI[6], tyD, txD[6], tyI, txI[6], tyD,
// right - 4
mtxI[4],mtyD, mtxD[4],mtyI, txI[4],mtyD, txD[4],mtyI,
mtxI[4], tyD, mtxI[4],mtyD, txI[4], tyD, txI[4],mtyD,
mtxD[4], tyI, mtxI[4], tyD, txD[4], tyI, txI[4], tyD,
// left - 3
mtxI[3],mtyD, mtxD[3],mtyI, txI[3],mtyD, txD[3],mtyI,
mtxI[3], tyD, mtxI[3],mtyD, txI[3], tyD, txI[3],mtyD,
mtxD[3], tyI, mtxI[3], tyD, txD[3], tyI, txI[3], tyD,
// top - 5
mtxI[5],mtyD, mtxD[5],mtyI, txI[5],mtyD, txD[5],mtyI,
mtxI[5], tyD, mtxI[5],mtyD, txI[5], tyD, txI[5],mtyD,
mtxD[5], tyI, mtxI[5], tyD, txD[5], tyI, txI[5], tyD,
// bottom - 2
mtxI[2],mtyD, mtxD[2],mtyI, txI[2],mtyD, txD[2],mtyI,
mtxI[2], tyD, mtxI[2],mtyD, txI[2], tyD, txI[2],mtyD,
mtxD[2], tyI, mtxI[2], tyD, txD[2], tyI, txI[2], tyD,
// CORNERS
// near
mtxI[0],tyI, txI[0],mtyI, mtxI[0],mtyI,
mtxI[0],tyI, txI[0],mtyI, txI[0], tyI,
mtxI[0],tyI, txI[0],mtyI, mtxI[0],mtyI,
mtxI[0],tyI, txI[0],mtyI, txI[0], tyI,
// far
mtxI[0],tyI, txI[0],mtyI, mtxI[0],mtyI,
mtxI[0],tyI, txI[0],mtyI, txI[0], tyI,
mtxI[0],tyI, txI[0],mtyI, mtxI[0],mtyI,
mtxI[0],tyI, txI[0],mtyI, txI[0], tyI
};
// create a vertex array for the texture coordinates
VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);
texArray.set(0, tex.length / 2, tex);
// create the VertexBuffer
float posScale = 1.0f / (float)(I);
float texScale = 1.0f / (float)(2 * I);
m_diceVertBuffer = new VertexBuffer();
m_diceVertBuffer.setPositions(vertArray, posScale, null); // 1/UNITY scale, no bias
m_diceVertBuffer.setNormals(normArray);
m_diceVertBuffer.setTexCoords(0, texArray, texScale, null); // 1/(2*UNITY) scale, no bias
// the length of each triangle strip
int[] stripLen =
{
4,4,4, // face front
4,4,4, // face back
4,4,4, // face left
4,4,4, // face right
4,4,4, // face bottom
4,4,4, // face top
3,3,3,3, // near corners
3,3,3,3 // far corners
};
// create the index buffer for our object (this tells how to
// create triangle strips from the contents of the vertex buffer).
m_diceIndexBuffer = new TriangleStripArray(0, stripLen);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -