PDA

View Full Version : glRotatef() - not animating - in OS X Cocoa app



Jim Adkins
07-15-2010, 10:16 AM
Hi All,

This is my very first crack at OpenGL and I'm having difficulty with 'glRotatef()' ...

The behavior is like the rotation angle is not updating in a loop-type fashion. For instance, if I set a rotation angle of 45.0f, the image (made of a triangle strip) is displayed at a "static" 45 degree rotation.

Originally, I assumed (when I started this code yesterday) that I would have to make a call to "glRotatef()" in a loop, but all of the examples I've seen simply invoke the call once and pass in an angle variable.

I've seen one or two examples of the angle var being incremented "post-draw", but the vast majority do not include the var++. (And I'm not sure how the var increments without having the method called again - unless OpenGL is doing it under the covers???)

The following code is in Objective-C (using Cocoa). Again the image renders, but only a static image. The "zRot" var was added as a last-ditch effort from an example that I came across... but did not work.

Thanks

CODE:--------------------------------

#import "MyOpenGLView.h"

#include <OpenGL/gl.h>

@implementation MyOpenGLView

float zRot = 0.0f; //variable to hold z rotation angle

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); // Clear the color
glClear(GL_COLOR_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
}

static void drawAShip ()
{
glColor3f(1.0f, 0.85f, 0.35f);

glBegin(GL_TRIANGLE_STRIP);
{
glVertex3f( -0.2, -0.3, 0.0); // A
glVertex3f( 0.0, -0.3, 0.15); // B
glVertex3f( 0.0, 0.6, 0.0); // C

glVertex3f( 0.2, -0.3, 0.0); // D
glVertex3f( 0.0, -0.3, -0.15); // E
glVertex3f( -0.2, -0.3, 0.0); // F-close

}
glEnd();
}

static void setLighting()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

// Create light components
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position[] = { -3.5f, 0.5f, 3.5f, 1.0f };

// Assign created components to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glLightfv(GL_LIGHT0, GL_POSITION, position);
}

static void enableColorAndMaterialTracking()
{
// enable color tracking
glEnable(GL_COLOR_MATERIAL);

// set material properties which will be assigned by glColor
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
}

-(void) drawRect: (NSRect) bounds
{
init();

setLighting();

enableColorAndMaterialTracking();

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glPushMatrix();

glRotatef(zRot, 1, 0, 0);

drawAShip();

zRot += 0.1f; //increment the zRot variable

glPopMatrix();

glFlush();
}

@end