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