123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608 |
- /*!
- * \file src/Mesh.cpp
- * \brief OpenGL Mesh
- *
- * \author Mongoose
- */
-
- #include <stdlib.h>
-
- #include "global.h"
- #include "Mesh.h"
-
-
- ////////////////////////////////////////////////////////////
- // Constructors
- ////////////////////////////////////////////////////////////
-
- Mesh::Mesh()
- {
- mNumVertices = 0;
- mVertices = 0x0;
-
- mNumNormals = 0;
- mNormals = 0x0;
-
- mNumColors = 0;
- mColors = 0x0;
-
- mNumTris = 0;
- mTris = 0x0;
-
- mNumQuads = 0;
- mQuads = 0x0;
-
- mVertexArray = 0x0;
- mNormalArray = 0x0;
- mColorArray = 0x0;
-
- mTriangleCount = 0;
- mTriangleTextures = 0x0;
- mTriangleIndices = 0x0;
- mTriangleFlags = 0x0;
- mTriangleTexCoordArray = 0x0;
-
- mFlags = 0;
- mMode = MeshModeTexture;
- }
-
-
- Mesh::~Mesh()
- {
- for (unsigned int i = 0; i < mNumVertices; i++)
- delete [] mVertices[i];
- delete [] mVertices;
- mVertices = NULL;
-
- for (unsigned int i = 0; i < mNumNormals; i++)
- delete [] mNormals[i];
- delete [] mNormals;
- mNormals = NULL;
-
- for (unsigned int i = 0; i < mNumColors; i++)
- delete [] mColors[i];
- delete [] mColors;
- mColors = NULL;
-
- if (mTris)
- {
- for (unsigned int i = 0; i < mNumTris; ++i)
- {
- delete [] mTris[i].triangles;
- delete [] mTris[i].alpha_triangles;
- delete [] mTris[i].texcoors;
- delete [] mTris[i].texcoors2;
- }
-
- delete [] mTris;
- mTris = NULL;
- }
-
- if (mQuads)
- {
- for (unsigned int i = 0; i < mNumQuads; ++i)
- {
- delete [] mQuads[i].quads;
- delete [] mQuads[i].alpha_quads;
- delete [] mQuads[i].texcoors;
- delete [] mQuads[i].texcoors2;
- }
-
- delete [] mQuads;
- mQuads = NULL;
- }
-
- delete [] mVertexArray;
- mVertexArray = NULL;
-
- delete [] mNormalArray;
- mNormalArray = NULL;
-
- delete [] mColorArray;
- mColorArray = NULL;
-
- delete [] mTriangleTextures;
- mTriangleTextures = NULL;
-
- delete [] mTriangleIndices;
- mTriangleIndices = NULL;
-
- delete [] mTriangleFlags;
- mTriangleFlags = NULL;
-
- delete [] mTriangleTexCoordArray;
- mTriangleTexCoordArray = NULL;
- }
-
-
- ////////////////////////////////////////////////////////////
- // Public Accessors
- ////////////////////////////////////////////////////////////
-
- void Mesh::drawAlpha()
- {
- unsigned int i, j, k, index;
-
-
- // Render quadralaterals
- for (mQuads ? i = 0 : i = mNumQuads; i < mNumQuads; ++i)
- {
- switch (mMode)
- {
- case MeshModeWireframe:
- glColor3f(0.0, 0.0, 1.0);
- glBindTexture(GL_TEXTURE_2D, 0);
- break;
- case MeshModeSolid:
- // Bind WHITE texture for solid colors
- glBindTexture(GL_TEXTURE_2D, 0);
- break;
- case MeshModeTexture:
- case MeshModeMultiTexture:
- // Bind texture id for textures
- glBindTexture(GL_TEXTURE_2D, mQuads[i].texture+1);
- break;
- }
-
- glBegin(GL_QUADS);
-
- for (j = 0; j < mQuads[i].num_alpha_quads; ++j)
- {
- for (k = 0; k < 4; ++k)
- {
- index = mQuads[i].alpha_quads[j*4+k];
-
- glTexCoord2fv(mQuads[i].texcoors2[j*4+k]);
- glColor4fv(mColors[index]);
- glVertex3fv(mVertices[index]);
- }
- }
-
- glEnd();
- }
-
- // Render triangles
- for (mTris ? i = 0 : i = mNumTris; i < mNumTris; ++i)
- {
- switch (mMode)
- {
- case MeshModeWireframe:
- glColor3f(0.0, 1.0, 0.0);
- glBindTexture(GL_TEXTURE_2D, 0);
- break;
- case MeshModeSolid:
- // Bind WHITE texture for solid colors
- glBindTexture(GL_TEXTURE_2D, 0);
- break;
- case MeshModeTexture:
- case MeshModeMultiTexture:
- // Bind texture id for textures
- glBindTexture(GL_TEXTURE_2D, mTris[i].texture+1);
- break;
- }
-
- glBegin(GL_TRIANGLES);
-
- for (j = 0; j < mTris[i].num_alpha_triangles; ++j)
- {
- for (k = 0; k < 3; ++k)
- {
- index = mTris[i].alpha_triangles[j*3+k];
-
- glTexCoord2fv(mTris[i].texcoors2[j*3+k]);
- glColor4fv(mColors[index]);
- glVertex3fv(mVertices[index]);
- }
- }
-
- glEnd();
- }
- }
-
-
- void Mesh::drawSolid()
- {
- unsigned int i, j, k, index;
-
-
- if (mFlags & fMesh_UseVertexArray)
- {
- //glEnableClientState(GL_VERTEX_ARRAY);
- //glVertexPointer(3, GL_FLOAT, 0, mVertexArray);
-
- glPointSize(2.0f);
- glColor3f(1.0f, 1.0f, 1.0f);
- glBegin(GL_TRIANGLES);
-
- for (i = 0; i < mTriangleCount*3; ++i)
- {
- //glArrayElement(mTriangleIndices[i]);
- glVertex3fv(mVertexArray+mTriangleIndices[i]);
- }
-
- glEnd();
-
- glPointSize(1.0f);
-
- //! \fixme
- /*
- for (j = 0; j < mQuads[i].num_quads; ++j)
- {
- for (k = 0; k < 4; ++k)
- {
- index = mQuads[i].quads[j*4+k];
-
- glTexCoord2fv(mQuads[i].texcoors[j*4+k]);
- glArrayElement(mQuads[i].quads[j*4+k]);
- }
- }
- */
-
- return;
- }
-
- // Render quadralaterals
- for (mQuads ? i = 0 : i = mNumQuads; i < mNumQuads; ++i)
- {
- switch (mMode)
- {
- case MeshModeSolid:
- glColor3f(0.0, 0.0, 0.0);
- break;
- case MeshModeWireframe:
- // Bind WHITE texture for solid colors
- glBindTexture(GL_TEXTURE_2D, 0);
- break;
- #ifdef MULTITEXTURE
- case MeshModeMultiTexture:
- glActiveTextureARB(GL_TEXTURE0_ARB);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, mQuads[i].texture+1);
-
- glActiveTextureARB(GL_TEXTURE1_ARB);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, mQuads[i].bumpmap+1);
- break;
- #else
- case MeshModeMultiTexture:
- #endif
- case MeshModeTexture:
- // Bind texture id for textures
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glBindTexture(GL_TEXTURE_2D, mQuads[i].texture+1);
- break;
- }
-
- glBegin(GL_QUADS);
-
- for (j = 0; j < mQuads[i].num_quads; ++j)
- {
- for (k = 0; k < 4; ++k)
- {
- index = mQuads[i].quads[j*4+k];
-
- glColor4fv(mColors[index]);
-
- #ifdef MULTITEXTURE
- if (mMode == MeshModeMultiTexture)
- {
- glMultiTexCoord2fvARB(GL_TEXTURE0_ARB,
- mQuads[i].texcoors[j*4+k]);
- glMultiTexCoord2fvARB(GL_TEXTURE1_ARB,
- mQuads[i].texcoors[j*4+k]);
- }
- else
- #endif
- glTexCoord2fv(mQuads[i].texcoors[j*4+k]);
-
- glVertex3fv(mVertices[index]);
- }
- }
-
- glEnd();
- }
-
- // Render triangles
- for (mTris ? i = 0 : i = mNumTris; i < mNumTris; ++i)
- {
- switch (mMode)
- {
- case MeshModeSolid:
- glColor3f(1.0, 0.0, 0.0);
- break;
- case MeshModeWireframe:
- // Bind WHITE texture for solid colors
- glBindTexture(GL_TEXTURE_2D, 0);
- break;
- #ifdef MULTITEXTURE
- case MeshModeMultiTexture:
- glActiveTextureARB(GL_TEXTURE0_ARB);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, mTris[i].texture+1);
-
- glActiveTextureARB(GL_TEXTURE1_ARB);
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, mTris[i].bumpmap+1);
- break;
- #else
- case MeshModeMultiTexture:
- #endif
- case MeshModeTexture:
- // Bind texture id for textures
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glBindTexture(GL_TEXTURE_2D, mTris[i].texture+1);
- break;
- }
-
- glBegin(GL_TRIANGLES);
-
- for (j = 0; j < mTris[i].num_triangles; ++j)
- {
- for (k = 0; k < 3; ++k)
- {
- index = mTris[i].triangles[j*3+k];
-
- #ifdef MULTITEXTURE
- if (mMode == MeshModeMultiTexture)
- {
- glMultiTexCoord2fvARB(GL_TEXTURE0_ARB,
- mTris[i].texcoors[j*3+k]);
- glMultiTexCoord2fvARB(GL_TEXTURE1_ARB,
- mTris[i].texcoors[j*3+k]);
- }
- else
- #endif
- glTexCoord2fv(mTris[i].texcoors[j*3+k]);
-
- glColor4fv(mColors[index]);
- glVertex3fv(mVertices[index]);
- }
- }
-
- glEnd();
- }
-
- #ifdef MULTITEXTURE
- if (mMode == MeshModeMultiTexture)
- {
- glDisable(GL_TEXTURE_2D);
- glActiveTextureARB(GL_TEXTURE0_ARB);
- }
- #endif
- }
-
-
- ////////////////////////////////////////////////////////////
- // Public Mutators
- ////////////////////////////////////////////////////////////
-
- void Mesh::allocateColors(unsigned int n)
- {
- if (mColors)
- {
- for (unsigned int i = 0; i < mNumColors; i++)
- delete [] mColors[i];
- delete [] mColors;
- mNumColors = 0;
- }
-
- if (!n)
- {
- return;
- }
-
- mNumColors = n;
- mColors = new float *[mNumColors];
- for (unsigned int i = 0; i < mNumColors; i++)
- mColors[i] = new float[4];
- }
-
-
- void Mesh::allocateNormals(unsigned int n)
- {
- if (mNormals)
- {
- for (unsigned int i = 0; i < mNumNormals; i++)
- delete [] mNormals[i];
- delete [] mNormals;
- mNumNormals = 0;
- }
-
- if (!n)
- {
- return;
- }
-
- mNumNormals = n;
- mNormals = new float *[mNumNormals];
- for (unsigned int i = 0; i < mNumNormals; i++)
- mNormals[i] = new float[3];
- }
-
-
- void Mesh::allocateRectangles(unsigned int n)
- {
- if (mQuads)
- {
- mNumQuads = 0;
- delete [] mQuads;
- }
-
- if (!n)
- {
- return;
- }
-
- mNumQuads = n;
- mQuads = new rect_t[mNumQuads];
- }
-
-
- void Mesh::allocateTriangles(unsigned int n)
- {
- if (mTris)
- {
- mNumTris = 0;
- delete [] mTris;
- }
-
- if (!n)
- {
- return;
- }
-
- mNumTris = n;
- mTris = new tris_t[mNumTris];
- }
-
-
- void Mesh::allocateVertices(unsigned int n)
- {
- if (mVertices)
- {
- for (unsigned int i = 0; i < mNumVertices; i++)
- delete [] mVertices[i];
- delete [] mVertices;
- mNumVertices = 0;
- }
-
- if (!n)
- {
- return;
- }
-
- mNumVertices = n;
- mVertices = new float *[mNumVertices];
- for (unsigned int i = 0; i < mNumVertices; i++)
- mVertices[i] = new float[3];
- }
-
-
- void Mesh::bufferColorArray(unsigned int colorCount, float *colors)
- {
- if (mColors)
- {
- for (unsigned int i = 0; i < mNumColors; i++)
- delete [] mColors[i];
- delete [] mColors;
- mNumColors = 0;
- }
-
- if (!colorCount)
- {
- return;
- }
-
- mNumColors = colorCount;
- mColorArray = colors;
- }
-
-
- void Mesh::bufferNormalArray(unsigned int normalCount, float *normals)
- {
- if (mNormals)
- {
- for (unsigned int i = 0; i < mNumNormals; i++)
- delete [] mNormals[i];
- delete [] mNormals;
- mNumNormals = 0;
- }
-
- if (!normalCount)
- {
- return;
- }
-
- mNumNormals = normalCount;
- mNormalArray = normals;
- }
-
-
- void Mesh::bufferTriangles(unsigned int count,
- unsigned int *indices, float *texCoords,
- int *textures, unsigned int *flags)
- {
-
- mTriangleCount = count;
- mTriangleTextures = textures;
- mTriangleIndices = indices;
- mTriangleFlags = flags;
- mTriangleTexCoordArray = texCoords;
-
- //! \fixme sortTrianglesByTexture();
- }
-
-
- void Mesh::bufferVertexArray(unsigned int vertexCount, float *vertices)
- {
- if (mVertices)
- {
- for (unsigned int i = 0; i < mNumVertices; i++)
- delete [] mVertices[i];
- delete [] mVertices;
- mNumVertices = 0;
- }
-
- if (!vertexCount)
- {
- return;
- }
-
- mNumVertices = vertexCount;
- mVertexArray = vertices;
- mFlags |= fMesh_UseVertexArray;
- }
-
-
- void Mesh::setColor(unsigned int index,
- float r, float g, float b, float a)
- {
- assert(index < mNumColors);
-
- mColors[index][0] = r;
- mColors[index][1] = g;
- mColors[index][2] = b;
- mColors[index][3] = a;
- }
-
-
- void Mesh::setColor(unsigned int index, float rgba[4])
- {
- assert(index < mNumColors);
-
- mColors[index][0] = rgba[0];
- mColors[index][1] = rgba[1];
- mColors[index][2] = rgba[2];
- mColors[index][3] = rgba[3];
- }
-
-
- void Mesh::setNormal(unsigned int index, float i, float j, float k)
- {
- assert(index < mNumNormals);
-
- mNormals[index][0] = i;
- mNormals[index][1] = j;
- mNormals[index][2] = k;
- }
-
-
- void Mesh::setVertex(unsigned int index, float x, float y, float z)
- {
- assert(index < mNumVertices);
-
- mVertices[index][0] = x;
- mVertices[index][1] = y;
- mVertices[index][2] = z;
- }
-
-
- ////////////////////////////////////////////////////////////
- // Private Accessors
- ////////////////////////////////////////////////////////////
-
-
- ////////////////////////////////////////////////////////////
- // Private Mutators
- ////////////////////////////////////////////////////////////
|