OpenGL第三節:Viewport視口

LOpengGL.h://不變it

LUtil.h  //添加枚舉移動

enum ViewPortMode
{
  VIEWPORT_MODE_FULL,  //全屏
  VIEWPORT_MODE_HALF_CENTER, //中間
  VIEWPORT_MODE_HALF_TOP,//中間上
  VIEWPORT_MODE_QUAD,//四個小方塊
  VIEWPORT_MODE_RADAR//雷達
};文件

 

LUtil.cpp:枚舉

#include "LUtil.h"參數

bool initGL()
{
  glViewport( 0.f, 0.f, SCREEN_WIDTH, SCREEN_HEIGHT );//設置視口爲全屏,參數分別爲x,y,w,hvoid

  ......//其餘的不變模型

}render


void render()//freeglut的座標是這樣的:(0,0)在左下角,也就是x向右變大,y向上變大。不一樣的庫實現時的座標系統不一樣,不能一律而論。而OpenGL自己的座標系統是(0,0)在左上角。
{
  glClear( GL_COLOR_BUFFER_BIT );系統

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();//重置模型矩陣爲單位矩陣

  glTranslatef( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, 0.f );//移動到屏幕中心,參數分別爲x,y,z

  if( gViewportMode == VIEWPORT_MODE_FULL )//全屏
  {
    glViewport( 0.f, 0.f, SCREEN_WIDTH, SCREEN_HEIGHT );

    glBegin( GL_QUADS );
      glColor3f( 1.f, 0.f, 0.f );
      glVertex2f( -SCREEN_WIDTH / 2.f, -SCREEN_HEIGHT / 2.f );
      glVertex2f( SCREEN_WIDTH / 2.f, -SCREEN_HEIGHT / 2.f );
      glVertex2f( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );
      glVertex2f( -SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );
    glEnd();
  }
  else if( gViewportMode == VIEWPORT_MODE_HALF_CENTER )//中心
  {
    glViewport( SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f, SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );

    glBegin( GL_QUADS );
      glColor3f( 0.f, 1.f, 0.f );
      glVertex2f( -SCREEN_WIDTH / 2.f, -SCREEN_HEIGHT / 2.f );
      glVertex2f( SCREEN_WIDTH / 2.f, -SCREEN_HEIGHT / 2.f );
      glVertex2f( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );
      glVertex2f( -SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );
    glEnd();
  }
  else if( gViewportMode == VIEWPORT_MODE_HALF_TOP )
  {
    glViewport( SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 2.f, SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );//上邊

    glBegin( GL_QUADS );
      glColor3f( 0.f, 0.f, 1.f );
      glVertex2f( -SCREEN_WIDTH / 2.f, -SCREEN_HEIGHT / 2.f );
      glVertex2f( SCREEN_WIDTH / 2.f, -SCREEN_HEIGHT / 2.f );
      glVertex2f( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );
      glVertex2f( -SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );
    glEnd();
  }
  else if( gViewportMode == VIEWPORT_MODE_QUAD )//四個小方塊
  {
    glViewport( 0.f, 0.f, SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );//左下
    glBegin( GL_QUADS );
      glColor3f( 1.f, 0.f, 0.f );
      glVertex2f( -SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
      glVertex2f( SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
      glVertex2f( SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
      glVertex2f( -SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
    glEnd();

    glViewport( SCREEN_WIDTH / 2.f, 0.f, SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );//右下
    glBegin( GL_QUADS );
      glColor3f( 0.f, 1.f, 0.f );
      glVertex2f( -SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
      glVertex2f( SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
      glVertex2f( SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
      glVertex2f( -SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
    glEnd();

    glViewport( 0.f, SCREEN_HEIGHT / 2.f, SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );//左上
    glBegin( GL_QUADS );
      glColor3f( 0.f, 0.f, 1.f );
      glVertex2f( -SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
      glVertex2f( SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
      glVertex2f( SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
      glVertex2f( -SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
    glEnd();

    glViewport( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );//右上
    glBegin( GL_QUADS );
      glColor3f( 1.f, 1.f, 0.f );
      glVertex2f( -SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
      glVertex2f( SCREEN_WIDTH / 4.f, -SCREEN_HEIGHT / 4.f );
      glVertex2f( SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
      glVertex2f( -SCREEN_WIDTH / 4.f, SCREEN_HEIGHT / 4.f );
    glEnd();
  }
  else if( gViewportMode == VIEWPORT_MODE_RADAR )//雷達
  {
    glViewport( 0.f, 0.f, SCREEN_WIDTH, SCREEN_HEIGHT );//大的
    glBegin( GL_QUADS );
      glColor3f( 1.f, 1.f, 1.f );
      glVertex2f( -SCREEN_WIDTH / 8.f, -SCREEN_HEIGHT / 8.f );
      glVertex2f( SCREEN_WIDTH / 8.f, -SCREEN_HEIGHT / 8.f );
      glVertex2f( SCREEN_WIDTH / 8.f, SCREEN_HEIGHT / 8.f );
      glVertex2f( -SCREEN_WIDTH / 8.f, SCREEN_HEIGHT / 8.f );
      glColor3f( 0.f, 0.f, 0.f );
      glVertex2f( -SCREEN_WIDTH / 16.f, -SCREEN_HEIGHT / 16.f );
      glVertex2f( SCREEN_WIDTH / 16.f, -SCREEN_HEIGHT / 16.f );
      glVertex2f( SCREEN_WIDTH / 16.f, SCREEN_HEIGHT / 16.f );
      glVertex2f( -SCREEN_WIDTH / 16.f, SCREEN_HEIGHT / 16.f );
    glEnd();

    glViewport( SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f, SCREEN_WIDTH / 2.f, SCREEN_HEIGHT / 2.f );//小的
    glBegin( GL_QUADS );
      glColor3f( 1.f, 1.f, 1.f );
      glVertex2f( -SCREEN_WIDTH / 8.f, -SCREEN_HEIGHT / 8.f );
      glVertex2f( SCREEN_WIDTH / 8.f, -SCREEN_HEIGHT / 8.f );
      glVertex2f( SCREEN_WIDTH / 8.f, SCREEN_HEIGHT / 8.f );
      glVertex2f( -SCREEN_WIDTH / 8.f, SCREEN_HEIGHT / 8.f );
      glColor3f( 0.f, 0.f, 0.f );
      glVertex2f( -SCREEN_WIDTH / 16.f, -SCREEN_HEIGHT / 16.f );
      glVertex2f( SCREEN_WIDTH / 16.f, -SCREEN_HEIGHT / 16.f );
      glVertex2f( SCREEN_WIDTH / 16.f, SCREEN_HEIGHT / 16.f );
      glVertex2f( -SCREEN_WIDTH / 16.f, SCREEN_HEIGHT / 16.f );
    glEnd();
  }

  glutSwapBuffers();//刷新屏幕
}

void handleKeys( unsigned char key, int x, int y )
{
if( key == 'q' )//按下q鍵
{

  gViewportMode++;//枚舉默認值是0,1,2.。。。
  if( gViewportMode > VIEWPORT_MODE_RADAR )
  {
    gViewportMode = VIEWPORT_MODE_FULL;
  }
  }
}

...其餘文件內容不變

相關文章
相關標籤/搜索