在3D场景中仅移动许多相同对象中的一个。怎么样?

时间:2014-05-11 18:03:30

标签: c opengl

我使用下面的代码创建具有多个球体的场景,并且使用键盘方法我打算只移动其中一个(例如,第一个)。不幸的是,每当我按下一个控制键时,整个场景都会重新绘制(因为 glutPostRedisplay ,它会调用显示方法)。如何绕过这种行为,以便单个球体移动而其他球体保持旧位置?欢迎任何帮助。

class SolidSphere
{
protected:
    std::vector<GLfloat> vertices;
    std::vector<GLfloat> normals;
    std::vector<GLfloat> texcoords;
    std::vector<GLushort> indices;

public:
    SolidSphere(float radius, unsigned int rings, unsigned int sectors)
    {
        float const R = 1.0f / (float)(rings - 1);
        float const S = 1.0f / (float)(sectors - 1);
        unsigned int r, s;

        vertices.resize(rings * sectors * 3);
        normals.resize(rings * sectors * 3);
        texcoords.resize(rings * sectors * 2);
        std::vector<GLfloat>::iterator v = vertices.begin();
        std::vector<GLfloat>::iterator n = normals.begin();
        std::vector<GLfloat>::iterator t = texcoords.begin();
        for(r = 0; r < rings; r++) for(s = 0; s < sectors; s++) {
                float const x = sinf(M_PI * r * R) * cosf(2 * M_PI * s * S);
                float const y = sinf(-M_PI_2 + M_PI * r * R );                
                float const z = sinf(2.0f * M_PI * s * S) * sinf(M_PI * r * R );

                *t++ = s*S;
                *t++ = r*R;

                *v++ = x * radius;
                *v++ = y * radius;
                *v++ = z * radius;

                *n++ = x;
                *n++ = y;
                *n++ = z;
        }

        indices.resize(rings * sectors * 6);
        std::vector<GLushort>::iterator i = indices.begin();
        for(r = 0; r < rings - 1; r++) for(s = 0; s < sectors - 1; s++) {
            *i++ = r * sectors + s;
            *i++ = (r + 1) * sectors + (s + 1);
            *i++ = r * sectors + (s + 1);

            *i++ = r * sectors + s;
            *i++ = (r + 1) * sectors + s;
            *i++ = (r + 1) * sectors + (s + 1);
        }
    }

    void draw(GLfloat x, GLfloat y, GLfloat z)
    {
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glTranslatef(x,y,z);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glVertexPointer(3, GL_FLOAT, 0, &vertices[0]);
        glNormalPointer(GL_FLOAT, 0, &normals[0]);
        glTexCoordPointer(2, GL_FLOAT, 0, &texcoords[0]);
        glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT, &indices[0]);
        glPopMatrix();

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    }
};

SolidSphere **createSpheres()
{
    SolidSphere **spheres = new SolidSphere*[numSpheres];
    for (int i = 0; i < numSpheres; i++)
        spheres[i] = new SolidSphere(1, 12, 24);

    return spheres;
}

void display()
{
    SolidSphere **spheres = createSpheres();
    float const win_aspect = (float)win_width / (float)win_height;

    glViewport(0, 0, win_width, win_height);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(.6, 0, 0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, win_aspect, 1, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    for (int i = 0; i < numSpheres; i++)
    {
        posX = ((float)rand())/RAND_MAX * 4 - 2;
        posY = ((float)rand())/RAND_MAX * 4 - 2;
        posZ = ((float)rand())/RAND_MAX * 5 - 10;
        spheres[i]->draw(posX,posY,posZ);
    }
    for (int i = 0; i < numSpheres; i++)
    {
        delete spheres[i];
    }

    delete[] spheres;

    glutSwapBuffers();
}

void keyboard(unsigned char key, int x, int y)
{
    switch(key)
    {
    case 27:
        exit(0);
    case 'a':
        posX -= 0.05f;
        glutPostRedisplay();
        break;
    case 'd': 
        posX += 0.05f;
        glutPostRedisplay();
        break;
    case 's':
        posY -= 0.05f;
        glutPostRedisplay();
        break;
    case 'w':
        posY += 0.05f;
        glutPostRedisplay();
        break;
    case 'x':
        posZ -= 0.05f;
        glutPostRedisplay();
        break;
    case 'z': 
        posZ += 0.05f;
        glutPostRedisplay();
        break;
    }
}

1 个答案:

答案 0 :(得分:1)

不应使用值更改的变量计算非移动球体的坐标。为这些变量使用一组单独的变量。将绘制球体的所有代码移动到单独的函数中,以便根据需要使用不同的参数调用它;这样你就不必复制代码了。