Skip to content
Browse files

Add VertexC class for drawing with colours.

  • Loading branch information...
1 parent f719fe3 commit cd84846c8c454468cc40a6a7c76d8767688e333b @ChickenProp committed Dec 1, 2010
Showing with 102 additions and 16 deletions.
  1. +7 −5 src/debug-draw.cpp
  2. +38 −0 src/vertex.cpp
  3. +56 −11 src/vertex.h
  4. +1 −0 src/world.cpp
View
12 src/debug-draw.cpp
@@ -1,14 +1,16 @@
#include "debug-draw.h"
+#include "vertex.h"
void DebugDraw::drawLine(const btVector3 &from, const btVector3 &to,
const btVector3 &color)
{
- glColor3f(color.x(), color.y(), color.z());
- glBegin(GL_LINES);
- glVertex3f(from.x(), from.z(), from.y());
- glVertex3f(to.x(), to.z(), to.y());
- glEnd();
+ VertexC line[2] = {
+ VertexC( (ph::vec3f) from, color.x(), color.y(), color.z() ),
+ VertexC( (ph::vec3f) to, color.x(), color.y(), color.z() )
+ };
+
+ line[0].draw(GL_LINES, 2);
}
void DebugDraw::setDebugMode(int m) {
View
38 src/vertex.cpp
@@ -1,10 +1,48 @@
#include "vertex.h"
#include "includes.h"
+int VertexSup::stride = sizeof(VertexSup);
int Vertex::stride = sizeof(Vertex);
+int VertexC::stride = sizeof(VertexC);
+
+void VertexSup::setPointers () {
+ GLCheck( glVertexPointer(3, GL_FLOAT, stride, &x) );
+}
+
+void VertexSup::setClientState () {
+ GLCheck( glEnableClientState(GL_VERTEX_ARRAY) );
+}
+
+void VertexSup::draw (int mode, int count) {
+ setPointers();
+ setClientState();
+ GLCheck( glDrawArrays(mode, 0, count) );
+}
+
+void Vertex::setClientState() {
+ VertexSup::setClientState();
+
+ GLCheck( glDisableClientState(GL_COLOR_ARRAY) );
+ GLCheck( glEnableClientState(GL_TEXTURE_COORD_ARRAY) );
+}
void Vertex::setPointers() {
+ VertexSup::setPointers();
+
GLCheck( glVertexPointer(3, GL_FLOAT, stride, &x) );
GLCheck( glTexCoordPointer(2, GL_FLOAT, stride, &s) );
}
+void VertexC::setClientState() {
+ VertexSup::setClientState();
+
+ GLCheck( glEnableClientState(GL_COLOR_ARRAY) );
+ GLCheck( glDisableClientState(GL_TEXTURE_COORD_ARRAY) );
+}
+
+void VertexC::setPointers() {
+ VertexSup::setPointers();
+
+ GLCheck( glVertexPointer(3, GL_FLOAT, stride, &x) );
+ GLCheck( glColorPointer(4, GL_FLOAT, stride, &r) );
+}
View
67 src/vertex.h
@@ -3,34 +3,79 @@
#include "includes.h"
-class Vertex {
+// A vertex class that only has a position, which the others will inherit from.
+class VertexSup {
public:
// Intuitively, x and y are parallel to the ground, and z is "up".
// OpenGL has y being "up". So I swap the position of y and z, so now
// it works like I expect.
float x, z, y;
+
+ static int stride;
+
+ VertexSup (float _x, float _y, float _z)
+ : x(_x), y(_y), z(_z) {}
+
+ virtual void setPointers();
+ virtual void setClientState ();
+ virtual void draw (int mode, int count);
+};
+
+// The main vertex class, so it gets the shortest name.
+class Vertex : public VertexSup {
+public:
float s, t;
- /* Apparently a size-multiple of 32 bytes is good. Floats tend to be 4.
- If I ever start using normals, I can replace this with nx, ny, nz.
- Or I can put colors in. But not both.
- http://www.opengl.org/wiki/Vertex_Formats */
- float padding[3];
+ // Apparently a size-multiple of 32 bytes is good. Floats tend to be 4,
+ // and subclassing seems to add 4.
+ // http://www.opengl.org/wiki/Vertex_Formats
+ float padding[2];
static int stride;
- Vertex() : x(0), y(0), z(0), s(0), t(0) {}
+ Vertex () : VertexSup(0, 0, 0), s(0), t(0) {}
Vertex(float _x, float _y, float _z)
- : x(_x), y(_y), z(_z), s(0), t(0) {}
+ : VertexSup(_x, _y, _z), s(0), t(0) {}
Vertex(float _x, float _y, float _z, float _s, float _t)
- : x(_x), y(_y), z(_z), s(_s), t(_t) {}
+ : VertexSup(_x, _y, _z), s(_s), t(_t) {}
Vertex(ph::vec3f v, float _s, float _t)
- : x(v.x), y(v.y), z(v.z), s(_s), t(_t) {}
+ : VertexSup(v.x, v.y, v.z), s(_s), t(_t) {}
+
+ void setPointers ();
+ void setClientState ();
+};
+
+// The same thing, but with colours instead of texture coordinates.
+// This one doesn't need padding, since it's already 32 bytes.
+class VertexC : public VertexSup {
+public:
+ float r, g, b, a;
+
+ static int stride;
+
+ VertexC () : VertexSup(0, 0, 0), r(0), g(0), b(0), a(1) {}
+
+ VertexC (float _x, float _y, float _z)
+ : VertexSup(_x, _y, _z), r(0), g(0), b(0), a(1) {}
+
+ VertexC (float _x, float _y, float _z, float _r, float _g, float _b)
+ : VertexSup(_x, _y, _z), r(_r), g(_g), b(_b), a(1) {}
+
+ VertexC (float _x, float _y, float _z,
+ float _r, float _g, float _b, float _a)
+ : VertexSup(_x, _y, _z), r(_r), g(_g), b(_b), a(_a) {}
+
+ VertexC (ph::vec3f v, float _r, float _g, float _b)
+ : VertexSup(v.x, v.y, v.z), r(_r), g(_g), b(_b), a(1) {}
+
+ VertexC (ph::vec3f v, float _r, float _g, float _b, float _a)
+ : VertexSup(v.x, v.y, v.z), r(_r), g(_g), b(_b), a(_a) {}
- void setPointers();
+ void setPointers ();
+ void setClientState ();
};
#endif
View
1 src/world.cpp
@@ -78,6 +78,7 @@ void World::draw() {
GLCheck( glColor3f(0.5, 0.5, 0.5) );
GLCheck( glEnableClientState(GL_VERTEX_ARRAY) );
+ GLCheck( glDisableClientState(GL_COLOR_ARRAY) );
GLCheck( glVertexPointer(3, GL_FLOAT, 0, floor_vertices) );
GLCheck( glDrawArrays(GL_QUADS, 0, 4) );

0 comments on commit cd84846

Please sign in to comment.
Something went wrong with that request. Please try again.