So I am trying to learn how to use glViewport(), "MULTIPLE TIMES". Below is my code, I have tried to follow others examples, but they are either so convoluted with other things not relevant to what I am doing or, I do not understand what they are doing. By the way I am using glut to manage my windowing system. So the best way for me to learn is to get a simple example running for myself, then extrapolate from there. Below is my code for a simple program that divides the screen in two and draws two identical spheres, I can't figure out why the sphere on the right is getting stretched when gluPerspective() is identical for both view ports. Please, if you could just explain to me what I am doing wrong in my code it would greatly help. Outside resources are great, but i need simple, simple examples (not Nate Robinson examples).
GLfloat width = 800, height = 600, x_0 = 470, y_0 = 0;
int mainWindow;
void resize(int w, int h){
width=w;
height=h;
}
void draw(void){
glEnable(GL_SCISSOR_TEST);
glClearDepth(1.0);
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
GLfloat color[4] = {1.0, 0.0, 0.0, 1.0};
/*
* LEFT VIEW PORT
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, (float)width/2 , height);
gluPerspective(45, ((float)width/2)/(float)height, 1, 2000);
glMatrixMode(GL_MODELVIEW);
glScissor(0, 0, (float)width , height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 0.0, 0.0, 50.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0 );
glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT);
glPushMatrix();
glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
glutSolidSphere(10.0, 20, 40);
glPopMatrix();
glPopAttrib();
/*
* THE SECOND VIEW PORT
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport((float)width/2, 0, width , height);
gluPerspective(45, ((float)width)/2/((float)height), 1, 2000);
glMatrixMode(GL_MODELVIEW);
glScissor(((float)width/2), 0, width , height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt( 0.0, 0.0, 50.0,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0 );
glPushAttrib(GL_LIGHTING_BIT | GL_CURRENT_BIT);
glPushMatrix();
glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
glutSolidSphere(10.0, 20, 40);
glPopMatrix();
glPopAttrib();
glDisable(GL_SCISSOR_TEST);
glFlush();
glutSwapBuffers();
}
void glutAppInitialize(void){
GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_position[] = {1.0, 1.0, 0.0, 0.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
int main(int argc, char* argv[]){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(x_0, y_0);
glutInitWindowSize(width ,height);
glutAppInitialize();
mainWindow = glutCreateWindow("Tori App");
glutDisplayFunc(draw);
glutReshapeFunc(resize);
glutMainLoop();
return(0);
}
Note that I am playing around while I wait on a response, I got it to do what I wanted, but the solution makes no since, please see the lines of code, basically I moved the view ports start position to 1/4 of the screen instead of on half and I am no longer dividing by two on gluperspective.
/*
* THE SECOND VIEW PORT
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(width/4, 0, width , height);
gluPerspective(45, ((float)width)/(((float)height)), 1, 2000);
The second pair of arguments to glViewport are width and height, not the ending x/y position. Your second glViewport should be glViewport(width/2, 0, width/2, height)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With