/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_IMPORTER #include "vertex_array_model.h" #include "texture.h" #include "stdlibincl.h" #include using namespace std; ///////////// /// MODEL /// ///////////// /** * @brief Creates a 3D-VertexArrayModel. * * assigns it a Name and a Type */ VertexArrayModel::VertexArrayModel() { this->setClassID(CL_MODEL, "VertexArrayModel"); this->bFinalized = false; this->newStripe(); } /** * @brief deletes an VertexArrayModel. * * Looks if any from model allocated space is still in use, and if so deleted it. */ VertexArrayModel::~VertexArrayModel() { PRINTF(4)("Deleting VertexArrayModel "); if (this->getName()) { PRINT(4)("%s\n", this->getName()); } else { PRINT(4)("\n"); } } /** * @brief Draws the VertexArrayModels of all Groups. * * It does this by just calling the Lists that must have been created earlier. */ void VertexArrayModel::draw() const { PRINTF(4)("drawing 3D-VertexArrayModel %s\n", this->getName()); //1glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); glPushAttrib(GL_ALL_ATTRIB_BITS); // glEnable(GL_LIGHTING); glColorMaterial ( GL_FRONT_AND_BACK, GL_EMISSION ) ; glEnable (GL_COLOR_MATERIAL) ; // glBindTexture(GL_TEXTURE_2D, this->tex1->getTexture()); // glEnable(GL_TEXTURE_2D); glEnableClientState(GL_TEXTURE_COORD_ARRAY ); glTexCoordPointer(2, GL_FLOAT, 0, &this->texCoords1[0]); glEnableClientState(GL_VERTEX_ARRAY ); glVertexPointer(3, GL_FLOAT, 0, &this->vertices[0]); glEnableClientState(GL_NORMAL_ARRAY ); glNormalPointer(GL_FLOAT, 0, &this->normals[0]); glEnableClientState(GL_COLOR_ARRAY ); glColorPointer(3, GL_FLOAT, 0, &this->colors[0]); //glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); for (GLuint i = 1; i < this->stripes.size(); ++i) { glDrawElements( GL_TRIANGLE_STRIP, this->stripes[i] - this->stripes[i-1], GL_UNSIGNED_INT, &this->indices[this->stripes[i-1]] ); } glPopAttrib(); } ////////// // MESH // ////////// /** * @brief generates a new Stripe in this Model */ void VertexArrayModel::newStripe() { // no stripes of size 0 if (this->stripes.empty() || this->indices.size() != this->stripes.back()) this->stripes.push_back(this->indices.size()); } /** * @brief parses a vertex-String * @param x the X-coordinate of the Vertex to add. * @param y the Y-coordinate of the Vertex to add. * @param z the Z-coordinate of the Vertex to add. */ void VertexArrayModel::addVertex(float x, float y, float z) { this->vertices.push_back(x); this->vertices.push_back(y); this->vertices.push_back(z); this->pModelInfo.numVertices++; } /** * @brief adds a VertexNormal. * @param x The x coordinate of the Normal. * @param y The y coordinate of the Normal. * @param z The z coordinate of the Normal. * * If a vertexNormal line is found this function will inject it into the vertexNormal-Array */ void VertexArrayModel::addNormal(float x, float y, float z) { this->normals.push_back(x); this->normals.push_back(y); this->normals.push_back(z); this->pModelInfo.numNormals++; } /** * @brief adds a Texture Coordinate * @param u The u coordinate of the TextureCoordinate. * @param v The y coordinate of the TextureCoordinate. * * If a TextureCoordinate line is found this function will inject it into the TextureCoordinate-Array */ void VertexArrayModel::addTexCoor(float u, float v, float w, float x) { this->texCoords1.push_back(u); this->texCoords1.push_back(v); //this->texCoords2.push_back(w); //this->texCoords2.push_back(x); this->pModelInfo.numTexCoor++; } void VertexArrayModel::addTexCoor(float u, float v) { this->texCoords1.push_back(u); this->texCoords1.push_back(v); this->pModelInfo.numTexCoor++; } /** * @brief adds a new Color * @param r the Red Component of the VertexColor to add. * @param g the Green Component of the VertexColor to add. * @param b the Blue of the VertexColor to add. */ void VertexArrayModel::addColor(float r, float g, float b) { this->colors.push_back(r); this->colors.push_back(g); this->colors.push_back(b); // FIXME } /** * adds a new Face * @param faceElemCount the number of Vertices to add to the Face. * @param type The information Passed with each Vertex */ void VertexArrayModel::addIndice(GLuint indice) { this->indices.push_back(indice); } /** * @brief Finalizes an Object. This can be done outside of the Class. */ void VertexArrayModel::finalize() { // finalize the Arrays this->newStripe(); this->bFinalized = true; } ///////////// // TESTING // ///////////// /** * @brief Includes a default model * * This will inject a Cube, because this is the most basic model. */ void VertexArrayModel::planeModel(float sizeX, float sizeY, unsigned int resolutionX, unsigned int resolutionY) { GLuint i, j; for (i = 0; i < resolutionY; i++) { for (j = 0; j < resolutionX; j++) { this->addVertex((float)i - (float)sizeY/2.0, 0.0, (float)j - (float)sizeX/2.0); this->addNormal(0.0, 1, 0.0); this->addTexCoor((float)i/(float)resolutionY, (float)j/(float)resolutionY); this->addColor((float)i/20.0, 0.0, (float)j/20.0); } } for (i = 0; i < resolutionY-1; i++) { for (j = 0; j < resolutionX; j++) { this->addIndice( resolutionY*i + j ); this->addIndice( resolutionY*(i+1) + j ); } this->newStripe(); } } #include /** * @brief builds a Triangle Stripped sphere * @param radius: radius * @param loops: the count of loops * @param segmentsPerLoop how many Segments per loop */ void VertexArrayModel::spiralSphere(const float radius, const unsigned int loops, const unsigned int segmentsPerLoop) { for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) { float theta = 0; float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop; float sinTheta = std::sin(theta); float sinPhi = std::sin(phi); float cosTheta = std::cos(theta); float cosPhi = std::cos(phi); this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); this->addTexCoor(0,0); /// FIXME this->addColor(.125,.436,.246); ///FIXME } for (unsigned int loopNumber = 0; loopNumber <= loops; ++loopNumber) { for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) { float theta = (loopNumber * PI / loops) + ((PI * loopSegmentNumber) / (segmentsPerLoop * loops)); if (loopNumber == loops) { theta = PI; } float phi = loopSegmentNumber * 2 * PI / segmentsPerLoop; float sinTheta = std::sin(theta); float sinPhi = std::sin(phi); float cosTheta = std::cos(theta); float cosPhi = std::cos(phi); this->addVertex(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); this->addNormal(radius * cosPhi * sinTheta, radius * sinPhi * sinTheta, radius * cosTheta); this->addTexCoor(0,0); //FIXME this->addColor(.125,.436,.246); } } for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) { this->addIndice(loopSegmentNumber); this->addIndice(segmentsPerLoop + loopSegmentNumber); } for (unsigned int loopNumber = 0; loopNumber < loops; ++loopNumber) { for (unsigned int loopSegmentNumber = 0; loopSegmentNumber < segmentsPerLoop; ++loopSegmentNumber) { this->addIndice( ((loopNumber + 1) * segmentsPerLoop) + loopSegmentNumber); this->addIndice( ((loopNumber + 2) * segmentsPerLoop) + loopSegmentNumber); } } } /** * @brief print out some nice debug information about this VertexArrayModel. */ void VertexArrayModel::debug() const { PRINT(0)("VertexArrayModel (%s): debug\n", this->getName()); PRINT(0)("Stripes: %d; Indices: %d; Vertices: %d; Normals %d; TextCoords %d; Colors %d\n", this->stripes.size(), this->indices.size(), this->vertices.size()/3, this->normals.size()/3, this->texCoords1.size()/2, this->colors.size() )/3; for (GLuint i = 1; i < this->stripes.size(); ++i) { PRINT(0)("Stripe-%d (s:%d:e:%d):: ", i, this->stripes[i-1], this->stripes[i]); for (GLuint j = this->stripes[i-1] ; j < this->stripes[i]; j++) { PRINT(0)("%d->", this->indices[j]); } PRINT(0)("\n"); } }