Skip to content

Commit

Permalink
Go back to a single vertex class, which has both color and texcoords.
Browse files Browse the repository at this point in the history
It's more flexible this way.
  • Loading branch information
ChickenProp committed Dec 8, 2010
1 parent ae3a60e commit e182eee
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 20 deletions.
37 changes: 35 additions & 2 deletions src/vertex.cpp
Expand Up @@ -3,8 +3,41 @@

int Vertex::stride = sizeof(Vertex);

void Vertex::setPointers() {
Vertex::Vertex ()
: hasTexture(false) {}
Vertex::Vertex (float x, float y, float z)
: x(x), y(y), z(z),
hasTexture(false) {}
Vertex::Vertex (float x, float y, float z, float s, float t)
: x(x), y(y), z(z),
hasTexture(true), s(s), t(t),
r(1), g(1), b(1), a(1) {}
Vertex::Vertex (float x, float y, float z,
float r, float g, float b, float a)
: x(x), y(y), z(z),
hasTexture(false),
r(r), g(g), b(b), a(a) {}
Vertex::Vertex (const ph::vec3f &v, float s, float t)
: x(v.x), y(v.y), z(v.z),
hasTexture(true), s(s), t(t),
r(1), g(1), b(1), a(1) {}
Vertex::Vertex (const ph::vec3f &v, float r, float g, float b, float a)
: x(v.x), y(v.y), z(v.z),
hasTexture(false),
r(r), g(g), b(b), a(a) {}

void Vertex::setupPointers() {
GLCheck( glVertexPointer(3, GL_FLOAT, stride, &x) );
GLCheck( glTexCoordPointer(2, GL_FLOAT, stride, &s) );
GLCheck( glColorPointer(4, GL_FLOAT, stride, &r) );

if (hasTexture)
GLCheck( glTexCoordPointer(2, GL_FLOAT, stride, &s) );
}

void Vertex::setupClientState() {
GLCheck( glEnableClientState(GL_VERTEX_ARRAY) );
GLCheck( glEnableClientState(GL_COLOR_ARRAY) );

if (hasTexture)
GLCheck( glEnableClientState(GL_TEXTURE_COORD_ARRAY) );
}
38 changes: 22 additions & 16 deletions src/vertex.h
Expand Up @@ -9,28 +9,34 @@ class Vertex {
// 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;

bool hasTexture;
float s, t;
float r, g, b, a;

/* 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.
/* Apparently a size-multiple of 32 bytes is good. This makes it 64. If
I ever start using normals, I can put those in.
http://www.opengl.org/wiki/Vertex_Formats */
float padding[3];
char padding[27];

static int stride;

Vertex() : x(0), y(0), z(0), s(0), t(0) {}

Vertex(float _x, float _y, float _z)
: x(_x), y(_y), z(_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) {}

Vertex(ph::vec3f v, float _s, float _t)
: x(v.x), y(v.y), z(v.z), s(_s), t(_t) {}

void setPointers();
Vertex ();
Vertex (float x, float y, float z);
Vertex (float x, float y, float z, float s, float t);
Vertex (float x, float y, float z,
float r, float g, float b, float a=1.0f);
Vertex (const ph::vec3f &v, float s, float t);
Vertex (const ph::vec3f &v, float r, float g, float b, float a=1.0f);

Vertex* setPos(float x, float y, float z);
Vertex* setPos(const ph::vec3f &pos);
Vertex* setTexCoords(float s, float t);
Vertex* setHasTexture(bool h);
Vertex* setColor(float r, float g, float b, float a=1.0f);

void setupPointers();
void setupClientState();
};

#endif
2 changes: 1 addition & 1 deletion src/wall.cpp
Expand Up @@ -53,6 +53,6 @@ Wall::~Wall() {
}

void Wall::draw() {
corners[0].setPointers();
corners[0].setupPointers();
GLCheck( glDrawArrays(GL_QUADS, 0, 4) );
}
2 changes: 1 addition & 1 deletion src/world.cpp
Expand Up @@ -90,7 +90,7 @@ void World::draw() {

GLCheck( glEnableClientState(GL_TEXTURE_COORD_ARRAY) );

cube_vertices[0].setPointers();
cube_vertices[0].setupPointers();

GLCheck( glColor3f(1, 1, 1) );
GLCheck( glDrawElements(GL_QUADS, 24, GL_UNSIGNED_SHORT, cube_faces) );
Expand Down

0 comments on commit e182eee

Please sign in to comment.