纹理阵列访问不工作Opengl 3.3,GLSL 330

时间:2014-10-29 03:15:31

标签: opengl glsl opengl-3

我正在尝试使用2D纹理数组作为纹理图集。我有以下代码来渲染单个方块。如果我用简单的颜色输出替换片段着色器中的纹理查找,则呈现正方形,并且如果我使用并设置简单的2D纹理而不是2d纹理阵列,则它会正确渲染其中一个纹理。但是,在尝试创建和使用2d Texture阵列时,我只是得到一个黑屏。是什么给了什么?

//Shader Sources
const GLchar* vertexsource =
    "#version 330\n"
    "in vec2 position;"
    "in vec2 texcoord;"
    "out vec2 f_texcoord;"
    "void main() {"
    "   gl_Position = vec4(position, 0.0, 1.0);"
    "   f_texcoord = texcoord;"
    "}";
const GLchar* fragmentsource =
    "#version 330\n"
    "in vec2 f_texcoord;"
    "out vec4 color;"
    "uniform float time;"
    "uniform sampler2DArray sample;"
    "void main() {"
    "   float layer = step(sin(time), 0.0);"
    "   color = texture(sample, vec3(f_texcoord, 0.0));\n"
    "   //color = vec4(1.0,1.0,1.0,1.0);\n"
    "}";

//Vertex Data
GLfloat vertices[] = {
    //Verts         Texture verts
    -0.5f, 0.5f,    0.0f, 0.0f,
    0.5f, 0.5f,     1.0f, 0.0f,
    0.5f, -0.5f,    1.0f, 1.0f,
    -0.5f, -0.5f,   0.0f, 1.0f
};

GLuint elements[] = {
    0, 1, 2,
    2, 3, 0
};

Shaders::SetLogger(log_);
VertexShader vertexshader("D3 Vertex Shader", vertexsource);
FragmentShader fragshader("D3 Frag Shader", fragmentsource);
program = new ShaderProgram("D3 Shader Program");
program->AttachShader(vertexshader);
program->AttachShader(fragshader);
program->LinkProgram();
glUseProgram(program->glIdentifier());

GLint posattrib = program->GetAttributeLocation("position");
GLint texattrib = program->GetAttributeLocation("texcoord");
//Dont actually need this because only one Texture unit in use
GLint coloruniform = program->GetUniformLocation("sample");

GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

glEnableVertexAttribArray(posattrib);
glVertexAttribPointer(posattrib, 2, GL_FLOAT, GL_FALSE,
                      4 * sizeof(GLfloat),
                      (GLvoid*)0);

glEnableVertexAttribArray(texattrib);
glVertexAttribPointer(texattrib, 2, GL_FLOAT, GL_FALSE,
                      4 * sizeof(GLfloat),
                      (GLvoid*)(2 * sizeof(GLfloat)));

//glUniform1i(coloruniform, 0);

int width, height;
unsigned char* image = SOIL_load_image("sample.png", &width, &height,
                                       0, SOIL_LOAD_RGBA);

int width2, height2;
unsigned char* image2 = SOIL_load_image("sample2.png", &width2, &height2,
                                       0, SOIL_LOAD_RGBA);
errorstream << glGetError() << " ";

glActiveTexture(GL_TEXTURE0);
GLuint texture;
glGenTextures(1, &texture);
errorstream << glGetError() << " ";
glBindTexture(GL_TEXTURE_2D_ARRAY, texture);    
errorstream << glGetError() << " ";

glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 
             width, height, 2,
             0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)0);
errorstream << glGetError() << " ";
log_->Debug("Requested memory for texture");


glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 
                0, 0, 0, 
                width, height, 1,
                GL_RGBA, GL_UNSIGNED_BYTE, image);
errorstream << glGetError() << " ";
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 
                0, 0, 1, 
                width2, height2, 1, 
                GL_RGBA, GL_UNSIGNED_BYTE, image2);
errorstream << glGetError() << " ";


glUniform1i(coloruniform, 0);
errorstream << glGetError() << " ";
log_->Debug(errorstream.str());

实际绘图代码:

GLint timeuniform = program->GetUniformLocation("time");
GLint sampleuniform = program->GetUniformLocation("sample");
glActiveTexture(GL_TEXTURE0);
glUniform1f(timeuniform, frametimer_.RunningTime().asSeconds());
glUniform1i(sampleuniform, 0);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
window_->display();

编辑:在jozxyqk建议的修改中编辑

1 个答案:

答案 0 :(得分:3)

[ EDIT ]可能的原因:

  • 纹理/采样器没有正确绑定(见下文)。

  • 纹理为incomplete(另见this)。

    常见问题是GL默认使用mipmaps(大多数教程/示例代码都是在没有mipmaps的情况下启动的)。一个简单的解决方法是:glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    有关详细信息,请参阅GL4.5 specs, sec 8.17 "Texture Completeness "

      

    如果所有纹理图像和纹理参数都被认为是完整的纹理   需要使用纹理来进行纹理应用。

         

    ...

         

    使用前面的定义,纹理完成,除非满足以下条件:

         
        
    • 缩小过滤器需要一个mipmap(既不是NEAREST也不是LINEAR),纹理不是mipmap完成。
    •   
  • 纹理坐标错误(例如全部为零,第一个纹素恰好为黑色)。通过使用像素颜色的坐标,可以轻松验证。

  • 以上工作正常,图片中的数据只是黑色。

要绑定纹理,

  • 使用glActiveTexture

    致电GL_TEXTURE0 + texIndex

    在设置过程中不需要这样做,但在绘图之前选择要绑定的纹理单元。

    https://www.opengl.org/sdk/docs/man/docbook4/xhtml/glActiveTexture.xml

  • 致电glBindTexture

    glActiveTexture之后<{>} a related post on opengl.org

  • 设置采样器统一glUniform1i(samplerLocation, texIndex)←主要问题,我认为

    texIndex 0GL_TEXTURE01GL_TEXTURE1等。使用GL_TEXTURE0 + texIndex是一个方便的捷径。

    您的代码使用GL_TEXTURE0代替索引,0x84C033984