OpenGL custom quads have lighting bugs - c++

I'm trying to figure out how to write my own primitives like gluSolidCube()..
In the following code I'm drawing 2 quads. One with the help of this method (the red one) and another one - by my own with the help of glBegin()/glEnd() (the blue one).
glPushMatrix();
glRotatef(-angleX, 0, 0, 1);
glRotatef(angleY, 0, 1, 0);
glColor3f(1, 0, 0);
glTranslatef(0.6, 0, 0);
glutSolidCube(1);
glColor3f(0, 0, 1);
glTranslatef(-1.2, 0, 0);
glPolygonMode(GL_FRONT, GL_FILL);
glShadeModel(GL_SMOOTH);
glBegin(GL_QUADS);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
glPopMatrix();
So as you can see at the following screenshots the lightnings on the red quad is correct unlike the blue one. How to solve this?

OpenGL fixed-function lighting won't work correctly without sensible vertex/face normals.
You need to supply some, perhaps via glNormal().

Related

OpenGL displaying a square instead of a cube

I am starting to use tinker with OpenGL and I want to draw a cube. I went to a tutorial, followed that, and only got this weird square.
Here's my code.
#include <GLFW/glfw3.h>
void drawCube() {
float rotate_x{ 193 };
float rotate_y{ 112 };
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0); glVertex3f(0.5, -0.5, -0.5);
glColor3f(0.0, 1.0, 0.0); glVertex3f(0.5, 0.5, -0.5);
glColor3f(0.0, 0.0, 1.0); glVertex3f(-0.5, 0.5, -0.5);
glColor3f(1.0, 0.0, 1.0); glVertex3f(-0.5, -0.5, -0.5);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glLoadIdentity();
glRotatef(30, 0.0, 1.0, 0.0);
glEnd();
}
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Triangle", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
drawCube();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I'm using GLFW to do this, and I'm building it in Visual Studio 2017 Community Edition.
I also tried to rotate it at the end of drawCube().
The tutorial I followed was https://www.wikihow.com/Make-a-Cube-in-OpenGL but I tweaked it a bit to use glfw.
You are drawing a single polygon with 24 vertices. Since OpenGL requires the polygon to be concave and planar (which your data isn't) it draws some random stuff.
If you want to draw a cube, you either have to split the drawing the call glBegin(GL_POLYGON) and glEnd() for each face separately (as the tutorial shows in the second code sample in section 4), or you can change the primitive mode to GL_QUADS.
Note, that the OpenGL version you are using (fixed function pipeline) is outdated for more than 10 years now. You might want to consider switching to (at least) OpenGL 3.3 core profile and use shader.
Camera
I do not see you are setting the matrices anywhere... So my bet is that your matrices are unit resulting in 2D view no matter how you tweak the rendered axis aligned data.
Use gluPerspective for the GL_PROJECTION. Its a bit tricky to set up the perspective 3D view properly for rookies (as it views in -Z direction which is exact opposite than identity matrix) so your mesh is in viewable area.
CULL_FACE
for starters try glDisable(GL_CULL_FACE) as your data will most likely contain winding errors and you will not know what faces you already have or not otherwise.
cube mesh
right now you are hardcoding it using glVertex calls its much easier to use separate arrays for points and faces (QUADS or TRIANGLES). Also as BDL pointed out you are using forbidden commands inside glBegin/glEnd calls they are ignored and slows things down due to error handling.
lighting
to feel the 3D better you should also add normals for each face and enable lights. Otherwise you would need to color each face with different color to see the "3D".
Take a look at my:
complete GL+GLSL+VAO/VBO C++ example
just ignore the advanced stuff (GLSL,VAO/VBO...) and look for view settings and the cube mesh data. You just need to render it with for loop instead of VAO/VBO. Here example:
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
#ifndef vao_indices
glBegin(GL_QUADS);
for (int i=0;i<6*4*3;i+=3)
{
glNormal3fv(vao_nor+i);
glColor3fv (vao_col+i);
glVertex3fv(vao_pos+i);
}
glEnd();
#else
int i,j,k;
const GLfloat vao_nor[]=
{
// nx ny nz
0.0, 0.0,-1.0,
0.0, 0.0,+1.0,
0.0,-1.0, 0.0,
+1.0, 0.0, 0.0,
0.0,+1.0, 0.0,
-1.0, 0.0, 0.0,
};
glBegin(GL_QUADS);
for (j=0;j<6*4;j++)
{
i=vao_ix[j]; i+=i+i;
k=j>>2; k+=k+k;
glNormal3fv(vao_nor+k);
glColor3fv (vao_col+i);
glVertex3fv(vao_pos+i);
}
glEnd();
#endif
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
using the mesh from gl_simple in that linked QA in both modes (indices and direct).

Resource Leak opengl/win32

Hello I've recently started learning win32/opengl and I managed to write a function that displays a multi colored cube within a window. My problem is there is a resource leak but I'm stumped and not sure what exactly I'm forgetting to delete.
NOTE I have narrowed it down to be within this function
void display()
{
g.hglrc = wglCreateContext(g.hdc);
wglMakeCurrent(g.hdc, g.hglrc);
// make the color a white hue
glClearColor(1.0F, 1.0F, 1.0F, 1.0F);
// Clear screen and Z-buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Rotate when user changes rotate_x and rotate_y
glRotatef(rotate_x, 1.0, 0.0, 0.0);
glRotatef(rotate_y, 0.0, 1.0, 0.0);
//Multi-colored side - FRONT
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0); glVertex3f(0.5, -0.5, -0.5); // P1 is red
glColor3f(0.0, 1.0, 0.0); glVertex3f(0.5, 0.5, -0.5); // P2 is green
glColor3f(0.0, 0.0, 1.0); glVertex3f(-0.5, 0.5, -0.5); // P3 is blue
glColor3f(1.0, 0.0, 1.0); glVertex3f(-0.5, -0.5, -0.5); // P4 is purple
glEnd();
// White side - BACK
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
wglMakeCurrent(NULL, NULL);
SwapBuffers(g.hdc);
ReleaseDC(g.hwnd, g.hdc);
wglDeleteContext(g.hglrc);
}
g.hglrc = wglCreateContext(g.hdc);
Don't do that.
You don't create a rendering context every time you need to redraw the screen. You create it once; it should only go away when your window goes away.
Now, that doesn't necessarily justify why creating and destroying a rendering context would leave resources lying around. But that's irrelevant; you shouldn't do it because of performance. Rendering context creation and destruction is not a fast process, nor is it intended to be.

How do I make an open cube (missing one surface) without getting effect where sides are mirrored or switched in OpenGL?

I am trying to create a cube that has one surface missing (an open box in effect), but when I make it, the side opposite to the missing side gets super imposed on where the opening should be when the object is rotated.
Here is what I mean:
View of Front and back of Cube angled:
How do I get rid of the weird color overrides going on?
Here is my current code:
#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#include <stdlib.h>
#include <GL/glut.h>
void display();
void viewButtons();
double rotationY = 0;
double rotationX = 0;
void display()
{
//Clear the screen and clear z-buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Rotate when user changes rotate_x and rotate_y
glRotatef(rotationX, 1.0, 0.0, 0.0);
glRotatef(rotationY, 0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
//Vertices and colors
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5); // P1 is red
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.5, 0.5, -0.5); // P2 is green
glColor3f(0.0, 0.0, 1.0);
glVertex3f(-0.5, 0.5, -0.5); // P3 is blue
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-0.5, -0.5, -0.5); // P4 is purple
glEnd();
/*// White side - BACK
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();*/
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 1.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glEnd();
glFlush();
glutSwapBuffers();
}
void viewButtons(int button, int x, int y)
{
if (button == GLUT_KEY_RIGHT)
rotationY += 5;
else if (button == GLUT_KEY_LEFT)
rotationY -= 5;
else if (button == GLUT_KEY_UP)
rotationX += 5;
else if (button == GLUT_KEY_DOWN)
rotationX -= 5;
//update display
glutPostRedisplay();
}
int main(int argc, char **argv) {
// init GLUT and create window
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(720, 640);
glutCreateWindow("Room with objects");
// register callbacks
glutDisplayFunc(display);
glutSpecialFunc(viewButtons);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
You need to enable the depth test. Put this somewhere, in initialization, in your program:
glEnable(GL_DEPTH_TEST);

OpenGL rendering causes graphical glitch

Code is below. When the program runs, it generates a more or less correct image, but there are some graphical glitches. Here are some images:. Any ideas on what could be causing this? The weirdest part is that the issue appears to form on triangles, even though the image is being drawn with polygons (quads).
void buildList() {
cubelist = glGenLists(1);
glNewList(cubelist,GL_COMPILE);
// Multi-colored side - FRONT
glBegin(GL_POLYGON);
glColor3b(0,0,0);
glVertex3f( -0.5, -0.5, -0.5);
glVertex3f( -0.5, 0.5, -0.5);
glVertex3f( 0.5, 0.5, -0.5);
glVertex3f( 0.5, -0.5, -0.5);
// Black side - BACK
glBegin(GL_POLYGON);
glColor3b(0,0,0);
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glEnd();
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
glEndList();
}
void setupGL() {
glClearColor(1, 1, 1, 0);
glEnable(GL_DEPTH_CLAMP);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glCullFace(GL_FRONT_AND_BACK);
}
int main(int, char const**)
{
sf::Window window(sf::VideoMode(1600, 1200), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
//initWorld();
setupGL();
buildList();
while (running)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Process events
sf::Event event;
handleInput(window);
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
running = false;
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
running = false;
}
}
glCallList(cubelist);
// Update the window (c is a "Camera" object that just sets the perspective. I can provide code if necessary)
c.draw();
window.display();
}
return EXIT_SUCCESS;
}
EDIT: So changing the GL_POLYGONS statements to GL_QUADS seemed to fix the problem, but I'm still somewhat curious as to why.
Your first glBegin doesn't have a matching glEnd, this causes all the other glBegins to be ignored until the next glEnd. This means the front face leaked out to the back face.
That said immediate mode and the related displayList are deprecated, instead look into VBOs to store the vertex data.

