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

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