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

Revision 3127, 3.8 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
[3021]7#ifdef _CRT_SET
8        #define _CRTDBG_MAP_ALLOC
9        #include <stdlib.h>
10        #include <crtdbg.h>
11
12        // redefine new operator
13        #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
14        #define new DEBUG_NEW
15#endif
16
[3127]17using namespace std;
[3021]18
[2776]19namespace CHCDemoEngine
[2751]20{
21
[2756]22Geometry::Geometry(Vector3 *vertices,
23                                   Vector3 *normals,
[2980]24                                   Texcoord2 *texcoords,
[2781]25                                   int numVertices,
[3126]26                                   bool delData,
27                                   Vector3 *tangents):
[2756]28mVertices(vertices),
29mNormals(normals),
30mTexCoords(texcoords),
[3126]31mTangents(tangents),
[2756]32mNumVertices(numVertices),
33mVboId(-1)
[2642]34{
[2795]35        mHasTexture = (mTexCoords != NULL);
[3126]36        mHasTangents = (mTangents != NULL);
[2795]37
[2756]38        Prepare();
[2781]39
40        if (delData)
41        {
[2795]42                DEL_ARRAY_PTR(mVertices);
43                DEL_ARRAY_PTR(mNormals);
44                DEL_ARRAY_PTR(mTexCoords);
[3126]45                DEL_ARRAY_PTR(mTangents);
[2781]46        }
[2642]47}
48
[2786]49
[2781]50Geometry::~Geometry()
51{
[2795]52        DEL_ARRAY_PTR(mVertices);
53        DEL_ARRAY_PTR(mNormals);
54        DEL_ARRAY_PTR(mTexCoords);
[3126]55        DEL_ARRAY_PTR(mTangents);
[2642]56
[2781]57        // delete vbo
58        glDeleteBuffersARB(1, &mVboId);
59}
60
61       
[2756]62void Geometry::Prepare()
[2642]63{
[2756]64        CalcBoundingBox();
[2642]65
[2756]66        int dataSize = mNumVertices * 6;
[2642]67
[3126]68        if (mTexCoords) dataSize += mNumVertices * 2;
69        if (mTangents) dataSize += mNumVertices * 3;
70
[2756]71        float *data = new float[dataSize];
[2642]72
[2756]73        for (int i = 0; i < mNumVertices; ++ i)
74        {
75                ((Vector3 *)data)[i] = mVertices[i];
76                ((Vector3 *)data)[i + mNumVertices] = mNormals[i];
77        }
[2746]78
[3126]79        float *currentPData = data + mNumVertices * 6;
80
81        if (mTangents)
82        {
83                for (int i = 0; i < mNumVertices; ++ i)
[3127]84                        ((Vector3 *)data)[mNumVertices * 2 + i] = mTangents[i];
[3126]85
[3127]86                for (int i = 0; i < 3; ++ i)
87                        cout << ((Vector3 *)data)[mNumVertices * 2 + i] << " ";
88
[3126]89                currentPData += mNumVertices * 3;
90        }
91
[2756]92        if (mTexCoords)
93        {
[2980]94                for (int i = 0; i < mNumVertices; ++ i)
[3126]95                        ((Texcoord2 *)currentPData)[i] = mTexCoords[i];
[2756]96        }
97
[3126]98
[2756]99        glGenBuffersARB(1, &mVboId);
100        glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
101
[3126]102        int currentPVbo = 0;
103
[2756]104        glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
105
[3126]106        currentPVbo += mNumVertices * sizeof(Vector3);
107        glNormalPointer(GL_FLOAT, 0, (char *)NULL + currentPVbo);
108
109        currentPVbo += mNumVertices * 2 * sizeof(Vector3);
110
111        if (mTangents)
112        {
113                glColorPointer(3, GL_FLOAT, 0, (char *)NULL + currentPVbo);
114                currentPVbo += mNumVertices * sizeof(Vector3);
115        }
116
[2756]117        if (mTexCoords)
118        {
[3126]119                glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPVbo);
[2756]120        }
[2786]121
[3126]122        glBufferDataARB(GL_ARRAY_BUFFER_ARB, dataSize * sizeof(float),
123                            (float *)data, GL_STATIC_DRAW_ARB);
[2756]124
125        glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
126
127        // data handled by graphics driver from now on
128        delete [] data;
[2642]129}
130
[2746]131
[2773]132void Geometry::Render(RenderState *state)
[2642]133{
[3127]134        if (mHasTangents)
135                glEnableClientState(GL_COLOR_ARRAY);
136
[2773]137        if (state->GetCurrentVboId() != mVboId)
138        {
139                glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
[2795]140               
[3127]141                int currentPointer;
[2795]142
[2773]143                glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
[2642]144
[3127]145                if (mHasTangents)
146                {
147                        glColorPointer(3, GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
148                        currentPointer = mNumVertices * 3 * sizeof(Vector3);
149                }
150                else
151                        currentPointer = mNumVertices * 2 * sizeof(Vector3);
152
153                if (mHasTexture)
154                        glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPointer);
155
[2795]156                glVertexPointer(3, GL_FLOAT, 0, (char *)NULL); 
[2642]157
[2773]158                state->SetCurrentVboId(mVboId);
[3127]159
[2773]160        }
[2756]161
162        // don't render first degenerate index
163        glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
[3127]164
165        if (mHasTangents)
166                glDisableClientState(GL_COLOR_ARRAY);
[2642]167}
168
169
[2756]170void Geometry::CalcBoundingBox()
[2642]171{
[2756]172        mBoundingBox.Initialize();
173
174        for (int i = 0; i < mNumVertices; ++ i)
175        {
176                mBoundingBox.Include(mVertices[i]);
177        }
[2642]178}
179
180
[2756]181const AxisAlignedBox3& Geometry::GetBoundingBox() const
[2642]182{
[2756]183        return mBoundingBox;
[2642]184}
185
186
[2755]187}
Note: See TracBrowser for help on using the repository browser.