Get camera inside my skybox

So I am working on a homework assignment and I have managed to create my skybox and it looks correct, the only problem is that my camera is outside the skybox.
I tried the command gluLookAt thinking maybe that would focus me into the box, but it didn't work. Here's the code that I have now. If anyone could let me know what I'm doing wrong it would be greatly appreciated:
gluLookAt(
0,0,0,
0,0,0
0,1,0);
glPushMatrix();
//load identity matrix
glLoadIdentity();
//update x, y and z rotation directions
glRotatef(currentRotation[1], 1.0, 0.0, 0.0); //x rotation
glRotatef(currentRotation[2], 0.0, 1.0, 0.0); //y rotation
glRotatef(currentRotation[3], 0.0, 0.0, 1.0); //z rotation
/*
//update scale of display
glScalef(currentScaling[1],
currentScaling[2],
currentScaling[3]);*/
//translate the image
glTranslatef(currentTranslation[1],
currentTranslation[2],
currentTranslation[3]);
/* Clear buffers */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D); // Enable texturing from now on
//front quadrant
glBindTexture(GL_TEXTURE_2D,frontTextureId); // select which texture to use
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(1.0 ,0.0); glVertex3f(-0.5, 0.5, 0.5);
glTexCoord2f(0.0 ,0.0); glVertex3f(0.5, 0.5, 0.5);
glTexCoord2f(0.0 ,1.0); glVertex3f(0.5, 0.5, -0.5);
glTexCoord2f(1.0 ,1.0); glVertex3f(-0.5, 0.5, -0.5);
glEnd();
//left quadrant
glBindTexture(GL_TEXTURE_2D, leftTextureId);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, 0.5, -0.5);
glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, -0.5, -0.5);
glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, -0.5, 0.5);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, 0.5, 0.5);
glEnd();
//back quadrant
glBindTexture(GL_TEXTURE_2D, backTextureId);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.5, -0.5, -0.5);
glTexCoord2f(1.0, 0.0); glVertex3f(0.5, -0.5, 0.5);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, 0.5);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5, -0.5);
glEnd();
//right quadrant
glBindTexture(GL_TEXTURE_2D, rightTextureId);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, -0.5);
glTexCoord2f(1.0, 0.0); glVertex3f(0.5, 0.5, 0.5);
glTexCoord2f(0.0, 0.0); glVertex3f(0.5, -0.5, 0.5);
glTexCoord2f(0.0, 1.0); glVertex3f(0.5, -0.5, -0.5);
glEnd();
//up quadrant
glBindTexture(GL_TEXTURE_2D, upTextureId);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, 0.5);
glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, 0.5, 0.5);
glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, 0.5);
glTexCoord2f(0.0, 1.0); glVertex3f(0.5, -0.5, 0.5);
glEnd();
//down quadrant
glBindTexture(GL_TEXTURE_2D, downTextureId);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, -0.5);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, 0.5, -0.5);
glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, -0.5);
glTexCoord2f(1.0, 0.0); glVertex3f(0.5, -0.5, -0.5);
glEnd();
Thank you for any help you can provide!
I think your problem is in the glulookat
try with this values
gluLookAt(0.0, 0.0 , 2.0 ,
0.0, 0.0, 0.0,
0.0, 1.0, 0.0);
Few problems at a glance: gluLookAt arguments: first three are camera position, next three are camera target - should not be the same, camera must have valid direction vector. Next, your skybox is only 1.0 unit size - make it bigger. Use a constant instead of 0.5 everywhere, that way you can control it in one place.
Well you do have a glTranslate() call in there, so unless you're passing it zeros, that's probably why you're not in the box.