C 仅移动三维场景中许多相同对象中的一个。怎么用?

C 仅移动三维场景中许多相同对象中的一个。怎么用?,c,opengl,C,Opengl,我使用下面的代码创建具有多个球体的场景,并使用键盘方法仅移动其中一个球体(例如第一个)。不幸的是,每次我按下一个控制键,整个场景都会被重新绘制(因为GLUTPOSTRESDISPLAY,它会调用显示方法)。如何绕过此行为,使单个球体移动,而其他球体保持其原有位置?欢迎任何帮助 class SolidSphere { protected: std::vector<GLfloat> vertices; std::vector<GLfloat> normals;

我使用下面的代码创建具有多个球体的场景,并使用键盘方法仅移动其中一个球体(例如第一个)。不幸的是,每次我按下一个控制键,整个场景都会被重新绘制(因为GLUTPOSTRESDISPLAY,它会调用显示方法)。如何绕过此行为,使单个球体移动,而其他球体保持其原有位置?欢迎任何帮助

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;
    }
}
类SolidSphere
{
受保护的:
向量顶点;
std::向量法线;
std::向量texcoords;
std::向量指数;
公众:
SolidSphere(浮点半径、无符号整数环、无符号整数扇区)
{
浮子常数R=1.0f/(浮子)(环-1);
浮点数常数S=1.0f/(浮点数)(扇区-1);
无符号整数r,s;
顶点。调整大小(环*扇区*3);
法线。调整大小(环*扇区*3);
调整大小(环*扇区*2);
std::vector::iterator v=顶点。begin();
std::vector::iterator n=normals.begin();
std::vector::iterator t=texcoords.begin();
对于(r=0;r绘制(posX,posY,posZ);
}
对于(int i=0;i
不应使用值发生变化的变量计算非移动球体的坐标。对这些变量使用一组单独的变量。将绘制球体的所有代码移动到单独的函数中,以便根据需要使用不同的参数调用它;这样您就不必重复代码。

谢谢!你的意思是把draw()移出课堂吗?