如何解决为什么不加载纹理?
在openGL中工作,不确定为什么我的纹理没有加载。我一直在Learningopengl网站上学习,我的代码最终在大多数领域变得非常相似/复制。彻底,但我认为我缺少一些小东西。目前,我正在得到一个黑色矩形。这是我的“角色”类,因为我怀疑问题出在这里:
#include "Character.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
Character::~Character(){}
Character::Character(const char* texturePath,const char* vertShaderPath,const char* fragShaderPath)
{
Shader shader(vertShaderPath,fragShaderPath);
this->shader = &shader;
float vertices[] = {
// positions // texture coords
0.5f,0.5f,0.0f,1.0f,// top right
0.5f,-0.5f,// bottom right
-0.5f,// bottom left
-0.5f,1.0f // top left
};
unsigned int indices[] = {
0,1,3,2,3
};
unsigned int VBO,EBO;
glGenVertexArrays(1,&VAO);
glGenBuffers(1,&VBO);
glGenBuffers(1,&EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
glVertexAttribPointer(0,GL_FLOAT,GL_FALSE,5 * sizeof(float),(void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1,(void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
//
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
// load and generate the texture
int width,height,nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* data = stbi_load(texturePath,&width,&height,&nrChannels,0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D,GL_RGB,width,GL_UNSIGNED_BYTE,data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
this->shader->Use(); //Call before setting uniforms.
glUniform1i(glGetUniformLocation(this->shader->ID,"texture"),0);
}
void Character::draw()
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texture);
shader->Use();
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
}
如果有帮助,这是我的仓库:https://github.com/PapaVinny/RogueAttempt/tree/master/RougeAttempt/RougeAttempt
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。