rev |
line source |
nuclear@0
|
1 #include "objectgen.h"
|
nuclear@0
|
2 #include "n3dmath.h"
|
nuclear@0
|
3
|
nuclear@0
|
4 struct Quad {
|
nuclear@0
|
5 Index verts[4];
|
nuclear@0
|
6 Vector3 norm;
|
nuclear@0
|
7 };
|
nuclear@0
|
8
|
nuclear@0
|
9 ///////////////////////////////////////////////
|
nuclear@0
|
10 // --==( Generate a tesselated plane )==-- //
|
nuclear@0
|
11 ///////////////////////////////////////////////
|
nuclear@0
|
12
|
nuclear@0
|
13 void ObjGen::CreatePlane(GraphicsContext *gc, float size, dword subdivisions, TriMesh **mesh, byte LODLevels) {
|
nuclear@0
|
14
|
nuclear@0
|
15 dword VertexNum = (subdivisions+2) * (subdivisions+2);
|
nuclear@0
|
16 dword TriNum = ((subdivisions+1) * (subdivisions+1)) << 1;
|
nuclear@0
|
17 int QuadNum = TriNum >> 1;
|
nuclear@0
|
18 int QuadsRow = subdivisions+1; // quads per row
|
nuclear@0
|
19 int VertsRow = subdivisions+2; // vertices per row
|
nuclear@0
|
20 int VertsCol = VertsRow;
|
nuclear@0
|
21
|
nuclear@0
|
22 Vertex *varray = new Vertex[VertexNum];
|
nuclear@0
|
23 Triangle *tarray = new Triangle[TriNum];
|
nuclear@0
|
24
|
nuclear@0
|
25 for(int j=0; j<VertsCol; j++) {
|
nuclear@0
|
26 for(int i=0; i<VertsRow; i++) {
|
nuclear@0
|
27
|
nuclear@0
|
28 float u = (float)i/(float)QuadsRow;
|
nuclear@0
|
29 float v = (float)j/(float)QuadsRow;
|
nuclear@0
|
30 varray[j*VertsRow+i] = Vertex(Vector3(u - 0.5f, 1.0f - v - 0.5f, 0), u, v, 0x00ffffff);
|
nuclear@0
|
31 varray[j*VertsRow+i].pos *= size;
|
nuclear@0
|
32 }
|
nuclear@0
|
33 }
|
nuclear@0
|
34
|
nuclear@0
|
35 // first seperate the quads and then triangulate
|
nuclear@0
|
36 Quad *quads = new Quad[QuadNum];
|
nuclear@0
|
37
|
nuclear@0
|
38 for(int i=0; i<QuadNum; i++) {
|
nuclear@0
|
39 quads[i].verts[0] = i + i/QuadsRow;
|
nuclear@0
|
40 quads[i].verts[1] = quads[i].verts[0] + 1;
|
nuclear@0
|
41 quads[i].verts[2] = quads[i].verts[0] + VertsRow;
|
nuclear@0
|
42 quads[i].verts[3] = quads[i].verts[1] + VertsRow;
|
nuclear@0
|
43 }
|
nuclear@0
|
44
|
nuclear@0
|
45 for(int i=0; i<QuadNum; i++) {
|
nuclear@0
|
46 tarray[i<<1] = Triangle(quads[i].verts[0], quads[i].verts[1], quads[i].verts[3]);
|
nuclear@0
|
47 tarray[(i<<1)+1] = Triangle(quads[i].verts[0], quads[i].verts[3], quads[i].verts[2]);
|
nuclear@0
|
48 }
|
nuclear@0
|
49
|
nuclear@0
|
50 for(dword i=0; i<VertexNum; i++) {
|
nuclear@0
|
51 varray[i].normal = Vector3(0, 0, -1);
|
nuclear@0
|
52 }
|
nuclear@0
|
53
|
nuclear@0
|
54 *mesh = new TriMesh(LODLevels, gc);
|
nuclear@0
|
55 (*mesh)->SetData(varray, tarray, VertexNum, TriNum);
|
nuclear@0
|
56
|
nuclear@0
|
57 delete [] quads;
|
nuclear@0
|
58 delete [] varray;
|
nuclear@0
|
59 delete [] tarray;
|
nuclear@0
|
60 }
|
nuclear@0
|
61
|
nuclear@0
|
62
|
nuclear@0
|
63
|
nuclear@0
|
64
|
nuclear@0
|
65 /////////////////////////////////////////////////////////
|
nuclear@0
|
66 // --==( Generate Cube without shared vertices )==-- //
|
nuclear@0
|
67 /////////////////////////////////////////////////////////
|
nuclear@0
|
68
|
nuclear@0
|
69 void ObjGen::CreateCube(GraphicsContext *gc, float size, TriMesh **mesh, byte LODLevels) {
|
nuclear@0
|
70
|
nuclear@0
|
71 Vertex verts[36];
|
nuclear@0
|
72 verts[0] = Vertex(Vector3(-1, +1, -1), 0, 0);
|
nuclear@0
|
73 verts[1] = Vertex(Vector3(+1, +1, -1), 1, 0);
|
nuclear@0
|
74 verts[2] = Vertex(Vector3(+1, -1, -1), 1, 1);
|
nuclear@0
|
75 verts[3] = Vertex(Vector3(-1, +1, -1), 0, 0);
|
nuclear@0
|
76 verts[4] = Vertex(Vector3(+1, -1, -1), 1, 1);
|
nuclear@0
|
77 verts[5] = Vertex(Vector3(-1, -1, -1), 0, 1);
|
nuclear@0
|
78 verts[6] = Vertex(Vector3(-1, +1, +1), 0, 0);
|
nuclear@0
|
79 verts[7] = Vertex(Vector3(-1, +1, -1), 1, 0);
|
nuclear@0
|
80 verts[8] = Vertex(Vector3(-1, -1, -1), 1, 1);
|
nuclear@0
|
81 verts[9] = Vertex(Vector3(-1, +1, +1), 0, 0);
|
nuclear@0
|
82 verts[10] = Vertex(Vector3(-1, -1, -1), 1, 1);
|
nuclear@0
|
83 verts[11] = Vertex(Vector3(-1, -1, +1), 0, 1);
|
nuclear@0
|
84 verts[12] = Vertex(Vector3(-1, +1, +1), 1, 0);
|
nuclear@0
|
85 verts[13] = Vertex(Vector3(-1, -1, +1), 1, 1);
|
nuclear@0
|
86 verts[14] = Vertex(Vector3(+1, +1, +1), 0, 0);
|
nuclear@0
|
87 verts[15] = Vertex(Vector3(+1, +1, +1), 0, 0);
|
nuclear@0
|
88 verts[16] = Vertex(Vector3(-1, -1, +1), 1, 1);
|
nuclear@0
|
89 verts[17] = Vertex(Vector3(+1, -1, +1), 0, 1);
|
nuclear@0
|
90 verts[18] = Vertex(Vector3(+1, +1, -1), 0, 0);
|
nuclear@0
|
91 verts[19] = Vertex(Vector3(+1, +1, +1), 1, 0);
|
nuclear@0
|
92 verts[20] = Vertex(Vector3(+1, -1, +1), 1, 1);
|
nuclear@0
|
93 verts[21] = Vertex(Vector3(+1, +1, -1), 0, 0);
|
nuclear@0
|
94 verts[22] = Vertex(Vector3(+1, -1, +1), 1, 1);
|
nuclear@0
|
95 verts[23] = Vertex(Vector3(+1, -1, -1), 0, 1);
|
nuclear@0
|
96 verts[24] = Vertex(Vector3(-1, +1, -1), 0, 1);
|
nuclear@0
|
97 verts[25] = Vertex(Vector3(+1, +1, +1), 1, 0);
|
nuclear@0
|
98 verts[26] = Vertex(Vector3(+1, +1, -1), 1, 1);
|
nuclear@0
|
99 verts[27] = Vertex(Vector3(-1, +1, +1), 0, 0);
|
nuclear@0
|
100 verts[28] = Vertex(Vector3(+1, +1, +1), 1, 0);
|
nuclear@0
|
101 verts[29] = Vertex(Vector3(-1, +1, -1), 0, 1);
|
nuclear@0
|
102 verts[30] = Vertex(Vector3(-1, -1, +1), 0, 1);
|
nuclear@0
|
103 verts[31] = Vertex(Vector3(-1, -1, -1), 0, 0);
|
nuclear@0
|
104 verts[32] = Vertex(Vector3(+1, -1, +1), 1, 1);
|
nuclear@0
|
105 verts[33] = Vertex(Vector3(-1, -1, -1), 0, 0);
|
nuclear@0
|
106 verts[34] = Vertex(Vector3(+1, -1, -1), 1, 0);
|
nuclear@0
|
107 verts[35] = Vertex(Vector3(+1, -1, +1), 1, 1);
|
nuclear@0
|
108
|
nuclear@0
|
109 Triangle tri[12];
|
nuclear@0
|
110 for(int i=0; i<12; i++) {
|
nuclear@0
|
111 tri[i].vertices[0] = i*3;
|
nuclear@0
|
112 tri[i].vertices[1] = i*3+1;
|
nuclear@0
|
113 tri[i].vertices[2] = i*3+2;
|
nuclear@0
|
114 }
|
nuclear@0
|
115
|
nuclear@0
|
116 for(int i=0; i<36; i++) {
|
nuclear@0
|
117 verts[i].pos *= size / 2.0f;
|
nuclear@0
|
118 verts[i].color = 0x00ffffff;
|
nuclear@0
|
119 }
|
nuclear@0
|
120
|
nuclear@0
|
121 TriMesh *tmpmesh = new TriMesh(LODLevels, gc);
|
nuclear@0
|
122 tmpmesh->SetData(verts, tri, 36, 12);
|
nuclear@0
|
123
|
nuclear@0
|
124 tmpmesh->CalculateNormals();
|
nuclear@0
|
125
|
nuclear@0
|
126 *mesh = tmpmesh;
|
nuclear@0
|
127 }
|
nuclear@0
|
128
|
nuclear@0
|
129
|
nuclear@0
|
130 /////////////////////////////////////
|
nuclear@0
|
131 // --==( Generate Cylinder )==-- //
|
nuclear@0
|
132 /////////////////////////////////////
|
nuclear@0
|
133 /*
|
nuclear@0
|
134 void ObjGen::CreateCylinder(float radius, float height, int sides, int hsegs, bool caps, TriMesh **mesh, byte LODLevels) {
|
nuclear@0
|
135
|
nuclear@0
|
136 int slices = hsegs + 1;
|
nuclear@0
|
137 unsigned long vertex_count = sides * slices + (caps ? (sides + 1)<<1 : 0);
|
nuclear@0
|
138 unsigned long tri_count = (sides << 1) * (slices - 1) + (caps ? sides : 0);
|
nuclear@0
|
139
|
nuclear@0
|
140 Vertex *varray = new Vertex[vertex_count];
|
nuclear@0
|
141 Triangle *tarray = new Triangle[tri_count];
|
nuclear@0
|
142
|
nuclear@0
|
143 Vertex *vptr = varray;
|
nuclear@0
|
144 Triangle *tptr = tarray;
|
nuclear@0
|
145
|
nuclear@0
|
146 // create a circle by rotating a vector around the y axis
|
nuclear@0
|
147 Vector3 x_axis(radius, 0, 0);
|
nuclear@0
|
148
|
nuclear@0
|
149 float rot_rads = TwoPi / (float)sides;
|
nuclear@0
|
150 for(int i=0; i<sides; i++) {
|
nuclear@0
|
151 Vector3 pos = x_axis;
|
nuclear@0
|
152 pos.Rotate(0, rot_rads * (float)i, 0);
|
nuclear@0
|
153
|
nuclear@0
|
154 *vptr++ = pos;
|
nuclear@0
|
155 */ |