简体   繁体   English

OpenGL绘制线的窗口高度

[英]opengl draw line height of window

I would like to know how to draw the length of a line with respect to the the dimensions of the enclosing window. 我想知道如何根据封闭窗口的尺寸绘制线的长度。 Note that I am using the combination of GLUT and OpenGL. 请注意,我正在使用GLUT和OpenGL的组合。

For example, say I wanted to draw a line from the bottom center of the screen (I assume this would be at coordinate (WINDOW_LENGTH/2, 0) to the center of the window (WINDOW_LENGTH/2, WINDOW_HEIGHT/2) 例如,假设我想从屏幕的底部中心画一条线(我假设这将在坐标(WINDOW_LENGTH / 2,0)到窗口中心(WINDOW_LENGTH / 2,WINDOW_HEIGHT / 2)

How do I do this in OpenGL? 如何在OpenGL中执行此操作? Right now I have the following: 现在,我有以下内容:

//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                                                                                                                              
}

This gives me the following result: 这给了我以下结果: 在此处输入图片说明

What does not make sense to me is that my window has dimension 400 X 400 but the coordinates: glVertex2f(0, 0) and glVertex2f(0, .25) . 对我来说没有意义的是我的窗口尺寸为400 X 400,但是坐标为: glVertex2f(0, 0)glVertex2f(0, .25) draw a line from about the center of the window to about 80% of the height of the window. 在大约窗口中心到大约窗口高度的80%处画一条线。 I have a few speculations: 我有一些推测:

I know that my call to glTranslatef(0, 0, -1); 我知道我对glTranslatef(0, 0, -1);调用glTranslatef(0, 0, -1); sets the origin to the global coordinate (0, 0, -1) What is puzzling to me: 将原点设置为全局坐标(0, 0, -1)我感到困惑的是:

  1. How does the -1 correspond to moving the image that far over? -1如何对应于将图像移远?
  2. Does the .25 in the second coordinate correspond to 25% of the height? 第二个坐标中的.25是否对应于高度的25%?
  3. What would the code look like to draw a line from (WINDOW_LENGTH/2, 0) to (WINDOW_LENGTH/2, WINDOW_HEIGHT/2) That is the line from the bottom center of the window to the center of the window. 从(WINDOW_LENGTH / 2,0)到(WINDOW_LENGTH / 2,WINDOW_HEIGHT / 2)的代码看起来像一条线,即从窗口底部中心到窗口中心的线。

If you need more information let me know. 如果您需要更多信息,请告诉我。

Let me try answer your questions: 让我尝试回答您的问题:

  1. You are working in a model-view (world to view) system. 您正在模型视图(查看世界)系统中工作。 So you start modelling in world coordinates and transform it to the view coordinates. 因此,您开始在世界坐标中建模并将其转换为视图坐标。 Therefore, glTranslatef is moving your world 1 coordinates points away from de camera. 因此,glTranslatef会将您的世界移离摄像机1个坐标点。

  2. What you do in openGL is weakly related to your windows coordinates. 您在openGL中所做的工作与Windows坐标几乎没有关系。 So, 0.25 really means 0.25 to openGL and nothing more. 因此,0.25实际上对openGL意味着0.25,仅此而已。 This means that you can stablish any semantics to points, like meters, kilometres, milimeters and so on. 这意味着您可以稳定点的任何语义,例如米,公里,毫米等。 The correlation between openGL buffer and windows coordinates is stablished in the function gluPerspective, in which it says, basically, the region of your world that must be mapped to your windows coordinate system. 在函数gluPerspective中建立了openGL缓冲区和Windows坐标之间的相关性,其中基本上说了必须映射到Windows坐标系的世界区域。 The second function, glViewport only says how this map will be translated to you windows coordinates. 第二个功能glViewport仅说明如何将此地图转换为Windows坐标。 In your case, you are telling to use all the windows. 在您的情况下,您要使用所有窗口。

  3. As I said before, you need to manipulate your gluPerspective to control how much the openGL world will be mapped to your windows coordinates. 如前所述,您需要操纵gluPerspective来控制将openGL世界映射到Windows坐标的数量。 To do that, you change the angle of the perspective. 为此,您可以更改透视图的角度。 Greater angles, more openGL regions will be mapped, and you get an effect of zoom out. 更大的角度,将映射更多的openGL区域,您将获得缩小的效果。 Lesser angles, less openGL regions will be mapped, and you get an effect of zoom in 较小的角度,较少的openGL区域将被映射,您将获得放大效果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM