[2642] | 1 | #include "Geometry.h"
|
---|
[2755] | 2 | #include "Triangle3.h"
|
---|
[2756] | 3 | #include "glInterface.h"
|
---|
[2773] | 4 | #include "RenderState.h"
|
---|
[2642] | 5 |
|
---|
| 6 |
|
---|
[3021] | 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 |
|
---|
[2776] | 18 | namespace CHCDemoEngine
|
---|
[2751] | 19 | {
|
---|
| 20 |
|
---|
[2756] | 21 | Geometry::Geometry(Vector3 *vertices,
|
---|
| 22 | Vector3 *normals,
|
---|
[2980] | 23 | Texcoord2 *texcoords,
|
---|
[2781] | 24 | int numVertices,
|
---|
| 25 | bool delData):
|
---|
[2756] | 26 | mVertices(vertices),
|
---|
| 27 | mNormals(normals),
|
---|
| 28 | mTexCoords(texcoords),
|
---|
| 29 | mNumVertices(numVertices),
|
---|
| 30 | mVboId(-1)
|
---|
[2642] | 31 | {
|
---|
[2795] | 32 | mHasTexture = (mTexCoords != NULL);
|
---|
| 33 |
|
---|
[2756] | 34 | Prepare();
|
---|
[2781] | 35 |
|
---|
| 36 | if (delData)
|
---|
| 37 | {
|
---|
[2795] | 38 | DEL_ARRAY_PTR(mVertices);
|
---|
| 39 | DEL_ARRAY_PTR(mNormals);
|
---|
| 40 | DEL_ARRAY_PTR(mTexCoords);
|
---|
[2781] | 41 | }
|
---|
[2642] | 42 | }
|
---|
| 43 |
|
---|
[2786] | 44 |
|
---|
[2781] | 45 | Geometry::~Geometry()
|
---|
| 46 | {
|
---|
[2795] | 47 | DEL_ARRAY_PTR(mVertices);
|
---|
| 48 | DEL_ARRAY_PTR(mNormals);
|
---|
| 49 | DEL_ARRAY_PTR(mTexCoords);
|
---|
[2642] | 50 |
|
---|
[2781] | 51 | // delete vbo
|
---|
| 52 | glDeleteBuffersARB(1, &mVboId);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 |
|
---|
[2756] | 56 | void Geometry::Prepare()
|
---|
[2642] | 57 | {
|
---|
[2756] | 58 | CalcBoundingBox();
|
---|
[2642] | 59 |
|
---|
[2756] | 60 | int dataSize = mNumVertices * 6;
|
---|
[2642] | 61 |
|
---|
[2756] | 62 | if (mTexCoords)
|
---|
| 63 | dataSize += mNumVertices * 2;
|
---|
| 64 |
|
---|
| 65 | float *data = new float[dataSize];
|
---|
[2642] | 66 |
|
---|
[2756] | 67 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
| 68 | {
|
---|
| 69 | ((Vector3 *)data)[i] = mVertices[i];
|
---|
| 70 | ((Vector3 *)data)[i + mNumVertices] = mNormals[i];
|
---|
| 71 | }
|
---|
[2746] | 72 |
|
---|
[2756] | 73 | if (mTexCoords)
|
---|
| 74 | {
|
---|
[2980] | 75 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
| 76 | ((Texcoord2 *)data)[mNumVertices * 3 + i] = mTexCoords[i];
|
---|
[2756] | 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 | }
|
---|
[2786] | 89 |
|
---|
[2756] | 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;
|
---|
[2642] | 99 | }
|
---|
| 100 |
|
---|
[2746] | 101 |
|
---|
[2773] | 102 | void Geometry::Render(RenderState *state)
|
---|
[2642] | 103 | {
|
---|
[2773] | 104 | if (state->GetCurrentVboId() != mVboId)
|
---|
| 105 | {
|
---|
| 106 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
[2795] | 107 |
|
---|
| 108 | if (mHasTexture)
|
---|
| 109 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + 2 * mNumVertices * sizeof(Vector3));
|
---|
| 110 |
|
---|
[2773] | 111 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
[2642] | 112 |
|
---|
[2795] | 113 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
[2642] | 114 |
|
---|
[2773] | 115 | state->SetCurrentVboId(mVboId);
|
---|
| 116 | }
|
---|
[2756] | 117 |
|
---|
| 118 | // don't render first degenerate index
|
---|
| 119 | glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
|
---|
[2642] | 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
[2756] | 123 | void Geometry::CalcBoundingBox()
|
---|
[2642] | 124 | {
|
---|
[2756] | 125 | mBoundingBox.Initialize();
|
---|
| 126 |
|
---|
| 127 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
| 128 | {
|
---|
| 129 | mBoundingBox.Include(mVertices[i]);
|
---|
| 130 | }
|
---|
[2642] | 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
[2756] | 134 | const AxisAlignedBox3& Geometry::GetBoundingBox() const
|
---|
[2642] | 135 | {
|
---|
[2756] | 136 | return mBoundingBox;
|
---|
[2642] | 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
[2755] | 140 | } |
---|