gpt4 book ai didi

c - 使用按键的 GLUT 旋转相机无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 03:20:07 24 4
gpt4 key购买 nike

我正在尝试围绕我加载的对象移动相机,但由于某种原因,按键不起作用。

我从我之前从事的一个项目中获取了相关代码,该项目有效。

有人可以指出他们为什么不工作的正确方向吗?

#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;
}

最佳答案

据我所知,所需的几何变化仅在调用 ReSizeGLScene() 时发生;因为键盘回调只会改变变量。而变量只能在 ReSizeGLScene() 中访问。
因此,这种变化可能只有在按下其中一个按钮然后例如按下时才能观察到。稍微拉伸(stretch)窗口。

我希望解决方案是从两个键盘回调中调用 ReSizeGLScene(),使用先前存储的高度和宽度值。
但是您可能想要对其进行优化并单独执行 gluLookAt(),以便从键盘回调和调整器中触发。

关于c - 使用按键的 GLUT 旋转相机无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47148918/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com