PDA

View Full Version : animation questions



hillxy
01-10-2002, 06:22 AM
Hi, friends,

I need help on this? I am doing simple(be complicated later) animation project using opengl. What I will do is just moving a ball
or cube from left to the right.

I am using

P4 2.0G, 256M PC 133,
inno3d Geforce2MX400 32 M,

with opengl, and glut3.7

The whole program is here"

//the beginning =====================
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>


GLboolean pause = GL_FALSE;
GLint shapecode = 1; //default is sphere
GLint control;
GLint draw;
void init(void)
{

glClearColor (1.0, 1.0, 1.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);

glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glFrontFace(GL_CW);
glCullFace(GL_BACK);
glMaterialf (GL_FRONT, GL_SHININESS, 64.0);

{
GLfloat pos[] = {0.,150.,1.,1.};
glLightfv(GL_LIGHT0, GL_POSITION, pos);
}
}


void display(void)
/* drawing happens here */
{
static float xpos= -7.0;
static int angle = 0.0;
static float rot = 0.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if(xpos < 7)
xpos = xpos+0.05;
else
xpos = -7.0;

angle = (angle + 2) % 360;

glTranslatef(xpos, -1.0, -15.0);
glRotatef((GLfloat)angle, 1., 1.,0.);

glutSolidCube(2.0);

glutSwapBuffers();

}


void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(40., 1.0, 10.0, 200.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void animate(void)
{
if (pause != GL_TRUE)
{
glutSetWindow(control);
glutPostRedisplay();

}
}


void mouse(int button, int state, int x, int y)
{
if(state == GLUT_DOWN)
pause = !pause;
}

int main(int argc, char **argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPT H);
glutInitWindowPosition(10, 10);
glutInitWindowSize(1000,1000);
control = glutCreateWindow("cube View");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(animate);
glutMouseFunc(mouse);

glutMainLoop();

return 0;
}
//the end ======================

It is simple, It moves, but not smooth with (1000x1000 window size. Is it because my
animation mothod is not right, or I need do sth else to my hardward setting?

Thanks a lot

Master Lolo
01-10-2002, 08:07 AM
Are you sure it's not moving TOO fast ?
A such simple app running on your hardware would get a very high frame rate, I think you find it "not smooth" because of your monitor refresh frequency ...
Try lowering a lot your translations and rotations speed (xpos = xpos+0.05 // angle = (angle + 2) % 360)
Hey suppose you render 540 fps, it means every second, your cube has spinned 3 times.

Hull
01-10-2002, 09:26 AM
Spoken by a true mastah... http://www.opengl.org/discussion_boards/ubb/wink.gif

hillxy
01-10-2002, 09:58 AM
It should be fast.

If I resize the window to a 400*400. the
spin and moving is pretty smooth, but not the
case for full screen.

How is it? if my monitor refreshes at 60Hz, but Opengl gives more fps than that? Will
It stick on 60Hz, and the CPU ang GPU keep
waiting for the next refresh cyccle, or just
throw out glflush() with no real effect on the screen.

If it is the latter, the cube should jump around, since next refresh cycle of monitor
may be anywhere for the application frame number.

How to check monitors refresh frequency?

Thanks.

hillxy
01-10-2002, 10:34 AM
Originally posted by Master Lolo:
Are you sure it's not moving TOO fast ?
A such simple app running on your hardware would get a very high frame rate, I think you find it "not smooth" because of your monitor refresh frequency ...
Try lowering a lot your translations and rotations speed (xpos = xpos+0.05 // angle = (angle + 2) % 360)
Hey suppose you render 540 fps, it means every second, your cube has spinned 3 times.

You siad it might because it is too fast. I change the rate to 0.005, ten times slower.
The moving turned out to be much slower than before.

Main point is that I can not see difference between the performance of the simple program on a PIII with generic Video card and that on my machine.

So what I think it might because I have not fully used the hardware acceloration.

Thanks, please give me more hint... thanks

Master Lolo
01-10-2002, 07:42 PM
I don't see any crap in your code. Try implementing small functions like increasing rotating speed by pressing left or right key (very easy in glut), try to debug stuff either by logging in file or by rendering text on the screen (easy in glut too).

No more hints than these, sorry ...

Thug
01-11-2002, 09:41 AM
I found a problem that may be causing you grief.

In your display function you have it setting the xpos to -7.0, angle to 0.0 and rot 0.0 every time you call display. Move the initialization code to your init function and everything should work.

hillxy
01-11-2002, 06:30 PM
Originally posted by Thug:
I found a problem that may be causing you grief.

In your display function you have it setting the xpos to -7.0, angle to 0.0 and rot 0.0 every time you call display. Move the initialization code to your init function and everything should work.

Hi, Thug,

xpos, angle and rot are static variables, they should be initalized just once. That is
not the problem. Thanks anyway.

Hillxy.

hillxy
01-11-2002, 06:38 PM
Originally posted by Master Lolo:
I don't see any crap in your code. Try implementing small functions like increasing rotating speed by pressing left or right key (very easy in glut), try to debug stuff either by logging in file or by rendering text on the screen (easy in glut too).

No more hints than these, sorry ...

Hi, Lolo,

I have admit some hardware secret in my system. It is a dual head video cards(inno3d)
with a ATI Rage 128 Pro. So I set up a 3 monitor system. Since the code seems no crap,
I got back to the system. When I just left one primary monitor with the inno3d(PCI) card, the simple application runs pretty well
even in the full screen, no flicker no jump.
but it goes worse when enabled any other monitors.

Now it turns out to be a hardware issue. Which card support dual head and can run opengl with but output? I checked inno3d webpage, it seems they have problem with
the second display for openGL.

Any advice?

thanks

Hillxy

Overmind
01-13-2002, 07:26 AM
Hi

It seems that your display callback doesn't get called in regular intervals. Perhaps that has got something to do with the refresh rates of the monitors that aren't exactly synchronized.

Anyways, you shouldn't expect your display callback to be called in regular intervals. Try to find out how much time has passed since your last display and increase your position according to that value.

Here is some pseudocode:



void display()
{
static int last_timer, position = 0;
int timer;

timer = query_timer();
position += (timer - last_timer) * speed;
last_timer = timer;

translate(position);
render();
}


This should produce a smooth movement no matter how often the display function gets called.

Hope that helps
Overmind

[This message has been edited by Overmind (edited 01-13-2002).]

jwatte
01-13-2002, 04:33 PM
To get (reasonably) smooth performance on Windows, you have to run your event polling in a tight loop. I expect the behavior under X-Windows would be the same. Chances are, GLUT tries to yield the CPU or calls your redisplay function using a WM_TIMER or some other such stutter-inducing thing.

hillxy
01-14-2002, 07:24 AM
Originally posted by Overmind:
Hi

It seems that your display callback doesn't get called in regular intervals. Perhaps that has got something to do with the refresh rates of the monitors that aren't exactly synchronized.

Anyways, you shouldn't expect your display callback to be called in regular intervals. Try to find out how much time has passed since your last display and increase your position according to that value.

Here is some pseudocode:



void display()
{
static int last_timer, position = 0;
int timer;

timer = query_timer();
position += (timer - last_timer) * speed;
last_timer = timer;

translate(position);
render();
}


This should produce a smooth movement no matter how often the display function gets called.

Hope that helps
Overmind

[This message has been edited by Overmind (edited 01-13-2002).]

Hi, Overmind

That is a good point for smooth moving. Thanks, I will try this.

Hillxy

Master Lolo
01-14-2002, 09:31 PM
Try searching for vertical synchronization too. AFAIK, dual screen and main screen full size rendering don't cohabitate very well. You're supposed to deactivate all secondary screen channels before running an app full screened.

hillxy
01-15-2002, 12:39 PM
Hi,

seems this has been a problem for a long time. I did a little deep research on "opengl" with "dual head" or "multi"..
There are quite a few discussion thread
asked essensially the same quation.

"Opengl on Multi monitor" ?

There is an article in realtimesoft.com
focused on this. (I think it is pretty good). Till now, I still can not find any definite solution for it.

Most solutions from various forums are
testing different combinations. I will follow
the mothod of two cards combination to
get opengl on multi mons. 3dLab's cards
are too expensive, though it suppose to
work out the problem perfectly.

I will tell whether it works or not. at least
I got the gl supporting the primary monitor and the other two enabled at the same time
without gl support.

Hillxy

hillxy
01-22-2002, 07:33 AM
I tried two PCI, geforce MX200 32 M. NOT working..... sad. give up. wont let bestbuy kick me out by return too many things. http://www.opengl.org/discussion_boards/ubb/smile.gif

Hillxy