1

So I am having this assignment which requires to create a skybox; the texture is provided as a single file. Bear in mind, I am very slow when it comes to understanding OpenGL, and this particular SkyBox has been driving me insane. I read up on every little thing on the internet that I thought could relate to my issue, but I either can't comprehend what is going on, which is not what I am after since I want to know what I am doing and not copy/paste some code, or the result ends up being something different.

The code relating to the skybox so far:

GLfloat cubeVertexData[108] =
{
    0.5f, -0.5f, -0.5f,
    0.5f, 0.5f, -0.5f,
    0.5f, -0.5f, 0.5f,
    0.5f, -0.5f, 0.5f,
    0.5f, 0.5f, -0.5f,
    0.5f, 0.5f, 0.5f,

    0.5f, 0.5f, -0.5f,
    -0.5f, 0.5f, -0.5f,
    0.5f, 0.5f, 0.5f,
    0.5f, 0.5f, 0.5f,
    -0.5f, 0.5f, -0.5f,
    -0.5f, 0.5f, 0.5f,

    -0.5f, 0.5f, -0.5f,
    -0.5f, -0.5f, -0.5f,
    -0.5f, 0.5f, 0.5f,
    -0.5f, 0.5f, 0.5f,
    -0.5f, -0.5f, -0.5f,
    -0.5f, -0.5f, 0.5f,
    //4
    -0.5f, -0.5f, -0.5f,
    0.5f, -0.5f, -0.5f,
    -0.5f, -0.5f, 0.5f,
    -0.5f, -0.5f, 0.5f,
    0.5f, -0.5f, -0.5f,
    0.5f, -0.5f, 0.5f,
    //5
    0.5f, 0.5f, 0.5f,
    -0.5f, 0.5f, 0.5f,
    0.5f, -0.5f, 0.5f,
    0.5f, -0.5f, 0.5f,
    -0.5f, 0.5f, 0.5f,
    -0.5f, -0.5f, 0.5f,
    //6
    0.5f, -0.5f, -0.5f,
    -0.5f, -0.5f, -0.5f,
    0.5f, 0.5f, -0.5f,
    0.5f, 0.5f, -0.5f,
    -0.5f, -0.5f, -0.5f,
    -0.5f, 0.5f, -0.5f
};

GLfloat textureCoordsSkyBox[72] = {
    //face 1
    0.75,0.33,      //    0,1,
    0.75,0.67,     //    1,1,
    0.5,0.33,     //    0,0,
    0.5,0.33,     //    0,0,
    0.75,0.67,    //    1,0,
    0.5,0.67,   //    1,1,

    //face 2
    0.5,1.0, //    1,1,
    0.25,1, //    0,1,
    0.5,0.67, //    1,0,
    0.5,0.67, //    1,0,
    0.25,1.0, //    0,1,
    0.25,0.67, //    1,1,
    //face 3
    0,0.67,//    1,1,
    0,0.33,//    0,1,
    0.25,0.67,//    1,0,
    0.25,0.67,//    1,0,
    0,0.33,//    0,1,
    0.25,0.33,//    0,0,
    //face 4
    0.25,0.0,//    0,1,
    0.5,0.0,//    1,1,
    0.25,0.33,//    0,0,
    0.25,0.33,//    0,0,
    0.5,0.0,//    1,1,
    0.5,0.33,//    0,0,
    //face 5
    0.5,0.67,//    1,0,
    0.25,0.67,//    0,0,
    0.5,0.33,//    1,1,
    0.5,0.33,//    1,1,
    0.25,0.67,//    0,0,
    0.25,0.33,//    0,1,
    //face 6
    0.75,0.33,//    1,1,
    1.0,0.33,//    0,1,
    0.75,0.67,//    1,0,
    0.75,0.67,//    1,0,
    1.0,0.33,//    0,1,
    1.0,0.67//    0,0
};

GLfloat gCubeVertexdataNormals[108] =
{
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,
    1.0f, 0.0f, 0.0f,

    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,
    0.0f, 1.0f, 0.0f,

    -1.0f, 0.0f, 0.0f,
    -1.0f, 0.0f, 0.0f,
    -1.0f, 0.0f, 0.0f,
    -1.0f, 0.0f, 0.0f,
    -1.0f, 0.0f, 0.0f,
    -1.0f, 0.0f, 0.0f,

    0.0f, -1.0f, 0.0f,
    0.0f, -1.0f, 0.0f,
    0.0f, -1.0f, 0.0f,
    0.0f, -1.0f, 0.0f,
    0.0f, -1.0f, 0.0f,
    0.0f, -1.0f, 0.0f,

    0.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f,
    0.0f, 0.0f, 1.0f,

    0.0f, 0.0f, -1.0f,
    0.0f, 0.0f, -1.0f,
    0.0f, 0.0f, -1.0f,
    0.0f, 0.0f, -1.0f,
    0.0f, 0.0f, -1.0f,
    0.0f, 0.0f, -1.0f
};

void DrawSkyBox() {
    glFrontFace(GL_CW); 

    glBindTexture(GL_TEXTURE_2D, textures[SKYIMAGE]);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, 0, gCubeVertexdataNormals);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, cubeVertexData);

    glTexCoordPointer(2, GL_FLOAT, 0, textureCoordsSkyBox);

    glDrawArrays(GL_TRIANGLES, 0, 36);

    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

Loading texture [part of code missing as I have different textures for other elements loaded here]:

.....
#define SKYIMAGE 5 //sky image 
const char *textureFiles[TEXTURE_COUNT] = { "stormydays.tga".... };

I am not sure if it is of importance, but I will also mention that the other textured elements of the scene do not have coordinates from 0-1, but from 100 to 1000. I tried previously to set up the textureCoordsSkyBox to 1000 in terms of coordinates, but still no result.

I imagine I do something completely wrong when it comes to the binding of the texture, but I am having a hard time comprehending how I can improve that part. Any advice would be deeply appreciated. Thank you in advance.

-Edit-

So this is the texture I am using:

What I am seeing is just black, besides the other elements of the scene like the back wall, some grass and some flowers [this is what I was referring to when I said textured elements of the scene]. I am attaching the code here of what part of my drawing the scene function looks like:

void RenderScene(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);

    gluLookAt(cameraX, cameraY, cameraZ,//eye
    50.00, 90.00, 50.00,//centre
    0.00, 1000.00, 0.00);//up

    glPushMatrix();

    DrawSkyBox();

    glPopMatrix();
    glPushMatrix();
    glTranslatef(0.0, 0.0, -100.0);
    glRotatef(-90.0, 1.0, 0.0, 0.0);

    drawTexturedSurface(IMAGE1); //grass

    glPopMatrix();

    glPushMatrix();
    glTranslatef(0.0, 0.0, -350.0);

    drawTexturedSurface(IMAGE4); //front stone wall

    glPopMatrix();
    glPushMatrix();
    .......
    glPopMatrix();

    drawGUI();
    glPushMatrix();

    glutSwapBuffers();
}

Also, it is the fixed version of OpenGL. I tried messing about with GL_LIGHTING, GL_CULL_FACE, GL_DEPTH_TEST and glDisableClientState but it is the same result. Also, I don't get any errors.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Kimera
  • 13
  • 6
  • what exactly is the output now? do you see the cube? or nothing? what is the texture? I see your texture coordinates are not `<0.0,1.0>` so you got whole skybox in single texture? you should post the texture image here so we can see. What GL version you are using (fixed function or shaders? if shaders where they are? I assume fixed function) there might be a lot of going wrongly like wrong cube size in respect to your projection matrix and or depth range, wrong lighting, wrong winding, wrong texture loader etc ... try disable `GL_LIGHTING,GL_CULL_FACE,GL_DEPTH_TEST` if it make a difference – Spektre Jan 04 '19 at 11:28
  • what is `glGetError` saying? Try and get back to us with comment notification and or question edit ... btw there are also different approaches like `CUBE_MAP` or procedural skybox etc ... take a look at this: [swift sphere combine star data](https://stackoverflow.com/a/40171880/2521214) – Spektre Jan 04 '19 at 11:29
  • also I see `glDisableClientState` Its been years I used this api so I am not sure if `activate` is needed or not ... maybe you just miss that What do you mean by texture elements? texture ids are `1,2,3...` texture coordinates are `<0.0,1.0>` if your texture matrix is not scaled (hope you are reseting it to unit texture) – Spektre Jan 04 '19 at 11:36
  • 1
    Edited the post with the information required. – Kimera Jan 04 '19 at 13:46

1 Answers1

1

In your case the skybox is a very tiny object in the center of the world. You have to draw the skybox in at the position of the camera. When you darw the sykbox, then you have to disable the depth test, this cause that the depth buffer is not written and all other parts of the scene cover the skybox. Further two-dimensional texturing has to be enabled by glEnable(GL_TEXTURE_2D), before the skybox geometry is drawn:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);

