Как исправить эту проблему с несколькими текстурами в Opengl - PullRequest
1 голос
/ 17 октября 2019

Я хочу отправить несколько текстур во фрагментный шейдер и затем смешать их там. Но по какой-то причине, когда я запускаю этот код, показывается только texture2. Здесь, в моем фрагментном шейдере, даже если я просто пишу color = color1;чем также я вижу texure2.

Мой фрагментный шейдер:

    static const char* fragmentShaderSource = "\n\
    #version 150 core\n\
    in vec2 Texcoord;\n\
    uniform sampler2D tex0;\n\
    uniform sampler2D tex1;\n\
    out vec4 color;\n\
    void main(){\n\
        vec4 color1=texture(tex0,Texcoord);\n\
        vec4 color2=texture(tex1,Texcoord);\n\
        color=mix(color1,color2,0.5);\n\
        //color=color1;\n\
    }\n\
    ";

Мой код для нескольких текстур:

    GLuint textures[2];
    glGenTextures(2, textures);
    int width, height;
    unsigned char* image;

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE0, textures[0]);
    image = SOIL_load_image("1.png", &width, &height, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    glUniform1i(glGetUniformLocation(program, "tex0"), 0);
    SOIL_free_image_data(image);

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


    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE1, textures[1]);
    image = SOIL_load_image("2.png", &width, &height, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    glUniform1i(glGetUniformLocation(program, "tex1"), 1);
    SOIL_free_image_data(image);
    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);

И это мой цикл окна:

    while (!glfwWindowShouldClose(window)) {
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        if (glfwGetKey(window, GLFW_KEY_E)) {
            break;
        }
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

Это весь мой код. При необходимости:

#define GLEW_STATIC
#include<gl\glew.h>
#include<GLFW\glfw3.h>
#include<iostream>
#include<SOIL.h>
//////////////////////////////////////
static const char* vertexShaderSource = "\n\
    #version 150 core\n\
    in vec2 position;\n\
    in vec3 inColor;\n\
    in vec2 texcoord;\n\
    out vec2 Texcoord;\n\
    out vec3 Color;                                                              

    void main(){\n\
        Color=inColor;\n\
        Texcoord=texcoord;\n\
        gl_Position=vec4(position,0,1);\n\
    }\n";

static const char* fragmentShaderSource = "\n\
    #version 150 core\n\
    //uniform vec4 fragmentColor;\n\
    in vec3 Color;\n\
    in vec2 Texcoord;\n\
    uniform sampler2D tex0;\n\
    uniform sampler2D tex1;\n\
    out vec4 color;\n\
    void main(){\n\
        vec4 color1=texture(tex0,Texcoord);\n\
        vec4 color2=texture(tex1,Texcoord);\n\
        color=mix(color1,color2,0.5);\n\
        //color=color1;\n\
    }\n\
    ";
//////////////////////////////////////


int main() {
    if (!glfwInit())
        return -1;

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    GLFWwindow* window = glfwCreateWindow(1024, 620, "Window", nullptr, nullptr);


    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;


    if (glewInit() != GLEW_OK)
        return -1;

    GLfloat triangleVertices[] = {
        0.5f,0.5f,1,0,0,1,0,
        0.5f,-0.5f,0,1,0,1,1,
        -0.5f,-0.5f,0,0,1,0,1,
        -0.5f,0.5f,0,0,0,0,0
    };

    GLuint vao; 
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

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

    GLuint elements[] = { 0,1,2 ,3,0,2};
    GLuint ebo;
    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

    ////////////////////////////////////////////////////////
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
    glCompileShader(vertexShader);
    GLint statusVertex;
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &statusVertex);
    if (!statusVertex) {
        char buffer[1024];
        glGetShaderInfoLog(vertexShader, 1024, NULL, buffer);
        std::cout << "Error Compiling vertexShader:" << buffer << std::endl;
    }



    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
    glCompileShader(fragmentShader);
    GLint statusFrag;
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &statusFrag);
    if (!statusFrag) {
        char buffer[1024];
        glGetShaderInfoLog(fragmentShader, 1024, NULL, buffer);
        std::cout << "Error Compiling fragmentShader:" << buffer << std::endl;
    }

    GLuint program = glCreateProgram();
    glAttachShader(program, vertexShader);
    glAttachShader(program, fragmentShader);
    glBindFragDataLocation(program, 0, "color");
    glLinkProgram(program);//making connection
    glUseProgram(program);
    //////////////////////////////////////////////////////////






    GLint positionAttrib = glGetAttribLocation(program, "position");
    glVertexAttribPointer(positionAttrib, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), nullptr);
    glEnableVertexAttribArray(positionAttrib);

    GLint colorAttrib = glGetAttribLocation(program, "inColor");
    glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), (void*)(2 * sizeof(GL_FLOAT)));
    glEnableVertexAttribArray(colorAttrib);

    GLint texAttrib = glGetAttribLocation(program, "texcoord");
    glVertexAttribPointer(texAttrib, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GL_FLOAT), (void*)(5 * sizeof(GL_FLOAT)));
    glEnableVertexAttribArray(texAttrib);

    GLuint textures[2];
    glGenTextures(2, textures);
    int width, height;
    unsigned char* image;

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE0, textures[0]);
    image = SOIL_load_image("1.png", &width, &height, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    glUniform1i(glGetUniformLocation(program, "tex0"), 0);
    SOIL_free_image_data(image);

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


    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE1, textures[1]);
    image = SOIL_load_image("2.png", &width, &height, 0, SOIL_LOAD_RGBA);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    glGenerateMipmap(GL_TEXTURE_2D);
    glUniform1i(glGetUniformLocation(program, "tex1"), 1);
    SOIL_free_image_data(image);
    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);


    /////////////////////////////////////////////////////////////
    while (!glfwWindowShouldClose(window)) {
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        //glDrawArrays(GL_TRIANGLES, 0, 3);

        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        if (glfwGetKey(window, GLFW_KEY_E)) {
            break;
        }
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

1 Ответ

1 голос
/ 17 октября 2019

glBindTexture принимает текстуру цель , а не единицу .

Цели: GL_TEXTURE_1D, GL_TEXTURE_2D, GL_TEXTURE_3D, GL_TEXTURE_CUBE_MAP, GL_TEXTURE_1 и GL_TEXTURE_1цели).

Текстура единица передается в glActiveTexture, это GL_TEXTURE0, GL_TEXTURE1 и т. д., и это также значение, установленное для униформы сэмплера.


Измените свой код на

    glActiveTexture(GL_TEXTURE0);
--- glBindTexture(GL_TEXTURE_2D, textures[0]);
+++ glBindTexture(GL_TEXTURE_2D, textures[0]);

и

    glActiveTexture(GL_TEXTURE1);
--- glBindTexture(GL_TEXTURE_2D, textures[1]);
+++ glBindTexture(GL_TEXTURE_2D, textures[1]);
...