1

I am trying to draw a simple, red triangle on screen. Without using an VBO the code works as intended. When trying to draw it by using an VBO (with or without shader), it simply has no effect.

My code:

//Works
glBegin(GL_TRIANGLES);
glVertex3f(0f, 0f, 0.0f);
glVertex3f(0.5f, 0f, 0.0f);
glVertex3f(0.5f,  0.5f, 0.0f);
glEnd();

//Does not work
int vertexArrayID = glGenVertexArrays();
glBindVertexArray(vertexArrayID);

float[] g_vertex_buffer_data = new float[]{
        0f, 0f, 0.0f,
        0.5f, 00f, 0.0f,
        0.5f,  0.5f, 0.0f,
};

int vertexbuffer = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, FloatBuffer.wrap(g_vertex_buffer_data), GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

glVertexAttribPointer(
        0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
        3,                  // size
        GL_FLOAT,           // type
        false,           // normalized?
        0,                  // stride
        0            // array buffer offset
);

glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle

glDisableVertexAttribArray(0);
glDeleteBuffers(vertexbuffer);
glDeleteVertexArrays(vertexArrayID);


System.out.println(glGetError());

glGetError() always returns 0.

I use a default, tutorial-copied shader to test my code (I link and bind the program before using above code):

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;

void main(){

    gl_Position.xyz = vertexPosition_modelspace;
    gl_Position.w = 1.0;
}
#version 330 core

// Ouput data
out vec3 color;

void main()
{
    // Output color = red
    color = vec3(1,0,0);
}
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Friwi
  • 482
  • 3
  • 13
  • shaders do not output to `glGetError()` instead they have their own error mesage system see [complete GL+GLSL+VAO/VBO C++ example](https://stackoverflow.com/a/31913542/2521214) and look for `glGetShaderInfoLog` and `glGetProgramInfoLog` usage. Also if you got nVidia its sometimes useful to use [compatible layout locations with fixed function](https://stackoverflow.com/a/20574219/2521214) (only for debug purposes naturally) – Spektre Jan 30 '20 at 09:02
  • Do you call glDrawArrays() in a loop or only once? Do you have glClear somewhere? Also check you float vertices array, fix that 00f value. – qbranchmaster Jan 30 '20 at 13:50

0 Answers0