/* 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 "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 the 3D-VertexArrayModels\n"); glDisable(GL_LIGHTING); glEnableClientState(GL_VERTEX_ARRAY ); glEnableClientState(GL_TEXTURE_COORD_ARRAY ); glEnableClientState(GL_NORMAL_ARRAY ); glEnableClientState(GL_COLOR_ARRAY ); glVertexPointer(3, GL_FLOAT, 0, this->vertices.getArray()); glNormalPointer(GL_FLOAT, 0, this->normals.getArray()); glTexCoordPointer(2, GL_FLOAT, 0, this->texCoords.getArray()); glColorPointer(3, GL_FLOAT, 0, this->colors.getArray()); for (GLuint i = 1; i < this->stripes.size(); ++i) { glDrawElements(GL_TRIANGLE_STRIP, this->stripes[i] - this->stripes[i-1], GL_UNSIGNED_BYTE, this->indices.getArray()+this->stripes[i-1]); // glDrawRangeElements(GL_TRIANGLE_STRIP, // this->stripes[i-1], // this->stripes[i], // this->indices.getCount(), // GL_UNSIGNED_BYTE, // this->indices.getArray()); } glEnable(GL_LIGHTING); } ////////// // MESH // ////////// /** * @brief generates a new Stripe in this Model */ void VertexArrayModel::newStripe() { this->stripes.push_back(this->indices.getCount()); } /** * @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.addEntry(x, y, 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.addEntry(x, y, 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) { this->texCoords.addEntry(u); this->texCoords.addEntry(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.addEntry(r, g, 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(GLubyte indice) { this->indices.addEntry(indice); } /** * @brief Finalizes an Object. This can be done outside of the Class. */ void VertexArrayModel::finalize() { // finalize the Arrays this->vertices.finalizeArray(); this->texCoords.finalizeArray(); this->normals.finalizeArray(); this->colors.finalizeArray(); this->indices.finalizeArray(); this->newStripe(); for (int i = 0 ; i < this->stripes.size(); i++) { printf("== %d ==\n", stripes[i]); } /* glEnableClientState(GL_VERTEX_ARRAY | GL_TEXTURE_COORD_ARRAY | GL_NORMAL_ARRAY); */ this->bFinalized = true; } ///////////// // TESTING // ///////////// /** * @brief Includes a default model * * This will inject a Cube, because this is the most basic model. */ void VertexArrayModel::planeModel() { unsigned int sizeX = 20, sizeY = 10; unsigned int i, j; for (i = 0; i < sizeY; i++) { for (j = 0; j < sizeX; j++) { this->addVertex((float)i - sizeY/2.0, sin(-(float)i/(float)sizeY*5.0) * cos((float)j/(float)sizeX*5.0) * 10.0 , (float)(j) - sizeX/2.0); this->addNormal((float)i/(float)sizeY, 1, (float)j/(float)sizeX); this->addTexCoor((float)i/(float)sizeY, (float)j/(float)sizeY); this->addColor((float)i/20.0, 0.0, (float)j/20.0); } } for (i = 0; i < sizeY-1; i++) { for (j = 0; j < sizeX; j++) { this->addIndice(sizeX*i + j); this->addIndice(sizeX*(i+1) + j); } if (i < sizeY -1 ) this->newStripe(); } }