source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/Geometry.cpp @ 2980

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