【问题标题】:GLUT rotating camera using key press not workingGLUT使用按键旋转相机不起作用
【发布时间】:2017-11-07 02:11:38
【问题描述】:

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

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

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

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

【问题讨论】:

  • 你有没有遇到过其他人在写文字时使用的那些曲折的小东西?例如,我在想:“.,;?”。请编辑您的问题以提高可读性,以便给人留下您重视他们的意见和他们在其上花费的时间的印象,即使这意味着您必须自己花一些心思和精力。
  • 你有没有尝试切换这两个:` glutKeyboardFunc(keyboard);` ` glutSpecialFunc(arrowkey);` 我的意思是使用glutKeyboardFunc(arrowkey);
  • 没有,因为我编辑评论只是切换箭头键和键盘参数。
  • 请描述/解释“不工作”。您按下其中一个按钮,然后调整窗口大小(以使ReSizeGLScene() 内部的几何更改有效,否则似乎没有影响)然后您观察到没有任何变化?
  • 我喜欢不幸的首字母缩略词,例如GLUT

标签: c glut freeglut


【解决方案1】:

据我所知,只有在调用 ReSizeGLScene() 时才会发生所需的几何变化;因为键盘回调只改变变量。而变量又只能在 ReSizeGLScene() 内部访问。
因此,只有在按下其中一个按钮然后例如按下按钮时才能观察到变化。把窗口拉长一点。

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

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    相关资源
    最近更新 更多