这是一段OpenGL代码, 目的是通过移动鼠标来旋转视角观察中间的橙色立方体
在旋转视角部分,当我加入第44行代码,也就是也能在y之旋转,旋转的时候会看起来有些吃力和奇怪。
把那一行删了就没问题了,但是不能看立方体的上方和下方了。
代码运行的时候,鼠标会一直停在中间,按ESC退出程序。
#define GLUT_DISABLE_ATEXIT_HACK
#define _USE_MATH_DEFINES
#define v(x) x/256.0
#include <iostream>
#include <cmath>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void display();
void reshape(int,int);
void timer(int);
void move(int,int);
void init();
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(0,0);
glutInitWindowSize(1536,864);
glutCreateWindow("title");
glutFullScreen();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0,timer,0);
glutPassiveMotionFunc(move);
init();
glutMainLoop();//in loop
return 0;
}
float mx,my;
float dis=-8.0;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//clear
glLoadIdentity();
glTranslatef(0,-1.0,dis);
glRotatef(mx,0.0,1.0,0.0);
glRotatef(my,0.5,0.0,0.5);//这段代码运行后转起来有点奇怪
//画立方体
glBegin(GL_QUADS);
glColor3f(1.0,0.5,0.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,-1.0);
glEnd();
//画线
glLineWidth(3);
glBegin(GL_LINES);
glColor3f(0.0,0.0,0.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(-1.0,1.0,1.0);
glVertex3f(-1.0,1.0,-1.0);
glVertex3f(1.0,1.0,1.0);
glVertex3f(1.0,1.0,-1.0);
glVertex3f(1.0,-1.0,1.0);
glVertex3f(1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,1.0);
glVertex3f(-1.0,-1.0,-1.0);
glEnd();
glutSwapBuffers();
}
void reshape(int w,int h){
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80,16.0/9.0,2.0,50.0);
glMatrixMode(GL_MODELVIEW);
}
void timer(int){
glutPostRedisplay();
glutTimerFunc(1000.0/60.0,timer,0);
//检测输入用WINDOWS库
if (GetKeyState(0x57)&0x8000){
if (dis<-5.0) dis+=0.1;
}else if (GetKeyState(0x53)&0x8000){
if (dis>-20.0) dis-=0.1;
}
if (GetKeyState(0x1B)&0x8000){//按ESC退出
exit(0);
}
}
void move(int x,int y){
mx+=(x-glutGet(GLUT_SCREEN_WIDTH)/2)*0.1;
my+=(y-glutGet(GLUT_SCREEN_HEIGHT)/2)*0.1;
glutWarpPointer(glutGet(GLUT_SCREEN_WIDTH)/2,glutGet(GLUT_SCREEN_HEIGHT)/2);
}
void init(){
glClearColor(v(135),v(206),v(250),1.0);
glEnable(GL_DEPTH_TEST);
}