gluLookAt(
    cameraX, cameraY, cameraZ,//eye
    50.00, 90.00, 50.00,//centre
    0.00, 1000.00, 0.00);//up

glDisable(GL_DEPTH_TEST);
glPushMatrix();   
glTranslatef(cameraX, cameraY, cameraZ); // model transformation to the camera position
glEnable(GL_TEXTURE_2D);
DrawSkyBox();
glPopMatrix();
glEnable(GL_DEPTH_TEST);
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • I translated to the original values I give cameraX, cameraY and cameraZ, but it was still black. I have a different function which allows me to move around the scene with the camera a bit. I did and I can see that behind the scene there is a very tiny flickering blue dot. I assume that is my skybox. I tried to bring it forward by messing with the translatef function but to no avail. I just have one question remaining, as now I am outside of the scope for my original question. Is gluPerspective(75.0f, fAspect, 1.0, 1000.0); the reason for this nonconsistency between position and camera? – Kimera Jan 05 '19 at 02:09
  • 1
    @Kimera No, It is the view matrix `gluLookAt`. Your skybox is probably clipped by the near plane of the perspective projection, because 1.0 is to far. Note the size of the skybox verticx coordinates is 0.5 Try `gluPerspective(75.0f, fAspect, 0.1, 1000.0);` - 0.1 instead of 1.0 – Rabbid76 Jan 05 '19 at 08:28
  • 1
    @Kimera There is also another way to do this obtain your `MODELVIEW` matrix with `double m[16]; glGetDoublev(GL_MODELVIEW_MATRIX,m);` set the position of `m` to zero (`m[12],m[13],m[14]=0.0`) and use that as `MODELVIEW` matrix for the skybox (`glLoadMatrix`) ... no need to `gluLookAt` again nor `glTranslate` and yes the znear plane is cutting your skybox off so either change your skybox size to fit or move the znear below `0.5` – Spektre Jan 05 '19 at 09:04
  • @Rabbid76, Spektre Thank you very much. It is finally showing. The only issue now is that is a simple color not textured [problem which I couldn't see before since it wasn't showing up], so I am going to mess around with the code more, see why that is. Thank you very much once again. – Kimera Jan 05 '19 at 14:19
  • @Kimera Do you `glEnable(GL_TEXTURE_2D)`? – Rabbid76 Jan 05 '19 at 20:49
  • 1
    @Kimera yep single color can mean either you forgot to enable `GL_TEXTURE_2D` or forgot to bind your texture or have wrongly loaded your texture from file to GL or have no or wrong texture coordinates or have wrong `GL_TEXTURE_MATRIX` (did you set it to unit with `glLoadIdentity()` ?) also some old GL implementations and I think aslso GL ES do not suport non power of 2 texture sizes but more likely is that you hit your max resolution of the texture some devices can not handle more than 2048x2048 ... – Spektre Jan 05 '19 at 21:33
  • 2
    @Spektre, @ Rabbid76 The problem was that I forgot to enable GL_TEXTURE_2D.... Please do pardon my idiocy, I can't imagine how I forgot that. Thank you very much for the great help, my skybox is perfectly fine now. – Kimera Jan 06 '19 at 04:33
  • 1
    @Kimera good to know your issue is solved :) as this Answer lead to solution you should accept it... btw notification works only for single user but IIRC author of the post you're commenting in is notified automatically ... but sometimes the notify engine do not work correctly ... – Spektre Jan 06 '19 at 08:22
  • 1
    @Kimera accepting answer is done by clicking on the checker near the Vote score of an answer... – Spektre Jan 07 '19 at 08:10