I have been following an opengl tutorial, but for some reason when i added the reshape function(for window resize to fix ratio). the opengl window doesnt show the object(shape) at all, just a black screen.
i dont know if i made a misspelling or something.
Is the reshape function messing things up?
#include <GL/gl.h>
#include <GL/glut.h>
void renderScene(void);
void changeSize(int w, int h);
int main(int argc, char **argv){
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Testing");
// register callbacks
glutDisplayFunc(renderScene);
// animation in reshaping
glutReshapeFunc(changeSize);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0.0);
glEnd();
glutSwapBuffers();
}
void changeSize(int w, int h){
if (h==0)
h = 1;
float ratio = 1.0*w/h;
// use the projection matrix
glMatrixMode(GL_PROJECTION);
//reset matrix
glLoadIdentity();
// set the viewpoint to be the entire window
glViewport(0,0,w,h);
// set the correct perspective
gluPerspective(45, ratio, 1, 1000);
// get back to the modelview
glMatrixMode(GL_MODELVIEW);
}
Thanks for any help!
This is from the Lighthouse3d tutorials.
PS: if i take out the reshape function, the triangle shows up(works fine).
Your triangle, as is, is not visible from that projection matrix you have defined with gluPerspective.
That matrix defines a view cone, pointing in the -z direction, with a near plane at z=-1 and far plane at z=-1000.
Your triangle lays on the z=0 plane, and as such is not visible.
You can either:
Move the triangle further back on the z direction (
glTranslate(0,0,-2), or move the vertices manually).Use a view matrix that moves the camera away from (0,0,0), such as with
gluLookAtUse an orthographic projection matrix that includes the z=0 plane, such as
glOrtho(-1, 1, -1, 1, -1, 1)