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