提问者:小点点

多纹理渲染(glActiveTexture beyong GL_TEXTURE0)不工作[重复]


我一直在尝试使用sampler2D array+glActiveTexture()向我的着色器发送多个纹理。 与GL_TEXTURE0&一起发送的第一个纹理; 索引为0的sampler2D数组效果良好。 然而,第二个纹理不渲染(重复第一个纹理)。 我一直在网上搜索一个解决方案,到目前为止没有任何效果。 所以,我来了。

如果你能帮个忙,我们将不胜感激。 如果你需要其他信息,请告诉我。

下面是我尝试调试的一些事情。

  1. 切换枚举GL_TEXTURE0和GL_TEXTURE1导致只呈现以前的第二个纹理(head2.png)。 这使我相信纹理文件解析工作正常。
  2. glActiveTexture后的glenable(GL_TEXTURE_2D)无法解决此问题。
  3. 确保我作为浮动发送的纹理ID抓取了正确的索引。 例如,color=aa[int(tid)],其中aa是具有不同颜色的vec4的数组。
  4. 即使我在着色器中硬编码索引,它也不会显示第二个纹理。 事实上,所有32个指数都会始终呈现第一纹理。

下面是我的主文件和使用的一些方法。

  • main.cxx
// C++
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <stdlib.h>

// OpenGL
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <FreeImage.h>

// Custom
#include "window.h"
#include "math.h"
#include "shader.h"

#include "texture.h"

using namespace std;
using namespace JDEngine;


int main()
{
    Window window("Test", 800, 800);
    window.SetClearColor(0.0, 0.0, 0.0, 1.0);


    Shader* shader =  new Shader("src/shaders/testvert.shader", "src/shaders/testfrag.shader");

    shader->Enable();
    shader->SetUniformMat4("pr_matrix", Mat4::Orthographic(-4,4,-4,4,-4,4));
    shader->Disable();


    GLfloat data[24] = {0,0,0,0,1,0,1,0,0,1,1,0, -1,-1,0,-1,0,0,0,-1,0,0,0,0};
    GLfloat uv[16] = {0,0,0,1,1,0,1,1, 0,0,0,1,1,0,1,1};
    GLfloat tid[8] = {0,0,0,0, 1,1,1,1}; // 0s going into GL_TEXTURE0, 1s to GL_TEXTURE1
    GLuint ind[12] = {0,1,2,2,1,3, 4,5,6,6,5,7};


    // For buffers & vertices
    GLuint vao, vbo, vbouv, vbotid, ibo;
    Texture* tex1 = new Texture("data/body2.png"); // yellow square
    Texture* tex2 = new Texture("data/head2.png"); // blue square
    cout << endl << tex1->GetTID() << " " << tex2->GetTID(); // spits out "1 2"

    glGenVertexArrays(1, &vao);    // Vertex Array Object
    glBindVertexArray(vao);

    glGenBuffers(1, &vbo);         // Vertex Buffer Object
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);


    glGenBuffers(1, &vbouv);         // Vertex Buffer Object
    glBindBuffer(GL_ARRAY_BUFFER, vbouv);
    glBufferData(GL_ARRAY_BUFFER, sizeof(uv), uv, GL_STATIC_DRAW);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);
    glEnableVertexAttribArray(1);
    glBindBuffer(GL_ARRAY_BUFFER, 0);


    glGenBuffers(1, &vbotid);         // Vertex Buffer Object
    glBindBuffer(GL_ARRAY_BUFFER, vbotid);
    glBufferData(GL_ARRAY_BUFFER, sizeof(tid), tid, GL_STATIC_DRAW);
    glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(GLfloat), 0);
    glEnableVertexAttribArray(2);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindVertexArray(0);

    glGenBuffers(1, &ibo);         // Index Buffer Object
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(ind), ind, GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);




    //glEnable(GL_DEPTH_TEST);
    while (!window.ShouldClose())
    {
        window.Clear();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // *********************************************************************
        shader->Enable();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, tex1->GetTID());

        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, tex2->GetTID());

        glBindVertexArray(vao);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
        glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_INT, nullptr); // seeing two yellow squares (GL_TEXTURE0 worked but not 1)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
        // *********************************************************************
        window.Update();
    }
    window.Close();
    return 0;
}

  • texture.cxx
#include "texture.h"
namespace JDEngine
{
    // ********************************************************************
    Texture::Texture(const string& filename)
    : mFileName(filename)
    {
        mTID = Load();
    }
    // ********************************************************************
    GLuint Texture::Load()
    {
        BYTE* pixels = LoadImage(mFileName.c_str(), &mWidth, &mHeight);

        GLuint result;
        glGenTextures(1, &result);
        glBindTexture(GL_TEXTURE_2D, result);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, mWidth, mHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
        glBindTexture(GL_TEXTURE_2D, 0);

        free(pixels);

        return result;
    }
    // ********************************************************************
}

  • TestFrag.Shader
#version 330 core

layout (location = 0) out vec4 color;

uniform sampler2D textures[32];

in DATA
{
    vec4 pos;
    vec2 uv;
    float tid;
} fs_in;

void main()
{
    color = texture(textures[int(fs_in.tid + 0.5)], fs_in.uv);
}


共1个答案

匿名用户

textures[int(fs_in.tid+0.5)]是未定义的行为,因为textures类型为Sampler2D,且fs_in.tid是片段着色器输入,不是动态统一的表达式。

参见OpenGL着色语言4.60规范-4.1.7。 不透明类型

纹理组合采样器类型是不透明类型,其声明和行为与上面对不透明类型的描述相同。 当聚合到着色器内的数组中时,只能使用动态统一的整数表达式对它们进行索引,否则结果将未定义。 [...]

我建议使用Sampler2Darray(请参阅采样器)而不是Sampler2Darray数组。
当您使用Sampler2Darray时,您根本不需要任何索引,因为“索引”在纹理查找时编码在纹理坐标的第三个分量中(请参阅texture)。