[英]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.