OpenGL|ES tutorials - Follows NeHe track.

Apprentice
Posts: 13
Joined: 2008.10
Post: #5
Rotating
Last time we discussed the easy addition of color to our triangle. This time we will be covering rotation. Again, this will be a short tutorial as it is really simple.
Lets begin by adding a variable to our AppController.h file called rtri. This will hold our value for how much we are rotating our triangle by.
Code:
    GLfloat                    rtri;
Because we are only using it in AppController.m we won't be adding a property or synthesizing.
Next open up AppController.m and we will add the meat to this. First we will want to initialize our rtri value so go into SetupValue and initialize it to 0.0f immediately after our glClearColor.
Code:
    rtri = 0.0f;
Next we go to the DrawView. Immediately after the glLoadIdentity we will want to rotate our view by the rtri rotation value. We rotate around the y-axis by rtri degrees with the following call.
Code:
    glRotatef(rtri,0.0f,1.0f,0.0f);                        // Rotate The Triangle On The Y axis ( NEW )
Once we have rotated our view, feel free to increment rtri by whatever makes you feel comfortable. I went ahead and added it after glDrawArrays.
Code:
    rtri+=1.0f;
    if(rtri > 360.0f)
    {
        rtri -= 360.0f;
    }
So there we have it. We rotate our triangle by 1 degree every frame.
Here are SetupView and DrawView in their entirety as of this lesson.
SetupView:
Code:
    const GLfloat            zNear = 0.1,
                            zFar = 1000.0,
                            fieldOfView = 60.0;
    GLfloat                    size;
    
    //Set the OpenGL projection matrix
    glMatrixMode(GL_PROJECTION);
    size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);
    CGRect rect = view.bounds;
    glFrustumf(-size, size, -size / (rect.size.width / rect.size.height), size / (rect.size.width / rect.size.height), zNear, zFar);
    glViewport(0, 0, rect.size.width, rect.size.height);
    
    //Make the OpenGL modelview matrix the default
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    // Clears the view with black
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    rtri = 0.0f;
DrawView:
Code:
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();                                    // Reset The Current Modelview Matrix
    glTranslatef(0.0f,2.0f,-6.0f);                        // Move Left 1.5 Units And Into The Screen 6.0
    glRotatef(rtri,0.0f,1.0f,0.0f);                        // Rotate The Triangle On The Y axis ( NEW )
    
    glVertexPointer(3, GL_FLOAT, 0, triVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(3, GL_UNSIGNED_BYTE, 0, triColors);
    glEnableClientState(GL_COLOR_ARRAY);
    
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);    
    
    rtri+=1.0f;
    if(rtri > 360.0f)
    {
        rtri -= 360.0f;
    }
Quote this message in a reply
Post Reply 


Messages In This Thread
OpenGL|ES tutorials - Follows NeHe track. - dragagon - Oct 26, 2008 10:58 PM
Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Track down an autorelease bug kendric 2 3,053 Jun 28, 2009 05:09 AM
Last Post: kendric