x404.co.uk
http://www.x404.co.uk/forum/

Help with some very basic OpenGL stuff...
http://www.x404.co.uk/forum/viewtopic.php?f=4&t=7005
Page 1 of 1

Author:  Fogmeister [ Tue Mar 16, 2010 10:09 am ]
Post subject:  Help with some very basic OpenGL stuff...

I've been following this... http://iphonedevelopment.blogspot.com/2 ... le-of.html ... tutorial series and it's very good!

I'm up to lesson 8 so far.

Anyway, I thought I'd adapt this version of the code... http://iphonedevelopment.blogspot.com/2 ... 4-let.html.

Code:
- (void)drawView:(GLView*)view;
{
   
    static GLfloat rot = 0.0;
   
    // This is the same result as using Vertex3D, just faster to type and
    // can be made const this way
    static const Vertex3D vertices[]= {
        {0, -0.525731, 0.850651},             // vertices[0]
        {0.850651, 0, 0.525731},              // vertices[1]
        {0.850651, 0, -0.525731},             // vertices[2]
        {-0.850651, 0, -0.525731},            // vertices[3]
        {-0.850651, 0, 0.525731},             // vertices[4]
        {-0.525731, 0.850651, 0},             // vertices[5]
        {0.525731, 0.850651, 0},              // vertices[6]
        {0.525731, -0.850651, 0},             // vertices[7]
        {-0.525731, -0.850651, 0},            // vertices[8]
        {0, -0.525731, -0.850651},            // vertices[9]
        {0, 0.525731, -0.850651},             // vertices[10]
        {0, 0.525731, 0.850651}               // vertices[11]
    };
   
    static const Color3D colors[] = {
        {1.0, 0.0, 0.0, 1.0},
        {1.0, 0.5, 0.0, 1.0},
        {1.0, 1.0, 0.0, 1.0},
        {0.5, 1.0, 0.0, 1.0},
        {0.0, 1.0, 0.0, 1.0},
        {0.0, 1.0, 0.5, 1.0},
        {0.0, 1.0, 1.0, 1.0},
        {0.0, 0.5, 1.0, 1.0},
        {0.0, 0.0, 1.0, 1.0},
        {0.5, 0.0, 1.0, 1.0},
        {1.0, 0.0, 1.0, 1.0},
        {1.0, 0.0, 0.5, 1.0}
    };
   
    static const GLubyte icosahedronFaces[] = {
        1, 2, 6,
        1, 7, 2,
        3, 4, 5,
        4, 3, 8,
        6, 5, 11,
        5, 6, 10,
        9, 10, 2,
        10, 9, 3,
        7, 8, 9,
        8, 7, 0,
        11, 0, 1,
        0, 11, 4,
        6, 2, 10,
        1, 6, 11,
        3, 5, 10,
        5, 4, 11,
        2, 7, 9,
        7, 1, 0,
        3, 9, 8,
        4, 8, 0,
    };
   
    static const Vector3D normals[] = {
        {0.000000, -0.417775, 0.675974},
        {0.675973, 0.000000, 0.417775},
        {0.675973, -0.000000, -0.417775},
        {-0.675973, 0.000000, -0.417775},
        {-0.675973, -0.000000, 0.417775},
        {-0.417775, 0.675974, 0.000000},
        {0.417775, 0.675973, -0.000000},
        {0.417775, -0.675974, 0.000000},
        {-0.417775, -0.675974, 0.000000},
        {0.000000, -0.417775, -0.675973},
        {0.000000, 0.417775, -0.675974},
        {0.000000, 0.417775, 0.675973},
    };
   
    glLoadIdentity();
    glTranslatef(0.0f,0.0f,-3.0f); 
    glRotatef(rot,1.0f,1.0f,1.0f);
    glClearColor(0.7, 0.7, 0.7, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vertices);
    glColorPointer(4, GL_FLOAT, 0, colors);
    glNormalPointer(GL_FLOAT, 0, normals);
    glDrawElements(GL_TRIANGLES, 60, GL_UNSIGNED_BYTE, icosahedronFaces);
   
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    static NSTimeInterval lastDrawTime;
    if (lastDrawTime)
    {
        NSTimeInterval timeSinceLastDraw = [NSDate timeIntervalSinceReferenceDate] - lastDrawTime;
        rot+=50 * timeSinceLastDraw;               
    }
    lastDrawTime = [NSDate timeIntervalSinceReferenceDate];
   
}


... to do what I want.

Basically, the code describes an icosahedron using vertices and then describes the faces of the icosahedron by linking set of vertices together.

I had a go at changing the vertices and faces to describe a cube but nothing.

My question is does the following code describe a cube? I think it does but it doesn't draw anything and I'm going insane!

Code:
static const Vertex3D vertices[]= {
        {1, 1, 0},             // vertices[0]
        {1, -1, 0},              // vertices[1]
        {-1, 1, 0},             // vertices[2]
        {-1, -1, 0},            // vertices[3]
        {1, 1, -2},             // vertices[4]
        {1, -1, -2},             // vertices[5]
        {-1, 1, -2},              // vertices[6]
        {-1, -1, -2},             // vertices[7]
    };


