我想知道如何根据封闭窗口的尺寸绘制一条线的长度。请注意,我使用的是 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);
将原点设置为全局坐标(0, 0, -1)
让我感到困惑的是:
- -1 如何对应于将图像移动那么远?
- 第二个坐标中的是否
.25
对应于高度的 25%? - 代码从 (WINDOW_LENGTH/2, 0) 到 (WINDOW_LENGTH/2, WINDOW_HEIGHT/2) 画一条线会是什么样子,即从窗口底部中心到窗口中心的线。
如果您需要更多信息,请告诉我。