[英]opengl draw line height of window
我想知道如何根据封闭窗口的尺寸绘制线的长度。 请注意,我正在使用GLUT和OpenGL的组合。
例如,假设我想从屏幕的底部中心画一条线(我假设这将在坐标(WINDOW_LENGTH / 2,0)到窗口中心(WINDOW_LENGTH / 2,WINDOW_HEIGHT / 2)
如何在OpenGL中执行此操作? 现在,我有以下内容:
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //
gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
}
//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glTranslatef(0, 0, -1);
glBegin(GL_LINES);
//lines
glVertex2f(0, 0);
glVertex2f(0, .25);
glEnd();
glutSwapBuffers(); //Send the 3D scene to the screen
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size
//Create the window
glutCreateWindow("Basic Shapes - videotutorialsrock.com");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
//glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
cout << "GLUT_WINDOW_X: " << GLUT_WINDOW_X << endl;
cout << "GlUT_WINDOW_Y: " << GLUT_WINDOW_Y << endl;
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
这给了我以下结果:
对我来说没有意义的是我的窗口尺寸为400 X 400,但是坐标为: glVertex2f(0, 0)
和glVertex2f(0, .25)
。 在大约窗口中心到大约窗口高度的80%处画一条线。 我有一些推测:
我知道我对glTranslatef(0, 0, -1);
调用glTranslatef(0, 0, -1);
将原点设置为全局坐标(0, 0, -1)
我感到困惑的是:
.25
是否对应于高度的25%? 如果您需要更多信息,请告诉我。
让我尝试回答您的问题:
您正在模型视图(查看世界)系统中工作。 因此,您开始在世界坐标中建模并将其转换为视图坐标。 因此,glTranslatef会将您的世界移离摄像机1个坐标点。
您在openGL中所做的工作与Windows坐标几乎没有关系。 因此,0.25实际上对openGL意味着0.25,仅此而已。 这意味着您可以稳定点的任何语义,例如米,公里,毫米等。 在函数gluPerspective中建立了openGL缓冲区和Windows坐标之间的相关性,其中基本上说了必须映射到Windows坐标系的世界区域。 第二个功能glViewport仅说明如何将此地图转换为Windows坐标。 在您的情况下,您要使用所有窗口。
如前所述,您需要操纵gluPerspective来控制将openGL世界映射到Windows坐标的数量。 为此,您可以更改透视图的角度。 更大的角度,将映射更多的openGL区域,您将获得缩小的效果。 较小的角度,较少的openGL区域将被映射,您将获得放大效果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.