Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Cube Class
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Catalyst




PostPosted: Sat Jun 14, 2003 11:26 am   Post subject: Cube Class

a simple class to allow you to easily make and render cubes in opengl

code:
class PCube
{
    public:
   
    float x,y,z;
    float radius;
   
    void Init (float X,float Y,float Z,float RAD)
    {
    x=X;
    y=Y;
    z=Z;
    radius=RAD;
    }
   
    void Render()
    {
   
    glBegin (GL_QUADS);
     
        glTexCoord2f(0,1); glVertex3f(x-radius, y-radius, z-radius);
        glTexCoord2f(0,0); glVertex3f(x-radius, y + radius, z-radius);
        glTexCoord2f(1,0); glVertex3f(x + radius, y + radius, z-radius);
        glTexCoord2f(1,1); glVertex3f(x + radius, y-radius, z-radius);

        glTexCoord2f(0,1); glVertex3f(x-radius, y-radius, z + radius);
        glTexCoord2f(0,0); glVertex3f(x-radius, y + radius, z + radius);
        glTexCoord2f(1,0); glVertex3f(x + radius, y + radius, z + radius);
        glTexCoord2f(1,1); glVertex3f(x + radius, y-radius, z + radius);

        glTexCoord2f(0,1); glVertex3f(x-radius, y-radius, z-radius);
        glTexCoord2f(0,0); glVertex3f(x-radius, y-radius, z + radius);
        glTexCoord2f(1,0); glVertex3f(x + radius, y-radius, z + radius);
        glTexCoord2f(1,1); glVertex3f(x + radius, y-radius, z-radius);
               
        glTexCoord2f(0,1); glVertex3f(x-radius, y + radius, z-radius);
        glTexCoord2f(0,0); glVertex3f(x-radius, y + radius, z + radius);
        glTexCoord2f(1,0); glVertex3f(x + radius, y + radius, z + radius);
        glTexCoord2f(1,1); glVertex3f(x + radius, y + radius, z-radius);

        glTexCoord2f(0,1); glVertex3f(x-radius, y-radius, z-radius);
        glTexCoord2f(0,0); glVertex3f(x-radius, y-radius, z + radius);
        glTexCoord2f(1,0); glVertex3f(x-radius, y + radius, z + radius);
        glTexCoord2f(1,1); glVertex3f(x-radius, y + radius, z-radius);

        glTexCoord2f(0,1); glVertex3f(x + radius, y-radius, z-radius);
        glTexCoord2f(0,0); glVertex3f(x + radius, y-radius, z + radius);
        glTexCoord2f(1,0); glVertex3f(x + radius, y + radius, z + radius);
        glTexCoord2f(1,1); glVertex3f(x + radius, y + radius, z-radius);                                                       
                                                                                                           
    glEnd();
   
    }

};
Sponsor
Sponsor
Sponsor
sponsor
Homer_simpson




PostPosted: Sat Jun 14, 2003 12:20 pm   Post subject: (No subject)

also add this to your class... it makes pyramids =)

code:
void CreatePyramid(float x, float y, float z, int width, int height)
{
        glBegin(GL_TRIANGLES);   
               
                glColor3ub(255, 0, 0);   glVertex3f(x, y + height, z);          // Top point
                glColor3ub(0, 255, 255); glVertex3f(x - width, y - height, z - width);  // Bottom left point
                glColor3ub(255, 0, 255); glVertex3f(x + width, y - height, z - width);  // Bottom right point

                glColor3ub(255, 0, 0);   glVertex3f(x, y + height, z);          // Top point
                glColor3ub(0, 255, 255); glVertex3f(x + width, y - height, z + width);  // Bottom right point
                glColor3ub(255, 0, 255); glVertex3f(x - width, y - height, z + width);  // Bottom left point

                glColor3ub(255, 0, 0);   glVertex3f(x, y + height, z);          // Top point
                glColor3ub(255, 0, 255); glVertex3f(x - width, y - height, z + width);  // Front bottom point
                glColor3ub(0, 255, 255); glVertex3f(x - width, y - height, z - width);  // Bottom back point

                glColor3ub(255, 0, 0);   glVertex3f(x, y + height, z);          // Top point
                glColor3ub(255, 0, 255); glVertex3f(x + width, y - height, z - width);  // Bottom back point
                glColor3ub(0, 255, 255); glVertex3f(x + width, y - height, z + width);  // Front bottom point
                       
        glEnd();
Homer_simpson




PostPosted: Sat Jun 14, 2003 1:28 pm   Post subject: (No subject)

and also
code:
void DrawSphere(float x, float y, float z, float radius)
{
        GLUquadricObj *pSphere = gluNewQuadric();                     
        glPushMatrix();         
        glTranslatef(x, y, z);             
        gluSphere(pSphere, radius, 15, 15);               
        glPopMatrix();                   
        gluDeleteQuadric(pSphere);                                         
}
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: