码迷,mamicode.com
首页 > 其他好文 > 详细

gluOrtho2D与比例尺之间的关系

时间:2016-12-12 15:24:27      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:gluortho2d 比例尺缩放

说明

      gluOrtho2D(0.0, WINDOWWIDTH , WINDOWHEIGHT/2, 0.0);第三个参数取其一半,打点的Y坐标扩大了一倍.渲染直线的情况下,通过将第二参数和第三参数进行按比例尺放缩,实际上,可达到渲染图形缩放的效果。所以gluOrtho2D函数定义了屏幕坐标多少个单位像素表示投影矩阵的单位一


代码

#include <gl/glut.h>

#include <Windows.h>


#define WINDOWHEIGHT 480

#define WINDOWWIDTH  800


void myInit()

{

  glClearColor(1.0,1.0,1.0,0.0);

  glLineWidth(3.0);

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  //gluOrtho2D(0.0, WINDOWWIDTH, 0.0, WINDOWHEIGHT);

  gluOrtho2D(0.0, WINDOWWIDTH , WINDOWHEIGHT/2, 0.0);

}


void myDisplay(void)

{

  glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glFlush();

}


void myMouse(int button,int state,int x,int y)

{

  if(state==GLUT_DOWN)

  {

    glPointSize(10);

      glBegin(GL_POINTS);

        glColor3f(1.0f, 0.0f, 0.0f);

        //glVertex2f(x, WINDOWHEIGHT-y);

        glVertex2f(x,  y);

      glEnd();

    glFlush();

  }

}


int main(int argc, char* argv[])

{

  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);

  glutInitWindowSize(WINDOWWIDTH, WINDOWHEIGHT);

  glutInitWindowPosition(100,150);

  glutCreateWindow("OpenGL 窗口1");

  glutDisplayFunc(&myDisplay);

  glutMouseFunc(&myMouse);

  myInit();

  glutMainLoop();

  return 0;

}


代码2

#include <gl/glut.h>

#include <Windows.h>


#define WINDOWHEIGHT 480

#define WINDOWWIDTH  800


void myInit(int scale)

{

  glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glFlush();

  glMatrixMode(GL_PROJECTION);

  glLoadIdentity();

  gluOrtho2D(0.0, WINDOWWIDTH/scale , WINDOWHEIGHT/scale, 0.0);

}


void myDisplay(void)

{

  glClearColor(1.0f, 1.0f, 1.0f, 0.0f);

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glFlush();

}


void myMouse(int button,int state,int x,int y)

{

  static int scale = 2;

  if(state==GLUT_DOWN)

  {

    myInit(scale);

    scale = scale + 2;

    glLineWidth(10);

      glBegin(GL_LINES);

        glColor3f(1.0f, 0.0f, 0.0f);

        glVertex2f(10,  10);

        glVertex2f(20,  20);

      glEnd();

    glFlush();

  }

}


int main(int argc, char* argv[])

{

  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);

  glutInitWindowSize(WINDOWWIDTH, WINDOWHEIGHT);

  glutInitWindowPosition(100,150);

  glutCreateWindow("OpenGL 窗口1");

  glutDisplayFunc(&myDisplay);

  glutMouseFunc(&myMouse);

  glutMainLoop();

  return 0;

}








gluOrtho2D与比例尺之间的关系

标签:gluortho2d 比例尺缩放

原文地址:http://fengyuzaitu.blog.51cto.com/5218690/1881861

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!