所以我想学习如何使用glViewport(),“多次”。下面是我的代码,我试着跟随其他人的例子,但他们要么是如此复杂的其他事情不相关的我在做什么,或者,我不明白他们在做什么。顺便说一句,我用glut来管理我的窗口系统。所以对我来说,最好的学习方法就是给自己一个简单的例子,然后从中推断。下面是一个简单程序的代码,该程序将屏幕分成两部分并绘制两个相同的球体,我无法理解当两个视图端口的glupperspective()相同时,右侧的球体为什么会拉伸。拜托,如果你能向我解释一下我在代码中做错了什么,那将大有帮助。外部资源是伟大的,但我需要简单,简单的例子(不是内特罗宾逊的例子)。
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);
}
请注意,我在等待响应时四处游荡,我得到了我想要的结果,但是解决方案没有,因为,请看代码行,基本上我将视图端口的起始位置移动到屏幕的1/4,而不是一半,并且我不再在gluperspective上除以2。
/*
* THE SECOND VIEW PORT
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(width/4, 0, width , height);
gluPerspective(45, ((float)width)/(((float)height)), 1, 2000);