说明
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;
}