1
\$\begingroup\$

I make OpenGL render framework for my bachelor graduate work. I created simple Model and Shader classes. But when I tried to render some vertices, they do not render. I saw with an OpenGL profiler, that I haven't vertex data, but in code I bind it.

There's my main class:

GLfloat vertex_positions[] = {
        0.0f, 0.5f, 1.0f, 0.0f, 0.0f,
        0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
        -0.5f, -0.5f, 0.0f, 0.0f, 1.0f
};
GLchar *vertexShader = (GLchar *)
                "#version 150\n"
                "\n"
                "in vec2 position;\n"
                "\n"
                "void main()\n"
                "{\n"
                "    gl_Position = vec4(position, 0.0, 1.0);\n"
                "}";
GLchar *fragmentShader = (GLchar *)
        "#version 400 core\n"
        "\n"
        "out vec4 fColor;\n"
        "\n"
        "void main()\n"
        "{\n"
        "    fColor = vec4(0.5, 0.4, 0.8, 1.0);\n"
        "}";
static const GLfloat vertex_colors[] =
        {
                1.0f, 1.0f, 1.0f, 1.0f,
                1.0f, 1.0f, 0.0f, 1.0f,
                1.0f, 0.0f, 1.0f, 1.0f,
                0.0f, 1.0f, 1.0f, 1.0f
        };
Shader shader;
shader.CompileShader(Shader::ShaderType::Fragment, fragmentShader);
shader.CompileShader(Shader::ShaderType::Vertex, vertexShader);
glBindFragDataLocation(*shader.GetProgram(), 0, "fColor");
shader.Link();
Texture texture((GLfloat *) vertex_colors, 16);
Model model (vertex_positions, &shader, &texture);
while (!glfwWindowShouldClose(window))
{
    static const float black[] = { 0.2f, 0.3f, 1.0f, 1.0f };
    glClearBufferfv(GL_COLOR, 0, black);
    shader.Bind();
    model.Draw();
    glfwPollEvents();
    glfwSwapBuffers(window);
}

And my model class:

void Model::Draw() {
glBindVertexArray(_vao);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glDrawArrays(GL_TRIANGLES, 0, 3);
}

Model::Model(GLfloat meshVert[], int m_size, GLuint vertexInd[], int         
v_size, Shader *material, Texture *texture) {
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);

glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*m_size, meshVert,      
   GL_STATIC_DRAW);

GLint posAttrib = glGetAttribLocation(*material->GetProgram(), "position");
glEnableVertexAttribArray((GLuint) posAttrib);
glVertexAttribPointer((GLuint) posAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);

GLint colAttrib = glGetAttribLocation(*material->GetProgram(), "color");
glEnableVertexAttribArray((GLuint) colAttrib);
glVertexAttribPointer((GLuint) colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));

}

P.S.: OpenGL profiler breaks on command glDrawArrays, but when I saw data info, it was empty. That's why I think I lost some data bind.

P.S.S: System: macOS Sierra, Intel HD 6000.

\$\endgroup\$
  • \$\begingroup\$ You don't need to bind your vbo for drawing \$\endgroup\$ – cozmic Oct 9 '16 at 10:09
  • \$\begingroup\$ Ok, thank you. But even if I delete bind from draw, it doesn't help. \$\endgroup\$ – Айрат Насыров Oct 9 '16 at 15:22
  • \$\begingroup\$ Have you a destructor that deletes your GL objects? And no copy constructor? \$\endgroup\$ – Maximus Minimus Aug 31 '18 at 8:28
0
\$\begingroup\$

Your shader doesn't use color attribute, but you are trying to use it in C++ code.

Comment out these lines:

GLint colAttrib = glGetAttribLocation(*material->GetProgram(), "color");
glEnableVertexAttribArray((GLuint) colAttrib);
glVertexAttribPointer((GLuint) colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you, but it doesn't help. Maybe you found any other error in code? \$\endgroup\$ – Айрат Насыров Oct 9 '16 at 15:21
0
\$\begingroup\$

Your polygon is wound clock-wise which by default will get culled if GL_CULL_FACE is enabled.

Try calling glDisable(GL_CULL_FACE); to disable face culling, it should be disabled by default but it might not be the case due to glfw.

Also try calling glDisable(GL_DEPTH_TEST) before rendering as well.

And your model class instantiation does not match your class constructor:

Model model (vertex_positions, &shader, &texture);
Model::Model(GLfloat meshVert[], int m_size, GLuint vertexInd[], int v_size, Shader *material, Texture *texture)
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

In your call to glDrawArrays() you're telling it to only draw a single triangle (3 vertices). The last parameter, count is the number of indices to draw. From the man page:

count

Specifies the number of indices to be rendered.

Unless you're only drawing a single triangle, you probably need to give it a larger number.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.