~glmark2-dev/glmark2/libmatrix-util

« back to all changes in this revision

Viewing changes to mesh.cpp

  • Committer: Alexandros Frantzis
  • Date: 2010-07-07 10:32:18 UTC
  • Revision ID: git-v1:d2683504fc3cc84e7f3aefd917147037706cecf0
Initial release of original OpenGL code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "mesh.h"
 
2
 
 
3
Texel::Texel()
 
4
{
 
5
    u = 0;  v = 0;
 
6
}
 
7
 
 
8
Texel::Texel(GLfloat pU, GLfloat pV)
 
9
{
 
10
    u = pU; v = pV;
 
11
}
 
12
    
 
13
Mesh::Mesh()
 
14
{
 
15
    mPolygonQty = 0;
 
16
    mVertexQty = 0;
 
17
    mMode = GL_QUADS;
 
18
    mVertex = 0;
 
19
}
 
20
 
 
21
Mesh::~Mesh()
 
22
{
 
23
    delete [] mVertex;
 
24
    glDeleteLists(1, mBuildList);
 
25
    //deleteArray
 
26
}
 
27
 
 
28
void Mesh::make_cube()
 
29
{
 
30
    mPolygonQty = 6;
 
31
    mVertexQty = 4 * mPolygonQty;
 
32
    mMode = GL_QUADS;
 
33
    mVertex = new Vertex[mVertexQty];
 
34
    
 
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);
 
39
 
 
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);
 
44
    
 
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);
 
49
 
 
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);
 
54
 
 
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);
 
59
 
 
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);
 
64
 
 
65
    for(unsigned i = 0; i < mVertexQty / 4; i += 4)
 
66
    {
 
67
        Vector3f n;
 
68
        n = normal(mVertex[i].v, mVertex[i + 1].v, mVertex[i + 2].v);
 
69
        for(unsigned j = 0; j < 4; j++)
 
70
            mVertex[i + j].n = n;
 
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);
 
75
    }
 
76
}
 
77
 
 
78
void Mesh::make_torus()
 
79
{
 
80
    unsigned wraps_qty = 64;
 
81
    unsigned per_wrap_qty = 64;
 
82
    float major_radius = 0.8;
 
83
    float minor_radius = 0.4;
 
84
    unsigned i, j;
 
85
    unsigned k = 0;
 
86
    
 
87
    Vector3f a, b, c, d, n;
 
88
 
 
89
    mMode = GL_TRIANGLES;
 
90
    mVertexQty = wraps_qty * per_wrap_qty * 6;
 
91
    mVertex = new Vertex[mVertexQty];
 
92
    
 
93
    for(i = 0; i < wraps_qty; i++)
 
94
        for(j = 0; j < per_wrap_qty; j++)
 
95
        {
 
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;
 
103
            
 
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;
 
108
 
 
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;
 
116
 
 
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;
 
121
 
 
122
            n = normal(a, b, c);
 
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++;
 
126
            n = normal(a, b, c);
 
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++;            
 
130
        }    
 
131
}
 
132
 
 
133
void Mesh::render()
 
134
{
 
135
    glBegin(mMode);
 
136
    glColor3f(1.0f, 0.0f, 0.0f);
 
137
    for(unsigned i = 0; i < mVertexQty; i++)
 
138
    {
 
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);
 
142
    }
 
143
    glEnd();
 
144
}
 
145
 
 
146
void Mesh::render_array()
 
147
{
 
148
    glEnableClientState(GL_VERTEX_ARRAY);
 
149
    glEnableClientState(GL_NORMAL_ARRAY);
 
150
 
 
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);
 
154
 
 
155
    glDisableClientState(GL_VERTEX_ARRAY);
 
156
    glDisableClientState(GL_NORMAL_ARRAY);
 
157
}
 
158
 
 
159
void Mesh::build_list()
 
160
{
 
161
#ifdef _DEBUG
 
162
    printf("Building list for mesh...        ");
 
163
#endif
 
164
 
 
165
    mBuildList = glGenLists(1);
 
166
    glNewList(mBuildList, GL_COMPILE);
 
167
    render();
 
168
    glEndList();
 
169
 
 
170
#ifdef _DEBUG
 
171
    printf("[ Done ]\n");
 
172
#endif
 
173
}
 
174
 
 
175
void Mesh::build_vbo()
 
176
{
 
177
#ifdef _DEBUG
 
178
    printf("Building vbo for mesh...         ");
 
179
#endif
 
180
 
 
181
    Vector3f *vertex;
 
182
    Texel *texel;
 
183
    Vector3f *normal;
 
184
    
 
185
    vertex = new Vector3f[mVertexQty];
 
186
    texel = new Texel[mVertexQty];
 
187
    normal = new Vector3f[mVertexQty];
 
188
    
 
189
    for(unsigned i = 0; i < mVertexQty; i++)
 
190
    {
 
191
        vertex[i] = mVertex[i].v;
 
192
        texel[i] = mVertex[i].t;
 
193
        normal[i] = mVertex[i].n;
 
194
    }
 
195
 
 
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
 
199
        // Load The Data
 
200
        glBufferDataARB(GL_ARRAY_BUFFER_ARB, mVertexQty * sizeof(Vector3f), vertex, GL_STATIC_DRAW_ARB);
 
201
 
 
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
 
205
        // Load The Data
 
206
        glBufferDataARB(GL_ARRAY_BUFFER_ARB, mVertexQty * sizeof(Vector3f), normal, GL_STATIC_DRAW_ARB);
 
207
 
 
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
 
211
        // Load The Data
 
212
        glBufferDataARB(GL_ARRAY_BUFFER_ARB, mVertexQty * sizeof(Texel), texel, GL_STATIC_DRAW_ARB);
 
213
 
 
214
    glBindBuffer(GL_ARRAY_BUFFER, 0);
 
215
    
 
216
    delete [] vertex;
 
217
    delete [] texel;
 
218
    delete [] normal;
 
219
#ifdef _DEBUG
 
220
    printf("[ Done ]\n");
 
221
#endif
 
222
}
 
223
 
 
224
void Mesh::render_vbo()
 
225
{
 
226
    glEnableClientState(GL_VERTEX_ARRAY);
 
227
    glEnableClientState(GL_NORMAL_ARRAY);
 
228
 
 
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);
 
233
 
 
234
    glDrawArrays(GL_TRIANGLES, 0, mVertexQty);
 
235
 
 
236
    glBindBuffer(GL_ARRAY_BUFFER, 0);
 
237
 
 
238
    glDisableClientState(GL_NORMAL_ARRAY);
 
239
    glDisableClientState(GL_VERTEX_ARRAY); 
 
240
}