#include "stdafx.h"
#include <GL/glut.h>
#include <iostream>
#pragma comment( lib, "glut32.lib")
//注意:glut.h与glut32.lib版本要一样,否则会出现链接错误:无法解析的外部符号__imp____glutInitWithExit@12,该符号在函数 _glutInit_ATEXIT_HACK@8 中被引用,因为版本不同,可能函数的名称或参数列表不同,即找不到函数的定义)
using namespace std;
double g_dAngle= 0.0f;
void Init(void)
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
}
void Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glLoadIdentity();
glTranslatef( 0.0f, 0.0f , -2.0f);
glRotatef( g_dAngle, 0.0f, 0.0f, 1.0f);
//draw a square
glBegin( GL_QUADS);
glVertex3f( 0.5f, 0.5f, 0.0f);
glVertex3f( -0.5f, 0.5f, 0.0f);
glVertex3f( -0.5f, -0.5f, 0.0f);
glVertex3f( 0.5f, -0.5f, 0.0f);
glEnd();
//glFlush();
glutSwapBuffers(); //双缓冲
}
void Reshape(int w,int h)
{
glViewport(0,0 ,(GLsizei)w ,(GLsizei)h );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 90.0f, (GLfloat)w/ (GLfloat)h, 0.1f, 1000.0f );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void OnTimer( int iTimerIndex)
{
//更新角度
g_dAngle += 2.0f;
if ( g_dAngle > 360.0f)
{
g_dAngle -= 360.0f;
}
glutPostRedisplay();//刷新显示
glutTimerFunc( 50, OnTimer, 0);
}
void Keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case 'w': //前
break;
case 's': //后
break;
case 'a': //左
break;
case 'd': //右
break;
case 'q': //上
break;
case 27:
exit(0);
break;
}
}
void MouseEvent(int button, int state, int x, int y)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
cout<<"GLUT_LEFT_BUTTON"<<endl;
break;
case GLUT_RIGHT_BUTTON:
cout<<"GLUT_RIGHT_BUTTON"<<endl;
break;
case GLUT_MIDDLE_BUTTON:
cout<<"GLUT_MIDDLE_BUTTON"<<endl;
break;
}
}
void MotionMove(int x,int y)
{
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(450,450);
glutInitWindowPosition(200,200);
glutCreateWindow("Hello");
Init();
glutDisplayFunc( Display);
glutReshapeFunc( Reshape);
//定时器
glutTimerFunc( 50, OnTimer, 0 );
glutKeyboardFunc( Keyboard);
glutMouseFunc( MouseEvent);
glutMotionFunc( MotionMove);
glutMainLoop();
return 0;
}