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

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