diff --git a/src/vertexlist.cpp b/src/vertexlist.cpp new file mode 100644 index 000000000..d6a427af5 --- /dev/null +++ b/src/vertexlist.cpp @@ -0,0 +1,194 @@ +// vertexlist.cpp +// +// Copyright (C) 2001, Chris Laurel +// +// 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 +// of the License, or (at your option) any later version. + +#include +#include "gl.h" +#include "glext.h" +#include "vecgl.h" +#include "vertexlist.h" + +using namespace std; + + +VertexList::VertexList(uint32 _parts, uint32 initialVertexPoolSize) : + parts(_parts), + nVertices(0), + maxVertices(0), + vertices(NULL), + diffuseColor(1.0f, 1.0f, 1.0f), + bbox() +{ + if (initialVertexPoolSize > 0) + { + maxVertices = initialVertexPoolSize; + vertices = new VertexPart[vertexSize * maxVertices]; + } + + vertexSize = 3; + if ((parts & VertexNormal) != 0) + vertexSize += 3; + if ((parts & VertexColor0) != 0) + vertexSize += 1; + if ((parts & TexCoord0) != 0) + vertexSize += 2; + if ((parts & TexCoord1) != 0) + vertexSize += 2; +} + + +VertexList::~VertexList() +{ + if (vertices != NULL) + delete[] vertices; +} + + +void VertexList::render() +{ + GLsizei stride = sizeof(VertexPart) * vertexSize; + uint32 start = 3; + + // Vertex points + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(3, GL_FLOAT, stride, static_cast(vertices)); + + // Vertex normals + if ((parts & VertexNormal) != 0) + { + glEnableClientState(GL_NORMAL_ARRAY); + glNormalPointer(GL_FLOAT, stride, static_cast(&vertices[start])); + start += 3; + } + else + { + glDisableClientState(GL_NORMAL_ARRAY); + } + + // Vertex color + if ((parts & VertexColor0) != 0) + { + glEnableClientState(GL_COLOR_ARRAY); + glColorPointer(4, GL_UNSIGNED_BYTE, stride, + static_cast(&vertices[start])); + start += 1; + } + else + { + glDisableClientState(GL_COLOR_ARRAY); + glColor(diffuseColor); + } + + // Texture coordinates + if ((parts & TexCoord0) != 0) + { + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, stride, + static_cast(&vertices[start])); + start += 2; + } + else + { + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + + if ((parts & TexCoord1) != 0) + { + // glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, stride, + static_cast(&vertices[start])); + start += 2; + } + + glDrawArrays(GL_TRIANGLES, 0, nVertices); +} + + +void VertexList::addVertex(const Vertex& v) +{ + if (nVertices == maxVertices) + { + if (maxVertices == 0) + { + vertices = new VertexPart[16 * vertexSize]; + maxVertices = 16; + } + else + { + VertexPart* newVertices = new VertexPart[maxVertices * 2 * vertexSize]; + copy(vertices, vertices + nVertices * vertexSize, newVertices); + delete[] vertices; + vertices = newVertices; + maxVertices *= 2; + } + } + + uint32 n = nVertices * vertexSize; + vertices[n++].f = v.point.x; + vertices[n++].f = v.point.y; + vertices[n++].f = v.point.z; + if ((parts & VertexNormal) != 0) + { + vertices[n++].f = v.normal.x; + vertices[n++].f = v.normal.y; + vertices[n++].f = v.normal.z; + } + if ((parts & VertexColor0) != 0) + { + vertices[n].c[0] = (int) (v.color.red() * 255.99f); + vertices[n].c[1] = (int) (v.color.green() * 255.99f); + vertices[n].c[2] = (int) (v.color.blue() * 255.99f); + vertices[n].c[3] = (int) (v.color.alpha() * 255.99f); + n++; + } + if ((parts & TexCoord0) != 0) + { + vertices[n++].f = v.texCoords[0].x; + vertices[n++].f = v.texCoords[0].y; + } + if ((parts & TexCoord1) != 0) + { + vertices[n++].f = v.texCoords[1].x; + vertices[n++].f = v.texCoords[1].y; + } + + bbox.include(v.point); + + nVertices++; +} + + +AxisAlignedBox VertexList::getBoundingBox() const +{ + return bbox; +} + + +Color VertexList::getDiffuseColor() const +{ + return diffuseColor; +} + +void VertexList::setDiffuseColor(Color color) +{ + diffuseColor = color; +} + + +// Apply a translation and uniform scale to the vertices +void VertexList::transform(Vec3f translation, float scale) +{ + for (uint32 i = 0; i < nVertices; i++) + { + uint32 n = i * vertexSize; + Vec3f tv = (Vec3f(vertices[n].f, vertices[n + 1].f, vertices[n + 2].f) - translation) * scale; + vertices[n ].f = tv.x; + vertices[n + 1].f = tv.y; + vertices[n + 2].f = tv.z; + } +} diff --git a/src/vertexlist.h b/src/vertexlist.h new file mode 100644 index 000000000..e4e39bb36 --- /dev/null +++ b/src/vertexlist.h @@ -0,0 +1,74 @@ +// vertexlist.h +// +// Copyright (C) 2001, Chris Laurel +// +// 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 +// of the License, or (at your option) any later version. + +#ifndef _VERTEXLIST_H_ +#define _VERTEXLIST_H_ + +#include "basictypes.h" +#include "vecmath.h" +#include "color.h" +#include "aabox.h" + + +class VertexList +{ + public: + enum { + VertexNormal = 0x01, + VertexColor = 0x02, + VertexColor0 = 0x02, + VertexColor1 = 0x04, + TexCoord0 = 0x08, + TexCoord1 = 0x10, + }; + + class Vertex + { + public: + Point3f point; + Vec3f normal; + Color color; + Point2f texCoords[2]; + }; + + union VertexPart + { + float f; + unsigned char c[4]; + }; + + public: + VertexList(uint32 _parts, uint32 initialVertexPoolSize = 0); + ~VertexList(); + + void addVertex(const Vertex& v); + + Color getDiffuseColor() const; + void setDiffuseColor(Color); + + void render(); + + AxisAlignedBox getBoundingBox() const; + void transform(Vec3f translation, float scale); + + private: + uint32 parts; + uint32 vertexSize; + + uint32 nVertices; + uint32 maxVertices; + VertexPart* vertices; + + Color diffuseColor; + + AxisAlignedBox bbox; +}; + +#endif // _VERTEXLIST_H_ +