## 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;     }```

 Messages In This Thread OpenGL|ES tutorials - Follows NeHe track. - dragagon - Oct 25, 2008, 10:58 PM OpenGL|ES tutorials - Follows NeHe track. - dragagon - Oct 26, 2008, 09:52 PM OpenGL|ES tutorials - Follows NeHe track. - dragagon - Oct 26, 2008, 10:03 PM OpenGL|ES tutorials - Follows NeHe track. - dragagon - Oct 26, 2008, 10:42 PM OpenGL|ES tutorials - Follows NeHe track. - dragagon - Oct 26, 2008 10:58 PM OpenGL|ES tutorials - Follows NeHe track. - dragagon - Oct 28, 2008, 11:12 AM OpenGL|ES tutorials - Follows NeHe track. - tachykixorz - Oct 28, 2008, 05:28 PM OpenGL|ES tutorials - Follows NeHe track. - XxtraLarGe - Oct 29, 2008, 02:51 AM OpenGL|ES tutorials - Follows NeHe track. - cubeur - Oct 30, 2008, 11:28 PM OpenGL|ES tutorials - Follows NeHe track. - dragagon - Oct 30, 2008, 11:39 PM OpenGL|ES tutorials - Follows NeHe track. - miketucker - Nov 5, 2008, 12:31 PM OpenGL|ES tutorials - Follows NeHe track. - Andy1988 - Nov 15, 2008, 08:09 PM OpenGL|ES tutorials - Follows NeHe track. - arekkusu - Nov 16, 2008, 12:33 PM OpenGL|ES tutorials - Follows NeHe track. - Andy1988 - Nov 16, 2008, 12:43 PM OpenGL|ES tutorials - Follows NeHe track. - bronxbomber92 - Nov 16, 2008, 01:59 PM OpenGL|ES tutorials - Follows NeHe track. - nixta - Nov 23, 2008, 05:49 PM OpenGL|ES tutorials - Follows NeHe track. - DubhDevelopment - Nov 24, 2008, 06:38 AM OpenGL|ES tutorials - Follows NeHe track. - JLatte - Nov 24, 2008, 12:25 PM OpenGL|ES tutorials - Follows NeHe track. - Jake - Nov 24, 2008, 08:31 PM OpenGL|ES tutorials - Follows NeHe track. - lightlab - Nov 28, 2008, 05:22 PM OpenGL|ES tutorials - Follows NeHe track. - Michael Kerley - Nov 29, 2008, 01:39 AM OpenGL|ES tutorials - Follows NeHe track. - lightlab - Nov 30, 2008, 08:18 AM OpenGL|ES tutorials - Follows NeHe track. - aceallways - Dec 29, 2008, 09:51 AM OpenGL|ES tutorials - Follows NeHe track. - BJMcKay - Dec 31, 2008, 04:18 PM OpenGL|ES tutorials - Follows NeHe track. - davidB - Jan 4, 2009, 08:30 AM OpenGL|ES tutorials - Follows NeHe track. - sherwing - Jan 6, 2009, 02:18 AM OpenGL|ES tutorials - Follows NeHe track. - nasif_20 - Jan 15, 2009, 10:54 PM OpenGL|ES tutorials - Follows NeHe track. - cmason - Jan 25, 2009, 07:46 PM OpenGL|ES tutorials - Follows NeHe track. - cmason - Jan 26, 2009, 07:13 AM OpenGL|ES tutorials - Follows NeHe track. - cmason - Jan 26, 2009, 07:52 AM OpenGL|ES tutorials - Follows NeHe track. - atlas3650 - Feb 10, 2009, 05:30 PM OpenGL|ES tutorials - Follows NeHe track. - miq01 - Feb 27, 2009, 07:10 AM OpenGL|ES tutorials - Follows NeHe track. - dragagon - Feb 28, 2009, 10:49 PM OpenGL|ES tutorials - Follows NeHe track. - Pega88 - Mar 29, 2009, 08:21 AM OpenGL|ES tutorials - Follows NeHe track. - BreakYman - Apr 6, 2009, 06:25 PM OpenGL|ES tutorials - Follows NeHe track. - steve_mock - Apr 21, 2009, 02:20 PM OpenGL|ES tutorials - Follows NeHe track. - Resrick - Apr 22, 2009, 03:30 PM OpenGL|ES tutorials - Follows NeHe track. - warmi - Apr 24, 2009, 07:14 AM OpenGL|ES tutorials - Follows NeHe track. - steve_mock - Apr 29, 2009, 05:27 AM OpenGL|ES tutorials - Follows NeHe track. - sthaqu - May 19, 2009, 11:10 AM