0
\$\begingroup\$
void Mesh::render() {
    glBindVertexArray(id);
    glUseProgram(programID);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesID)
    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);    
    glUseProgram(0);
    glBindTexture(GL_TEXTURE_2D, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);
}

Hi fellows! I'm an OpenGL newbie and having a problem with my indices and my tex_coords. Before I used indices, the Texture displayed fine. But when using glDrawElements instead of glDrawArrays I just see a black triangle...

Here is my vertex data:

float* positions = new float[9]{
    -1.0f, -1.0f, 0.0f, //1
    1.0f, -1.0f, 0.0f,  //2
    0.0f, 1.0f, 0.0f    //3
}

float* tex_coords = new float[6]{
    0.0f, 0.0f, //1
    1.0f, 0.0f, //2
    0.5f, 1     //3
}

int* indices = new int[3]{
   0, 1, 2
}

As you can see it's just a triangle, nothing fancy.

My shaders are normal hello-world-texture-shaders:

1.Vertex Shader:

#version 430 core

layout (location=0) in vec3 vertex_pos;
layout (location=1) in vec2 tex_coords;

out vec3 out_color;
out vec2 out_tex_coord;

void main(){
    gl_Position = vec4(vertex_pos, 1.0);
    out_color = vec3(0.0, 1.0, 0.0);
    out_tex_coord = tex_coords;
}
  1. Fragment Shader:

    #version 420 core
    
    in vec3 out_color;
    in vec2 out_tex_coord;
    
    uniform sampler2D diffuse_texture;
    
    out vec4 color;
    
    void main(){
        color = texture(diffuse_texture, out_tex_coord);
    }
    

Does anyone know how to solve this? Thanks in advance ;)

---EDIT---

The Mesh setup:

    glBindVertexArray(vaoID);

    glBindBuffer(GL_ARRAY_BUFFER, positions_buffer_id);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 3, positions, GL_STATIC_DRAW); 
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0));
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, tex_coords_buffer_id);
    glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * 3, tex_coords, GL_STATIC_DRAW);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
    glEnableVertexAttribArray(1);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer_id);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3, indices, GL_STATIC_DRAW);

    //Unbind
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindVertexArray(0);

I also played around with various textures and noticed, that the Triangle isn't black, but that it's got a color. The color seems to be the mixed value of all pixels contained by the Texture.

\$\endgroup\$
  • \$\begingroup\$ Do you bind the index buffer when the vertex array object is also bound? \$\endgroup\$ – Bálint Aug 30 '18 at 23:31
  • \$\begingroup\$ Also, do you unbind the vao before loading data into the index buffer? \$\endgroup\$ – Bálint Aug 30 '18 at 23:34
  • \$\begingroup\$ I've added my Mesh setup to the question. \$\endgroup\$ – Daniel Mehlber Aug 31 '18 at 12:13
  • 1
    \$\begingroup\$ do you call glGenBuffers for your buffers? Where do you create the uniform id for your texture sampler? Where do you activate your texture i.e. glActivateTexture? \$\endgroup\$ – Kyy13 Aug 31 '18 at 17:12
  • \$\begingroup\$ Can you show texture initialization code? Have you put linear or near filtering modes ? \$\endgroup\$ – Paritosh Kulkarni Sep 14 '18 at 2:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.