PDA

View Full Version : Rotation of objects - n00b question


ThrottleMonkey
2003.03.12, 09:09 PM
Hi again :)

I've been messing around with the NEHE tutorials and have been able to draw all sorts of neato colored shapes and stuff.

I've been doing so with just typing the below kind of code (my sample code) from lesson 4 and I've been happy with it.

Now, having been able to do that (and totally forgetting that at the bottom of each NEHE page, they have the Cocoa version of the code... so I've been doing things kind of bland.

What am I getting at? Here it is:
I don't understand everything and I don't claim to... but is there an easier way to make objects rotate?

Having looked at the Cocoa code, they do all the "full screen" stuff that makes everything complicated.

Here's some sample code of mine:
-----
#import "MyOpenGLView.h"

#include <OpenGL/gl.h>

@implementation MyOpenGLView
- (void) drawRect: (NSRect) bounds
{
glClearColor( 1.0, 1.0, 1.0, 1.0 ) ;
glClear( GL_COLOR_BUFFER_BIT ) ;

glBegin(GL_TRIANGLES);
glColor3f( 0.0, 0.0, 1.0 ) ;
glVertex3f( 0.0, 1.0, 0.0);
glColor3f( 0.0, 1.0, 0.0 ) ;
glVertex3f(-1.0,-1.0, 0.0);
glColor3f( 1.0, 0.0, 0.0 ) ;
glVertex3f( 1.0,-1.0, 0.0);
glEnd() ;

glColor3f( 0.5, 0.5, 1.0 ) ;
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

glBegin(GL_QUADS);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();

glFlush();
}

@end
------

Now, it would seem pretty easy to add some kind of increment (in the samples, they have "rtri" and "rquad") without having to do all the "full screen" checks that NEHE tutorials do.

Is all that stuff really needed or can you just add:

rquad+=0.2f;

and have some goto routine that starts a loop and keeps redrawing it?

Or do I need to sit down and actually learn what all going to "full screen" does?

ghettotek
2003.03.12, 09:48 PM
im not quite sure exactly what it is youre asking, but ill take a stab at it...

I don't understand everything and I don't claim to... but is there an easier way to make objects rotate

the easiest way to rotate objects, afaik, is to make a call to glRotate[f/d](degrees,x-axis,y-axis,z-axis). for instance, if you make the following call:

glRotated(90.0,0.0,1.0,0.0);

anything drawn after this will be rotated 90 degrees along the Y axis.

the "full screen" stuff you are talking about shouldnt have anything to do with the rotation of matricies in OpenGL (again, afaik). also, make sure you understand what a matrix is, and the usage of glPushMatrix and glPopMatrix if you plan on rotating objects independently of one another.

here is the man page for glRotate:


NAME
glRotated, glRotatef - multiply the current matrix by a
rotation matrix

C SPECIFICATION
void glRotated( GLdouble angle,
GLdouble x,
GLdouble y,
GLdouble z )
void glRotatef( GLfloat angle,
GLfloat x,
GLfloat y,
GLfloat z )

PARAMETERS
angle Specifies the angle of rotation, in degrees.

x, y, z
Specify the x, y, and z coordinates of a vector,
respectively.

DESCRIPTION
glRotate produces a rotation of angle degrees around the
vector (x, y, z). The current matrix (see glMatrixMode)
is multiplied by a rotation matrix with the product
angle Specifies the angle of rotation, in degrees.

x, y, z
Specify the x, y, and z coordinates of a vector,
respectively.

DESCRIPTION
glRotate produces a rotation of angle degrees around the
vector (x, y, z). The current matrix (see glMatrixMode)
is multiplied by a rotation matrix with the product
replacing the current matrix, as if glMultMatrix were
called with the following matrix as its argument:

x^2(1-c)+c xy(1-c)-zs xz(1-c)+ys 0
yx(1-c)+zs y^2(1-c)+c yz(1-c)-xs 0
xz(1-c)-ys yz(1-c)+xs z^2(1-c)+c 0
0 0 0 1

Where c = cos (angle), s = sin (angle), and ||(x, y, z)||
= 1 (if not, the GL will normalize this vector).

If the matrix mode is either GL_MODELVIEW or
GL_PROJECTION, all objects drawn after glRotate is called
are rotated. Use glPushMatrix and glPopMatrix to save and
restore the unrotated coordinate system.

NOTES
This rotation follows the right-hand rule, so if the vec-
tor (x, y, z) points toward the user, the rotation will be
counterclockwise.

Josh
2003.03.12, 10:29 PM
- (void) drawRect: (NSRect) bounds
{
glClearColor( 1.0, 1.0, 1.0, 1.0 ) ;
glClear( GL_COLOR_BUFFER_BIT ) ;

glRotatef(rot, 1.0, 0.0, 0.0);

glBegin(GL_TRIANGLES);
glColor3f( 0.0, 0.0, 1.0 ) ;
glVertex3f( 0.0, 1.0, 0.0);
glColor3f( 0.0, 1.0, 0.0 ) ;
glVertex3f(-1.0,-1.0, 0.0);
glColor3f( 1.0, 0.0, 0.0 ) ;
glVertex3f( 1.0,-1.0, 0.0);
glEnd() ;

glColor3f( 0.5, 0.5, 1.0 ) ;
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

glBegin(GL_QUADS);
glVertex3f (0.25, 0.25, 0.0);
glVertex3f (0.75, 0.25, 0.0);
glVertex3f (0.75, 0.75, 0.0);
glVertex3f (0.25, 0.75, 0.0);
glEnd();

glFlush();

rot += 0.3
}
rot should be a GLfloat in your view's interface. Basically the above code will rotate your triangle every time your view updates. To make it spin at a constant rate, you should have a timer that calls drawRect at a define interval.

BTW, I can't check that code right now but it *should* work. :blush:

ThrottleMonkey
2003.03.13, 04:33 PM
Ok, here's another silly question: What's a good way to set up a timer?

Would you use something like:

int i;

{
i = 1;

while ( i > 0 )
{
i++;
}
}

I know that's way crude... but I've never dealth with timers in GL before.

OneSadCookie
2003.03.13, 04:47 PM
:shock:

No, you wouldn't do that.

Precisely how you'd do it depends on what windowing/event API you're using. If you're using Cocoa, you'd use an NSTimer.

Docs are here:
file:///System/Library/Frameworks/Foundation.framework/Versions/C/Resources/English.lproj/Documentation/Reference/ObjC_classic/Classes/NSTimer.html

But I'm afraid I can't be much more help without knowing what you want from your timer and what classes it's interacting with.

Hog
2003.03.14, 08:01 AM
concerning GL, i seriously don't know what you mean.

for a regular timer, in Carbon you'd do something like InstallEventLoopTimer(...),
but to be absolutely sure how much time passed you could use the Microseconds(UnsignedWide*) function and measure yourself.

what you wrote looks nothing close to being a timer, it just looks like a loop from 0 to (1<<31) to me

ThrottleMonkey
2003.03.14, 02:15 PM
thanks for pointing out the docs so I can learn how to program the timer

Josh
2003.03.15, 10:28 PM
Here's a basic timer in Cocoa:

// note: updateTimer should be a NSTimer * in your interface file

- (void)awakeFromNib
{
updateTimer = [[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(update) userInfo:nil repeats:YES] retain];
}
This will update the screen (and rotate the images if you used something like my example code) every 20th of a second.

Hog
2003.03.16, 06:18 AM
Originally posted by jabber
This will update the screen (and rotate the images if you used something like my example code) every 20th of a second.

looks more like every 5th of a second to me

Josh
2003.03.16, 03:51 PM
Originally posted by c_dev
looks more like every 5th of a second to me Whoops! You're right! :lol: