I am trying to solve a bug with my excercice of VBO/OpenGL, but after some hours (days), I am unable to found what is wrong.
The problem is the windows stay black, the cube is not (correctly) drawn.
-
The OpenGL context seem to be correctly created for OpenGL 3.2
Vertex Shader:
#version 150 core
uniform mat4 RotationMatrix;
uniform mat4 ProjectionMatrix;
in vec3 in_Position;
void main(void)
{
gl_Position = ProjectionMatrix * RotationMatrix * vec4(in_Position, 1.0);
}
VBO initialization:
// <-- Create OpenGL 3.2 context
// <-- Load Shaders
// <-- Set ClearColor, clearDepth, DepthFUnc, enable Depth
// <-- Set Viewport, init projection and modelview matrix.
float positionData[] =
{
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
};
GLuint indexes[] =
{
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
0, 3, 7, 0, 7, 4,
1, 5, 6, 1, 6, 2,
0, 4, 5, 0, 5, 1,
2, 3, 7, 2, 7, 6
};
glGenBuffers(2, vboHandles);
glBindBuffer( GL_ARRAY_BUFFER, vboHandles[0]);
glBufferData(GL_ARRAY_BUFFER, 24*sizeof(float), positionData, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboHandles[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36*sizeof(GLuint),
indexes, GL_STATIC_DRAW);
In the OpenGL draw function:
//<--Clear buffer, set Uniform variables for projection and modelview
glBindBuffer(GL_ARRAY_BUFFER, vboHandles[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboHandles[1]);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, NULL);
//<-- Flush Opengl and swap buffers
Correct / Incorrect
EDITED: Fragment shader:
#version 150 core
out vec4 out_Color;
void main(void){
out_Color = vec4(1.0, 0.0, 0.0, 1.0f);
}
EDITED 2: Adding context creation
SDL_Window* window;
SDL_GLContext context;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
return 0;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
if ((window = SDL_CreateWindow( "Practica 4 and lot of stuff for the final prictice",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN)) == NULL)
{
SDL_Quit();
return 0;
}
if (NULL== (context = SDL_GL_CreateContext(window)))
{
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
glewExperimental = GL_TRUE;
GLuint glewStatus = glewInit();
if (glewStatus != GLEW_OK)
{
cout << "Error" << endl;
}