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

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