PDA

View Full Version : Skybox help needed



Mofoliage
04-21-2011, 02:12 PM
Hi, I am pretty new to OpenGL and only really feel like I am beginning to understand what is going on. I am trying to get a basic sky box to appear around a previously finished project/lesson however it is the first thing that I have done that I have not had a good tutorial for. This may be completely the wrong way to go about doing this but here is the code I have so far to create the skybox:


void skybox(void) {

glPushMatrix();
glClear(GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

gluLookAt( 0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
0.0, 1.0, 0.0);

glPushAttrib(GL_ENABLE_BIT);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_BLEND);
glDepthMask(0);

glColor4f(1.0, 1.0, 1.0, 1.0);

glBegin(GL_QUADS); //FRONT
glVertex3f(-1, 1, 1);
glVertex3f( 1, 1, 1);
glVertex3f( 1, -1, 1);
glVertex3f(-1, -1, 1);
glEnd();

glBegin(GL_QUADS); //BACK
glVertex3f(-1, 1, -1);
glVertex3f( 1, 1, -1);
glVertex3f( 1, -1, -1);
glVertex3f(-1, -1, -1);
glEnd();

glBegin(GL_QUADS); //TOP
glVertex3f(-1, 1, 1);
glVertex3f(-1, 1, -1);
glVertex3f( 1, 1, -1);
glVertex3f( 1, 1, 1);
glEnd();

glBegin(GL_QUADS); //BOTTOM
glVertex3f(-1, -1, 1);
glVertex3f( 1, -1, 1);
glVertex3f( 1, -1, -1);
glVertex3f(-1, -1, -1);
glEnd();

glBegin(GL_QUADS); //RIGHT SIDE
glVertex3f( 1, 1, 1);
glVertex3f( 1, -1, 1);
glVertex3f( 1, -1, -1);
glVertex3f( 1, 1, -1);
glEnd();

glBegin(GL_QUADS); //LEFT SIDE
glVertex3f(-1, 1, 1);
glVertex3f(-1, -1, 1);
glVertex3f(-1, -1, -1);
glVertex3f(-1, 1, -1);
glEnd();

glPopAttrib();
glDepthMask(1);
glPopMatrix();
}

At the moment it clears the buffer bit, resets the camera, disables a bunch of stuff including the depth buffer, draws a 2 unit white cube about the origin (the background of the project is currently black so I should be able to tell when I get it working), and then reverts back to old attrib settings.

I am calling this at the beginning of my display function before anything else is rendered. Currently all that happens is that a white square appears behind everything else I am drawing which I think must mean that at least one of the faces of the cube is being drawn but no others.

If anyone could help me out and point me in the right direction I would be very grateful. Also if anyone knows of a good tutorial to achieve a skybox which they think would be good for a beginner please send me the link.

Thanks

Edit:

I thought it might be useful to include the display code:

glutDisplayFunc(display);


void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

skybox();

gluLookAt( 0.0, 10.0, 20.0,
0.0, 10.0, 0.0,
0.0, 1.0, 0.0);

init_lights();
glCallList(ground);
render_windmill(0.0, 0.0);

glFlush();
glutSwapBuffers();
return;
}

zeoverlord
04-22-2011, 03:35 AM
well your on the right track, the reason why only one quad shows up could be that your z.near could be set at 1 in which case anything but the front most quad could get culled which is why i use glVertex3f(-10, 10, 10); instead of glVertex3f(-1, 1, 1);.

The second problem that it could be is that they are all facing away from the camera try glDisable(GL_CULL_FACE); and if all goes white then that's your problem, just flip the faces inwards as needed, it's easier if you color each face with a different color.

Then just give them some texture coordinates(+textures) and that should be it

Mofoliage
04-22-2011, 06:07 AM
Hi, the z.near was set to one so I did as you suggested and changed the cube edge length to 10.

Thanks alot for the help.