3
\$\begingroup\$

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();
}
\$\endgroup\$
  • \$\begingroup\$ Your title doesn't match your question. I suggest changing it so it does. \$\endgroup\$ – Sidar Oct 22 '12 at 19:37
  • \$\begingroup\$ made your recommended change to the title \$\endgroup\$ – user22020 Oct 23 '12 at 3:31
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – MichaelHouse Oct 22 '12 at 19:51
  • \$\begingroup\$ I have always done it that way, but yeah I think I will try the way you are suggesting, it is cleaner. \$\endgroup\$ – user22020 Oct 23 '12 at 0:25
  • \$\begingroup\$ Soo much cleaner and it works solid! \$\endgroup\$ – user22020 Oct 23 '12 at 0:31
  • \$\begingroup\$ 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. \$\endgroup\$ – user22020 Oct 23 '12 at 0:35
  • \$\begingroup\$ I appreciate your help Byte56! \$\endgroup\$ – user22020 Oct 23 '12 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy