簡體   English   中英

OepnGL 采樣 multitpe 紋理返回相同的紋理顏色

[英]OepnGL sampling multitpe textures returns same texture color

主程序

#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('\0');
        
        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);

兩者都返回相同的紋理顏色,但實際上與 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); 不起作用。

附: 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。 紋理函數

[...] 一些紋理函數(非“Lod”和非“Grad”版本)可能需要隱式導數。 在非均勻控制流和非片段着色器紋理提取中,隱式導數未定義。 [...]

分別為非均勻流量控制

查找兩個紋理並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); 
}

我想如果我有一個編譯好的多個紋理的例子,它可以正常工作,那么我可以找到我的代碼改變我的代碼行與示例進行比較的問題。 所以我找到了這個。

教程

示例代碼

它是基於 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)));

在我添加兩個字符“//”后,結果如我所願。 我不確定一行代碼出現問題的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM