Improve material loading and add coordinate system model
This commit is contained in:
@@ -59,6 +59,9 @@ int main(int argc, char **argv)
|
||||
shader->compile();
|
||||
Model m("nanosuit/nanosuit.obj", glm::vec4(1.0f, 0.0f, 0.0f, 1.0f), shader);
|
||||
|
||||
Model coords("coordinate-system/coordsys.obj", glm::vec4(1.0f, 0.0f, 0.0f, 1.0f), shader);
|
||||
|
||||
coords.realize();
|
||||
m.realize();
|
||||
m.setModelMatrix(glm::scale(glm::mat4(1.0f), glm::vec3(0.2f, 0.2f, 0.2f)));
|
||||
|
||||
@@ -159,6 +162,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
m.render();
|
||||
coords.render();
|
||||
|
||||
glm::mat4 current_model_matrix = m.getModelMatrix();
|
||||
current_model_matrix = glm::translate(glm::mat4(1.0f), glm::vec3(3.0f, 0.0f, 0.0f)) * current_model_matrix;
|
||||
|
95
src/material.cpp
Normal file
95
src/material.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
#include <opengl-playground/material.hpp>
|
||||
|
||||
|
||||
|
||||
Material::Material()
|
||||
{
|
||||
shininess_strength = 0.0f;
|
||||
shininess = 1.0f;
|
||||
this->setDiffuseColor(glm::vec3(0.0f));
|
||||
this->setAmbientColor(glm::vec3(0.0f));
|
||||
this->setSpecularColor(glm::vec3(0.0f));
|
||||
}
|
||||
|
||||
Material::Material(const glm::vec3 &diff_color, const glm::vec3 &ambient_color, const glm::vec3 &specular_color) : Material()
|
||||
{
|
||||
this->setDiffuseColor(diff_color);
|
||||
this->setAmbientColor(ambient_color);
|
||||
this->setSpecularColor(specular_color);
|
||||
}
|
||||
|
||||
void Material::setSpecularColor(const glm::vec3 &color)
|
||||
{
|
||||
this->specular_color = color;
|
||||
}
|
||||
|
||||
void Material::setAmbientColor(const glm::vec3 &color)
|
||||
{
|
||||
this->ambient_color = color;
|
||||
}
|
||||
|
||||
void Material::setDiffuseColor(const glm::vec3 &color)
|
||||
{
|
||||
this->diff_color = color;
|
||||
}
|
||||
|
||||
void Material::pushSpecularTexture(std::shared_ptr<OpenGlTexture> texture)
|
||||
{
|
||||
this->specular_textures.push_back(texture);
|
||||
}
|
||||
|
||||
void Material::pushAmbientTexture(std::shared_ptr<OpenGlTexture> texture)
|
||||
{
|
||||
this->ambient_textures.push_back(texture);
|
||||
}
|
||||
|
||||
void Material::pushDiffuseTexture(std::shared_ptr<OpenGlTexture> texture)
|
||||
{
|
||||
this->diffuse_textures.push_back(texture);
|
||||
}
|
||||
|
||||
void Material::pushSpecularTexture(const std::vector<std::shared_ptr<OpenGlTexture>> &textures)
|
||||
{
|
||||
this->specular_textures.insert(this->specular_textures.end(), textures.begin(), textures.end());
|
||||
}
|
||||
|
||||
void Material::pushAmbientTexture(const std::vector<std::shared_ptr<OpenGlTexture>> &textures)
|
||||
{
|
||||
this->ambient_textures.insert(this->ambient_textures.end(), textures.begin(), textures.end());
|
||||
}
|
||||
|
||||
void Material::pushDiffuseTexture(const std::vector<std::shared_ptr<OpenGlTexture>> &textures)
|
||||
{
|
||||
this->diffuse_textures.insert(this->diffuse_textures.end(), textures.begin(), textures.end());
|
||||
}
|
||||
|
||||
const std::vector<std::shared_ptr<OpenGlTexture>> &Material::getSpecularTextures()
|
||||
{
|
||||
return this->specular_textures;
|
||||
}
|
||||
|
||||
const std::vector<std::shared_ptr<OpenGlTexture>> &Material::getAmbientTextures()
|
||||
{
|
||||
return this->ambient_textures;
|
||||
}
|
||||
|
||||
const std::vector<std::shared_ptr<OpenGlTexture>> &Material::getDiffuseTextures()
|
||||
{
|
||||
return this->diffuse_textures;
|
||||
}
|
||||
|
||||
const glm::vec3 &Material::getDiffuseColor()
|
||||
{
|
||||
return this->diff_color;
|
||||
}
|
||||
|
||||
const glm::vec3 &Material::getAmbientColor()
|
||||
{
|
||||
return this->ambient_color;
|
||||
}
|
||||
|
||||
const glm::vec3 &Material::getSpecularColor()
|
||||
{
|
||||
return this->specular_color;
|
||||
}
|
||||
|
75
src/mesh.cpp
75
src/mesh.cpp
@@ -1,17 +1,16 @@
|
||||
#include <opengl-playground/mesh.hpp>
|
||||
#include <opengl-playground/globalcanvassettings.hpp>
|
||||
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <iostream>
|
||||
|
||||
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<unsigned int> indices,
|
||||
std::vector<std::shared_ptr<OpenGlTexture> > textures,
|
||||
const Material &mat,
|
||||
std::shared_ptr<OpenGlShaderProgram> shader) :
|
||||
OpenGlGraphics(shader)
|
||||
{
|
||||
this->vertices = vertices;
|
||||
this->indices = indices;
|
||||
this->textures = textures;
|
||||
this->material = mat;
|
||||
}
|
||||
|
||||
void Mesh::realize()
|
||||
@@ -52,24 +51,62 @@ void Mesh::render(const glm::mat4 &model_matrix, const glm::mat4 &normal_matrix)
|
||||
this->shaderprog->setUniformMat4("projection_view_matrix", GlobalCanvasSettings::getPVMatrix());
|
||||
this->shaderprog->setUniformVec3("camera_position", GlobalCanvasSettings::getCameraPosition());
|
||||
|
||||
int spec_num = 0;
|
||||
int diff_num = 0;
|
||||
auto diffTex = this->material.getDiffuseTextures();
|
||||
auto specTex = this->material.getSpecularTextures();
|
||||
auto ambTex = this->material.getAmbientTextures();
|
||||
|
||||
/* Setup the texture samplers */
|
||||
for (unsigned int i = 0; i < this->textures.size(); i++) {
|
||||
int tex_slot = 0;
|
||||
for (unsigned int i = 0; i < diffTex.size(); i++, tex_slot++) {
|
||||
std::string uniform_name;
|
||||
textures[i]->use(i);
|
||||
switch (textures[i]->getType()) {
|
||||
case TEXTURE_DIFFUSE:
|
||||
uniform_name = "material.diffuse" + std::to_string(diff_num++);
|
||||
this->shaderprog->setUniformInt(uniform_name, i);
|
||||
break;
|
||||
case TEXTURE_SPECULAR:
|
||||
uniform_name = "material.specular" + std::to_string(spec_num++);
|
||||
this->shaderprog->setUniformInt(uniform_name, i);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
diffTex[i]->use(tex_slot);
|
||||
uniform_name = "material.diffuse" + std::to_string(i);
|
||||
this->shaderprog->setUniformInt(uniform_name, i);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < specTex.size(); i++, tex_slot++) {
|
||||
std::string uniform_name;
|
||||
specTex[i]->use(tex_slot);
|
||||
uniform_name = "material.specular" + std::to_string(i);
|
||||
this->shaderprog->setUniformInt(uniform_name, i);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < ambTex.size(); i++, tex_slot++) {
|
||||
std::string uniform_name;
|
||||
specTex[i]->use(tex_slot);
|
||||
uniform_name = "material.ambient" + std::to_string(i);
|
||||
this->shaderprog->setUniformInt(uniform_name, i);
|
||||
}
|
||||
|
||||
if (diffTex.size() == 0) {
|
||||
this->shaderprog->setUniformVec3("material.diff_color", this->material.getDiffuseColor());
|
||||
this->shaderprog->setUniformBool("material.use_diff_color", true);
|
||||
} else {
|
||||
this->shaderprog->setUniformBool("material.use_diff_color", false);
|
||||
}
|
||||
|
||||
if (specTex.size() == 0) {
|
||||
this->shaderprog->setUniformVec3("material.spec_color", this->material.getSpecularColor());
|
||||
this->shaderprog->setUniformBool("material.use_spec_color", true);
|
||||
} else {
|
||||
this->shaderprog->setUniformBool("material.use_spec_color", false);
|
||||
}
|
||||
|
||||
if (specTex.size() == 0) {
|
||||
this->shaderprog->setUniformVec3("material.spec_color", this->material.getSpecularColor());
|
||||
this->shaderprog->setUniformBool("material.use_spec_color", true);
|
||||
} else {
|
||||
this->shaderprog->setUniformBool("material.use_spec_color", false);
|
||||
}
|
||||
|
||||
if (diffTex.size() == 0 && ambTex.size() == 0) {
|
||||
this->shaderprog->setUniformVec3("material.amb_color", this->material.getAmbientColor());
|
||||
this->shaderprog->setUniformInt("material.use_amb_color", 2);
|
||||
} else {
|
||||
if (diffTex.size() > 0)
|
||||
this->shaderprog->setUniformInt("material.use_amb_color", 1);
|
||||
else
|
||||
this->shaderprog->setUniformInt("material.use_amb_color", 0);
|
||||
}
|
||||
|
||||
glBindVertexArray(this->vao);
|
||||
|
@@ -1,4 +1,5 @@
|
||||
#include <opengl-playground/model.hpp>
|
||||
#include <opengl-playground/material.hpp>
|
||||
#include <iostream>
|
||||
|
||||
Model::Model(const std::string &path, const glm::vec4 &static_color, std::shared_ptr<OpenGlShaderProgram> shader)
|
||||
@@ -47,8 +48,7 @@ void Model::processNode(aiNode *node, const aiScene *scene)
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::shared_ptr<OpenGlTexture>> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type,
|
||||
TextureType tex_type)
|
||||
std::vector<std::shared_ptr<OpenGlTexture>> Model::loadMaterialTextures(aiMaterial *mat, aiTextureType type)
|
||||
{
|
||||
std::vector<std::shared_ptr<OpenGlTexture>> tex_vector;
|
||||
|
||||
@@ -66,7 +66,7 @@ std::vector<std::shared_ptr<OpenGlTexture>> Model::loadMaterialTextures(aiMateri
|
||||
}
|
||||
|
||||
if (!already_loaded) {
|
||||
auto new_texture = std::make_shared<OpenGlTexture>(tex_type, str.C_Str());
|
||||
auto new_texture = std::make_shared<OpenGlTexture>(str.C_Str());
|
||||
new_texture->loadFromImagePath(this->directory);
|
||||
tex_vector.push_back(new_texture);
|
||||
this->allocated_textures.push_back(new_texture);
|
||||
@@ -80,7 +80,7 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene)
|
||||
{
|
||||
std::vector<Vertex> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<std::shared_ptr<OpenGlTexture>> textures;
|
||||
Material mesh_material;
|
||||
|
||||
for(unsigned int i = 0; i < mesh->mNumVertices; i++)
|
||||
{
|
||||
@@ -115,15 +115,30 @@ Mesh Model::processMesh(aiMesh *mesh, const aiScene *scene)
|
||||
|
||||
if(mesh->mMaterialIndex >= 0)
|
||||
{
|
||||
aiColor3D col;
|
||||
|
||||
aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex];
|
||||
auto diffuseTextures = loadMaterialTextures(material, aiTextureType_DIFFUSE, TEXTURE_DIFFUSE);
|
||||
auto specularTextures = loadMaterialTextures(material, aiTextureType_SPECULAR, TEXTURE_SPECULAR);
|
||||
textures.insert(textures.end(), diffuseTextures.begin(), diffuseTextures.end());
|
||||
textures.insert(textures.end(), specularTextures.begin(), specularTextures.end());
|
||||
auto diffuseTextures = loadMaterialTextures(material, aiTextureType_DIFFUSE);
|
||||
auto specularTextures = loadMaterialTextures(material, aiTextureType_SPECULAR);
|
||||
auto ambientTextures = loadMaterialTextures(material, aiTextureType_AMBIENT);
|
||||
|
||||
mesh_material.pushDiffuseTexture(diffuseTextures);
|
||||
mesh_material.pushSpecularTexture(specularTextures);
|
||||
mesh_material.pushAmbientTexture(ambientTextures);
|
||||
|
||||
if (!material->Get(AI_MATKEY_COLOR_AMBIENT, col))
|
||||
mesh_material.setAmbientColor(glm::vec3(col.r, col.g, col.b));
|
||||
if (!material->Get(AI_MATKEY_COLOR_SPECULAR, col))
|
||||
mesh_material.setSpecularColor(glm::vec3(col.r, col.g, col.b));
|
||||
if (!material->Get(AI_MATKEY_COLOR_DIFFUSE, col))
|
||||
mesh_material.setDiffuseColor(glm::vec3(col.r, col.g, col.b));
|
||||
|
||||
material->Get(AI_MATKEY_SHININESS, mesh_material.shininess);
|
||||
material->Get(AI_MATKEY_SHININESS_STRENGTH, mesh_material.shininess_strength);
|
||||
}
|
||||
|
||||
|
||||
Mesh my_mesh = Mesh(vertices, indices, textures, this->shaderprog);
|
||||
Mesh my_mesh = Mesh(vertices, indices, mesh_material, this->shaderprog);
|
||||
my_mesh.realize();
|
||||
return my_mesh;
|
||||
}
|
||||
|
@@ -208,6 +208,11 @@ void OpenGlShaderProgram::setUniformVec4(const std::string &uniform, const glm::
|
||||
}
|
||||
}
|
||||
|
||||
void OpenGlShaderProgram::setUniformBool(const std::string &uniform, bool value)
|
||||
{
|
||||
this->setUniformInt(uniform, value ? 1 : 0);
|
||||
}
|
||||
|
||||
int OpenGlShaderProgram::getUniformLoc(const std::string &uniform)
|
||||
{
|
||||
if (!this->compiled)
|
||||
|
@@ -4,7 +4,7 @@
|
||||
#include <stb/stb_image.h>
|
||||
#include <iostream>
|
||||
|
||||
OpenGlTexture::OpenGlTexture(TextureType type, const std::string &texture_path)
|
||||
OpenGlTexture::OpenGlTexture(const std::string &texture_path)
|
||||
{
|
||||
glGenTextures(1, &this->texture);
|
||||
glBindTexture(GL_TEXTURE_2D, this->texture);
|
||||
@@ -12,11 +12,11 @@ OpenGlTexture::OpenGlTexture(TextureType type, const std::string &texture_path)
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
this->type = type;
|
||||
this->texture_path = texture_path;
|
||||
this->blend = 1.0f;
|
||||
}
|
||||
|
||||
OpenGlTexture::OpenGlTexture() : OpenGlTexture(TEXTURE_DIFFUSE, "")
|
||||
OpenGlTexture::OpenGlTexture() : OpenGlTexture("")
|
||||
{
|
||||
|
||||
}
|
||||
@@ -79,7 +79,3 @@ const std::string &OpenGlTexture::getPath()
|
||||
return this->texture_path;
|
||||
}
|
||||
|
||||
TextureType OpenGlTexture::getType()
|
||||
{
|
||||
return this->type;
|
||||
}
|
||||
|
Reference in New Issue
Block a user