OpenGL 采样多个纹理 returns 相同的纹理颜色

OepnGL sampling multitpe textures returns same texture color

main.cpp

#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glad/glad.h>
#include <fstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <SOIL/SOIL.h>

typedef struct vertex{
    float x; float y; float z;
    float texX; float texY;
    float texIndex;
} vertex;

void readTextFile(const char* path, std::string* dst);

int main(){
    std::cout << 3 << std::endl;
    
    GLFWwindow* window;
    
    if(!glfwInit())
        return -1;
    
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    
    window = glfwCreateWindow(640, 480, "Geometry Shader Bathing with Texture Atlas", NULL, NULL);
    if(!window){    
        glfwTerminate();
        return -1;
    }
    
    glfwMakeContextCurrent(window);
    
    gladLoadGL();
    
    glfwSwapInterval(1);
    glFrontFace(GL_CW);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    
    //create and set vertex buffer
    vertex vertices[] = {
        -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
        -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
         0.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        
         0.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
        -1.0f,  0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
         0.0f,  0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
        
         0.0f,  0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
         0.0f,  1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
         1.0f,  0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
        
         1.0f,  0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
         0.0f,  1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
         1.0f,  1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
    };
    
    GLuint vertexBuffer;
    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
    
    //create vertex shader
    std::string* shaderSource = new std::string();
    
    readTextFile("shader/vertexShader.glsl", shaderSource);
    const char* vertexShaderSourcePtr = shaderSource->c_str();
    
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSourcePtr, NULL);
    glCompileShader(vertexShader);

    GLint status;
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);
    if(status == GL_FALSE){
        char buffer[512];
        glGetShaderInfoLog(vertexShader, 512, NULL, buffer);
        std::cout << "vertexShader FAIL : " << std::endl <<
            shaderSource <<std::endl 
            << buffer << std::endl;
    }else{
        std::cout << "vertexShader compile success!" << std::endl;
    }
    
    //create fragment shader
    readTextFile("shader/fragmentShader.glsl", shaderSource);
    const char* fragmentShaderSourcePtr = shaderSource->c_str();
    
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSourcePtr, NULL);
    glCompileShader(fragmentShader);
    
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &status);
    if(status == GL_FALSE){
        char buffer[512];
        glGetShaderInfoLog(fragmentShader, 512, NULL, buffer);
        std::cout << "fragmentShader FAIL : " << std::endl <<
            shaderSource <<std::endl 
            << buffer << std::endl;
    }else{
        std::cout << "fragmentShader compile success!" << std::endl;
    }
    
    delete shaderSource;
    
    //create and set program
    GLint program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);
    
    glBindAttribLocation(program, 0, "inPos");
    glBindAttribLocation(program, 1, "inTex");
    glBindAttribLocation(program, 2, "inTexIndex");
    
    glLinkProgram(program);
    //set frameBuffer(renderTarget) index and fragmentShader output name
    glBindFragDataLocation(program, 0, "outColor"); 
    glUseProgram(program);
    
    //create and set texture
    int width = 384;
    int height = 384;
    unsigned char* textureRawData0 = SOIL_load_image("resource/character/test0.png", &width, &height, 0, SOIL_LOAD_RGBA);
    unsigned char* textureRawData1 = SOIL_load_image("resource/character/test1.png", &width, &height, 0, SOIL_LOAD_RGBA);
    
    glUniform1i(glGetUniformLocation(program, "texture0"), 0);
    glUniform1i(glGetUniformLocation(program, "texture1"), 1);
    
    GLuint texture1;
    glGenTextures(1, &texture1);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture1);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureRawData0);
    SOIL_free_image_data(textureRawData0);
    
    std:: cout << "texture0: " << glGetUniformLocation(program, "texture0") << std::endl;
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    GLuint texture2;
    glGenTextures(1, &texture2);
    glActiveTexture(GL_TEXTURE0 + 1);
    glBindTexture(GL_TEXTURE_2D, texture2);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureRawData1);
    SOIL_free_image_data(textureRawData1);

    std:: cout << "texture1: " << glGetUniformLocation(program, "texture1")  << std::endl;
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    
    
    //input layout : vertex Attribute
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    
    
    glLinkProgram(program);
    
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), 0);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)(5 * sizeof(float)));
    
    
    while(!glfwWindowShouldClose(window)){
        int width, height;
        glfwGetFramebufferSize(window, &width, &height);
        
        glViewport(0, 0, width, height);
        glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        
        glDrawArrays(GL_TRIANGLES, 0, 12);
        
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    
    glfwTerminate();
    return 0;
}

