opengl 为什么它是当我打开我的游戏与微星加力覆盖切换对我的纹理没有工作如何他们应该

lawou6xi  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(106)

每当我打开我的游戏时,MSI加力燃烧器的覆盖层切换到纹理似乎被移动了一个,所以第一个被设置为第二个,第二个到第三个,等等。但如果我不使用覆盖层,而加载一切都很好。我有一种感觉,这是做纹理id在openGL,但我真的不知道。我已经字面上通过我的整个项目,并没有发现。
下面是我认为相关的代码,因为我的项目相当大:
主呈现类:


# include "main_renderer.h"

Renderer::Renderer(Shader *shader, Player *player, World *world): shader(shader), player(player), world(world){
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glEnable(GL_STENCIL);

    float vertices[] = {
       // vertices I really don`t think these are the problem so im not going to add them all    
    }; 

    unsigned int count = 0;
    for (unsigned int z = 0; z < 1; z++){
    for (unsigned int x = 0; x < 16; x++){
        for (unsigned int y = 0; y < 16; y++){
            cubePositions[count] = glm::vec3( 0.0f + x,  0.0f + z,  0.0f + y);
            count += 1;
        };
    };
    };//generates a 16 by 16 grid of cubes

    glGenVertexArrays(1, &cubeVAO);
    glGenBuffers(1, &VBO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glBindVertexArray(cubeVAO);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
    glEnableVertexAttribArray(2);

    unsigned int id_texture1 = loadTexture( "../textures/grass.png", false);
    unsigned int id_texture2 = loadTexture( "../textures/awesomeface.png", true);
    unsigned int id_texture3 = loadTexture( "../textures/container2.png", true);

    shader->use();  
    shader->setInt("grass", 0);
    shader->setInt("face", 1);
    shader->setInt("container", 2);
};

void Renderer::update(){
    extern float width, height;
    extern double currentFrame;

    shader->use();

    glm::mat4 projection = glm::perspective(glm::radians(player -> Zoom), (float) width /  (float) height, 0.05f, 500.0f);
    glm::mat4 view = player -> GetViewMatrix();
    shader->setMat4("projection", projection);
    shader->setMat4("view", view); 

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, 1);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, 2);
    glActiveTexture(GL_TEXTURE2);
    glBindTexture(GL_TEXTURE_2D, 3);  

    glBindVertexArray(cubeVAO);
        for (unsigned int i = 0; i < sizeof(cubePositions) / sizeof(cubePositions[0]); i++){
            glm::mat4 model = glm::mat4(1.0f);
            model = glm::translate(model, cubePositions[i]);
            shader->setMat4("model", model);

            glDrawArrays(GL_TRIANGLES, 0, 36);
        }
};

void Renderer::exit(){
    glDeleteVertexArrays(1, &cubeVAO);
    glDeleteBuffers(1, &VBO);
    glfwTerminate();
};

unsigned int Renderer::loadTexture(char const * path, bool linear){
    unsigned int textureID;
    std::cout<<textureID;
    glGenTextures(1, &textureID);

    int width, height, nrComponents;
    unsigned char *data = stbi_load(path, &width, &height, &nrComponents, 0);
    if (data){
        GLenum format;
        if (nrComponents == 1)
            format = GL_RED;
        else if (nrComponents == 3)
            format = GL_RGB;
        else if (nrComponents == 4)
            format = GL_RGBA;

        glBindTexture(GL_TEXTURE_2D, textureID);
        glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
        glGenerateMipmap(GL_TEXTURE_2D);

        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        if (linear){
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        }else{
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        };

        stbi_image_free(data);
    }
    else{
        std::cout << "Texture failed to load at path: " << path << std::endl;
        stbi_image_free(data);
    };

    return textureID;
};

片段着色器:


# version 330 core

out vec4 FragColor;

uniform sampler2D grass;
uniform sampler2D face;
uniform sampler2D container;

in vec2 TexCoords;

void main(){
    vec4 color = texture(face, TexCoords);
    if (color.a < 0.5) {
        color = texture(grass, TexCoords); 
    }
    FragColor = color;
}

我认为问题出在那里的某个地方,因为昨天一切都很好,我添加了几个小的变化,一切似乎都坏了,可悲的是,我的最后一次备份是正确的之前,我实现了纹理,这真的很烦人。

6uxekuva

6uxekuva1#

我已经解决了这个问题!在看了比我愿意承认的时间更长的时间后,事实证明我是极其无能的,当绑定纹理的每一帧我只是绑定到纹理在第1,第2和第3插槽,但如果另一个程序正在使用这些插槽之一,如微星加力覆盖,然后当创建纹理时,它会将它们添加到第2,第三个等插槽因此每当纹理绑定时,它会检查第一个插槽并使用该纹理。基本上有一个新的第一个纹理移动纹理的效果。为了解决这个问题,我简单地在主头文件中声明unsigned int id_texture1, id_texture2, id_texture3;,并在绑定时使用它们的纹理id,如下所示:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id_texture1); etc...

相关问题