openGL键盘控制三角形旋转



#include "stdafx.h"
#include
#include"gl.h"
#include"glut.h"


//确定多边形绕法
bool bWinding = TRUE;


//旋转参数
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;


void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);


//旋转图形
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);//绕x轴旋转
glRotatef(yRot, 0.0f, 1.0f, 0.0f);//绕y轴旋转


//设置点的大小以及线的宽度
glPointSize(5);
glLineWidth(5);


//设置多边形绕法方向--顺时针/逆时针
if (bWinding)
glFrontFace(GL_CW);
else
glFrontFace(GL_CCW);
//绘制三角形
glBegin(GL_TRIANGLES);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(0, 60, 0);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-60, -60, 0);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(60, -60, 0);


glEnd();
//刷新命令缓冲区
glutSwapBuffers();


}
//改变大小
void ChangeSize(int w, int h)
{
if (0 == h)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-100.0f, 100.0f, -100.0*h / w, 100.0f*h / w,-100.0f,200.0f);
else
glOrtho(-100.0f*w/h, 100.0f*w/h, -100.0f, 100.0f , -100.0f, 200.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


}


void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);


}
 




void SpecialKeys(int key, int x, int y)
{
if (key == GLUT_KEY_UP)
xRot -= 5.0f;
if (key == GLUT_KEY_DOWN)
xRot += 5.0f;
if (key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if (key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if (xRot > 360.0f)
xRot = 0.0f;
if (xRot < -1.0f)
xRot = 359.0f;
if (yRot > 360.0f)
yRot = 0.0f;
if (yRot <-1.0f)
yRot = 359.0f;
//刷新窗口
glutPostRedisplay();




}


int _tmain(int argc, _TCHAR* argv[])
{
glutInit(&argc, (char**)argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("多边形绘制");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
//设置响应回调函数
glutSpecialFunc(SpecialKeys);
SetupRC();
glutMainLoop();
return 0;
}

你可能感兴趣的:(openGL键盘控制三角形旋转)