void readTextFile(const char* path, std::string* dst){
    std::ifstream f;
    f.open(path);
    
    if(f.is_open()){
        std::string buff;
        dst->clear();
        while(!f.eof()){
            std::getline(f, buff);
            dst->append(buff);
            dst->push_back('\n');
        }
        
        dst->push_back('[=11=]');
        
        f.close();
    }else{
        std::cout << "파일 열기 실패 : " << *dst << std::endl;
    }
}

ps

#version 330 core

in vec2 outTex;
in float outTexIndex;
out vec4 outColor;

uniform sampler2D texture0;
uniform sampler2D texture1;

void main(){
    if(outTexIndex == 0.0f){
        outColor = texture(texture0, outTex);
    }

    else if(outTexIndex == 1.0f){
        outColor = texture(texture1, outTex) + vec4(0.5f, 0.5f, 0.5f, 1.0f);
    }
}

输入布局(顶点属性)工作正常。 TexIndex 已正确传输。 1.0f 和 0.0f

但问题是

//片段着色器

texture(texture0, outTex);

texture(texture1, outTex);

两者 returns 相同的纹理颜色,但它实际上是与 test1.png 和 test2.png 不同的纹理。我的代码有什么问题?

++ 我按照 @Rabbid76

的回答尝试了下面的片段着色器
#version 330 core

in vec2 outTex;
in float outTexIndex;
out vec4 outColor;

uniform sampler2D texture0;
uniform sampler2D texture1;

void main()
{
    vec4 c1 = texture(texture0, outTex) + vec4(0.5f, 0.0f, 0.0f, 1.0f);
    vec4 c2 = texture(texture1, outTex) + vec4(0.0f, 0.5f, 0.0f, 1.0f);
    outColor = mix(c1, c2, outTexIndex); 
}

已应用颜色但仍从相同纹理采样。看起来有点像 glActiveTexture(i);不起作用。

ps。 test1.png 和 test2.png 是完全不同的画面。上面的截图是 test1.png

++当我变了

glGenTexture(1, &texture0);
glGenTexture(1, &texture1);

glGenTexture(0, &texture0);
glGenTexture(0, &texture1);

然后 sampler2D texture0 和 sampler2D texture1 都从 texture1 test1.png 采样(不是形成 texture0,它是 test0.png)

发生了什么事?

++

#version 330

in vec3 inPos;
in vec2 inTex;
in float inTexIndex;

out vec2 outTex;
out float outTexIndex;

void main(){
    outTex = inTex;
    gl_position = vec4(inPos, 1.0f);
    outTexIndex = inTexIndex;
}

由于条件 if(outTexIndex == 0.0f) 取决于片段着色器输入,这可能会导致未定义的行为。

查看(最近的)OpenGL Shading Language 4.60 Specification - 8.9. Texture Functions

[...] Some texture functions (non-“Lod” and non-“Grad” versions) may require implicit derivatives. Implicit derivatives are undefined within non-uniform control flow and for non-fragment shader texture fetches. [...]

分别Non-uniform flow control.

查找纹理和 mix 颜色以解决问题:

#version 330 core

in vec2 outTex;
in float outTexIndex;
out vec4 outColor;

uniform sampler2D texture0;
uniform sampler2D texture1;

void main()
{
    vec4 c1 = texture(texture0, outTex);
    vec4 c2 = texture(texture1, outTex) + vec4(0.5f, 0.5f, 0.5f, 1.0f);
    outColor = mix(c1, c2, outTexIndex); 
}

我想如果我有一个工作正常的多个纹理的编译示例,那么我可以发现我的代码将我的代码行更改为与示例比较的行的问题。所以我找到了这个。

tutorial

example code

它是基于 SDL 的。所以我更改了初始化代码并循环到glfw。它在我的环境中运行良好。

即使我将代码的纹理加载部分更改为与示例完全相同,问题仍未解决。所以一定是其他部分造成了问题。终于找到这个了

//input layout : vertex Attribute
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

//*****here!*****
//glLinkProgram(program);    
    
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE, sizeof(vertex), (void*)(5 * sizeof(float)));

我只加了两个字符“//”后,结果如我所愿。 我不确定一行代码出问题的原因。