#include <iostream>
#include <windows.h>
#include <gl/glut.h>
#include <math.h>
#include <gl/gl.h>
/* back to openGL 2016/9/19 magic */
//thanks to: http://blog.****.net/bill_ming/article/details/7662809
using namespace std; #define pi 3.1415926
#define GLUT_WHEEL_UP 3 //定义滚轮操作
#define GLUT_WHEEL_DOWN 4 int mx,my;
int ry=;int rx=; //turnning angle around X/Y axis following right-hand rule
int i=;
GLdouble a=0.2; //size of teapot
bool mouseisdown=false;
bool loopr=false; void timer(int p)
{
ry-=;
glutPostRedisplay(); //marks the current window as needing to be redisplayed.
if (loopr)
glutTimerFunc(,timer,);
} void motion(int x, int y)
{
if(mouseisdown==true)
{
// cout<<mx<<" "<<my<<" "<<i<<endl;
ry+=x-mx;
rx+=y-my;
mx=x;my=y;
glutPostRedisplay();
}
} void mouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON)
{
// cout<<"left click!"<<endl;
mx=x;my=y; //Initialize mx my 9/20 if(state == GLUT_DOWN)
{ mouseisdown=true; loopr=false;}
else
mouseisdown=false;
}
if (button== GLUT_RIGHT_BUTTON)
if(state == GLUT_DOWN)
{loopr=true; glutTimerFunc(,timer,);}
if (button== GLUT_MIDDLE_BUTTON)
{
// cout<<"wheel"<<endl;
if(state == GLUT_DOWN){
a+=0.1;
glutPostRedisplay();
}
}
} void special(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_LEFT:
ry-=;
glutPostRedisplay();
break; case GLUT_KEY_RIGHT:
ry+=;
glutPostRedisplay();
break; case GLUT_KEY_UP:
rx+=;
glutPostRedisplay();
break; case GLUT_KEY_DOWN:
rx-=;
glutPostRedisplay();
break; }
} void special_1(unsigned char key, int x, int y){ switch(key){
case 'a':
a+=0.1;
glutPostRedisplay();
break;
case 'z':
a-=0.1;
glutPostRedisplay();
break;
}
} void display0(void)
{
float red[]={,,};
float blue[]={,,};
float green[]={,,};
float yellow[]={,,}; glClearColor(,,,);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glRotatef(ry,,,);
glRotatef(rx,,,);
glColor3fv(green); glutWireTeapot(a); glFlush();
} int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE| GLUT_RGBA);
glutInitWindowSize (, );
glutInitWindowPosition(,);
glutCreateWindow ("A TEAPOT"); //window initializing glutDisplayFunc (display0);
glutMouseFunc(mouse); //message processing function
glutMotionFunc(motion);
glutSpecialFunc(special);
glutKeyboardFunc(special_1);
glutMainLoop(); return ;
}
back to openGL
这是一个初级的openGL编程例子,实现了程序对键鼠操作消息的处理:
1.鼠标拖拽使模型跟随光标旋转;
2.按'A'或双击鼠标中键放大,按'Z'缩小;
3.单击鼠标右键使模型旋转;
4.方向键(上下左右)使模型旋转