source: GTP/trunk/App/Demos/Vis/CHC_revisited/Geometry.cpp @ 2756

Revision 2756, 2.0 KB checked in by mattausch, 17 years ago (diff)
Line 
1#include "Geometry.h"
2#include "Triangle3.h"
3#include "glInterface.h"
4
5
6namespace CHCDemo
7{
8
9Geometry::Geometry(Vector3 *vertices,
10                                   Vector3 *normals,
11                                   float *texcoords,
12                                   int numVertices):
13mVertices(vertices),
14mNormals(normals),
15mTexCoords(texcoords),
16mNumVertices(numVertices),
17mVboId(-1)
18{
19        Prepare();
20}
21
22
23void Geometry::Prepare()
24{
25        CalcBoundingBox();
26
27        int dataSize = mNumVertices * 6;
28
29        if (mTexCoords)
30                dataSize += mNumVertices * 2;
31       
32        float *data = new float[dataSize];
33
34        for (int i = 0; i < mNumVertices; ++ i)
35        {
36                ((Vector3 *)data)[i] = mVertices[i];
37                ((Vector3 *)data)[i + mNumVertices] = mNormals[i];
38        }
39
40        if (mTexCoords)
41        {
42                for (int i = 0; i < mNumVertices * 2; ++ i)
43                        data[mNumVertices * 6 + i] = mTexCoords[i];
44        }
45
46        glGenBuffersARB(1, &mVboId);
47       
48        glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
49
50        glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
51        glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
52
53        if (mTexCoords)
54        {
55                glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + 2 * mNumVertices * sizeof(Vector3));
56        }
57        glBufferDataARB(GL_ARRAY_BUFFER_ARB,
58                            dataSize * sizeof(float),
59                            (float *)data,
60                                        GL_STATIC_DRAW_ARB);
61
62        glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
63
64        // data handled by graphics driver from now on
65        delete [] data;
66}
67
68
69void Geometry::Render()
70{
71        glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
72
73        glVertexPointer(3, GL_FLOAT, 0, (char *)NULL); 
74        glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
75
76        if (mTexCoords)
77                glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + 2 * mNumVertices * sizeof(Vector3));
78
79        // don't render first degenerate index
80        glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
81}
82
83
84void Geometry::CalcBoundingBox()
85{
86        mBoundingBox.Initialize();
87
88        for (int i = 0; i < mNumVertices; ++ i)
89        {
90                mBoundingBox.Include(mVertices[i]);
91        }
92}
93
94
95const AxisAlignedBox3& Geometry::GetBoundingBox() const
96{
97        return mBoundingBox;
98}
99
100
101}
Note: See TracBrowser for help on using the repository browser.