Archived

This topic is now archived and is closed to further replies.

Flickering and buggy drawings

This topic is 5380 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, a little beginners problem here... All my OGL programs kinda bugs out when it moves/repositions/rotates on some angles. It''d be awesome if u could help me get to the bottom of this, because I have this problem in all my OGL programs... here is just a simplified program to make it easy for you guys to read my code... I''m simply drawing a glutSolidCone that rotates. Here it is: --------- #include <GL/glut.h> // Also includes gl.h and glu.h GLint mainWindow; // Window handle ID /////////INIT_OGL///////////////////////////////////////////// void initOGL(void){ glMatrixMode(GL_PROJECTION); //Define projection-type glLoadIdentity(); gluPerspective(45.0, 1.0, 0, 50); gluLookAt(3.0, 15.0, 15.0, 0, 0, 0, 0, 1.0, 0); } /////////SETLIGHT///////////////////////////////////////////// void setLight( void ){ GLfloat lightIntensity[] = {1.0, 0, 0, 0}; // Color of light GLfloat lightPosition[] = {-10, 0, 0, 0}; GLfloat globalAmbient[] = {1, 0, 0, 1}; //Global red light glLightfv(GL_LIGHT0, GL_AMBIENT, lightIntensity); glLightfv(GL_LIGHT0, GL_POSITION, lightPosition); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalAmbient); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); } /////////DRAWSCENE//////////////////////////////////////////// void drawScene(void){ glClearColor(0, 0, 0, 0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.1, 0.9, 0.0); glutSolidCone(1,2,4,5); glFlush(); glutSwapBuffers(); } /////////UPDATEIDLE/////////////////////////////////////////// void updateIdle(void){ glRotatef(3,0,1,0); // spin the object glutPostRedisplay(); } /////////MAIN///////////////////////////////////////////////// int main(int argc, char **argv){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutInitWindowSize(800,600); glutInitWindowPosition(0,0); mainWindow = glutCreateWindow("(C) 2003 Trond Borg"); glutDisplayFunc(drawScene); glutIdleFunc(updateIdle); glutSetWindow(mainWindow); initOGL(); setLight(); glutMainLoop(); return 0; } --------- Thanks dudes "That''s Tron. He fights for the users."

Share this post


Link to post
Share on other sites
I'm not sure what specific problems 'bugs-out' referes to but here are few issues I noticed, and some solutions:

1) Depth issues
In order to keep triangles from the back of your cone from showing up in front you need to turn on backface culling ( glEnable( GL_CULL_FACE) ) to avoid drawing triangles that are facing away from the camera. For scenes with complex or multiple objects you'll also want to use the depth buffer:
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH)
glEnable(GL_DEPTH_TEST)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BIT)

2) Transformations
After you set up your projection in initOGL, you should switch to the modelview matrix ( glMatrixMode(GL_MODELVIEW) ) before doing model transformations.
Accumulating rotations (like calling glRotatef in updateIdle) will quickly lead to strange distortions of your modelview matrix due to mathematical error inherent in floating point math. A better way is to store and update the rotation angle in a variable and rebuild the modelview matrix every time in drawScene():

float yRot = 0;

void drawScene(void){
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.1, 0.9, 0.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(yRot,0,1,0); // spin the object

glutSolidCone(1,2,4,5);
//glFlush(); // glFlush is done implicitly during glutSwapBuffers
glutSwapBuffers();
}

void updateIdle(void){
yRot += 3;
glutPostRedisplay();
}



[edited by - kronq on March 23, 2003 2:01:52 AM]

Share this post


Link to post
Share on other sites
Thanks alot, I learned alot from that!!!
It fixed the bugges that I had, namely that the it showed some weird lines inside the cone, but now I have another issue...

After putting in glEnable( GL_CULL_FACE), it does not show the cone in certain angles... it just goes black when it is rotated away from my view kinda...

Any ideas on that?

Thanks again!!!

Share this post


Link to post
Share on other sites
I''m glad to help

quote:
Original post by trondb
After putting in glEnable( GL_CULL_FACE), it does not show the cone in certain angles... it just goes black when it is rotated away from my view kinda...



Ok, so now the problem there is that glutSolidCone doesn''t actually draw a closed surface -- there is no disk closing the cone at the bottom. If you want it to see the inside of the cone, you can turn off CULL_FACE but leave DEPTH_TEST on. In general, once you start drawing large scenes you''ll want to leave CULL_FACE on for performance reasons and make sure all your 3D objects are ''closed'' so that there is no way to see the inside (which won''t be drawn.)
Most sources on fast rendering techniques will tell you to avoid turning off CULL_FACE and instead draw polygons twice (once for each side) if they really need to be double sided.

Share this post


Link to post
Share on other sites
>gluPerspective(45.0, 1.0, 0, 50);

im really surprised it doesnt give you more problems with that line. *waving hand* "you want to go home and rethink your near clipping plane"

Share this post


Link to post
Share on other sites
Trienco,
as you are talking to a beginner in OGL programming here, maybe you can be a little more specific and give me an example of a better solution?

Share this post


Link to post
Share on other sites
thinking about backface culling. you''d be surprised how small the difference really is. a lot of "rules" changed along the graphics hardware. most books will tell you to never ever enable gl_normalize because its soooo slow, while since the geforce 1 i think nvidia even recommends to enable it.

depending on your card: just draw a lot of triangles (say 100000) and then check the fps with and without backface culling ,-)

Share this post


Link to post
Share on other sites