PDA

View Full Version : default camera



12-26-2000, 08:27 PM
I have written a simple glut program and I am having trouble getting any objects to appear on the sreen. The screen is black like clear color says. Opengl inits correctly and calls the display callback function correctly.

But what is the default camera position
in a glut program. What is the position of the camera and what is it looking at. I assume it is alooking at 0,0,0. But am I drawing my objects to big? Many beginner examples make some assumptions that I'm not sure about. ?

Thanks for help and sggestions.

softland_gh
12-26-2000, 11:23 PM
GLUT doesn't change the way a model is projected onto the screen. It's just responsible for creating rendering contexts and the windows it's going to render into, and triggers some events that can help you inializing and rendering your scene. Check for these things in your reshape function:

-That you are using glViewport properly.
-That you are inializing the projection matrix properly
-That you are using positive non-zero values with which you are clearing your depth buffer

For example, this code:

void reshape(int width, int height) {
glViewPort(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glFrustum(-1, 1, -1, 1, 1, 100);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glClearColor(0, 0, 0, 1);
glClearDepth(1);
}

will center your camera at the origin(0,0,0) and it will be looking at the negative z axis.

12-28-2000, 02:38 AM
As a newby, I had the same frustrating problem in past. In my case, the problem was the far clipping plane of the frustum, because the moder was too big in absolute value. I solved it increasing the last value of gluPerspective:
gluPerspective(45.0f, 1.333f, 100.0f, 1000000.0f);
http://www.opengl.org/discussion_boards/ubb/cool.gif

12-28-2000, 07:01 AM
Well, still getting just plain black. Here is a snippet of my code. After this in a display function then glClear,glFlush and glSwapBuffers. And all I get is black even if I simply draw a glutSolidSphere. Oh...this is ox x and project builder. My goal is simply to get something to appear. Any suggestions? Just doing wonder bread stuff here nothing special.


glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE ); //init with depth,rgb color and 2xbuffer
glutInitWindowSize(win_width,win_height); //set the size of the window
glutInitWindowPosition (win_pos_x, win_pos_y);//set the position of the window
glutInit(&argc, argv); //initialize glut with command line options.let command line override previous inits if there are any.
glutCreateWindow(wintitle); //create the window
glEnable(GL_LIGHTING); //turns on lighting
glEnable(GL_NORMALIZE); //enable automatic normalization
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST); //enable depth testing??
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //the background color
glClearDepth(1.0); //specify depth for clear buffer. [0,1]
glDepthFunc(GL_LEQUAL); //remove anything equal or farther away
glCullFace(GL_BACK);//remove the back of objects
glEnable(GL_CULL_FACE); //enable culling. make it faster.
gluPerspective(45.0f, 1.333f, 100.0f, 1000000.0f); //test suggestion

void display() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clears depth buffer and color buffer
glPushMatrix();
glutSolidSphere(1,10,10);
glPopMatrix();
glutSwapBuffers(); //swaps the buffers..should flush also
}

Deiussum
12-28-2000, 07:46 AM
I see a few problems with your code. First of all, you don't seem to have a glutMainLoop() call anywhere. I assume you have it there but just didn't paste it otherwise I don't think you would even get a window.

Second... It's usually best to put the gluPerspective stuff into the resize callback. You also don't explicitly set the matrix mode, which you probably should do. It would also be a good idea to load the identity matrix into the projection matrix before calling gluProjection. My resize function typically looks something like this.



void Resize(GLuint x, GLuint y)
{
glViewport(0,0,x,y);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 1.333f, 100.0f, 1000000.0f); // what you were using
glMatrixMode(GL_MODELVIEW); // I like to keep the matrix mode here unless changing the projection

}


The other problem I see is that you are doing a push/pop matrix, but never actually modifying it in between. And in your case, you will need to do that becuase glutSolidSphere will place the sphere at 0,0,0, which is in front of your front clipping plane. Inside your push/pop you should be able to do something like so...

glTranslatef(0.0, 0.0, -102.0);

That will push the "world" away from you by 102 units, which should put your sphere into your frustum.

You can also think of this as moving the camera to the position 0,0,102 if you prefer. Generally, though, the "camera" in OpenGL is fixed at 0,0,0. (Not looking at 0,0,0, but positioned there.) The view matrix and the model matrix are all combined into one (the modelview matrix) so if you want to think of it as moving the camera, you do the opposite of what you want the camera to do. For example, you effectively get the same effect by moving the camera by 0,0,102 as you do by moving the world by 0,0,-102.

One method I've used to simulate separate camera/world calculations is to do my "camera" translations in the resize function, or when the camera moves. Then for the world positioning, I put stuff inside push/pop calls. Again, when you move the camera, you need to call glLoadIdentity before resetting the camera transformations.

I'm not sure how clear that last description comes across. It's kind of hard to explain. You should get a copy of the red book for a better explanation of the modelview matrix stuff. You can either buy it or download an old version of it from somewhere. I don't remember where for sure, but if you do a search on the "Red Book" in this forum, there's a thread here somewhere with a link to it.

12-28-2000, 08:16 AM
hmm..I have the red and blue books and they have been helpful.Though, they use aux utilities for the windowing. Os x does not appear to support aux or maybe aux has changed. My books are about 5 years old. I am calling glutMainLoop and a window does get created. It is black. I put the push/pop matrix in the display function so I can preserve my initial state. Like a sand box I guess...

One more thing I did in another part of the code before the display function:

float mColor = {1.0f,1.0f,1.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, mColor);
glEnable(GL_LIGHT0);

So, I guess I should see a filled circle since there is no directional lighting yet. I assume glutSolidSphere handles normal vectors for me.

Deiussum
12-28-2000, 10:52 AM
The version of the red book I have uses glut, but the stuff on the model view matrix is pure OpenGL so you should be able to read up on that to learn about how the view works.

The pushMatrix/popMatrix are really pretty useless if you don't put stuff between them to transform the matrix. And you do need to do that with the info you gave. glutSolidSphere will position the center of the sphere at 0,0,0 unless you use transformations to position/rotate/scale it. With the frustum you are creating your near plane has a z- value of -100 and the far plane has a z-value of -1000000. Obviously, 0,0,0 does not lie between these planes so you need to do a glTransform before calling glutSolidSphere in order to get it between the near/far clipping planes.

And as I said before, be sure to know which matrix mode you are in and use glLoadIdentity as needed. (i.e. before a new gluPerspective) And re-read the chapter on camera vs. world transformations in the red book. It explains things pretty well there. http://www.opengl.org/discussion_boards/ubb/smile.gif

12-29-2000, 08:21 AM
ok. I read some of the red book. I think I like gluPerspective since it needs less arguements. Let me see if I got this strait. The view is initially looking down the z axis in the negative direction. The x axis is left to right on the screen going in the positive direction. The y axis is top to bottom on the screen going in the positive direction. So, if I put objects within the near and far values on the negative z axis I should see something. ( but I pass in positive values for near and far into gluPerspective )


Would this do for a reshape function?

//reshape handles window resize issues
void environment::reshape(int w,int h) {
//set the viewport to the current window
glViewport(0,0,w,h);
//get the arc tangent of the right triangle
//whose adjacent size is the "near" value
//and whose height is ht/2. Then convert
//to degrees by multiplying by 180/pie.
//field of view is 2 times the angle since
//we just cut the original triangle by half.
float fov = 2*atan((h*.5)/mNear)*(180.0/3.1415);
//aspect ratio is height divided by width
float asp = (float)w/(float)h;
//before we call gluPerspective you need to change
//to the projection matrix and clear that matrix to
//the identity matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//create the matrix
gluPerspective(fov,asp,mNear,mFar);
//set the matrix back to model view and clear
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Still getting just a black screen. I'm going to throw a party when this works. http://www.opengl.org/discussion_boards/ubb/smile.gif

Deiussum
12-29-2000, 08:51 AM
Yeah, the near and far clipping planes should always be specified as positive. That's becuase they represent how far in front of the "camera" the clipping planes will be. Effectively, the negative of those ends up being their z value. If you add one more line of code to your resize, you should see your triangle... you're setting up the projection, but still not translating the modelview so that it ends up in the clipping plane. Assuming you are using 0 as the z-value of your triangle, you should be able to get the triangle into the view by adding something like so...

// Get the z-value right in the middle of the clip planes by getting the average
float zPos = -(mNear + mFar)/2.0;

glTranslatef(0.0, 0.0, zPos);

12-29-2000, 06:57 PM
Well I got it to work. See a nice little sphere. Yippie. Weeks of misery have come to a glorious end.

But I had to go back to OS 9. Same code but my glut sphere appears in OS 9 and not X. Well, I guess thats why is called os x beta. I'll give it another try when the new os x developer cd comes out next month.

The program would crash on quit on os 9. I finally figured that one out. There was an additional extension that the new apple opengl installer did not remove. Something about classic compatibility. Had a big CL in the icon. Once I removed that extension all is well.