-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh.cpp
178 lines (121 loc) · 4.54 KB
/
Mesh.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*-----------------------------------------------------------------------------
Mesh.cpp
2009 Shamus Young
-------------------------------------------------------------------------------
This class is used to make constructing objects easier. It handles
allocating vertex lists, polygon lists, and suchlike.
If you were going to implement vertex buffers, this would be the place to
do it. Take away the _vertex member variable and store verts for ALL meshes
in a common list, which could then be unloaded onto the good 'ol GPU.
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <vector>
#include "glTypes.h"
#include "Mesh.h"
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CMesh::CMesh ()
{
_list = glGenLists(1);
_compiled = false;
_polycount = 0;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
CMesh::~CMesh ()
{
glDeleteLists (_list, 1);
_vertex.clear ();
_fan.clear ();
_quad_strip.clear ();
_cube.clear ();
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::VertexAdd (const GLvertex& v)
{
_vertex.push_back(v);
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::CubeAdd (const cube& c)
{
_cube.push_back(c);
_polycount += 5;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::QuadStripAdd (const quad_strip& qs)
{
_quad_strip.push_back(qs);
_polycount += (qs.index_list.size() - 2) / 2;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::FanAdd (const fan& f)
{
_fan.push_back(f);
_polycount += f.index_list.size() - 2;
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::Render ()
{
std::vector<quad_strip>::iterator qsi;
std::vector<cube>::iterator ci;
std::vector<fan>::iterator fi;
std::vector<int>::iterator n;
if (_compiled) {
glCallList (_list);
return;
}
for (qsi = _quad_strip.begin(); qsi < _quad_strip.end(); ++qsi) {
glBegin (GL_QUAD_STRIP);
for (n = qsi->index_list.begin(); n < qsi->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
}
for (ci = _cube.begin(); ci < _cube.end(); ++ci) {
glBegin (GL_QUAD_STRIP);
for (n = ci->index_list.begin(); n < ci->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[ci->index_list[7]].uv.x);
glVertex3fv (&_vertex[ci->index_list[7]].position.x);
glVertex3fv (&_vertex[ci->index_list[5]].position.x);
glVertex3fv (&_vertex[ci->index_list[3]].position.x);
glVertex3fv (&_vertex[ci->index_list[1]].position.x);
glEnd ();
glBegin (GL_QUADS);
glTexCoord2fv (&_vertex[ci->index_list[6]].uv.x);
glVertex3fv (&_vertex[ci->index_list[0]].position.x);
glVertex3fv (&_vertex[ci->index_list[2]].position.x);
glVertex3fv (&_vertex[ci->index_list[4]].position.x);
glVertex3fv (&_vertex[ci->index_list[6]].position.x);
glEnd ();
}
for (fi = _fan.begin(); fi < _fan.end(); ++fi) {
glBegin (GL_TRIANGLE_FAN);
for (n = fi->index_list.begin(); n < fi->index_list.end(); ++n) {
glTexCoord2fv (&_vertex[*n].uv.x);
glVertex3fv (&_vertex[*n].position.x);
}
glEnd ();
}
}
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void CMesh::Compile ()
{
glNewList (_list, GL_COMPILE);
Render ();
glEndList();
_compiled = true;
}