简体   繁体   English

为什么我不能移动3D立方体? OpenGL的

[英]Why can't I move my 3D cubes? OpenGL

My code is about creating 2 cubes, then rotating them at the same time, moving them at the same time, and scaling them at the same time. 我的代码是关于创建2个多维数据集,然后同时旋转它们,同时移动它们以及同时缩放它们。 For rotating and scaling they work, but moving doesn't. 对于旋转和缩放,它们起作用,但移动不起作用。 I think it's something to do with my glTranslatef() but I tried all the ways I know. 我认为这与我的glTranslatef()有关,但是我尝试了所有已知的方法。

The origin is the center of window and the cubes are always rotating. 原点是窗口的中心,并且多维数据集始终在旋转。

Fragment Code: 片段代码:

    static GLfloat x = 0.0f;
    static GLfloat y = 0.0f;

    void display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glLoadIdentity();
        glTranslatef(-(1.5f + x), y + 0.5f, 0.3f);
        glScalef(xScale, yScale, zScale);
        glRotatef(theta[0], 1.0, 0.0, 0.0);
        glRotatef(theta[1], 0.0, 1.0, 0.0);
        glRotatef(theta[2], 0.0, 0.0, 1.0);
        colorcube();
        glFlush();

        glLoadIdentity(); 
        glTranslatef(1.5f + x, y + 0.5f, 0.3f);
        glScalef(xScale, yScale, zScale);
        glRotatef(theta[0], 1.0, 0.0, 0.0);
        glRotatef(theta[1], 0.0, 1.0, 0.0);
        glRotatef(theta[2], 0.0, 0.0, 1.0);
        colorcube();

        glFlush();
        glutSwapBuffers();
    }

    void spinCube()
    {
        // idle callback, spin cube 2 degrees about selected axis
        if (negative == true) theta[axis] -= 0.5;
        else  if (negative == false) theta[axis] += 0.5;
        if (theta[axis] > 360.0) theta[axis] -= 360.0;
        //display();
        glutPostRedisplay();
    }

void actionKeys(unsigned char key, int x, int y ) 
{
    switch (key)
    {
case 'r': case 'R': 
            x += 0.5f;
            glutPostRedisplay();
            break;
        case 'l': case 'L':
            x += 0.5f;
            glutPostRedisplay();
            break;
        case 'u': case 'U':
            y += 0.5f;
            glutPostRedisplay();
            break;
        case 'd': case 'D':
            y -= 0.5f;
            glutPostRedisplay();
            break;
}
}

The problem is with variable scope in your actionKeys function. 问题在于您的actionKeys函数中的变量范围。 You have global variables named x and y and the function parameters are also named x and y. 您有名为x和y的全局变量,并且函数参数也分别名为x和y。 Inside the actionKeys function the function parameter x and y are the ones that are being updated, not the global ones. 在actionKeys函数内部,函数参数x和y是要更新的参数,而不是全局参数。 This is why your movement doesn't work since the x and y global variables are never updated. 这就是为什么您的运动不起作用的原因,因为x和y全局变量永远不会更新。 I would suggest renaming the global variables to have a different naming convention than your function parameters so that you don't run into this problem again in the future. 我建议重命名全局变量,使其具有不同于函数参数的命名约定,以免将来再次遇到此问题。 A common naming convention for global variables is to prefix them with g (for global) so you don't run into naming clashes with local variables. 全局变量的通用命名约定是为它们加上g(对于全局变量)前缀,这样您就不会遇到与局部变量的命名冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM