Fix material textures

This commit is contained in:
MrLetsplay 2022-10-10 09:54:36 +02:00
parent fbb7980ce7
commit 6d687e63d8
4 changed files with 30 additions and 34 deletions

View File

@ -63,24 +63,25 @@ Mesh::~Mesh() {
delete material; delete material;
} }
static void bindTextureUniform(Shader *shader, std::string name, std::shared_ptr<Texture> texture, GLuint *textureIndex) {
GLint loc = glGetUniformLocation(shader->id, name.c_str());
if(loc != -1) {
texture->use(*textureIndex);
glUniform1i(loc, *textureIndex - GL_TEXTURE0);
(*textureIndex)++;
}
}
void Mesh::draw(Shader *shader) { void Mesh::draw(Shader *shader) {
glBindVertexArray(vao); glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
GLint ambient = glGetUniformLocation(shader->id, "material.ambient"); GLuint textureIndex = GL_TEXTURE0;
// FIXME: is -1 bindTextureUniform(shader, "material.ambient", material->ambient, &textureIndex);
if(ambient != -1) { // Material can be passed to shader bindTextureUniform(shader, "material.diffuse", material->diffuse, &textureIndex);
material->ambient->use(GL_TEXTURE0); bindTextureUniform(shader, "material.specular", material->specular, &textureIndex);
glUniform1i(ambient, 0); glUniform1f(glGetUniformLocation(shader->id, "material.shininess"), material->shininess);
material->diffuse->use(GL_TEXTURE1);
glUniform1i(glGetUniformLocation(shader->id, "material.diffuse"), 1);
material->specular->use(GL_TEXTURE2);
glUniform1i(glGetUniformLocation(shader->id, "material.specular"), 2);
glUniform1f(glGetUniformLocation(shader->id, "material.shininess"), material->shininess);
}else {
std::cerr << "oof" << std::endl;
}
// Bind vertices + texture coordinates // Bind vertices + texture coordinates
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);

View File

@ -108,14 +108,21 @@ Mesh *parseMesh(MemoryBuffer *buf, std::string folderPath) {
} }
Mesh *loadMesh(std::string path) { Mesh *loadMesh(std::string path) {
std::size_t n = path.find('/'); std::size_t n = path.find_last_of('/');
std::string folderPath = n == std::string::npos ? "" : path.substr(n + 1); std::string folderPath = n == std::string::npos ? "" : path.substr(0, n + 1);
MemoryBuffer *buf = Resource::loadResource(path); MemoryBuffer *buf = Resource::loadResource(path);
Mesh *mesh = parseMesh(buf, folderPath); Mesh *mesh = parseMesh(buf, folderPath);
delete buf; delete buf;
return mesh; return mesh;
} }
static Material *makeMaterial(std::shared_ptr<Texture> ambient, std::shared_ptr<Texture> diffuse, std::shared_ptr<Texture> specular, float shininess) {
if(!ambient) ambient = Texture::generateColor(glm::vec3(1.0f));
if(!diffuse) diffuse = Texture::generateColor(glm::vec3(1.0f));
if(!specular) specular = Texture::generateColor(glm::vec3(1.0f));
return new Material(ambient, diffuse, specular, shininess);
}
std::map<std::string, Material *> parseMaterials(MemoryBuffer *buf, std::string folderPath) { std::map<std::string, Material *> parseMaterials(MemoryBuffer *buf, std::string folderPath) {
std::map<std::string, Material *> mats; std::map<std::string, Material *> mats;
@ -136,10 +143,7 @@ std::map<std::string, Material *> parseMaterials(MemoryBuffer *buf, std::string
if(cmd == "#") continue; if(cmd == "#") continue;
if(cmd == "newmtl") { if(cmd == "newmtl") {
if(name.length() > 0) { if(name.length() > 0) mats.emplace(name, makeMaterial(ambient, diffuse, specular, shininess));
Material *mat = new Material(ambient, diffuse, specular, shininess);
mats.emplace(name, mat);
}
str >> name; str >> name;
}else if(cmd == "Ka") { }else if(cmd == "Ka") {
@ -163,17 +167,14 @@ std::map<std::string, Material *> parseMaterials(MemoryBuffer *buf, std::string
} }
} }
if(name.length() > 0) { if(name.length() > 0) mats.emplace(name, makeMaterial(ambient, diffuse, specular, shininess));
Material *mat = new Material(ambient, diffuse, specular, shininess);
mats.emplace(name, mat);
}
return mats; return mats;
} }
std::map<std::string, Material *> loadMaterials(std::string path) { std::map<std::string, Material *> loadMaterials(std::string path) {
std::size_t n = path.find('/'); std::size_t n = path.find_last_of('/');
std::string folderPath = n == std::string::npos ? "" : path.substr(n + 1); std::string folderPath = n == std::string::npos ? "" : path.substr(0, n + 1);
MemoryBuffer *buf = Resource::loadResource(path); MemoryBuffer *buf = Resource::loadResource(path);
std::map<std::string, Material *> mats = parseMaterials(buf, folderPath); std::map<std::string, Material *> mats = parseMaterials(buf, folderPath);
delete buf; delete buf;

View File

@ -8,7 +8,7 @@
namespace kek { namespace kek {
static unsigned int allocTexture(unsigned char *data, int width, int height, float borderColor[4]) { static GLuint allocTexture(unsigned char *data, int width, int height, float borderColor[4]) {
GLuint id; GLuint id;
glGenTextures(1, &id); glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
@ -37,7 +37,7 @@ Texture::Texture(std::string texturePath) {
delete buf; delete buf;
float borderColor[] = { 1.0f, 0.0f, 1.0f, 1.0f }; float borderColor[] = { 1.0f, 0.0f, 1.0f, 1.0f };
allocTexture(data, width, height, borderColor); id = allocTexture(data, width, height, borderColor);
stbi_image_free(data); stbi_image_free(data);
} }

View File

@ -19,19 +19,13 @@ int main(int argc, char **argv) {
Scene *scene = new Scene(); Scene *scene = new Scene();
scene->addObject(test); scene->addObject(test);
MemoryBuffer *buf2 = Resource::loadResource("object/cube_colored/Cube.obj");
Mesh *mesh2 = ObjParser::parseMesh(buf2, "object/cube_colored/");
delete buf2;
for(int i = 0; i < 1; i++) { for(int i = 0; i < 1; i++) {
GameObject *test2 = new GameObject(); GameObject *test2 = new GameObject();
test2->addMesh(new Mesh(mesh2->vertices, mesh2->indices, mesh2->material)); test2->addMesh(ObjParser::loadMesh("object/cube_colored/Cube.obj"));
test2->moveTo(glm::vec3(0.0f, 5.0f, 3 * i)); test2->moveTo(glm::vec3(0.0f, 5.0f, 3 * i));
scene->addObject(test2); scene->addObject(test2);
} }
delete mesh2;
Engine::setActiveScene(scene); Engine::setActiveScene(scene);
Engine::start(); Engine::start();