每当我在打开MSI加力覆盖的情况下打开游戏时,纹理似乎会移动一个,所以第一个会设置为第二个,第二个设置为第三个,依此类推。但如果我在加载时不使用覆盖,一切都很好。我有一种感觉,这与openGL中的纹理ID有关,但我真的不知道。我真的完成了我的整个项目,却一无所获。
以下是我认为相关的代码,因为我的项目相当大:
主要渲染类别:
#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;
}
我认为问题出在那里,因为昨天一切都很好,我添加了一些小更改,一切似乎都坏了,遗憾的是,我的最后一次备份就在我实现纹理之前,这真的很烦人。
我已经解决了这个问题!看了很长时间后,我不得不承认,事实证明我非常无能,当绑定每一帧的纹理时,我只是绑定到第一、第二和第三个插槽中的纹理,但如果另一个程序正在使用其中一个插槽,如MSI加力覆盖,那么在创建纹理时,它会将它们添加到第二个插槽中,因此,每当绑定纹理时,它都会检查第一个插槽并使用该纹理。基本上具有将纹理移动到具有新的第一个纹理的纹理上的效果。为了解决这个问题,我只是在主头文件中声明了unsigned int id_texture1, id_texture2, id_texture3;
,并在绑定时使用了它们的纹理ID,如下所示:
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, id_texture1); etc...