glide_test1

view mesh.cpp @ 0:f3ddb2bb7024

first 3dfx glide test, initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 09 Mar 2014 06:27:58 +0200
parents
children
line source
1 #define _USE_MATH_DEFINES
2 #include <math.h>
3 #include "mesh.h"
5 #ifndef M_PI
6 #define M_PI 3.14159265359
7 #endif
9 void gen_sphere(std::vector<Vertex> &varr, float rad, int usub, int vsub)
10 {
11 }
13 static void torus_vertex(Vertex *vert, float u, float v, float rad, float tube_rad)
14 {
15 float theta = 2.0 * M_PI * u;
16 float phi = 2.0 * M_PI * v;
18 float x = 0.0f;
19 float y = sin(phi) * tube_rad;
20 float z = cos(phi) * tube_rad + rad;
22 vert->pos.x = z * sin(theta);
23 vert->pos.y = y;
24 vert->pos.z = -x * sin(theta) + z * cos(theta);
25 vert->pos.w = 1.0;
27 Vector3 cent;
28 cent.x = sin(theta) * rad;
29 cent.z = cos(theta) * rad;
31 vert->normal = normalize(Vector3(vert->pos) - cent);
32 vert->color = Vector4(1.0, 1.0, 1.0, 1.0);
33 }
35 void gen_torus(std::vector<Vertex> &varr, float rad, float tube_rad, int usub, int vsub)
36 {
37 int i, j;
39 if(usub < 3) usub = 3;
40 if(vsub < 3) vsub = 3;
42 float du = 1.0f / (float)usub;
43 float dv = 1.0f / (float)vsub;
46 for(i=0; i<usub; i++) {
47 float u = (float)i / (float)usub;
48 for(j=0; j<vsub; j++) {
49 float v = (float)j / (float)vsub;
50 Vertex vert[4];
52 torus_vertex(vert, u, v, rad, tube_rad);
53 torus_vertex(vert + 1, u, v + dv, rad, tube_rad);
54 torus_vertex(vert + 2, u + du, v + dv, rad, tube_rad);
55 torus_vertex(vert + 3, u + du, v, rad, tube_rad);
57 varr.push_back(vert[0]);
58 varr.push_back(vert[1]);
59 varr.push_back(vert[2]);
60 varr.push_back(vert[0]);
61 varr.push_back(vert[2]);
62 varr.push_back(vert[3]);
64 }
65 }
66 }