[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 |
|
---|
[3127] | 17 | using namespace std;
|
---|
[3021] | 18 |
|
---|
[2776] | 19 | namespace CHCDemoEngine
|
---|
[2751] | 20 | {
|
---|
| 21 |
|
---|
[2756] | 22 | Geometry::Geometry(Vector3 *vertices,
|
---|
| 23 | Vector3 *normals,
|
---|
[2980] | 24 | Texcoord2 *texcoords,
|
---|
[2781] | 25 | int numVertices,
|
---|
[3126] | 26 | bool delData,
|
---|
| 27 | Vector3 *tangents):
|
---|
[2756] | 28 | mVertices(vertices),
|
---|
| 29 | mNormals(normals),
|
---|
| 30 | mTexCoords(texcoords),
|
---|
[3126] | 31 | mTangents(tangents),
|
---|
[2756] | 32 | mNumVertices(numVertices),
|
---|
| 33 | mVboId(-1)
|
---|
[2642] | 34 | {
|
---|
[2795] | 35 | mHasTexture = (mTexCoords != NULL);
|
---|
[3126] | 36 | mHasTangents = (mTangents != NULL);
|
---|
[2795] | 37 |
|
---|
[2756] | 38 | Prepare();
|
---|
[2781] | 39 |
|
---|
| 40 | if (delData)
|
---|
| 41 | {
|
---|
[2795] | 42 | DEL_ARRAY_PTR(mVertices);
|
---|
| 43 | DEL_ARRAY_PTR(mNormals);
|
---|
| 44 | DEL_ARRAY_PTR(mTexCoords);
|
---|
[3126] | 45 | DEL_ARRAY_PTR(mTangents);
|
---|
[2781] | 46 | }
|
---|
[2642] | 47 | }
|
---|
| 48 |
|
---|
[2786] | 49 |
|
---|
[2781] | 50 | Geometry::~Geometry()
|
---|
| 51 | {
|
---|
[2795] | 52 | DEL_ARRAY_PTR(mVertices);
|
---|
| 53 | DEL_ARRAY_PTR(mNormals);
|
---|
| 54 | DEL_ARRAY_PTR(mTexCoords);
|
---|
[3126] | 55 | DEL_ARRAY_PTR(mTangents);
|
---|
[2642] | 56 |
|
---|
[2781] | 57 | // delete vbo
|
---|
| 58 | glDeleteBuffersARB(1, &mVboId);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 |
|
---|
[2756] | 62 | void Geometry::Prepare()
|
---|
[2642] | 63 | {
|
---|
[2756] | 64 | CalcBoundingBox();
|
---|
[2642] | 65 |
|
---|
[2756] | 66 | int dataSize = mNumVertices * 6;
|
---|
[2642] | 67 |
|
---|
[3126] | 68 | if (mTexCoords) dataSize += mNumVertices * 2;
|
---|
| 69 | if (mTangents) dataSize += mNumVertices * 3;
|
---|
| 70 |
|
---|
[2756] | 71 | float *data = new float[dataSize];
|
---|
[2642] | 72 |
|
---|
[2756] | 73 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
| 74 | {
|
---|
| 75 | ((Vector3 *)data)[i] = mVertices[i];
|
---|
| 76 | ((Vector3 *)data)[i + mNumVertices] = mNormals[i];
|
---|
| 77 | }
|
---|
[2746] | 78 |
|
---|
[3126] | 79 | float *currentPData = data + mNumVertices * 6;
|
---|
| 80 |
|
---|
| 81 | if (mTangents)
|
---|
| 82 | {
|
---|
| 83 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
[3153] | 84 | {
|
---|
| 85 | Vector3 tangent = Normalize(mTangents[i]);
|
---|
| 86 | Vector3 normal = Normalize(mNormals[i]);
|
---|
| 87 |
|
---|
[3154] | 88 | float dotProd = DotProd(tangent, normal);
|
---|
| 89 | //tangent -= normal * dotProd;tangent = Normalize(tangent);
|
---|
[3153] | 90 |
|
---|
| 91 | //mTangents[i] = tangent;
|
---|
[3154] | 92 | mTangents[i] = tangent * 0.5f + Vector3(0.5f);
|
---|
[3153] | 93 | }
|
---|
| 94 |
|
---|
| 95 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
[3127] | 96 | ((Vector3 *)data)[mNumVertices * 2 + i] = mTangents[i];
|
---|
[3126] | 97 |
|
---|
| 98 | currentPData += mNumVertices * 3;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[2756] | 101 | if (mTexCoords)
|
---|
| 102 | {
|
---|
[2980] | 103 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
[3126] | 104 | ((Texcoord2 *)currentPData)[i] = mTexCoords[i];
|
---|
[2756] | 105 | }
|
---|
| 106 |
|
---|
[3126] | 107 |
|
---|
[2756] | 108 | glGenBuffersARB(1, &mVboId);
|
---|
| 109 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
| 110 |
|
---|
[3126] | 111 | int currentPVbo = 0;
|
---|
| 112 |
|
---|
[2756] | 113 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
| 114 |
|
---|
[3126] | 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 | {
|
---|
[3154] | 122 | //cout << "dotprods: " << endl;
|
---|
[3153] | 123 |
|
---|
[3154] | 124 | /*for (int i = 0; i < 100; ++ i)
|
---|
[3153] | 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;
|
---|
[3154] | 132 | */
|
---|
[3153] | 133 |
|
---|
[3154] | 134 | for (int i = 0; i < 50; ++ i)
|
---|
| 135 | {
|
---|
| 136 | cout << mTexCoords[i].first << "," << mTexCoords[i].second << " ";
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[3126] | 139 | glColorPointer(3, GL_FLOAT, 0, (char *)NULL + currentPVbo);
|
---|
| 140 | currentPVbo += mNumVertices * sizeof(Vector3);
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[2756] | 143 | if (mTexCoords)
|
---|
| 144 | {
|
---|
[3126] | 145 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPVbo);
|
---|
[2756] | 146 | }
|
---|
[2786] | 147 |
|
---|
[3126] | 148 | glBufferDataARB(GL_ARRAY_BUFFER_ARB, dataSize * sizeof(float),
|
---|
| 149 | (float *)data, GL_STATIC_DRAW_ARB);
|
---|
[2756] | 150 |
|
---|
| 151 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
|
---|
| 152 |
|
---|
| 153 | // data handled by graphics driver from now on
|
---|
| 154 | delete [] data;
|
---|
[2642] | 155 | }
|
---|
| 156 |
|
---|
[2746] | 157 |
|
---|
[2773] | 158 | void Geometry::Render(RenderState *state)
|
---|
[2642] | 159 | {
|
---|
[3127] | 160 | if (mHasTangents)
|
---|
[3153] | 161 | {
|
---|
[3127] | 162 | glEnableClientState(GL_COLOR_ARRAY);
|
---|
[3153] | 163 | }
|
---|
[3154] | 164 |
|
---|
[2773] | 165 | if (state->GetCurrentVboId() != mVboId)
|
---|
| 166 | {
|
---|
| 167 | glBindBufferARB(GL_ARRAY_BUFFER_ARB, mVboId);
|
---|
[2795] | 168 |
|
---|
[3127] | 169 | int currentPointer;
|
---|
[2795] | 170 |
|
---|
[2773] | 171 | glNormalPointer(GL_FLOAT, 0, (char *)NULL + mNumVertices * sizeof(Vector3));
|
---|
[2642] | 172 |
|
---|
[3127] | 173 | if (mHasTangents)
|
---|
| 174 | {
|
---|
[3153] | 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));
|
---|
[3127] | 179 | currentPointer = mNumVertices * 3 * sizeof(Vector3);
|
---|
[3153] | 180 | //glClientActiveTextureARB(GL_TEXTURE0_ARB);
|
---|
| 181 |
|
---|
[3127] | 182 | }
|
---|
| 183 | else
|
---|
| 184 | currentPointer = mNumVertices * 2 * sizeof(Vector3);
|
---|
| 185 |
|
---|
| 186 | if (mHasTexture)
|
---|
| 187 | glTexCoordPointer(2, GL_FLOAT, 0, (char *)NULL + currentPointer);
|
---|
| 188 |
|
---|
[2795] | 189 | glVertexPointer(3, GL_FLOAT, 0, (char *)NULL);
|
---|
[2642] | 190 |
|
---|
[2773] | 191 | state->SetCurrentVboId(mVboId);
|
---|
[3127] | 192 |
|
---|
[2773] | 193 | }
|
---|
[2756] | 194 |
|
---|
| 195 | // don't render first degenerate index
|
---|
| 196 | glDrawArrays(GL_TRIANGLES, 0, mNumVertices);
|
---|
[3127] | 197 |
|
---|
| 198 | if (mHasTangents)
|
---|
| 199 | glDisableClientState(GL_COLOR_ARRAY);
|
---|
[2642] | 200 | }
|
---|
| 201 |
|
---|
| 202 |
|
---|
[2756] | 203 | void Geometry::CalcBoundingBox()
|
---|
[2642] | 204 | {
|
---|
[2756] | 205 | mBoundingBox.Initialize();
|
---|
| 206 |
|
---|
| 207 | for (int i = 0; i < mNumVertices; ++ i)
|
---|
| 208 | {
|
---|
| 209 | mBoundingBox.Include(mVertices[i]);
|
---|
| 210 | }
|
---|
[2642] | 211 | }
|
---|
| 212 |
|
---|
| 213 |
|
---|
[2756] | 214 | const AxisAlignedBox3& Geometry::GetBoundingBox() const
|
---|
[2642] | 215 | {
|
---|
[2756] | 216 | return mBoundingBox;
|
---|
[2642] | 217 | }
|
---|
| 218 |
|
---|
| 219 |
|
---|
[3259] | 220 | Vector3 *Geometry::GetVertices(int &numVertices) const
|
---|
| 221 | {
|
---|
| 222 | numVertices = mNumVertices;
|
---|
| 223 | return mVertices;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 |
|
---|
| 227 | Vector3 *Geometry::GetNormals(int &numNormals) const
|
---|
| 228 | {
|
---|
| 229 | numNormals = mNumVertices;
|
---|
| 230 | return mNormals;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 |
|
---|
| 234 | Vector3 *Geometry::GetTangents(int &numTangents) const
|
---|
| 235 | {
|
---|
| 236 | numTangents = mHasTangents ? mNumVertices : 0;;
|
---|
| 237 | return mTangents;
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | Texcoord2 *Geometry::GetTexCoords(int &numTexCoords) const
|
---|
| 242 | {
|
---|
| 243 | numTexCoords = mHasTangents ? mNumVertices : 0;;
|
---|
| 244 | return mTexCoords;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
[2755] | 248 | } |
---|