I'm attempting to generate a sky box for my OpenGL scene by using GLSL to texture quads. However, the sky box simply becomes black when I attempt to use the texture to generate the sky box color. The sky box works when I manually set the color, so I have basically narrowed the problem down to something being wrong with how I'm setting up the texture. I also messed around with a bunch of different eyeDirection vector values for the texture function, and I still get only a black square.
Here is my Fragment Shader:
#version 450 compatibility
layout(binding=0) uniform samplerCube currTexture;
smooth in vec3 eyeDirection;
out vec4 fragmentColor;
void main() {
fragmentColor = texture(currTexture, eyeDirection);
//fragmentColor = vec4(eyeDirection, 1.0);
}
Here is my vertex shader:
#version 450
uniform mat4 projection;
uniform mat4 modelView;
in vec4 aPosition;
smooth out vec3 eyeDirection;
void main() {
mat3 inverseModelView = inverse(mat3(modelView));
vec3 unprojected = (inverse(projection) * aPosition).xyz;
eyeDirection = inverseModelView * unprojected;
// eyeDirection = aPosition.xyz;
//gl_Position = aPosition.xyww;
gl_Position = new vec4(aPosition.x, aPosition.y, 1.0, aPosition.w );
}
Here is where I initialize the texture:
// initializes all the necessary texture values
void TrainView::initTextures() {
// loading in texture maps
SDL_Surface* xPos = IMG_Load("SkyBoxXpos.png");
SDL_Surface* xNeg = IMG_Load("SkyBoxXneg.png");
SDL_Surface* yPos = IMG_Load("SkyBoxYpos.png");
SDL_Surface* yNeg = IMG_Load("SkyBoxYneg.png");
SDL_Surface* zPos = IMG_Load("SkyBoxZpos.png");
SDL_Surface* zNeg = IMG_Load("SkyBoxZneg.png");
if (!xPos)
printf("IMG_Load: %s\n", IMG_GetError());
if (!xNeg)
printf("IMG_Load: %s\n", IMG_GetError());
if (!yPos)
printf("IMG_Load: %s\n", IMG_GetError());
if (!yNeg)
printf("IMG_Load: %s\n", IMG_GetError());
if (!zPos)
printf("IMG_Load: %s\n", IMG_GetError());
if (!zNeg)
printf("IMG_Load: %s\n", IMG_GetError());
// handle error
glGenTextures(1, &skyBoxTexture);
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_CUBE_MAP);
glBindTexture(GL_TEXTURE_CUBE_MAP, skyBoxTexture);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
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);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
// files are 24-bit bmp files
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL_RGBA, xPos->w, xPos->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, xPos->pixels);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL_RGBA, xNeg->w, xNeg->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, xNeg->pixels);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL_RGBA, yPos->w, yPos->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, yPos->pixels);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL_RGBA, yNeg->w, yNeg->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, yNeg->pixels);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL_RGBA, zPos->w, zPos->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, zPos->pixels);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL_RGBA, zNeg->w, zNeg->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, zNeg->pixels);
glBindTexture(GL_TEXTURE_CUBE_MAP, NULL);
}
Here is where I attempt to draw/render the sky box:
// handles everything for drawing the sky box
void TrainView::drawSkyBox(){
glUseProgram(skyBoxShader);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, skyBoxTexture);
int loc = glGetUniformLocation(skyBoxShader, "modelView");
GLfloat mvFl[16], projFl[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mvFl);
glUniformMatrix4fv(loc, 1, GL_FALSE, mvFl);
loc = glGetUniformLocation(skyBoxShader, "projection");
glGetFloatv(GL_PROJECTION_MATRIX, projFl);
glUniformMatrix4fv(loc, 1, GL_FALSE, projFl);
loc = glGetUniformLocation(skyBoxTexture, "currTexture");
glUniform1i(loc, 0);
// not sure if this is necessary or done as intended by opengl
GLuint sampler;
glGenSamplers(1, &sampler);
glBindSampler(0, sampler);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glBegin(GL_QUADS);
glVertex3f(-1.0, -1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(-1.0, 1.0, 0.0);
glEnd();
glUseProgram(NULL);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
}
glGenerateMipmap()in there. The sampler object you tried adding will probably do more damage than good. - Reto Koradi