1
\$\begingroup\$

So after a long attempt at DirectX I switched back to good old OpenGL. Now I'm running into this weird problem. My code involves shaders, of course, and drawing a basic cube. But I'm getting this weird shape. I've never seen this before. I was just wondering what could possibly be causing this? Thanks very much!

Initialization:

lightingShader->GenerateVAO();
lightingShader->GenerateVBO();

lightingShader->SetVertices(vertices, sizeof(vertices), GL_STATIC_DRAW);

lightingShader->BindVAO();
{
    lightingShader->AddAttribute(0, 3, GL_FLOAT, GL_FALSE, 3, 0);
}
lightingShader->UnbindVAO();

Rendering:

lightingShader->Use();

GLint objectColorLoc = lightingShader->GetUniformLocation("objectColor");
GLint lightColorLoc = lightingShader->GetUniformLocation("lightColor");

glUniform3f(objectColorLoc, 1, 0.5f, 0.31f);
glUniform3f(lightColorLoc, 1, 0.5f, 1);

GLint modelLoc = lightingShader->GetUniformLocation("model");
GLint viewLoc = lightingShader->GetUniformLocation("view");
GLint projLoc = lightingShader->GetUniformLocation("projection");

glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(game_->GetCamera()->View()));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(game_->GetCamera()->Projection()));

lightingShader->BindVAO();
{
    glm::mat4 model;
    glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));

    glDrawArrays(GL_TRIANGLES, 0, 36);
}
lightingShader->UnbindVAO();

Result:

enter image description here

As you can see, position and color is set fine, but I still get that weird shape.

\$\endgroup\$
5
  • \$\begingroup\$ If you think your problem involves your shaders then post your shader code. The issue could be in the vertex shader, or it could be in the way you are creating your vertex array. \$\endgroup\$
    – Yattabyte
    Nov 7, 2015 at 16:45
  • \$\begingroup\$ Unrelated to your problem, but why do you have member functions for generating and binding VAOs and VBOs on a class that apparently represents an OpenGL program or program pipeline object? The idea that you'd only ever need one VBO/VAO for a particular program, and that each program has a unique vertex format (and therefore might need it's own VBO/VAO) seems short-sighted to me \$\endgroup\$
    – bcrist
    Nov 7, 2015 at 20:40
  • \$\begingroup\$ Yeah it's a very bad layout. My software is only in alpha \$\endgroup\$ Nov 7, 2015 at 21:01
  • 1
    \$\begingroup\$ You've obfuscated your vertex attribute bindings, but note that strides and offsets are in bytes. Consider using a graphics debugger like RenderDoc or CodeXL to see what data is actually used to draw. \$\endgroup\$ Nov 7, 2015 at 21:12
  • \$\begingroup\$ Ahh, prim-vomit. The simplest, and least useful, of all the primitive shapes. \$\endgroup\$ Jan 6, 2016 at 16:27

1 Answer 1

1
\$\begingroup\$

It could be that you are mixing up the multiplication (of the vertices with your matrices in your vertex shader) in the shader. Mind you, this is inverted for OpenGL, as it uses column-major matrices as opposed to DirectX. It will still compile and if I recall correctly this is because it simply decides to transpose where necessary.

Also, make sure you are using glm's functions to modify the matrices, you might be editing the matrices by hand in a row-major fashion.

\$\endgroup\$
2
  • \$\begingroup\$ This is how I set my position: gl_Position = projection * view * model * vec4(position, 1); \$\endgroup\$ Nov 7, 2015 at 23:02
  • \$\begingroup\$ That seems correct. Have you tried removing the matrices one at a time? This can help exclude some issues (i.e. first remove multiplication with view, then model etc.). The same applies though, try to verify if your matrices are constructed in a column major fashion and also, like posted in one of the other comments, show how you are binding your vertex attributes. \$\endgroup\$
    – Athos
    Nov 8, 2015 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .