PDA

View Full Version : Animation problem, again...



The_V|k|ng
02-28-2002, 11:11 AM
Heya guys!

I am fairly new to OpenGL-programming, using GLUT. My first program looks like this:

#include <windows.h>
#include <gl\glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

#define PI 3.1415265359


#pragma argsused

static GLfloat spin = 0.0;
GLfloat red = 1.0, dred = -0.005,
green = 1.0, dgreen = -0.01,
blue = 1.0, dblue = -0.015,
g = 8, i = 0, dg = 5;

GLenum errCode;
const GLubyte *errString;

const GLubyte *glVersion, *glVendor, *glRenderer;
const dspin = 1.0;


void glerror (int msg)
{
if ((errCode =glGetError()) != GL_NO_ERROR)
{
errString = gluErrorString(errCode);
fprintf(stderr, "\n OGL-Error: %s %i", errString, msg);
}
}

void init (void)
{
glEnable(GL_DEPTH_TEST);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);

glutSetCursor (GLUT_CURSOR_NONE);
glutFullScreen ();
}

void display (void)
{
i = i + 0.05;
if (i > 360) i = 0;

red = red + dred;
if ((red < 0.0) &#0124; &#0124; (red > 1.0)) dred = -dred;
green = green + dgreen;
if ((green < 0.0) &#0124; &#0124; (green > 1.0)) dgreen = -dgreen;
blue = blue + dblue;
if ((blue < 0.0) &#0124; &#0124; (blue > 1.0)) dblue = -dblue;

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f (red, green, blue);

glPushMatrix ();

glLoadIdentity();
glRotatef (spin, 1.0f, 1.0f, 0.1f);
// printf("\nSpin: %f", spin);


// dg = sin (i);
// g = 10 + dg;

glerror(0);

glBegin(GL_QUADS);
glColor3f (red, green, blue);
glVertex3f (g, g, g);
glVertex3f (g, -g, g);
glVertex3f (-g, -g, g);
glVertex3f (-g, g, g);

glColor3f (0.0, 0.0, 1.0);

glVertex3f (g, g, -g);
glVertex3f (g, -g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, g, -g);

glColor3f (0.0, 1.0, 0.0);

glVertex3f (g, g, g);
glVertex3f (g, g, -g);
glVertex3f (-g, g, -g);
glVertex3f (-g, g, g);

glColor3f (1.0, 0.0, 0.0);

glVertex3f (g, -g, g);
glVertex3f (g, -g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, -g, g);

glColor3f (1.0, 0.0, 1.0);

glVertex3f (g, g, g);
glVertex3f (g, g, -g);
glVertex3f (g, -g, -g);
glVertex3f (g, -g, g);

glColor3f (1.0, 1.0, 0.0);
glVertex3f (-g, g, g);
glVertex3f (-g, g, -g);
glVertex3f (-g, -g, -g);
glVertex3f (-g, -g, g);

glEnd;

// glutWireCube (0.1*g);

glerror(1);

glPopMatrix();
glutSwapBuffers();
glFlush();
glerror(2);

}


void SpinDisplay (void)
{
spin = spin + dspin;
if (spin > 360) spin = spin - 360;

glutPostRedisplay();
}


void reshape (int width, int height)
{
glViewport (0, 0, (GLsizei) width, (GLsizei) height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity;
glOrtho (-20.0, 20.0, -15.0, 15.0, -15.0, 15.0); //AspectRatio beachten!!!!!!

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
}

void mouse (int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON :
if (state == GLUT_DOWN) glutIdleFunc (SpinDisplay);
break;
case GLUT_RIGHT_BUTTON :
if (state == GLUT_DOWN) glutIdleFunc (NULL);
break;
default :
break;
}
}



void keyboard ( unsigned char key, int x, int y )
{
switch ( key ) {
case 27: /* Escape key */
printf ("\n\n Shutting down...");
glutDestroyWindow(1);
exit ( 0 );
break;
default:
break;
}
}



int main ( int argc, char** argv )
{
printf("\n*** W

nexusone
02-28-2002, 11:16 AM
What is the problem?

nemesis
02-28-2002, 02:57 PM
Oh!, pretty code, really beautiful!
But... I was just wondering the same thing as nexusone... http://www.opengl.org/discussion_boards/ubb/rolleyes.gif

[This message has been edited by nemesis (edited 02-28-2002).]

zeckensack
02-28-2002, 03:02 PM
Don't know much about glut but don't you have to register your mouse and keyboard event handlers? Before entering the mouse handler at least once, you'll get no animation.

Also, use "glEnd();" instead of "glEnd;", will save you a few warnings http://www.opengl.org/discussion_boards/ubb/wink.gif

[This message has been edited by zeckensack (edited 02-28-2002).]

john
02-28-2002, 03:20 PM
the brackets means 'call this variable'.

glEnd;

is just an address of the function glEnd. glEnd() means 'call this address'.

no, glEnd; by itself doesn't mean a hell of a lot, but then you can write 1; and that doesn't mean anything either. (its an expression with the result of 1 that's just thrown away). this is legit C:



void goat(void)
{
}

void main(void)
{
int llama;

llama=5; /* assignment */
llama; /* evals to 5, but the result is thrown away */
5; /* ditto. doesn't mean anything */

goat(); /* call the goat */
goat; /* evaluates to some address */
}

cheers,
John

zeckensack
02-28-2002, 04:15 PM
john: at first I thought the same (before the edit) but when I tried it out in MSVC6 (in a C++ module) it threw a warning (empty parameter list) but it called the function. Is this a feat of C++ or is VC wrong?

john
02-28-2002, 06:06 PM
howdy,

er... weird. from what i've always understood, () is required in C to call a procedure. (What if you wanted to copy a function's pointer for use with qsort, for example? You dont' wna tot be calling the comparison when you pass the comparison function pointer to qsort!!)

I tried the code as a C source with gcc. It compiled without warnings, but didnt' call the function. i force a C++ compile and it gave me a warning that the statement is a refernece, noit call, to function 'goat'. It still didn't call it.

I think visual C++ is being stupid. what if I did this:



typedef void (*funcptr)(void);

void goat(void)
{
printf("goat!!\n");
}

void goatMaster(funcptr animal)
{
animal();
}

void main(void)
{
goatMaster(goat);
}

does goat get called when we call goatMaster? It shouldn't.

cheers,
John

Furrage
03-01-2002, 04:50 AM
Dudes, I'm looking for a good C++ forum that set up much like this. That way, when C++ topics come up (like what is being discussed here) I can discuss it there, instead of here where it annoys some OpenGL forum users. Any suggestions?

nexusone
03-01-2002, 06:11 AM
You know a lot of newbies problems is that they do not know how to program ether.
So those who have a problem of helping newbies with there program beyond opengl, should get a grip.
How are they to know it is not an opengl problem anyway.

http://www.opengl.org/discussion_boards/ubb/smile.gif


Originally posted by Furrage:
Dudes, I'm looking for a good C++ forum that set up much like this. That way, when C++ topics come up (like what is being discussed here) I can discuss it there, instead of here where it annoys some OpenGL forum users. Any suggestions?

[This message has been edited by nexusone (edited 03-01-2002).]

[This message has been edited by nexusone (edited 03-01-2002).]

Furrage
03-01-2002, 07:36 AM
I agree, but I've seen a lot of people complain about C/C++ stuff in the forum before. So rather than "offend" them over something trivial I'd rather find a C/C++ forum http://www.opengl.org/discussion_boards/ubb/smile.gif . Plus I'd really like to discuss a number of non OpenGL topics and the forums I'm finding so far are really poor compared to this one (Pop ups everywhere, you have to click a link to each reply to see it, etc, etc). Assuming you guys have more web experience than me and that you use other forums, it would be nice to know if you've found any.


Originally posted by nexusone:
You know a lot of newbies problems is that they do not know how to program ether. So those who have a problem of helping newbies with there program beyond opengl, should get a grip. How are they to know it is not an opengl problem anyway.

http://www.opengl.org/discussion_boards/ubb/smile.gif


[This message has been edited by Furrage (edited 03-01-2002).]