123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652 |
- /*!
- * \file src/Mesh.cpp
- * \brief OpenGL Mesh
- *
- * \author Mongoose
- */
-
- #include <stdlib.h>
-
- #include "global.h"
- #include "TextureManager.h"
- #include "Mesh.h"
-
- void Mesh::addTexturedRectangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 d,
- uint16_t textile) {
- texturedRectangles.emplace_back(a, b, c, d, textile);
- }
-
- void Mesh::addTexturedTriangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, uint16_t textile) {
- texturedTriangles.emplace_back(a, b, c, glm::vec3(), textile);
- }
-
- void Mesh::addColoredRectangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, glm::vec3 d, float re,
- float gr, float bl) {
- coloredRectangles.emplace_back(a, b, c, d, -1, re, gr, bl);
- }
-
- void Mesh::addColoredTriangle(glm::vec3 a, glm::vec3 b, glm::vec3 c, float re, float gr, float bl) {
- coloredTriangles.emplace_back(a, b, c, glm::vec3(), -1, re, gr, bl);
- }
-
- void Mesh::addNormal(glm::vec3 n) {
- normals.emplace_back(n);
- }
-
- void Mesh::drawAlpha() {
- if ((texturedRectangles.size() == 0)
- && (texturedTriangles.size() == 0)
- && (coloredRectangles.size() == 0)
- && (coloredTriangles.size() == 0)
- && (normals.size() == 0)) {
- drawAlphaOld();
- return;
- }
-
- // TODO
- }
-
- void Mesh::drawSolid() {
- if ((texturedRectangles.size() == 0)
- && (texturedTriangles.size() == 0)
- && (coloredRectangles.size() == 0)
- && (coloredTriangles.size() == 0)
- && (normals.size() == 0)) {
- drawSolidOld();
- return;
- }
-
- for (auto& q : texturedRectangles) {
- if (mMode == MeshModeWireframe) {
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- /*
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(q.a.x, q.a.y, q.a.z);
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(q.b.x, q.b.y, q.b.z);
- glTexCoord2f(1.0f, 1.0f);
- glVertex3f(q.c.x, q.c.y, q.c.z);
- glTexCoord2f(0.0f, 1.0f);
- glVertex3f(q.d.x, q.d.y, q.d.z);
- glEnd();
- */
- } else if (mMode == MeshModeSolid) {
- // TODO
- } else if (mMode == MeshModeTexture) {
- //getTextureManager().getTile(q.texture).displayRectangle(q.a, q.b, q.c, q.d);
- }
- }
-
- for (auto& t : texturedTriangles) {
- if (mMode == MeshModeWireframe) {
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- /*
- glBegin(GL_TRIANGLES);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(t.a.x, t.a.y, t.a.z);
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(t.b.x, t.b.y, t.b.z);
- glTexCoord2f(1.0f, 1.0f);
- glVertex3f(t.c.x, t.c.y, t.c.z);
- glEnd();
- */
- } else if (mMode == MeshModeSolid) {
- // TODO
- } else if (mMode == MeshModeTexture) {
- //getTextureManager().getTile(t.texture).displayTriangle(t.a, t.b, t.c);
- }
- }
-
- for (auto& q : coloredRectangles) {
- if (mMode == MeshModeWireframe) {
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- /*
- glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(q.a.x, q.a.y, q.a.z);
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(q.b.x, q.b.y, q.b.z);
- glTexCoord2f(1.0f, 1.0f);
- glVertex3f(q.c.x, q.c.y, q.c.z);
- glTexCoord2f(0.0f, 1.0f);
- glVertex3f(q.d.x, q.d.y, q.d.z);
- glEnd();
- */
- } else if (mMode == MeshModeSolid) {
- // TODO
- } else if (mMode == MeshModeTexture) {
- // TODO
- }
- }
-
- for (auto& t : coloredTriangles) {
- if (mMode == MeshModeWireframe) {
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- /*
- glBegin(GL_TRIANGLES);
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(t.a.x, t.a.y, t.a.z);
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(t.b.x, t.b.y, t.b.z);
- glTexCoord2f(1.0f, 1.0f);
- glVertex3f(t.c.x, t.c.y, t.c.z);
- glEnd();
- */
- } else if (mMode == MeshModeSolid) {
- // TODO
- } else if (mMode == MeshModeTexture) {
- // TODO
- }
- }
- }
-
-
- ////////////////////////////////////////////////////////////
- // 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 = nullptr;
-
- for (unsigned int i = 0; i < mNumNormals; i++)
- delete [] mNormals[i];
- delete [] mNormals;
- mNormals = nullptr;
-
- for (unsigned int i = 0; i < mNumColors; i++)
- delete [] mColors[i];
- delete [] mColors;
- mColors = nullptr;
-
- 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 = nullptr;
- }
-
- 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 = nullptr;
- }
-
- delete [] mVertexArray;
- mVertexArray = nullptr;
-
- delete [] mNormalArray;
- mNormalArray = nullptr;
-
- delete [] mColorArray;
- mColorArray = nullptr;
-
- delete [] mTriangleTextures;
- mTriangleTextures = nullptr;
-
- delete [] mTriangleIndices;
- mTriangleIndices = nullptr;
-
- delete [] mTriangleFlags;
- mTriangleFlags = nullptr;
-
- delete [] mTriangleTexCoordArray;
- mTriangleTexCoordArray = nullptr;
- }
-
-
- ////////////////////////////////////////////////////////////
- // Public Accessors
- ////////////////////////////////////////////////////////////
-
- void Mesh::drawAlphaOld() {
- 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);
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- break;
- case MeshModeSolid:
- // Bind WHITE texture for solid colors
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- break;
- case MeshModeTexture:
- case MeshModeMultiTexture:
- // Bind texture id for textures
- getTextureManager().bindTextureId(mQuads[i].texture);
- 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);
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- break;
- case MeshModeSolid:
- // Bind WHITE texture for solid colors
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- break;
- case MeshModeTexture:
- case MeshModeMultiTexture:
- // Bind texture id for textures
- getTextureManager().bindTextureId(mTris[i].texture);
- 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::drawSolidOld() {
- unsigned int i, j, k, index;
-
- /*
-
-
- // 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
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- break;
- #ifdef MULTITEXTURE
- case MeshModeMultiTexture:
- glActiveTextureARB(GL_TEXTURE0_ARB);
- glEnable(GL_TEXTURE_2D);
- getTextureManager().bindTextureId(mQuads[i].texture);
-
- glActiveTextureARB(GL_TEXTURE1_ARB);
- glEnable(GL_TEXTURE_2D);
- getTextureManager().bindTextureId(mQuads[i].bumpmap);
- break;
- #else
- case MeshModeMultiTexture:
- #endif
- case MeshModeTexture:
- // Bind texture id for textures
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- getTextureManager().bindTextureId(mQuads[i].texture);
- 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
- getTextureManager().bindTextureId(TEXTURE_WHITE, TextureManager::TextureStorage::SYSTEM);
- break;
- #ifdef MULTITEXTURE
- case MeshModeMultiTexture:
- glActiveTextureARB(GL_TEXTURE0_ARB);
- glEnable(GL_TEXTURE_2D);
- getTextureManager().bindTextureId(mTris[i].texture);
-
- glActiveTextureARB(GL_TEXTURE1_ARB);
- glEnable(GL_TEXTURE_2D);
- getTextureManager().bindTextureId(mTris[i].bumpmap);
- break;
- #else
- case MeshModeMultiTexture:
- #endif
- case MeshModeTexture:
- // Bind texture id for textures
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- getTextureManager().bindTextureId(mTris[i].texture);
- 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
- ////////////////////////////////////////////////////////////
|