3D OpenGL Cube

Zenith
Unregistered
 
Post: #1
I'm trying to generate a 3D OpenGL Cube... I looked at the BoingX example, and that is a sphere... how might I change that to be a simple cube?

I understand all the vertex stuff, I make 4, but there isn't any mesh around it...

theirs looks like:
Code:
int x;
    int index = 0;
    
    float v1x, v1y, v1z;
    float v2x, v2y, v2z;
    float d;
    
    int theta, phi;
    
    float theta0, theta1;
    float phi0, phi1;
    
    Vertex vtx[4];
    
    float delta = M_PI / 8.0f;

    // 8 vertical segments
    for(theta = 0; theta < 8; theta++)
    {
        theta0 = theta*delta;
        theta1 = (theta+1)*delta;
        
        // 16 horizontal segments
        for(phi = 0; phi < 16; phi++)
        {
            phi0 = phi*delta;
            phi1 = (phi+1)*delta;
            
            // For now, generate 4 full points.            
            vtx[0].x = r * sin(theta0)*cos(phi0);
            vtx[0].y = r * cos(theta0);
            vtx[0].z = r * sin(theta0)*sin(phi0);

            vtx[1].x = r * sin(theta0)*cos(phi1);
            vtx[1].y = r * cos(theta0);
            vtx[1].z = r * sin(theta0)*sin(phi1);

            vtx[2].x = r * sin(theta1)*cos(phi1);
            vtx[2].y = r * cos(theta1);
            vtx[2].z = r * sin(theta1)*sin(phi1);

            vtx[3].x = r * sin(theta1)*cos(phi0);
            vtx[3].y = r * cos(theta1);
            vtx[3].z = r * sin(theta1)*sin(phi0);
            
            // Generate normal
            if(theta >= 4)
            {
                v1x = vtx[1].x - vtx[0].x;
                v1y = vtx[1].y - vtx[0].y;
                v1z = vtx[1].z - vtx[0].z;
    
                v2x = vtx[3].x - vtx[0].x;
                v2y = vtx[3].y - vtx[0].y;
                v2z = vtx[3].z - vtx[0].z;
            }
            else
            {
                v1x = vtx[0].x - vtx[3].x;
                v1y = vtx[0].y - vtx[3].y;
                v1z = vtx[0].z - vtx[3].z;
    
                v2x = vtx[2].x - vtx[3].x;
                v2y = vtx[2].y - vtx[3].y;
                v2z = vtx[2].z - vtx[3].z;
            }
            
            vtx[0].nx = (v1y * v2z) - (v2y * v1z);
            vtx[0].ny = (v1z * v2x) - (v2z * v1x);
            vtx[0].nz = (v1x * v2y) - (v2x * v1y);
            
            d = 1.0f/sqrt(vtx[0].nx*vtx[0].nx +
                      vtx[0].ny*vtx[0].ny +
                      vtx[0].nz*vtx[0].nz);
            
            vtx[0].nx *= d;
            vtx[0].ny *= d;
            vtx[0].nz *= d;
            
            // Generate color            
            if((theta ^ phi) & 1)
            {
                vtx[0].r = 1.0f;
                vtx[0].g = 1.0f;
                vtx[0].b = 1.0f;
                vtx[0].a = 1.0f;            
            }
            else
            {
                vtx[0].r = 1.0f;
                vtx[0].g = 0.0f;
                vtx[0].b = 0.0f;
                vtx[0].a = 1.0f;
            }
            
            // Replicate vertex info
            for(x = 0; x < 4; x++)
            {
                vtx[x].nx = vtx[0].nx;
                vtx[x].ny = vtx[0].ny;
                vtx[x].nz = vtx[0].nz;
                vtx[x].r = vtx[0].r;
                vtx[x].g = vtx[0].g;
                vtx[x].b = vtx[0].b;
                vtx[x].a = vtx[0].a;
            }
            
            // Store vertices
            boingData[index++] = vtx[0];
            boingData[index++] = vtx[1];
            boingData[index++] = vtx[2];
            boingData[index++] = vtx[3];
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
I suggest you do the NeHe tutorial on solid objects... it's about number 3 or 4.
Quote this message in a reply
Mars_999
Unregistered
 
Post: #3
The code you have posted looks like a sphere is going to be rendered not a cube. As OSC said look into nehe tutorials on it. Cubes aren't hard but require you to think in 3D. You will have 6 sides. So you will need 24 glVertex3f() calls. To descirbe your cube. HTH
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Cube auto-rotate sakrist 2 3,029 May 5, 2009 03:31 AM
Last Post: Ingemar
  OpenGL ES - Drawing a simple cube help. MattCairns 7 12,494 Oct 10, 2008 05:26 PM
Last Post: Frogblast
  Pre-generated cube map Fenris 2 3,595 Apr 5, 2007 07:01 PM
Last Post: nich
  Transformation from unit cube to view frustum TomorrowPlusX 10 6,470 Jul 12, 2005 11:32 AM
Last Post: TomorrowPlusX
  Cube Environment PowerMacX 3 3,005 Aug 7, 2003 09:36 PM
Last Post: MattDiamond