I am a beginner to OpenGL learning modern OpenGL from http://www.arcsynthesis.org/gltut/. Instead of using the miserable GLfloat[] = { … lots of data … } as vertex data; I thought I would try to use simple structs as shown below:
struct Coord
{
float X, Y, Z, W;
};
struct Color
{
float R, G, B, A;
} const RED(1.0f, .0f, .0f), GREEN(.0f, 1.0f, .0f), BLUE(.0f, .0f, 1.0f);
struct Vertex
{
Coord Position;
Color Color;
};
struct Triangle { Vertex Vertices[3]; };
So I create a triangle and send it to GL_ARRAY_BUFFER and when redisplay is called I set the vertex attribute pointers (0 being position, 1 being color):
std::vector<Triangle> triangles;
triangles.push_back(Triangle(
Vertex(Coord(.5f, .75f, .0f), GREEN),
Vertex(Coord(.5f, -.75f, .0f), GREEN),
Vertex(Coord(.0f, .0f, .0f), GREEN)
));
gl::BufferData(gl::GL_ARRAY_BUFFER, triangles.size() * sizeof(Triangle),
&triangles[0], gl::GL_STATIC_DRAW);
gl::VertexAttribPointer(0, 4, gl::GL_FLOAT, gl::GL_FALSE, sizeof(Color), 0);
gl::VertexAttribPointer(1, 4, gl::GL_FLOAT, gl::GL_FALSE, sizeof(Coord),
reinterpret_cast<const void*>(16));
gl::DrawArrays(gl::GL_TRIANGLES, 0, triangles.size() * 3);
This should produce an isosceles triangle that is completely green but what is instead produced is:
Any ideas? This program using a GLfloat array works as expected.