我试图将相机移动到我装载的物体周围,但由于某些原因,按键操作不起作用。
我从之前正在进行的项目中获取了相关代码,该代码正常运行。
有人能指出我为什么不工作的正确方向吗?
#include <math.h>
#include <stdio.h>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
#elif __linux__
#include <GL/glut.h>
#endif
float camera_position_x = 0.0, camera_position_y = 0.1, camera_position_z = 0.2;
float line_of_sight_x = 0.0, line_of_sight_y = 0.2, line_of_sight_z = 0.0;
GLuint objObject;
char ch='1';
void InitGL (int width, int height) // We call this right after our OpenGL window is created.
{
glClearColor (255.0f, 255.0f, 255.0f, 0.0f); // This will clear the background color to white
glClearDepth (1.0); // Enables clearing of the depth buffer
glDepthFunc (GL_LESS); // The type of depth test to do
glEnable (GL_DEPTH_TEST); // Enables depth testing
glShadeModel (GL_SMOOTH); // Enables smooth color shading
glMatrixMode (GL_PROJECTION);
glLoadIdentity (); // Reset the projection matrix
gluPerspective (45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f); // Calculate the aspect ratio of the window
glMatrixMode (GL_MODELVIEW);
}
void loadObj(char *fname)
{
FILE *fp;
int read;
GLfloat x, y, z;
char ch;
objObject=glGenLists(1);
fp=fopen(fname,"r");
if (!fp)
{
printf("can't open file %s\n", fname);
exit(1);
}
glPointSize(2.0);
glNewList(objObject, GL_COMPILE);
{
glPushMatrix();
glBegin(GL_POINTS);
while(!(feof(fp)))
{
read=fscanf(fp,"%c %f %f %f",&ch,&x,&y,&z);
if(read==4&&ch=='v')
{
glVertex3f(x,y,z);
}
}
glEnd();
}
glPopMatrix();
glEndList();
fclose(fp);
}
void ReSizeGLScene (int width, int height)
{
if (height == 0) // Prevent A Divide By Zero If The Window Is Too Small
{
height = 1;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (90, (GLfloat)width / (GLfloat)height, 1.0, 200.0);
//gluLookAt(0.0, 0.1, 0.2, 0.0, 0.2, 0.0, 0.0, 0.0, 1.0);
gluLookAt(camera_position_x, camera_position_y, camera_position_z,
line_of_sight_x, line_of_sight_y, line_of_sight_z, 0.0, 0.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void drawobjObject()
{
glPushMatrix();
glTranslatef(0,-40.00,-105);
glColor3f(1.0,0.23,0.27);
glScalef(600,600,600);
glCallList(objObject);
glPopMatrix();
}
void display(void)
{
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
drawobjObject();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y) {
switch(key)
{
case 'a':
line_of_sight_y += 3.0f;
camera_position_y += 3.0f;
break;
case 'z':
line_of_sight_y -= 3.0f;
camera_position_y -= 3.0f;
break;
default: break;
}
}
void arrowkey(int key, int x, int y) {
switch(key) {
case GLUT_KEY_UP :
line_of_sight_z += 3.0f;
camera_position_z += 3.0f;
break;
case GLUT_KEY_DOWN :
line_of_sight_z -= 3.0f;
camera_position_z -= 3.0f;
break;
case GLUT_KEY_LEFT :
line_of_sight_x += 3.0f;
camera_position_x += 3.0f;
break;
case GLUT_KEY_RIGHT:
line_of_sight_x -= 3.0f;
camera_position_x -= 3.0f;
break;
}
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(800,450);
glutInitWindowPosition(20,20);
glutCreateWindow("ObjLoader");
glutReshapeFunc(ReSizeGLScene);
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutSpecialFunc(arrowkey);
loadObj("bunny.obj");
glutMainLoop();
return 0;
}
答案 0 :(得分:1)
据我所知,只有在调用x
时才会发生所需的几何变化;因为键盘回调只改变变量。而变量只能在ReSizeGLScene()
内访问
因此,当按下其中一个按钮然后例如按钮时,可能只能观察到这种变化。拉开窗户。
我希望解决方案是从两个键盘回调中调用ReSizeGLScene()
,使用先前存储的高度和宽度值。
但是你可能想要优化它并单独执行ReSizeGLScene()
,从键盘回调和大小调整器中触发。