PDA

View Full Version : basic question



drdolittle
10-17-2008, 10:54 AM
#include<GL/glut.h>
#include<GL/gl.h>

void display (void)
{
//1 glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0.0,0.0,1.0);
glBegin(GL_POLYGON);
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();
}
void init (void)
{
glClearColor(0.0,0.1,0.0,0.0);
//2 glClear(GL_COLOR_BUFFER_BIT);
glOrtho(0.0,1.0,0.0,1.0,-1.0,1.0);
}

int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
glutInitWindowSize(300,300);
glutCreateWindow("test_hello");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

I am a newbie in openGL. In the above program can somebody tell me what's the difference in placing the command glClear(....) in positions commented and marked 1 and 2 ?

Best,
Pkri

Jan
10-17-2008, 11:48 AM
glClear will clear the screen to the given clear-color. Your init-function is called only once at startup, at this point it is not necessary to clear the screen, so (2) is not needed, but it doesn't do anything bad either.

Your display-function is called each time a new frame is rendered (several times per second). Here it makes sense to clear the screen, before anything is rendered, to have a clear background (slightly green ?? weird choice for a background color).

To clearly see the difference, don't clear in the display-function and then move around in the scene a bit, you will see why that is not what you want.

Jan.

overlay
10-17-2008, 01:04 PM
Anything but black or white is actually a good choice for a background color for debugging! :-)

If your background is black, and you have a bug in your code and nothing seems to be drawn, you don't know if it was because of some culling (nothing really rasterized), or depth test, or because your lighting setup is wrong (reversed normal, wrong light setup), etc
...

If the background is white, and nothing is drawn it can be the default color of the window system, or culling, or in the case you are using textures, some wrong texture setup, etc...

drdolittle
10-17-2008, 05:21 PM
Thanks for your explanation. it cleared my doubts. i have one more question akin to that. Would it matter if i call glDisplayFunc() first and then call init() ?

The tint of Green color as background was a mere accident :)

Jan
10-18-2008, 02:56 AM
No, i think that would make no difference.

glutDisplayFunc(display) simply tells glut the function to use for display later. "display" is not yet called, at all.

It is glutMainLoop() that starts a loop, calling "display" to render each frame. This one must be called after "init", otherwise init would not be done at the right time.

Jan.

drdolittle
10-19-2008, 01:32 PM
I bumped into bouncing square example now. Unfortunately, there is no complete code on the book. Can somebody help me how to make the square move in the following code ?

#include<GL/glut.h>
#include<GL/gl.h>

GLfloat x1=0.0;
GLfloat y1=0.0;
GLfloat rsize=25.0;

GLfloat xstep=1.0;
GLfloat ystep=1.0;

GLfloat windowWidth;
GLfloat windowHeight;

void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0,0.0,0.0);

glRectf(x1,y1,x1+rsize,y1+rsize);

glutSwapBuffers();
}
void TimerFunction(int value)
{
if(x1>windowWidth-rsize||x1<-windowWidth)
xstep=-xstep;

if(y1>windowHeight||y1<-windowHeight+rsize)
ystep=-ystep;

x1 += xstep;
y1 += ystep;

}

void SetupRC(void)
{
glClearColor(0.0,0.0,0.0,0.0);
glOrtho(0,100,0,100,-1,1);
}

int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(300,300);
glutCreateWindow("Bounce");
SetupRC();
glutDisplayFunc(RenderScene);
glutTimerFunc(33,TimerFunction,1);

glutMainLoop();

return 0;
}


Best,
Pkri

drdolittle
10-20-2008, 11:35 AM
I got it to work.......thanks for the help.