8
Texel::Texel(GLfloat pU, GLfloat pV)
24
glDeleteLists(1, mBuildList);
28
void Mesh::make_cube()
31
mVertexQty = 4 * mPolygonQty;
33
mVertex = new Vertex[mVertexQty];
35
mVertex[0].v = Vector3f( 1.0f, 1.0f,-1.0f);
36
mVertex[1].v = Vector3f(-1.0f, 1.0f,-1.0f);
37
mVertex[2].v = Vector3f(-1.0f, 1.0f, 1.0f);
38
mVertex[3].v = Vector3f( 1.0f, 1.0f, 1.0f);
40
mVertex[4].v = Vector3f( 1.0f,-1.0f, 1.0f);
41
mVertex[5].v = Vector3f(-1.0f,-1.0f, 1.0f);
42
mVertex[6].v = Vector3f(-1.0f,-1.0f,-1.0f);
43
mVertex[7].v = Vector3f( 1.0f,-1.0f,-1.0f);
45
mVertex[8].v = Vector3f( 1.0f, 1.0f, 1.0f);
46
mVertex[9].v = Vector3f(-1.0f, 1.0f, 1.0f);
47
mVertex[10].v = Vector3f(-1.0f,-1.0f, 1.0f);
48
mVertex[11].v = Vector3f( 1.0f,-1.0f, 1.0f);
50
mVertex[12].v = Vector3f( 1.0f,-1.0f,-1.0f);
51
mVertex[13].v = Vector3f(-1.0f,-1.0f,-1.0f);
52
mVertex[14].v = Vector3f(-1.0f, 1.0f,-1.0f);
53
mVertex[15].v = Vector3f( 1.0f, 1.0f,-1.0f);
55
mVertex[16].v = Vector3f(-1.0f, 1.0f, 1.0f);
56
mVertex[17].v = Vector3f(-1.0f, 1.0f,-1.0f);
57
mVertex[18].v = Vector3f(-1.0f,-1.0f,-1.0f);
58
mVertex[19].v = Vector3f(-1.0f,-1.0f, 1.0f);
60
mVertex[20].v = Vector3f(-1.0f, 1.0f, 1.0f);
61
mVertex[21].v = Vector3f(-1.0f, 1.0f,-1.0f);
62
mVertex[22].v = Vector3f(-1.0f,-1.0f,-1.0f);
63
mVertex[23].v = Vector3f(-1.0f,-1.0f, 1.0f);
65
for(unsigned i = 0; i < mVertexQty / 4; i += 4)
68
n = normal(mVertex[i].v, mVertex[i + 1].v, mVertex[i + 2].v);
69
for(unsigned j = 0; j < 4; j++)
71
mVertex[i + 0].t = Texel(0.0f, 0.0f);
72
mVertex[i + 1].t = Texel(1.0f, 0.0f);
73
mVertex[i + 2].t = Texel(1.0f, 1.0f);
74
mVertex[i + 3].t = Texel(0.0f, 1.0f);
78
void Mesh::make_torus()
80
unsigned wraps_qty = 64;
81
unsigned per_wrap_qty = 64;
82
float major_radius = 0.8;
83
float minor_radius = 0.4;
87
Vector3f a, b, c, d, n;
90
mVertexQty = wraps_qty * per_wrap_qty * 6;
91
mVertex = new Vertex[mVertexQty];
93
for(i = 0; i < wraps_qty; i++)
94
for(j = 0; j < per_wrap_qty; j++)
96
float wrap_frac = j / (float)per_wrap_qty;
97
float phi = 2 * M_PI * wrap_frac;
98
float theta = 2 * M_PI * (i + wrap_frac) / (float)wraps_qty;
99
float r = major_radius + minor_radius * (float)cos(phi);
100
a.x = (float)sin(theta) * r;
101
a.y = minor_radius * (float)sin(phi);
102
a.z = (float)cos(theta) * r;
104
theta = 2 * M_PI * (i + wrap_frac + 1) / (float)wraps_qty;
105
b.x = (float)sin(theta) * r;
106
b.y = minor_radius * (float)sin(phi);
107
b.z = (float)cos(theta) * r;
109
wrap_frac = (j + 1) / (float)per_wrap_qty;
110
phi = 2 * M_PI * wrap_frac;
111
theta = 2 * M_PI * (i + wrap_frac) / (float)wraps_qty;
112
r = major_radius + minor_radius * (float)cos(phi);
113
c.x = (float)sin(theta) * r;
114
c.y = minor_radius * (float)sin(phi);
115
c.z = (float)cos(theta) * r;
117
theta = 2 * M_PI * (i + wrap_frac + 1) / (float)wraps_qty;
118
d.x = (float)sin(theta) * r;
119
d.y = minor_radius * (float)sin(phi);
120
d.z = (float)cos(theta) * r;
123
mVertex[k].n = n; mVertex[k].v = a; k++;
124
mVertex[k].n = n; mVertex[k].v = b; k++;
125
mVertex[k].n = n; mVertex[k].v = c; k++;
127
mVertex[k].n = n; mVertex[k].v = b; k++;
128
mVertex[k].n = n; mVertex[k].v = c; k++;
129
mVertex[k].n = n; mVertex[k].v = d; k++;
136
glColor3f(1.0f, 0.0f, 0.0f);
137
for(unsigned i = 0; i < mVertexQty; i++)
139
glNormal3f(mVertex[i].n.x, mVertex[i].n.y, mVertex[i].n.z);
140
glTexCoord2f(mVertex[i].t.u, mVertex[i].t.v);
141
glVertex3f(mVertex[i].v.x, mVertex[i].v.y, mVertex[i].v.z);
146
void Mesh::render_array()
148
glEnableClientState(GL_VERTEX_ARRAY);
149
glEnableClientState(GL_NORMAL_ARRAY);
151
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), &mVertex[0].v.x);
152
glNormalPointer(GL_FLOAT, sizeof(Vertex), &mVertex[0].n.x);
153
glDrawArrays(mMode, 0, mVertexQty);
155
glDisableClientState(GL_VERTEX_ARRAY);
156
glDisableClientState(GL_NORMAL_ARRAY);
159
void Mesh::build_list()
162
printf("Building list for mesh... ");
165
mBuildList = glGenLists(1);
166
glNewList(mBuildList, GL_COMPILE);
171
printf("[ Done ]\n");
175
void Mesh::build_vbo()
178
printf("Building vbo for mesh... ");
185
vertex = new Vector3f[mVertexQty];
186
texel = new Texel[mVertexQty];
187
normal = new Vector3f[mVertexQty];
189
for(unsigned i = 0; i < mVertexQty; i++)
191
vertex[i] = mVertex[i].v;
192
texel[i] = mVertex[i].t;
193
normal[i] = mVertex[i].n;
196
// Generate And Bind The Vertex Buffer
197
glGenBuffersARB(1, &mVBOVertices); // Get A Valid Name
198
glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVBOVertices); // Bind The Buffer
200
glBufferDataARB(GL_ARRAY_BUFFER_ARB, mVertexQty * sizeof(Vector3f), vertex, GL_STATIC_DRAW_ARB);
202
// Generate And Bind The normal Buffer
203
glGenBuffersARB(1, &mVBONormals); // Get A Valid Name
204
glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVBONormals); // Bind The Buffer
206
glBufferDataARB(GL_ARRAY_BUFFER_ARB, mVertexQty * sizeof(Vector3f), normal, GL_STATIC_DRAW_ARB);
208
// Generate And Bind The Texture Coordinate Buffer
209
glGenBuffersARB(1, &mVBOTexCoords); // Get A Valid Name
210
glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVBOTexCoords); // Bind The Buffer
212
glBufferDataARB(GL_ARRAY_BUFFER_ARB, mVertexQty * sizeof(Texel), texel, GL_STATIC_DRAW_ARB);
214
glBindBuffer(GL_ARRAY_BUFFER, 0);
220
printf("[ Done ]\n");
224
void Mesh::render_vbo()
226
glEnableClientState(GL_VERTEX_ARRAY);
227
glEnableClientState(GL_NORMAL_ARRAY);
229
glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVBONormals);
230
glNormalPointer(GL_FLOAT, 0, 0);
231
glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVBOVertices);
232
glVertexPointer(3, GL_FLOAT, 0, 0);
234
glDrawArrays(GL_TRIANGLES, 0, mVertexQty);
236
glBindBuffer(GL_ARRAY_BUFFER, 0);
238
glDisableClientState(GL_NORMAL_ARRAY);
239
glDisableClientState(GL_VERTEX_ARRAY);