Reply to topic  [ 9 posts ] 
Help with some very basic OpenGL stuff... 
Author Message
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Mar 16, 2010 10:09 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Jun 18, 2009 5:10 pm
Posts: 5836
Reply with quote
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?

_________________
Jim

Image


Tue Mar 16, 2010 10:26 am
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Mar 16, 2010 10:42 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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.

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Mar 16, 2010 11:47 am
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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?

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Mar 16, 2010 4:08 pm
Profile WWW
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 5288
Location: ln -s /London ~
Reply with quote
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!

_________________
timark_uk wrote:
Gay sex is better than no sex

timark_uk wrote:
Edward Armitage is Awesome. Yes, that's right. Awesome with a A.


Tue Mar 16, 2010 4:11 pm
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Tue Mar 16, 2010 10:01 pm
Profile WWW
Spends far too much time on here
User avatar

Joined: Thu Apr 23, 2009 9:40 pm
Posts: 4876
Location: Newcastle
Reply with quote
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

_________________
Twitter
Charlie Brooker:
Macs are glorified Fisher-Price activity centres for adults; computers for scaredy cats too nervous to learn how proper computers work; computers for people who earnestly believe in feng shui.


Wed Mar 17, 2010 12:52 am
Profile
I haven't seen my friends in so long
User avatar

Joined: Thu Apr 23, 2009 7:35 pm
Posts: 6580
Location: Getting there
Reply with quote
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

_________________
Oliver Foggin - iPhone Dev

JJW009 wrote:
The count will go up until they stop counting. That's the way counting works.


Doodle Sub!
Game Of Life

Image Image


Wed Mar 17, 2010 6:51 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software.