Hey there,
I’ve just got round to drawing a skybox with my little engine. I’m using this geometry (SQRT_3_3 is a define that equals to the value of the reciprocal square root of 3):
skybox::skybox(void) {
glGenBuffers(2, this->m_bufferIDs);
glBindBuffer(GL_ARRAY_BUFFER, this->m_bufferIDs[0]);
float vertices[24] = { // the idea is that all vectors have a length of 1 so that position can also be used as cubemap texture coords
-SQRT_3_3, -SQRT_3_3, -SQRT_3_3,
SQRT_3_3, -SQRT_3_3, -SQRT_3_3,
-SQRT_3_3, SQRT_3_3, -SQRT_3_3,
SQRT_3_3, SQRT_3_3, -SQRT_3_3,
-SQRT_3_3, -SQRT_3_3, SQRT_3_3,
SQRT_3_3, -SQRT_3_3, SQRT_3_3,
-SQRT_3_3, SQRT_3_3, SQRT_3_3,
SQRT_3_3, SQRT_3_3, SQRT_3_3
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->m_bufferIDs[1]);
byte indices[24] = {
1, 5, 7, 3, // positive x
2, 0, 4, 6, // negative x
4, 5, 7, 6, // positive y
0, 1, 3, 2, // negative y
0, 1, 5, 4, // positive z
3, 2, 6, 7 // negative z
};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}
Drawn like this:
void skybox::draw(math::vec3 *pos, math::mat3x3 *rot) {
glDisable(GL_DEPTH_TEST); // skybox should be drawn behind anything else
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->m_bufferIDs[1]);
glBindBuffer(GL_ARRAY_BUFFER, this->m_bufferIDs[0]);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawElements(GL_QUADS, 6 * 4, GL_UNSIGNED_BYTE, 0);
glEnable(GL_DEPTH_TEST);
}
Using a cube map which is created this way:
cubeMap::cubeMap(const char *name, byte *data, imageInfo_t *info)
: texture(name), m_width(info->width), m_height(info->height) {
// TODO: mipmapping
glGenTextures(1, (GLuint *)(&this->m_id));
glBindTexture(GL_TEXTURE_CUBE_MAP, this->m_id);
for (int i = 0; i < 6; i++) {
if (info->format == GL_RGBA8)
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0, info->format, info->width, info->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data + info->ofsCMSides[i]);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
With these shaders (vertex and fragment, respectively):
void main() {
gl_Position = ftransform();
gl_TexCoord[0].xyz = gl_Position;
}
uniform samplerCube tex0;
void main() {
vec3 cube = vec3(textureCube(tex0, gl_TexCoord[0].xyz));
gl_FragColor = vec4(cube, 1.0);
}
Here’s the problem. The skybox is drawn OK, the texture is there and everything appears correctly until the camera moves. What happens is that the geometry visibly moves, but the textures do not. I.e., they “slide” over the geometry in such a way that I always end up looking at the same side of the sky, regardless of my camera’s orientation. What am I doing wrong?