I have changed more than this in the actual program but at work and can't work out the faces but I've gone through and done all the changes I though necessary.

Author:  rustybucket [ Tue Mar 16, 2010 10:26 am ]
Post subject:  Re: Help with some very basic OpenGL stuff...

Fogmeister wrote:
My question is does the following code describe a cube? I think it does but it doesn't draw anything and I'm going insane!

Code:
static const Vertex3D vertices[]= {
        {1, 1, 0},             // vertices[0]
        {1, -1, 0},              // vertices[1]
        {-1, 1, 0},             // vertices[2]
        {-1, -1, 0},            // vertices[3]
        {1, 1, -2},             // vertices[4]
        {1, -1, -2},             // vertices[5]
        {-1, 1, -2},              // vertices[6]
        {-1, -1, -2},             // vertices[7]
    };


I have changed more than this in the actual program but at work and can't work out the faces but I've gone through and done all the changes I though necessary.

Yes it is a cube.

Have you changed the faces and normals to compensate though?

Author:  Fogmeister [ Tue Mar 16, 2010 10:42 am ]
Post subject:  Re: Help with some very basic OpenGL stuff...

rustybucket wrote:
Fogmeister wrote:
My question is does the following code describe a cube? I think it does but it doesn't draw anything and I'm going insane!

Code:
static const Vertex3D vertices[]= {
        {1, 1, 0},             // vertices[0]
        {1, -1, 0},              // vertices[1]
        {-1, 1, 0},             // vertices[2]
        {-1, -1, 0},            // vertices[3]
        {1, 1, -2},             // vertices[4]
        {1, -1, -2},             // vertices[5]
        {-1, 1, -2},              // vertices[6]
        {-1, -1, -2},             // vertices[7]
    };


I have changed more than this in the actual program but at work and can't work out the faces but I've gone through and done all the changes I though necessary.

Yes it is a cube.

Have you changed the faces and normals to compensate though?
Thanks.

Yes, I drew out a net of a cube and numbered the vertices according to the 3D model then worked out 2 triangles for each face.

When I get home I'll be able to show you the faces and normals. The normals were calculated using a command line program detailed in the link. They looked fairly normal (sorry) for a cube so I assume I did it correctly :D.

I'll be able to post my entire code when I get home.

Thanks again

Author:  Fogmeister [ Tue Mar 16, 2010 11:47 am ]
Post subject:  Re: Help with some very basic OpenGL stuff...

If I was to describe the code but get the faces the wrong way round (i.e. facing inwards instead of outwards) would that cause it to not draw anything?

Maybe I should go through the faces again.

Author:  Fogmeister [ Tue Mar 16, 2010 4:08 pm ]
Post subject:  Re: Help with some very basic OpenGL stuff...

Just a quick question,

I know what the faces array does but what exactly does the normals array do?

I'm guessing it defines the vector direction at which light bounces off the vertices... or something?

Author:  EddArmitage [ Tue Mar 16, 2010 4:11 pm ]
Post subject:  Re: Help with some very basic OpenGL stuff...

Fogmeister wrote:
I'm guessing it defines the vector direction at which light bounces off the vertices... or something?

Yup.

When I get home I was going to read through my OpenGL notes, and if they were any good offer to send you a copy. Hopefully I'll remember!

Author:  Fogmeister [ Tue Mar 16, 2010 10:01 pm ]
Post subject:  Re: Help with some very basic OpenGL stuff...

Gah! I feel like such an idiot!

I decided to take it back to lesson 2 and tried to take the triangle and expand it into 3D.

I then realised I was trying to set up the faces array of GLfloats! It needed to be GLubytes.

Sorted now :D

Author:  finlay666 [ Wed Mar 17, 2010 12:52 am ]
Post subject:  Re: Help with some very basic OpenGL stuff...

Fogmeister wrote:
Gah! I feel like such an idiot!

I decided to take it back to lesson 2 and tried to take the triangle and expand it into 3D.

I then realised I was trying to set up the faces array of GLfloats! It needed to be GLubytes.

Sorted now :D


You can (usually) get away with making any right angled quad side out of 2 triangles, as triangles are quicker to render than quads, IIRC 2 triangles are quicker than one quad (due to potential complexities of the quad)*

The matrix itself doesn't increase a huge amount though, as IIRC its clockwise so you just need to remember the order they are added

* It has been 2 years since I did any proper 3d graphics programming, I can't remember if it was quicker on the gamecube or not though. **

** From the person who is making a game with NO visuals just to demonstrate the capabilities of my sound programming skillz :D

Author:  Fogmeister [ Wed Mar 17, 2010 6:51 am ]
Post subject:  Re: Help with some very basic OpenGL stuff...

With the iPhone you only get the option of triangles.

It doesn't do anything else.

There's a lot of stuff that OpenGL ES doesn't do and it's normally the first stuff you learn in OpenGL. (Or so the guy who wrote this tutorial said).

Anyway, I'm at the point where I can get my control system up and running now :D

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/