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