PDA

View Full Version : Skybox cubemap problem



sam_thedancer
08-25-2015, 08:37 AM
Greetings:
I am trying to set up a cubemap skybox in fixed-function. Code is below. All I see are white and black rectangles - textures don't seem to draw at all. I know my getbmp function to read in the 6 cube textures is ok, because I tried it in a different (regular texture, not cubemap) program.
Btw, the texture coords are just a test to see one face of the cube (I know they need to be rotated for a practical skybox).

Any pointers as to what I am doing wrong?

Thanks in advance,
Sam


void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);

glGenTextures(1, &texture);

glEnable(GL_TEXTURE_CUBE_MAP);

BitMapFile *image[6];

image[0] = getbmp("../Textures/ForestCubemapTextures/posx.bmp");
image[1] = getbmp("../Textures/ForestCubemapTextures/negx.bmp");
image[2] = getbmp("../Textures/ForestCubemapTextures/posy.bmp");
image[3] = getbmp("../Textures/ForestCubemapTextures/negy.bmp");
image[4] = getbmp("../Textures/ForestCubemapTextures/posz.bmp");
image[5] = getbmp("../Textures/ForestCubemapTextures/negz.bmp");

glBindTexture(GL_TEXTURE_CUBE_MAP, texture);

glTexImage2D(GL_TEXTURE_CUBE_MAP, 0, GL_RGBA, 2048, 2048, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

for (int face = 0; face < 6; face++)
{
int target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
glTexSubImage2D(target, 0, 0, 0, 2048, 2048, GL_RGBA, GL_UNSIGNED_BYTE, image[face]);
}
glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
}

void drawscene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glBindTexture(GL_TEXTURE_CUBE_MAP, texture);
glBegin(GL_POLYGON);
glTexCoord3f(-1.0, -1.0, -1.0); glVertex3f(-100.0, -100.0, -99.0);
glTexCoord3f(1.0, -1.0, -1.0); glVertex3f(100.0, -100.0, -99.0);
glTexCoord3f(1.0, 1.0, -1.0); glVertex3f(100.0, 100.0, -99.0);
glTexCoord3f(-1.0, 1.0, -1.0); glVertex3f(-100.0, 100.0, -99.0);
glEnd();

glutSwapBuffers();
}

bobtedbob
08-28-2015, 12:18 AM
Try replacing this



glTexImage2D(GL_TEXTURE_CUBE_MAP, 0, GL_RGBA, 2048, 2048, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

for (int face = 0; face < 6; face++)
{
int target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
glTexSubImage2D(target, 0, 0, 0, 2048, 2048, GL_RGBA, GL_UNSIGNED_BYTE, image[face]);
}


with this



for (int face = 0; face < 6; face++)
{
int target = GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
glTexImage2D(target, 0, GL_RGBA, 2048, 2048, 0, GL_RGBA, GL_UNSIGNED_BYTE,image[face]);
}

sam_thedancer
08-28-2015, 10:05 AM
Thanks, bobtedbob.
Still no joy but I see your suggestion makes sense because glTexImage2D(GL_TEXTURE_CUBE_MAP, ...) is not even legal.

If someone has fixed-function cubemap skybox code which they might be willing to share that would be highly appreciated.

bobtedbob
08-29-2015, 11:05 PM
Some more things

you need disable depth testing/masking for skyboxes (as you need to always be inside the cube). Also used a CW ordering because you are inside the cube and you want the inside draw not the outside)

For me I use GL_TRIANGLES and I set the uv coordinates to be the same as the vertex. Here are the coordinates I use, you should be able to convert this to fixed pipeline easily



// 7---6
// 3-|-2 |
// |-4--|5
// 0---1
float[] vertexArray = new float[]
{
-size, -size, size, // front bottom left
size, -size, size, // front bottom right
size, size, size, // front top right
-size, size, size, // front top left


-size, -size, -size,//back bottom left
size, -size, -size, //back bottom right
size, size, -size, // back top right
-size, size, -size // back top left
};


float[] uvArray = new float[]
{
-size, -size, size, // front bottom left
size, -size, size, // front bottom right
size, size, size, // front top right
-size, size, size, // front top left


-size, -size, -size,//back bottom left
size, -size, -size, //back bottom right
size, size, -size, // back top right
-size, size, -size // back top left
};

sam_thedancer
08-30-2015, 02:20 PM
Thanks again for the help.
Finally got it working. Took many more hours than I thought it would:-)