Question

I use the below code to create scene with multiple spheres, and with the keyboard method I intend to move only one of them (first one for example). Unfortunately, every time I press a control key the whole scene gets redrawn (because of glutPostRedisplay, which recalls the display method). How do I bypass this behavior, so that a single sphere moves and the others keep their old positions? Any help is welcome.

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;
    }
}
Was it helpful?

Solution

The non-moving spheres' coordinates should not be calculated using the variables whose values change. Use a separate set of variables for those . Move all the code for drawing a sphere into a separate function so you can call it with different parameters as needed; that way you don't have to duplicate the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top