123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
-
-
- #include "global.h"
- #include "Game.h"
- #include "Render.h"
- #include "TextureManager.h"
- #include "utils/pixel.h"
- #include "StaticMesh.h"
-
- TexturedTriangle::TexturedTriangle(int i[3], float s[6], int tex, unsigned short trans) {
- index[0] = i[0];
- index[1] = i[1];
- index[2] = i[2];
- st[0] = s[0];
- st[1] = s[1];
- st[2] = s[2];
- st[3] = s[3];
- st[4] = s[4];
- st[5] = s[5];
- texture = tex;
- transparency = trans;
- }
-
- void TexturedTriangle::display(float *vertices, float *colors, float *normals) {
- assert(vertices != NULL);
-
- if ((getRender().getMode() != Render::modeWireframe)
- && (getRender().getMode() != Render::modeSolid)) {
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glBindTexture(GL_TEXTURE_2D, texture + 1);
- }
-
- glBegin(GL_TRIANGLES);
-
- switch (getRender().getMode()) {
- case Render::modeSolid:
- case Render::modeVertexLight:
- if (colors != NULL) {
- glColor3fv(colors + index[0]);
- glTexCoord2fv(st);
- glVertex3fv(vertices + (index[0] * 3));
-
- glColor3fv(colors + index[1]);
- glTexCoord2fv(st + 2);
- glVertex3fv(vertices + (index[1] * 3));
-
- glColor3fv(colors + index[2]);
- glTexCoord2fv(st + 4);
- glVertex3fv(vertices + (index[2] * 3));
- } else if (normals != NULL) {
- glNormal3fv(normals + (index[0] * 3));
- glTexCoord2fv(st);
- glVertex3fv(vertices + (index[0] * 3));
-
- glNormal3fv(normals + (index[1] * 3));
- glTexCoord2fv(st + 2);
- glVertex3fv(vertices + (index[1] * 3));
-
- glNormal3fv(normals + (index[2] * 3));
- glTexCoord2fv(st + 4);
- glVertex3fv(vertices + (index[2] * 3));
- } else {
- glTexCoord2fv(st);
- glVertex3fv(vertices + (index[0] * 3));
- glTexCoord2fv(st + 2);
- glVertex3fv(vertices + (index[1] * 3));
- glTexCoord2fv(st + 4);
- glVertex3fv(vertices + (index[2] * 3));
- }
- break;
-
- case Render::modeWireframe:
- glVertex3fv(vertices + (index[0] * 3));
- glVertex3fv(vertices + (index[1] * 3));
- glVertex3fv(vertices + (index[2] * 3));
- break;
-
- default:
- glTexCoord2fv(st);
- glVertex3fv(vertices + (index[0] * 3));
- glTexCoord2fv(st + 2);
- glVertex3fv(vertices + (index[1] * 3));
- glTexCoord2fv(st + 4);
- glVertex3fv(vertices + (index[2] * 3));
- }
-
- glEnd();
- }
-
- #ifdef EXPERIMENTAL
-
- #include <map>
- std::map<unsigned int, unsigned int> gColorTextureHACK;
-
- int setupTextureColor(float *colorf) {
- unsigned char color[4];
- unsigned int colorI;
- unsigned int texture;
-
- color[0] = (unsigned char)(colorf[0]*255.0f);
- color[1] = (unsigned char)(colorf[1]*255.0f);
- color[2] = (unsigned char)(colorf[2]*255.0f);
- color[3] = (unsigned char)(colorf[3]*255.0f);
-
- ((unsigned char *)(&colorI))[3] = color[0];
- ((unsigned char *)(&colorI))[2] = color[1];
- ((unsigned char *)(&colorI))[1] = color[2];
- ((unsigned char *)(&colorI))[0] = color[3];
-
- try {
- texture = gColorTextureHACK.at(colorI);
- } catch (...) {
- unsigned char *image = generateColorTexture(color, 32, 32, 32);
- texture = getTextureManager().loadBufferSlot(image, 32, 32,
- RGBA, 32, getTextureManager().getTextureCount());
- delete [] image;
- }
-
- return texture;
- }
-
- #endif
-
- StaticMesh::StaticMesh(TombRaider &tr, unsigned int index) {
- int count, texture;
- int vertexIndices[6];
- float st[12];
- float color[4];
- unsigned short transparency;
-
- if (!tr.isMeshValid(index)) {
- dontshow = true;
- return;
- } else {
- dontshow = false;
- }
-
-
- tr.getMeshCollisionInfo(index, center, &radius);
-
-
-
- tr.getMeshVertexArrays(index,
- &vertexCount, &vertices,
- &normalCount, &normals,
- &colorCount, &colors);
-
-
- count = tr.getMeshTexturedTriangleCount(index);
- for (int i = 0; i < count; i++) {
- tr.getMeshTexturedTriangle(index, i,
- vertexIndices, st,
- &texture, &transparency);
- triangles.push_back(
- new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
- }
-
-
- count = tr.getMeshColoredTriangleCount(index);
- for (int i = 0; i < count; i++) {
- tr.getMeshColoredTriangle(index, i,
- vertexIndices, color);
- st[0] = color[0];
- st[1] = color[1];
- st[2] = color[2];
- st[3] = color[3];
- st[4] = 1.0;
- st[5] = 1.0;
-
- #ifdef EXPERIMENTAL
- texture = setupTextureColor(color);
- #else
- texture = 0;
- #endif
- transparency = 0;
-
- triangles.push_back(
- new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
- }
-
-
- count = tr.getMeshTexturedRectangleCount(index);
- for (int i = 0; i < count; i++) {
- tr.getMeshTexturedRectangle(index, i,
- vertexIndices, st,
- &texture, &transparency);
- triangles.push_back(
- new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
- triangles.push_back(
- new TexturedTriangle(vertexIndices + 3, st + 6, texture + getGame().getTextureStart(), transparency));
- }
-
-
- count = tr.getMeshColoredRectangleCount(index);
- for (int i = 0; i < count; i++) {
- tr.getMeshColoredRectangle(index, i,
- vertexIndices, color);
-
- st[0] = color[0];
- st[1] = color[1];
- st[2] = color[2];
- st[3] = color[3];
- st[4] = 1.0;
- st[5] = 1.0;
-
- #ifdef EXPERIMENTAL
- texture = setupTextureColor(color);
- #else
- texture = 0;
- #endif
- transparency = 0;
-
- triangles.push_back(
- new TexturedTriangle(vertexIndices, st, texture + getGame().getTextureStart(), transparency));
- triangles.push_back(
- new TexturedTriangle(vertexIndices + 3, st, texture + getGame().getTextureStart(), transparency));
- }
- }
-
- StaticMesh::~StaticMesh() {
- while (!triangles.empty()) {
- delete triangles.back();
- triangles.pop_back();
- }
-
- delete [] vertices;
- delete [] normals;
- delete [] colors;
- }
-
- void StaticMesh::display() {
- if (!dontshow) {
-
-
-
-
-
- glColor3ubv(WHITE);
-
- if (getRender().getMode() == Render::modeWireframe)
- glColor3ubv(WHITE);
-
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glBindTexture(GL_TEXTURE_2D, 1);
-
- for (unsigned int i = 0; i < triangles.size(); i++)
- triangles.at(i)->display(vertices, colors, normals);
- }
- }
-
- float StaticMesh::getRadius() {
- return radius;
- }
|