您的位置:首页 > 运维架构

OpenGL视口变换函数:glViewport and the relationship between glViewport and gluOtho

2012-10-21 02:57 483 查看




OpenGL视口变换函数:glViewport

/*From following blog, we can now know the relationship between glViewport and gluOtho.
As we can see, both functions are used in the function "reshanpe()", which is function established for setting the viewing. glViewPort(w, h) serves as the function to clarify where to rendering in the window  and the relevant size. We know that the routine
of opengl is after making the objects in mathematics, then tell the screen to display the object. Thus gluOtho() provides the specific view to see the object, just like you are using the camera to choose where to taking photo. */

转自:http://hi.baidu.com/gongziya/item/df4a0128b12f26f751fd870d

调用glViewPort函数来决定视见区域,告诉OpenGL应把渲染之后的图形绘制在窗体的哪个部位。当视见区域是整个窗体时,OpenGL将把渲染结果绘制到整个窗口。

glViewPort(x:GLInt;y:GLInt;Width:GLSizei;Height:GLSizei);

    其中,参数X,Y指定了视见区域的左下角在窗口中的位置,一般情况下为(0,0),Width和Height指定了视见区域的宽度和高度。注意OpenGL使用的窗口坐标和WindowsGDI使用的窗口坐标是不一样的。图3.1-1表示了在WindowsGDI中的窗口坐标,而图3.1-2则是OpenGL所定义的窗口坐标。

                                                                                                                                                                 


                                                                                  图3.1-1                             
                                              

  
                                                                               图3.1-2

 

    例如,要设置如图3.1-3中的视见区域,我们应该调用函数:

      glViewPort(100,100,Width,Height);





                                                                               图3.1-3

glViewport()函数可以实现拆分窗口的功能,可以用不同的视口多次绘制同一个物体,如,为了创建两个并排的窗口,可以使用下面的这些函数,并辅以适当的模型视图和投影变换:

glViewport(0, 0, sizex/2, sizey);

                      ;

                      ;

                      ;

glViewport(sizex/2, 0,sizex/2, sizey);

 

下面是一个例子:

   #include <gl/glut.h>

//不显示控制台窗口

#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

void display()

{

//glMatrixMode( GL_MODELVIEW );

//glLoadIdentity();

    glClear( GL_COLOR_BUFFER_BIT );

glColor3f( 1.0, 0.0, 0.0 );

//画分割线,分成四个视见区

glViewport( 0, 0, 400, 400 ); 

glBegin( GL_LINES );

     glVertex2f( -1.0, 0 );

   glVertex2f( 1.0, 0 );

   glVertex2f( 0.0, -1.0 );

   glVertex2f( 0.0, 1.0 );

    glEnd();

//定义在左下角的区域

glColor3f( 0.0, 1.0, 0.0 );

glViewport( 0, 0, 200, 200 );

glBegin( GL_POLYGON );

   glVertex2f( -0.5, -0.5 );

     glVertex2f( -0.5, 0.5 );

   glVertex2f( 0.5, 0.5 );

   glVertex2f( 0.5, -0.5 );

glEnd();

//定义在右上角的区域

glColor3f( 0.0, 0.0, 1.0 );

glViewport( 200, 200, 200, 200 );//一定要注意,后面这两个参数是高度和宽度,而不是坐标

    glBegin( GL_POLYGON );

     glVertex2f( -0.5, -0.5 );

     glVertex2f( -0.5, 0.5 );

     glVertex2f( 0.5, 0.5 );

     glVertex2f( 0.5, -0.5 );

glEnd();

//定义在左上角的区域

glColor3f( 1.0, 0.0, 1.0 );

glViewport( 0, 200, 200, 200 );//一定要注意,后面这两个参数是高度和宽度,而不是坐标

glBegin( GL_POLYGON );

glVertex2f( -0.5, -0.5 );

glVertex2f( -0.5, 0.5 );

glVertex2f( 0.5, 0.5 );

glVertex2f( 0.5, -0.5 );

glEnd();

//定义在右下角

glColor3f( 1.0, 1.0, 0.0 );

glViewport( 200, 0, 200, 200 );//一定要注意,后面这两个参数是高度和宽度,而不是坐标

glBegin( GL_POLYGON );

glVertex2f( -0.5, -0.5 );

glVertex2f( -0.5, 0.5 );

glVertex2f( 0.5, 0.5 );

glVertex2f( 0.5, -0.5 );

glEnd();

glFlush();

}

void init()

{

    glClearColor( 0.0, 0.0, 0.0, 0.0 );

glColor3f( 1.0, 1.0, 1.0 );

glMatrixMode( GL_PROJECTION );

glLoadIdentity();

//定义剪裁面

gluOrtho2D( -1.0, 1.0, -1.0, 1.0 );

//glViewport( 0, 0, 200, 200 ); //放在这里不好使

}

int main( int argc, char ** argv )

{

     glutInit( &argc, argv );

glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );

glutInitWindowPosition( 100, 100 );

glutInitWindowSize( 400, 400 );

glutCreateWindow( "glViewport()" );

glutDisplayFunc( display );

     init();

glutMainLoop();

}

编译运行结果如下:

   



 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息