• OpenGL编程 基础篇(五)世界窗口和视口


    一、基本定义

    世界窗口:在世界坐标系中定义一个对齐的矩形(aligned rectangle,即矩阵的边与坐标轴平行)的窗口,这个世界窗口外的部分被裁减并不被绘制。OpenGL会自动地做剪裁。

    视口:在显示器的屏幕窗口上定义一个对齐的矩形的视口,OpenGL会自动建立世界窗口和视口的变换(包括缩放和平移)。当世界窗口中所有对象都被绘制时,对象在世界窗

    口中的部分会被自动地映射到视口中————换句话说,被映射到屏幕坐标中,即像素在显示器上的坐标。

    二、相关函数介绍

    1.对于二维绘图来说,世界窗口由函数gluOrtho2D()设定,它的原型是:
        void gluOrtho2D(GLDouble left,GLdouble right,GLdouble buttom,GLdouble top);
       对于三维的情况,有另外两个参数需要设定,后续讲述。

    2.视口的设定通过glViewport()函数,它的原型是:
        void glViewport(GLint x,GLint y,GLint width,GLint ehignt);//它设置窗口的左下角,以及宽度和高度。
        注意:因为openGL通过矩阵来完成所有的变换,因此GluOrtho2D()的调用必须在glMatrixModel(GL_PROJECTION)和glLoadIdentity()这两个函数之后。

    3.将设定窗口的部分放到setWindow()函数中,增加程序的可读性:

    void setWindow(GLdouble left,GLdouble right,GLdouble buttom,GLdouble top)
    {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(left,right,bottom,top); //eg: gluOrtho2D(0.0,640.0,0.0,480.0);
    }
    //为了让setViewport()更容易使用,我们将其参数作了轻微的调整使得它和setWindow()相匹配;
    //它们都是按左、右、下、上的顺序做参数
    void setViewport(GLint left,GLint right,GLint bottom,GLint top)
    {
    //right-left 等于宽度,top-bottom 等于高度
    glViewport(left,bottom,right-left,top-bottom);
    }

    三、应用样例——平铺

    setWindow(0,640.0,0,440.0);
    for(int i = 0;i < 5;i++)
    {
      for(int j = 0;j < 5;j++)
      {
        glViewport(i * 64,j * 44,64,44);
        drawPolylineFile("dino.dat");    //dino.dat为存储绘图数据的文件,drawPolylineFile为画该文件数据的函数
      }
    }

    四、程序示例

    从文件中读取数据绘制小房子,更改setWindow和setViewport的参数改变房子的大小

    dot.dat

    5
    5
    0
    0 0
    0 20
    10 25
    20 20
    20 0
    4
    1
    11 0
    11 12
    16 12
    16 0
    4 
    1
    20 0
    30 0
    30 10
    20 10
    4
    1
    24 0
    24 5
    27 5
    27 0
    2 
    1
    30 10
    20 16

    程序

    #include "stdafx.h"
    #include <glglut.h>
    #include <fstream>
    using namespace std;
    const int screenWidth = 640;
    const int screenHeight = 480;
    void myInit(){
        glClearColor(1.0, 1.0, 1.0, 0.0);
        glColor3f(0.0f, 0.0f, 0.0f);
        glPointSize(2.0);
        //glMatrixMode(GL_PROJECTION);
        //glLoadIdentity();
        //gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
    }
    void setWindow(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
    {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluOrtho2D(left, right, bottom, top); //eg:  gluOrtho2D(0.0,640.0,0.0,480.0);
    }
    void setViewport(GLint left, GLint right, GLint bottom, GLint top)
    {
        //right-left 等于宽度,top-bottom 等于高度
        glViewport(left, bottom, right - left, top - bottom);
    }
    void myDisplay(){
        fstream inStream;
        char filename[] = "dot.dat";
        inStream.open(filename, ios::in);
        if (inStream.fail()){ printf("Fail!!!!!
    "); return; }
        glClear(GL_COLOR_BUFFER_BIT);
        GLint numpolys, numLines, way, x, y;
        inStream >> numpolys;
        for (int j = 0; j < numpolys; j++)
        {
            inStream >> numLines;
            inStream >> way;
            if (way == 0){
                glBegin(GL_LINE_LOOP);
                for (int i = 0; i < numLines; i++)
                {
                    inStream >> x >> y;
                    glVertex2i(x, y);
                }
                glEnd();
            }
            else{
                glBegin(GL_LINE_STRIP);
                for (int i = 0; i < numLines; i++)
                {
                    inStream >> x >> y;
                    glVertex2i(x, y);
                }
                glEnd();
            }
            
        }
        glFlush();
        inStream.close();
    }
    int main(int argc, char **argv){
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        glutInitWindowSize(screenWidth, screenHeight);
        glutInitWindowPosition(100, 150);
        glutCreateWindow("Dot Plot of a Function");
        setWindow(0.0, 40.0, 0.0, 40.0);
        setViewport(0.0, 40.0, 0.0, 40.0);
        glutDisplayFunc(myDisplay);
        myInit();
        
        glutMainLoop();
    }
    View Code

    运行效果:

    setWindow(0.0, 40.0, 0.0, 40.0);
    setViewport(0.0, 40.0, 0.0, 40.0);

     

    setWindow(0.0, 400.0, 0.0, 240.0);
    setViewport(0.0, 400.0, 0.0, 240.0);

  • 相关阅读:
    Intellij IDEA Java web 项目搭建
    Spring的AOP原理
    为何有DAO与Service层?为何先搞Dao接口在搞DaoImpl实现?直接用不行吗?
    DAO层,Service层,Controller层、View层协同工作机制
    浅谈service、DAO层引入(转)
    java书籍推荐转
    Java点滴-List<Integer> list; 中尖括号的意思
    Composer更新慢的终极解决方案-转
    laravel门面DB返回数组配置
    laravel构建联合查询
  • 原文地址:https://www.cnblogs.com/starryxsky/p/7214724.html
Copyright © 2020-2023  润新知