vrshoot
view src/meshgen.cc @ 2:334d17aed7de
visual studio project files
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 02 Feb 2014 18:36:38 +0200 |
parents | |
children |
line source
1 #include "meshgen.h"
2 #include "mesh.h"
3 #include "logger.h"
5 // -------- sphere --------
7 #define SURAD(u) ((u) * 2.0 * M_PI)
8 #define SVRAD(v) ((v) * M_PI)
10 static Vector3 sphvec(float theta, float phi)
11 {
12 return Vector3(sin(theta) * sin(phi),
13 cos(phi),
14 cos(theta) * sin(phi));
15 }
17 void gen_sphere(Mesh *mesh, float rad, int usub, int vsub, float urange, float vrange)
18 {
19 if(usub < 4) usub = 4;
20 if(vsub < 2) vsub = 2;
22 int uverts = usub + 1;
23 int vverts = vsub + 1;
25 int num_verts = uverts * vverts;
26 int num_quads = usub * vsub;
27 int num_tri = num_quads * 2;
29 mesh->clear();
30 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
31 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
32 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
33 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
34 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
36 float du = urange / (float)(uverts - 1);
37 float dv = vrange / (float)(vverts - 1);
39 float u = 0.0;
40 for(int i=0; i<uverts; i++) {
41 float theta = SURAD(u * urange);
43 float v = 0.0;
44 for(int j=0; j<vverts; j++) {
45 float phi = SVRAD(v * vrange);
47 Vector3 pos = sphvec(theta, phi);
49 *varr++ = pos * rad;
50 *narr++ = pos;
51 *tarr++ = (sphvec(theta + 0.1, M_PI / 2.0) - sphvec(theta - 0.1, M_PI / 2.0)).normalized();
52 *uvarr++ = Vector2(u * urange, v * vrange);
54 if(i < usub && j < vsub) {
55 int idx = i * vverts + j;
56 *idxarr++ = idx;
57 *idxarr++ = idx + 1;
58 *idxarr++ = idx + vverts + 1;
60 *idxarr++ = idx;
61 *idxarr++ = idx + vverts + 1;
62 *idxarr++ = idx + vverts;
63 }
65 v += dv;
66 }
67 u += du;
68 }
69 }
72 // -------- cylinder --------
74 static Vector3 cylvec(float theta, float height)
75 {
76 return Vector3(sin(theta), height, cos(theta));
77 }
79 void gen_cylinder(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
80 {
81 if(usub < 4) usub = 4;
82 if(vsub < 1) vsub = 1;
84 int uverts = usub + 1;
85 int vverts = vsub + 1;
87 int num_body_verts = uverts * vverts;
88 int num_body_quads = usub * vsub;
89 int num_body_tri = num_body_quads * 2;
91 int capvverts = capsub ? capsub + 1 : 0;
92 int num_cap_verts = uverts * capvverts;
93 int num_cap_quads = usub * capsub;
94 int num_cap_tri = num_cap_quads * 2;
96 int num_verts = num_body_verts + num_cap_verts * 2;
97 int num_tri = num_body_tri + num_cap_tri * 2;
99 mesh->clear();
100 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
101 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
102 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
103 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
104 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
106 float du = urange / (float)(uverts - 1);
107 float dv = vrange / (float)(vverts - 1);
109 float u = 0.0;
110 for(int i=0; i<uverts; i++) {
111 float theta = SURAD(u);
113 float v = 0.0;
114 for(int j=0; j<vverts; j++) {
115 float y = (v - 0.5) * height;
116 Vector3 pos = cylvec(theta, y);
118 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
119 *narr++ = Vector3(pos.x, 0.0, pos.z);
120 *tarr++ = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
121 *uvarr++ = Vector2(u * urange, v * vrange);
123 if(i < usub && j < vsub) {
124 int idx = i * vverts + j;
126 *idxarr++ = idx;
127 *idxarr++ = idx + vverts + 1;
128 *idxarr++ = idx + 1;
130 *idxarr++ = idx;
131 *idxarr++ = idx + vverts;
132 *idxarr++ = idx + vverts + 1;
133 }
135 v += dv;
136 }
137 u += du;
138 }
141 // now the cap!
142 if(!capsub) {
143 return;
144 }
146 dv = 1.0 / (float)(capvverts - 1);
148 u = 0.0;
149 for(int i=0; i<uverts; i++) {
150 float theta = SURAD(u);
152 float v = 0.0;
153 for(int j=0; j<capvverts; j++) {
154 float r = v * rad;
156 Vector3 pos = cylvec(theta, height / 2.0) * r;
157 pos.y = height / 2.0;
158 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
160 *varr++ = pos;
161 *narr++ = Vector3(0, 1, 0);
162 *tarr++ = tang;
163 *uvarr++ = Vector2(u * urange, v);
165 pos.y = -height / 2.0;
166 *varr++ = pos;
167 *narr++ = Vector3(0, -1, 0);
168 *tarr++ = -tang;
169 *uvarr++ = Vector2(u * urange, v);
171 if(i < usub && j < capsub) {
172 unsigned int idx = num_body_verts + (i * capvverts + j) * 2;
174 unsigned int vidx[4] = {
175 idx,
176 idx + capvverts * 2,
177 idx + (capvverts + 1) * 2,
178 idx + 2
179 };
181 *idxarr++ = vidx[0];
182 *idxarr++ = vidx[2];
183 *idxarr++ = vidx[1];
184 *idxarr++ = vidx[0];
185 *idxarr++ = vidx[3];
186 *idxarr++ = vidx[2];
188 *idxarr++ = vidx[0] + 1;
189 *idxarr++ = vidx[1] + 1;
190 *idxarr++ = vidx[2] + 1;
191 *idxarr++ = vidx[0] + 1;
192 *idxarr++ = vidx[2] + 1;
193 *idxarr++ = vidx[3] + 1;
194 }
196 v += dv;
197 }
198 u += du;
199 }
200 }
202 // -------- cone --------
204 static Vector3 conevec(float theta, float y, float height)
205 {
206 float scale = 1.0 - y / height;
207 return Vector3(sin(theta) * scale, y, cos(theta) * scale);
208 }
210 void gen_cone(Mesh *mesh, float rad, float height, int usub, int vsub, int capsub, float urange, float vrange)
211 {
212 if(usub < 4) usub = 4;
213 if(vsub < 1) vsub = 1;
215 int uverts = usub + 1;
216 int vverts = vsub + 1;
218 int num_body_verts = uverts * vverts;
219 int num_body_quads = usub * vsub;
220 int num_body_tri = num_body_quads * 2;
222 int capvverts = capsub ? capsub + 1 : 0;
223 int num_cap_verts = uverts * capvverts;
224 int num_cap_quads = usub * capsub;
225 int num_cap_tri = num_cap_quads * 2;
227 int num_verts = num_body_verts + num_cap_verts;
228 int num_tri = num_body_tri + num_cap_tri;
230 mesh->clear();
231 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
232 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
233 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
234 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
235 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
237 float du = urange / (float)(uverts - 1);
238 float dv = vrange / (float)(vverts - 1);
240 float u = 0.0;
241 for(int i=0; i<uverts; i++) {
242 float theta = SURAD(u);
244 float v = 0.0;
245 for(int j=0; j<vverts; j++) {
246 float y = v * height;
247 Vector3 pos = conevec(theta, y, height);
249 Vector3 tang = (conevec(theta + 0.1, 0.0, height) - conevec(theta - 0.1, 0.0, height)).normalized();
250 Vector3 bitang = (conevec(theta, y + 0.1, height) - pos).normalized();
252 *varr++ = Vector3(pos.x * rad, pos.y, pos.z * rad);
253 *narr++ = cross_product(tang, bitang);
254 *tarr++ = tang;
255 *uvarr++ = Vector2(u * urange, v * vrange);
257 if(i < usub && j < vsub) {
258 int idx = i * vverts + j;
260 *idxarr++ = idx;
261 *idxarr++ = idx + vverts + 1;
262 *idxarr++ = idx + 1;
264 *idxarr++ = idx;
265 *idxarr++ = idx + vverts;
266 *idxarr++ = idx + vverts + 1;
267 }
269 v += dv;
270 }
271 u += du;
272 }
275 // now the bottom cap!
276 if(!capsub) {
277 return;
278 }
280 dv = 1.0 / (float)(capvverts - 1);
282 u = 0.0;
283 for(int i=0; i<uverts; i++) {
284 float theta = SURAD(u);
286 float v = 0.0;
287 for(int j=0; j<capvverts; j++) {
288 float r = v * rad;
290 Vector3 pos = conevec(theta, 0.0, height) * r;
291 Vector3 tang = (cylvec(theta + 0.1, 0.0) - cylvec(theta - 0.1, 0.0)).normalized();
293 *varr++ = pos;
294 *narr++ = Vector3(0, -1, 0);
295 *tarr++ = tang;
296 *uvarr++ = Vector2(u * urange, v);
298 if(i < usub && j < capsub) {
299 unsigned int idx = num_body_verts + i * capvverts + j;
301 unsigned int vidx[4] = {
302 idx,
303 idx + capvverts,
304 idx + (capvverts + 1),
305 idx + 1
306 };
308 *idxarr++ = vidx[0];
309 *idxarr++ = vidx[1];
310 *idxarr++ = vidx[2];
311 *idxarr++ = vidx[0];
312 *idxarr++ = vidx[2];
313 *idxarr++ = vidx[3];
314 }
316 v += dv;
317 }
318 u += du;
319 }
320 }
323 // -------- plane --------
325 void gen_plane(Mesh *mesh, float width, float height, int usub, int vsub)
326 {
327 if(usub < 1) usub = 1;
328 if(vsub < 1) vsub = 1;
330 mesh->clear();
332 int uverts = usub + 1;
333 int vverts = vsub + 1;
334 int num_verts = uverts * vverts;
336 int num_quads = usub * vsub;
337 int num_tri = num_quads * 2;
339 Vector3 *varr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, num_verts, 0);
340 Vector3 *narr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, num_verts, 0);
341 Vector3 *tarr = (Vector3*)mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, num_verts, 0);
342 Vector2 *uvarr = (Vector2*)mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, num_verts, 0);
343 unsigned int *idxarr = mesh->set_index_data(num_tri * 3, 0);
345 float du = 1.0 / (float)usub;
346 float dv = 1.0 / (float)vsub;
348 float u = 0.0;
349 for(int i=0; i<uverts; i++) {
350 float v = 0.0;
351 for(int j=0; j<vverts; j++) {
352 *varr++ = Vector3((u - 0.5) * width, (v - 0.5) * height, 0.0);
353 *narr++ = Vector3(0, 0, 1);
354 *tarr++ = Vector3(1, 0, 0);
355 *uvarr++ = Vector2(u, v);
357 if(i < usub && j < vsub) {
358 int idx = i * vverts + j;
360 *idxarr++ = idx;
361 *idxarr++ = idx + vverts + 1;
362 *idxarr++ = idx + 1;
364 *idxarr++ = idx;
365 *idxarr++ = idx + vverts;
366 *idxarr++ = idx + vverts + 1;
367 }
369 v += dv;
370 }
371 u += du;
372 }
373 }