Reputation: 1065
I'm currently following a tutorial on modern OpenGL. I copied a code from the said tutorial and tried it. The code below should display a triangle but only a blank window appears. I tried the tutorial before this one in which a point appears on the screen and it worked but this one does not work. What could be the problem?
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
GLuint VBO;
static void RenderSceneCB()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glutSwapBuffers();
}
static void InitializeGlutCallbacks()
{
glutDisplayFunc(RenderSceneCB);
}
static void CreateVertexBuffer()
{
static const GLfloat Vertices[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(1024, 768);
glutInitWindowPosition(100, 100);
glutCreateWindow("Tutorial 03");
InitializeGlutCallbacks();
// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
CreateVertexBuffer();
glutMainLoop();
return 0;
}
Upvotes: 0
Views: 1053
Reputation: 1
I ran into this problem myself on Ubuntu 12.04. This problem can also occur if you do not have a current graphics driver installed, as it will try to use OpenGL 1.x. Update your video driver so that your system uses modern OpenGL and try it again.
As far as I know, the lack of shaders isn't a problem because it "should" utilize default shaders automatically. Though that might just be me.
Upvotes: 0
Reputation: 473407
I see no evidence of any shaders. And you can't use glVertexAttribPointer
without shaders.
The tutorial you copy-and-pasted this code from probably had shaders in it somewhere. You should use them.
Upvotes: 1