I have been experiencing some strange behaviour using Opengl. I am doing something wrong, but I wasn't able to find out what it was.
I have HD Graphics 3000 accelerator and I have Opengl 3.0 installed on Ubuntu 14.04.
The problem is that nothing gets drawn sometimes especially when doing a small translation which should cause it to be visible. When rendering textures I get segmentation errors.
Here are important parts of the code:
Initiating SDL, creating opengl context, etc
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
exit(1);
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
mainwindow = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
maincontext = SDL_GL_CreateContext(mainwindow);
glewInit();;
SDL_GL_SetSwapInterval(1);
Initiating opengl:
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(double)width/(double)height,1,100.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING); //we enable lighting, to make the 3D object to 3D
glEnable(GL_LIGHT0);
glEnable(GL_TEXTURE_2D);
float col[]= {1.0,1.0,1.0,1.0}; //light color is white
glLightfv(GL_LIGHT0,GL_DIFFUSE,col);
glEnable(GL_NORMALIZE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Rendering:
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor ( 0.0, 0.0, 0.0, 1.0 );
glLoadIdentity();
glTranslatef(0,0,-5);
glColor3f(1,0,0);
glBegin(GL_TRIANGLES);
glVertex3f(-1,-1,-5);
glVertex3f(1,-1,-5);
glVertex3f(0,1,-5);
glEnd();
I actually render some loaded models but even this triangle is not shown for some reason. If I render the models, when loading the textures a segmentation fault error happens when using the automatic routine provided by SOIL for loading images to opengl.
Any ideas? I also check for errors in opengl and also when initiating GLEW, but there aren't any.
Ok, the problem was that I worked with Opengl (call lightning, set view, etc) without initiating SDL and creating a context. Strangely, Opengl didn't call any errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.