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

Revision 3294, 5.5 KB checked in by mattausch, 15 years ago (diff)
Line 
1#include "Geometry.h"
2#include "Triangle3.h"
3#include "glInterface.h"
4#include "RenderState.h"
5
6
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
17using namespace std;
18
19namespace CHCDemoEngine
20{
21
22Geometry::Geometry(Vector3 *vertices,
23                                   Vector3 *normals,
24                                   Texcoord2 *texcoords,
25                                   int numVertices,
26                                   bool delData,
27                                   Vector3 *tangents):
28mVertices(vertices),
29mNormals(normals),
30mTexCoords(texcoords),
31mTangents(tangents),
32mNumVertices(numVertices),
33mVboId(-1),
34mVboId2(-1),
35mIndices(NULL)
36{
37        mHasTexture = (mTexCoords != NULL);
38        mHasTangents = (mTangents != NULL);
39/*
40        mIndices = new unsigned int[mNumVertices];
41
42        for (int i = 0; i < mNumVertices; ++ i)
43        {
44                mIndices[i] = i;
45        }*/
46
47        // prepare vbos
48        Prepare();
49
50        if (delData)
51        {
52                DEL_ARRAY_PTR(mVertices);
53                DEL_ARRAY_PTR(mIndices);
54                DEL_ARRAY_PTR(mNormals);
55                DEL_ARRAY_PTR(mTexCoords);
56                DEL_ARRAY_PTR(mTangents);
57        }
58}
59
60
61Geometry::~Geometry()
62{
63        DEL_ARRAY_PTR(mVertices);
64        DEL_ARRAY_PTR(mNormals);
65        DEL_ARRAY_PTR(mTexCoords);
66        DEL_ARRAY_PTR(mTangents);
67        DEL_ARRAY_PTR(mIndices);
68
69        // delete vbos
70        glDeleteBuffersARB(1, &mVboId);
71        glDeleteBuffersARB(1, &mVboId2);
72}
73
74       
75void Geometry::Prepare()
76{
77        CalcBoundingBox();
78
79        int dataSize = mNumVertices * 6;
80
81        if (mTexCoords) dataSize += mNumVertices * 2;
82        if (mTangents) dataSize += mNumVertices * 3;
83
84        float *data = new float[dataSize];
85
86        for (int i = 0; i < mNumVertices; ++ i)
87        {
88                ((Vector3 *)data)[i] = mVertices[i];
89                ((Vector3 *)data)[i + mNumVertices] = mNormals[i];
90        }
91
92        float *currentPData = data + mNumVertices * 6;
93
94        if (mTangents)
95        {
96                for (int i = 0; i < mNumVertices; ++ i)
97                {
98                        Vector3 tangent = Normalize(mTangents[i]);
99                        Vector3 normal =  Normalize(mNormals[i]);
100
101                        float dotProd = DotProd(tangent, normal);
102                        //tangent -= normal * dotProd; tangent = Normalize(tangent);
103                        //mTangents[i] = tangent;
104                        mTangents[i] = tangent * 0.5f + Vector3(0.5f);
105                }
106
107                for (int i = 0; i < mNumVertices; ++ i)
108                {
109                        ((Vector3 *)data)[mNumVertices * 2 + i] = mTangents[i];
110                }
111
112                currentPData += mNumVertices * 3;
113        }
114
115        if (mTexCoords)
116        {
117                for (int i = 0; i < mNumVertices; ++ i)
118                {
119                        ((Texcoord2 *)currentPData)[i] = mTexCoords[i];
120                }
121        }
122
123
124        glGenBuffersARB(1, &mVboId);
125        glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
126
127        int currentPVbo = 0;
128
129        glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
130
131        currentPVbo += mNumVertices * sizeof(Vector3);
132        glNormalPointer(GL_FLOAT, 0, (char *)NULL + currentPVbo);
133
134        currentPVbo += mNumVertices * 2 * sizeof(Vector3);
135
136        if (mTangents)
137        {
138                // hack: use color pointer to store tangents
139                glColorPointer(3, GL_FLOAT, 0, (char *)NULL + currentPVbo);
140                currentPVbo += mNumVertices * sizeof(Vector3);
141        }
142
143        if (mTexCoords)
144        {
145                glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPVbo);
146        }
147
148        glBufferDataARB(GL_ARRAY_BUFFER_ARB, dataSize * sizeof(float),
149                            data, GL_STATIC_DRAW_ARB);
150
151        glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
152
153
154        ///////////
155        //-- the index array
156
157        if (mIndices)
158        {
159                glGenBuffersARB(1, &mVboId2);
160
161                glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mVboId2);
162                glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mNumVertices * sizeof(unsigned int),
163                        mIndices, GL_STATIC_DRAW_ARB);
164
165                int bufferSize;
166                glGetBufferParameterivARB(GL_ELEMENT_ARRAY_BUFFER_ARB, GL_BUFFER_SIZE_ARB, &bufferSize);
167                //cout << "Index Array in VBO: " << bufferSize << " bytes\n" << "id: " << mVboId2 << endl;
168
169                glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
170        }
171
172        // data handled by graphics driver from now on
173        delete [] data;
174}
175
176
177void Geometry::Render(RenderState *state)
178{
179        if (mHasTangents)
180        {
181                glEnableClientState(GL_COLOR_ARRAY);
182        }
183
184        if (state->GetCurrentVboId() != mVboId)
185        {
186                glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
187
188                int currentPointer;
189                glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
190
191                if (mHasTangents)
192                {
193                        glColorPointer(3, GL_FLOAT, 0, (char *)NULL + mNumVertices * 2 * sizeof(Vector3));
194                        currentPointer = mNumVertices * 3 * sizeof(Vector3);
195                }
196                else
197                {
198                        currentPointer = mNumVertices * 2 * sizeof(Vector3);
199                }
200
201                if (mHasTexture)
202                {
203                        glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPointer);
204                }
205
206                glVertexPointer(3, GL_FLOAT, 0, (char *)NULL); 
207                //glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, mVboId2);
208
209                /// update state
210                state->SetCurrentVboId(mVboId);
211        }
212
213        glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
214        //glDrawElements(GL_TRIANGLES, mNumVertices, GL_UNSIGNED_INT, 0);
215
216        if (mHasTangents) glDisableClientState(GL_COLOR_ARRAY);
217}
218
219
220void Geometry::CalcBoundingBox()
221{
222        mBoundingBox.Initialize();
223
224        for (int i = 0; i < mNumVertices; ++ i)
225        {
226                mBoundingBox.Include(mVertices[i]);
227        }
228}
229
230
231const AxisAlignedBox3& Geometry::GetBoundingBox() const
232{
233        return mBoundingBox;
234}
235
236
237Vector3 *Geometry::GetVertices(int &numVertices) const
238{
239        numVertices = mNumVertices;
240        return mVertices;
241}
242
243
244Vector3 *Geometry::GetNormals(int &numNormals) const
245{
246        numNormals = mNumVertices;
247        return mNormals;
248}
249
250
251Vector3 *Geometry::GetTangents(int &numTangents) const
252{
253        numTangents = mHasTangents ? mNumVertices : 0;;
254        return mTangents;
255}
256
257
258Texcoord2 *Geometry::GetTexCoords(int &numTexCoords) const
259{
260        numTexCoords = mHasTangents ? mNumVertices : 0;;
261        return mTexCoords;
262}
263
264
265}
Note: See TracBrowser for help on using the repository browser.