a2.cpp

来自「建立二维投影(以glOrtho函数)实例工程」· C++ 代码 · 共 59 行

CPP
59
字号
#include <windows.h>
#include <gl/glut.h>
#include <stdlib.h>

void RenderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(0.0f,0.0f,1.0f);
	//glRectf(-25.0f,25.0f,25.0f,-25.0f);
	glBegin(GL_TRIANGLES);
		glVertex2f(-100.0f,0.0f);
		glVertex2f(-300.0f,200.0f);
	    glVertex2f(440.0f,200.0f);
	glEnd();
	glFlush();
}
void SetupRC(void)
{
	glClearColor(1.0f,1.0f,1.0f,1.0f);
}

void ChangeSize(GLsizei w,GLsizei h)
{
	GLfloat aspectRatio;
	glViewport(50,50,w/2,h/2);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	aspectRatio=(GLfloat)w/(GLfloat)h;
	if(w<=h)
		glOrtho(-300.0,200.0,-200.0/aspectRatio,200.0/aspectRatio,1.0,-1.0);
	else
		glOrtho(-200.0*aspectRatio,200.0*aspectRatio,-200.0,200.0,1.0,-1.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}
void processNormalKeys(unsigned char key,int x,int y) 
{ 
      if(key==27) 
            exit(0); 
} 

void main(void)
{
	
	glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
	glutCreateWindow("glrect");
	

	glutDisplayFunc(RenderScene);
	glutReshapeFunc(ChangeSize);

	SetupRC();

	glutKeyboardFunc(processNormalKeys);

	glutMainLoop();


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?