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 |
|
---|
17 |
|
---|
18 | namespace CHCDemoEngine
|
---|
19 | {
|
---|
20 |
|
---|
21 | Geometry::Geometry(Vector3 *vertices,
|
---|
22 | Vector3 *normals,
|
---|
23 | Texcoord2 *texcoords,
|
---|
24 | int numVertices,
|
---|
25 | bool delData):
|
---|
26 | mVertices(vertices),
|
---|
27 | mNormals(normals),
|
---|
28 | mTexCoords(texcoords),
|
---|
29 | mNumVertices(numVertices),
|
---|
30 | mVboId(-1)
|
---|
31 | {
|
---|
32 | mHasTexture = (mTexCoords != NULL);
|
---|
33 |
|
---|
34 | Prepare();
|
---|
35 |
|
---|
36 | if (delData)
|
---|
37 | {
|
---|
38 | DEL_ARRAY_PTR(mVertices);
|
---|
39 | DEL_ARRAY_PTR(mNormals);
|
---|
40 | DEL_ARRAY_PTR(mTexCoords);
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 | Geometry::~Geometry()
|
---|
46 | {
|
---|
47 | DEL_ARRAY_PTR(mVertices);
|
---|
48 | DEL_ARRAY_PTR(mNormals);
|
---|
49 | DEL_ARRAY_PTR(mTexCoords);
|
---|
50 |
|
---|
51 | // delete vbo
|
---|
52 | glDeleteBuffersARB(1, &mVboId);
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|
56 | void Geometry::Prepare()
|
---|
57 | {
|
---|
58 | CalcBoundingBox();
|
---|
59 |
|
---|
60 | int dataSize = mNumVertices * 6;
|
---|
61 |
|
---|
62 | if (mTexCoords)
|
---|
63 | dataSize += mNumVertices * 2;
|
---|
64 |
|
---|
65 | float *data = new float[dataSize];
|
---|
66 |
|
---|
67 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
68 | {
|
---|
69 | ((Vector3 *)data)[i] = mVertices[i];
|
---|
70 | ((Vector3 *)data)[i + mNumVertices] = mNormals[i];
|
---|
71 | }
|
---|
72 |
|
---|
73 | if (mTexCoords)
|
---|
74 | {
|
---|
75 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
76 | ((Texcoord2 *)data)[mNumVertices * 3 + i] = mTexCoords[i];
|
---|
77 | }
|
---|
78 |
|
---|
79 | glGenBuffersARB(1, &mVboId);
|
---|
80 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
81 |
|
---|
82 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
83 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
84 |
|
---|
85 | if (mTexCoords)
|
---|
86 | {
|
---|
87 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + 2 * mNumVertices * sizeof(Vector3));
|
---|
88 | }
|
---|
89 |
|
---|
90 | glBufferDataARB(GL_ARRAY_BUFFER_ARB,
|
---|
91 | dataSize * sizeof(float),
|
---|
92 | (float *)data,
|
---|
93 | GL_STATIC_DRAW_ARB);
|
---|
94 |
|
---|
95 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
96 |
|
---|
97 | // data handled by graphics driver from now on
|
---|
98 | delete [] data;
|
---|
99 | }
|
---|
100 |
|
---|
101 |
|
---|
102 | void Geometry::Render(RenderState *state)
|
---|
103 | {
|
---|
104 | if (state->GetCurrentVboId() != mVboId)
|
---|
105 | {
|
---|
106 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
107 |
|
---|
108 | if (mHasTexture)
|
---|
109 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + 2 * mNumVertices * sizeof(Vector3));
|
---|
110 |
|
---|
111 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
112 |
|
---|
113 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
114 |
|
---|
115 | state->SetCurrentVboId(mVboId);
|
---|
116 | }
|
---|
117 |
|
---|
118 | // don't render first degenerate index
|
---|
119 | glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | void Geometry::CalcBoundingBox()
|
---|
124 | {
|
---|
125 | mBoundingBox.Initialize();
|
---|
126 |
|
---|
127 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
128 | {
|
---|
129 | mBoundingBox.Include(mVertices[i]);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | const AxisAlignedBox3& Geometry::GetBoundingBox() const
|
---|
135 | {
|
---|
136 | return mBoundingBox;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | } |
---|