Question

So I want to add some form of interactivity to my scene and make it so I can move a table in the scene around, using WSAD.

The function for creating a table is:

void drawTable(){


//texture stuff
glEnable(GL_TEXTURE_2D);

//draw legs
glBindTexture(GL_TEXTURE_2D, tableLegTexture);
drawCube(0.050,0.5,0.060,0,0,0);
drawCube(0.050,0.5,0.060,3,0,0);
drawCube(0.050,0.5,0.060,3,0,-1);
drawCube(0.050,0.5,0.060,0,0,-1);

//draw table top
glBindTexture(GL_TEXTURE_2D, tableTopTexture);
drawCube(2,0.050,1,1.5,0.5,-0.5);

glDisable(GL_TEXTURE_2D);
}

And in the render scene method I have:

glPushMatrix();
    glTranslatef(tableX, tableY, tableZ);
    glTranslatef(-0.5,0,3);
    drawTable();
glPopMatrix();

Here is the keyboard part:

void pressKey(int key, int xx, int yy){
    switch(key){
        case GLUT_KEY_LEFT : 
            deltaAngle = -0.01f;
            break;  
        case GLUT_KEY_RIGHT : 
            deltaAngle = 0.01f;
            break;  
        case GLUT_KEY_UP : 
            deltaMove = 0.5f;
            break;  
        case GLUT_KEY_DOWN : 
            deltaMove = -0.5f;
            break;  
        case 'W' :
            tableX = tableX + 1;    
            break;
    }
}

So I'm wondering. Is it relatively simple to do this? I have a method for moving throughout the level but this is moving the entire scene, so I'm not sure how I would go about moving only the table?

Was it helpful?

Solution

how I would go about moving only the table?

Add another transform(s) before your fixed translate:

glPushMatrix();
    glTranslatef( tableX, tableY, tableZ );
    glTranslatef(-0.5,0,3);
    drawTable();
glPopMatrix();

Modify table[X|Y|Z] in your keyboard callback and redraw.

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