GLSL纹理功能仅为Sky Box返回黑色

时间:2015-05-04 00:50:46

标签: c++ visual-studio opengl glsl

我正在尝试使用GLSL为纹理四边形生成我的OpenGL场景的天空盒。但是,当我尝试使用纹理生成天空盒颜色时,天空盒子变成黑色。当我手动设置颜色时,天空盒工作,所以我已经基本上将问题缩小到我设置纹理的错误。我还为纹理函数弄乱了一堆不同的eyeDirection矢量值,我仍然只得到一个黑色方块。

这是我的片段着色器:

#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);
}

这是我的顶点着色器:

#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 );
} 

这是我初始化纹理的地方:

// 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);

}

这是我尝试绘制/渲染天空盒的地方:

// 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);
}

1 个答案:

答案 0 :(得分:2)

您在此处为立方体纹理启用了mipmap采样:

glTexParameteri(GL_TEXTURE_CUBE_MAP,
                GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

但是,您没有为纹理创建mipmap。在spec语言中,这意味着你的纹理不是“mipmap完成”,这反过来使它不是“纹理完整”。对不完整纹理进行采样的结果是 BLACK

创建mipmap的最简单方法是在指定纹理数据后调用glGenerateMipmap() ,即在所有glTexImage2D()调用6个边之后:

glGenerateMipmap(GL_TEXTURE_CUBE_MAP);

如果您不需要mipmap,您只需将filter参数的值设置为不使用mipmap:

glTexParameteri(GL_TEXTURE_CUBE_MAP,
                GL_TEXTURE_MIN_FILTER, GL_LINEAR);

您尝试创建采样器对象的操作在这里没有帮助。如果要使用多个不同的采样属性相同纹理进行采样,则采样器对象非常有用。假设您想在同一个着色器中同时使用GL_LINEARGL_NEAREST采样相同的纹理,可以通过为此纹理创建两个采样器对象来实现。我不认为这是一个非常常见的用例,但有些情况下它会派上用场,而且在引入采样器对象之前是不可能的。