Fix material textures
This commit is contained in:
parent
fbb7980ce7
commit
6d687e63d8
@ -63,24 +63,25 @@ Mesh::~Mesh() {
|
||||
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) {
|
||||
glBindVertexArray(vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
|
||||
|
||||
GLint ambient = glGetUniformLocation(shader->id, "material.ambient");
|
||||
// FIXME: is -1
|
||||
if(ambient != -1) { // Material can be passed to shader
|
||||
material->ambient->use(GL_TEXTURE0);
|
||||
glUniform1i(ambient, 0);
|
||||
material->diffuse->use(GL_TEXTURE1);
|
||||
glUniform1i(glGetUniformLocation(shader->id, "material.diffuse"), 1);
|
||||
material->specular->use(GL_TEXTURE2);
|
||||
glUniform1i(glGetUniformLocation(shader->id, "material.specular"), 2);
|
||||
GLuint textureIndex = GL_TEXTURE0;
|
||||
bindTextureUniform(shader, "material.ambient", material->ambient, &textureIndex);
|
||||
bindTextureUniform(shader, "material.diffuse", material->diffuse, &textureIndex);
|
||||
bindTextureUniform(shader, "material.specular", material->specular, &textureIndex);
|
||||
glUniform1f(glGetUniformLocation(shader->id, "material.shininess"), material->shininess);
|
||||
}else {
|
||||
std::cerr << "oof" << std::endl;
|
||||
}
|
||||
|
||||
// Bind vertices + texture coordinates
|
||||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||
|
@ -108,14 +108,21 @@ Mesh *parseMesh(MemoryBuffer *buf, std::string folderPath) {
|
||||
}
|
||||
|
||||
Mesh *loadMesh(std::string path) {
|
||||
std::size_t n = path.find('/');
|
||||
std::string folderPath = n == std::string::npos ? "" : path.substr(n + 1);
|
||||
std::size_t n = path.find_last_of('/');
|
||||
std::string folderPath = n == std::string::npos ? "" : path.substr(0, n + 1);
|
||||
MemoryBuffer *buf = Resource::loadResource(path);
|
||||
Mesh *mesh = parseMesh(buf, folderPath);
|
||||
delete buf;
|
||||
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 *> mats;
|
||||
|
||||
@ -136,10 +143,7 @@ std::map<std::string, Material *> parseMaterials(MemoryBuffer *buf, std::string
|
||||
if(cmd == "#") continue;
|
||||
|
||||
if(cmd == "newmtl") {
|
||||
if(name.length() > 0) {
|
||||
Material *mat = new Material(ambient, diffuse, specular, shininess);
|
||||
mats.emplace(name, mat);
|
||||
}
|
||||
if(name.length() > 0) mats.emplace(name, makeMaterial(ambient, diffuse, specular, shininess));
|
||||
|
||||
str >> name;
|
||||
}else if(cmd == "Ka") {
|
||||
@ -163,17 +167,14 @@ std::map<std::string, Material *> parseMaterials(MemoryBuffer *buf, std::string
|
||||
}
|
||||
}
|
||||
|
||||
if(name.length() > 0) {
|
||||
Material *mat = new Material(ambient, diffuse, specular, shininess);
|
||||
mats.emplace(name, mat);
|
||||
}
|
||||
if(name.length() > 0) mats.emplace(name, makeMaterial(ambient, diffuse, specular, shininess));
|
||||
|
||||
return mats;
|
||||
}
|
||||
|
||||
std::map<std::string, Material *> loadMaterials(std::string path) {
|
||||
std::size_t n = path.find('/');
|
||||
std::string folderPath = n == std::string::npos ? "" : path.substr(n + 1);
|
||||
std::size_t n = path.find_last_of('/');
|
||||
std::string folderPath = n == std::string::npos ? "" : path.substr(0, n + 1);
|
||||
MemoryBuffer *buf = Resource::loadResource(path);
|
||||
std::map<std::string, Material *> mats = parseMaterials(buf, folderPath);
|
||||
delete buf;
|
||||
|
@ -8,7 +8,7 @@
|
||||
|
||||
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;
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
@ -37,7 +37,7 @@ Texture::Texture(std::string texturePath) {
|
||||
delete buf;
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -19,19 +19,13 @@ int main(int argc, char **argv) {
|
||||
Scene *scene = new Scene();
|
||||
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++) {
|
||||
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));
|
||||
scene->addObject(test2);
|
||||
}
|
||||
|
||||
delete mesh2;
|
||||
|
||||
Engine::setActiveScene(scene);
|
||||
|
||||
Engine::start();
|
||||
|
Loading…
Reference in New Issue
Block a user