Anonymous Entity
Anonymous Entity

Reputation: 3350

OpenGL vertex array not rendering

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?

Upvotes: 0

Views: 1311

Answers (1)

keltar
keltar

Reputation: 18409

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).

Upvotes: 2

Related Questions