Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Currently I am drawing a cube for a game that I am making and the cube draw method is below. My question is, what is the best way to draw a cube and to be able to easily find the face that the cursor is over? My draw method works just fine, but I am getting ready to start to add picking (this will be used to mold the cubes into other shaps), and would like to know the best way to find a face of the cube.

public void  Draw() {   
    // center point posX, posY, posZ
    float radius = size / 2;

    //top
    glPushMatrix();
    glBegin(GL_QUADS);
        {
            glColor3f(1.0f,0.0f,0.0f); // red
            glVertex3f(posX + radius, posY + radius, posZ - radius);
            glVertex3f(posX - radius, posY + radius, posZ - radius);
            glVertex3f(posX - radius, posY + radius, posZ + radius);
            glVertex3f(posX + radius, posY + radius, posZ + radius);
        }
    glEnd();
    glPopMatrix();

    //bottom
    glPushMatrix();
    glBegin(GL_QUADS);
        {
            glColor3f(1.0f,1.0f,0.0f); // ?? color
            glVertex3f(posX + radius, posY - radius, posZ + radius);
            glVertex3f(posX - radius, posY - radius, posZ + radius);
            glVertex3f(posX - radius, posY - radius, posZ - radius);
            glVertex3f(posX + radius, posY - radius, posZ - radius);
        }
    glEnd();
    glPopMatrix();

    //right side
    glPushMatrix();
    glBegin(GL_QUADS);
        {
            glColor3f(1.0f,0.0f,1.0f); // ?? color
            glVertex3f(posX + radius, posY + radius, posZ + radius);
            glVertex3f(posX + radius, posY - radius, posZ + radius);
            glVertex3f(posX + radius, posY - radius, posZ - radius);
            glVertex3f(posX + radius, posY + radius, posZ - radius);
        }
    glEnd();
    glPopMatrix();

    //left side
    glPushMatrix();
    glBegin(GL_QUADS);
        {
            glColor3f(0.0f,1.0f,1.0f); // ?? color
            glVertex3f(posX - radius, posY + radius, posZ - radius);
            glVertex3f(posX - radius, posY - radius, posZ - radius);
            glVertex3f(posX - radius, posY - radius, posZ + radius);
            glVertex3f(posX - radius, posY + radius, posZ + radius);
        }
    glEnd();
    glPopMatrix();

    //front side 
    glPushMatrix();
    glBegin(GL_QUADS);
        {
            glColor3f(0.0f,0.0f,1.0f); // blue 
            glVertex3f(posX + radius, posY + radius, posZ + radius);
            glVertex3f(posX - radius, posY + radius, posZ + radius);
            glVertex3f(posX - radius, posY - radius, posZ + radius);
            glVertex3f(posX + radius, posY - radius, posZ + radius);
        }
    glEnd();
    glPopMatrix();

    //back side
    glPushMatrix();
    glBegin(GL_QUADS);
        {
            glColor3f(0.0f,1.0f,0.0f); // green
            glVertex3f(posX + radius, posY - radius, posZ - radius);
            glVertex3f(posX - radius, posY - radius, posZ - radius);
            glVertex3f(posX - radius, posY + radius, posZ - radius);
            glVertex3f(posX + radius, posY + radius, posZ - radius);
        }
    glEnd();
    glPopMatrix();
}
share|improve this question
    
Your title doesn't match your question. I suggest changing it so it does. –  Sidar Oct 22 '12 at 19:37
    
made your recommended change to the title –  Kenneth Bray Oct 23 '12 at 3:31
add comment

1 Answer 1

up vote 3 down vote accepted

It depends on how you implement picking. It may not matter how you draw it, because you won't be detecting a face that's been drawn already. Since it's already been sent to the graphics card, it's usually more work to get information back from the drawn image than just checking your objects directly.

If you implement picking in the way I've described here, you'll just be casting a ray and checking it against the objects in your game world to see if there's an intersection. So it will matter how you're storing your objects in your game world. For example, it's very useful if you can query your world to find objects at a specific location. If your objects were just stored in an array, you'd suffer O(N) time for determining if an object existed at a specific point.

As a side note. Just looking at your drawing code, you probably want to modernize that a bit. Direct drawing like that is very out of date. Look into VBOs.

share|improve this answer
    
You can at least update your drawing code so that all the vertices are surrounded by a single glBegin() and glEnd(). Same goes for popping and pushing matrices. OpenGL knows that 4 vertices makes a quad, you don't have to break them all apart like that. –  Byte56 Oct 22 '12 at 19:51
    
I have always done it that way, but yeah I think I will try the way you are suggesting, it is cleaner. –  Kenneth Bray Oct 23 '12 at 0:25
    
Soo much cleaner and it works solid! –  Kenneth Bray Oct 23 '12 at 0:31
    
I like your idea of storing them by specific location, currently my game world is stored in a array of objects. I will look into location based storage of objects. –  Kenneth Bray Oct 23 '12 at 0:35
    
I appreciate your help Byte56! –  Kenneth Bray Oct 23 '12 at 0:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.