0

I'm trying to draw some basic triangles using opengl, but it's not rendering on screen. These are the relevant functions:

glewInit();
glClearColor(0.0, 0.0, 0.0, 1.0);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
Vertex vertices[] = {Vertex(Vector3f(0.0, 1.0, 0.0)),
                     Vertex(Vector3f(-1.0, -1.0, 0.0)),
                     Vertex(Vector3f(1.0, -1.0, 0.0))};

mesh.addVertices(vertices, 3);

Pastebin links to Vertex.hpp and Vector3f.hpp:

Vertex.hpp

Vector3f.hpp

/* 
 * Mesh.cpp:
 */
Mesh::Mesh()
{
    glGenBuffers(1, &m_vbo); // unsigned int Mesh::m_vbo
}

void Mesh::addVertices(Vertex vertices[4], int indexSize)
{
    m_size = indexSize * 3;
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    glBufferData(GL_ARRAY_BUFFER, m_size, vertices, GL_STATIC_DRAW);
}

void Mesh::draw()
{
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(Vertex), 0);
    glDrawArrays(GL_TRIANGLES, 0, m_size);
    glDisableVertexAttribArray(0);
}

It's just black if I call glClear otherwise just the random noise of a default window. I can make it draw a triangle by using the most primitive method:

glBegin(GL_TRIANGLES);
glColor3f(0.4, 0.0, 0.0);
glVertex2d(0.0, 0.5);
glVertex2d(-0.5, -0.5);
glVertex2d(0.5, -0.5);
glEnd();

That works and displays what it should do correctly, so I guess that at least says my application is not 100% busted. The tutorial I'm following is in Java, and I'm translating it to C++ SFML as I go along, so I guess it's possible that something got lost in translation so to speak, unless I'm just missing something really basic (more likely.)

How do we fix this so it uses the Vertex list to draw the triangle like it's supposed to?

2
  • It is clearly wriong that you use m_size both for the buffer size and the glDrawArrays count. And what exactly are Vertex and Vector3f? Commented Jun 26, 2014 at 19:56
  • @derhauss I updated the question with links to Vertex and Vector3f, please take a look. Commented Jun 26, 2014 at 22:46

1 Answer 1

2

So many mistakes. There are truly a lot of examples, in any language, so why?

const float pi = 3.141592653589793; is member field of Vector3f. Do you realise this is non-static member and it is included in each and every Vector3f you use, so your vectors actually have four elements - x, y, z, and pi? Did you informed GL about it, so it could skip this garbage data? I don't think so.

You using glVertexAttribPointer, but don't have active shader. There is no guarantee that position is in slot 0. Either use glVertexPointer, or use shader with position attribute bound to 0.

void Mesh::addVertices(Vertex vertices[4], int indexSize) what [4] supposed to mean here? While it is not an error, it is at least misguiding.

glBufferData(GL_ARRAY_BUFFER, m_size, vertices, GL_STATIC_DRAW); m_size is 3*3 in your example, while documentation says it should be array size in bytes - which is sizeof(Vertex) * indexSize.

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(Vertex), 0); why stride parameter is 4*sizeof(Vertex)? Either set it to 0 or write correct stride - which is sizeof(Vertex).

glDrawArrays(GL_TRIANGLES, 0, m_size); m_size is already [incorrectly] set as "vertex buffer size", while DrawArrays expects number of vertices to draw - which is m_size / sizeof(Vertex) (given m_size is calculated correctly).

Sign up to request clarification or add additional context in comments.

5 Comments

Since 6 problems just aren't enough, glEnableVertexAttribArray and glDisableVertexAttribArray are also the wrong functions when using fixed function vertex attributes.
Thanks for your help. I removed pi from Vector3f, I changed Mesh::draw to use glVertexPointer and glEnableClientState(GL_VERTEX_ARRAY) instead. I removed the unnecessary [4] and set m_size to array_size * sizeof(Vertex), I also set stride to sizeof(Vertex) and finally changed m_size in glDrawArrays to be m_size / sizeof(Vertex) as you suggested. However it's still just a black screen.
What colour is assigned at the moment of drawing? And either disable culling or change order to GL_CCW, since your triangle is counterclockwise.
@keltar It's a dark red, I added glColor3f(0.4,0.0,0.0) to the draw method. This is strange, when I disabled culling and depth test, it sometimes draws a red rectangle in different, seemingly random, parts of the screen. Hey we are starting to make progress!
@keltar: "[...] but don't have active shader. There is no guarantee that position is in slot 0." That is wrong. The spec guarantees that generic attribute 0 aliases the fixed function VERTEX attribute. It is even guaranteed that glVertexAttrib*(0,...) in an Begin/End block emits a vertex like glVertex*(...) did.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.