Import materials from files
This commit is contained in:
@@ -39,8 +39,10 @@ Texture Texture::FromFile(const char *filename) {
|
||||
}
|
||||
|
||||
Material::Material() {
|
||||
usesColor = false;
|
||||
usesTex = false;
|
||||
|
||||
shininess = 8;
|
||||
|
||||
alphaScissor = 0.0f;
|
||||
unshaded = false;
|
||||
cullBack = true;
|
||||
|
||||
@@ -22,10 +22,15 @@ public:
|
||||
};
|
||||
|
||||
struct Material {
|
||||
Color color;
|
||||
bool usesColor;
|
||||
Texture tex;
|
||||
bool usesTex;
|
||||
|
||||
Color ambient;
|
||||
Color diffuse;
|
||||
Color specular;
|
||||
|
||||
int shininess;
|
||||
|
||||
cfloat alphaScissor;
|
||||
bool unshaded;
|
||||
bool cullBack;
|
||||
|
||||
@@ -81,10 +81,13 @@ Mesh* Mesh::FromFile(const char *filename) {
|
||||
}
|
||||
|
||||
aiNode *root = scene->mRootNode;
|
||||
if (root->mNumChildren == 1) {
|
||||
root = root->mChildren[0];
|
||||
}
|
||||
Mesh *my_mesh = new Mesh();
|
||||
for (int i = 0; i < root->mNumMeshes; i++) {
|
||||
aiMesh *mesh_to_import = scene->mMeshes[root->mMeshes[i]];
|
||||
my_mesh->submeshes.push_back(aiMesh2SubMesh(mesh_to_import));
|
||||
my_mesh->submeshes.push_back(aiMesh2SubMesh(mesh_to_import, scene->mMaterials[mesh_to_import->mMaterialIndex]));
|
||||
}
|
||||
|
||||
my_mesh->SetupMesh();
|
||||
@@ -92,7 +95,7 @@ Mesh* Mesh::FromFile(const char *filename) {
|
||||
return my_mesh;
|
||||
}
|
||||
|
||||
SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh){
|
||||
SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh, aiMaterial* material){
|
||||
SubMesh *sub = new SubMesh();
|
||||
for (int i = 0; i < aimesh->mNumVertices; i++) {
|
||||
aiVector3D aiPosition = aimesh->mVertices[i];
|
||||
@@ -109,9 +112,35 @@ SubMesh *Mesh::aiMesh2SubMesh(aiMesh *aimesh){
|
||||
Index index(face[0], face[1], face[2]);
|
||||
sub->indices.push_back(index);
|
||||
}
|
||||
|
||||
// Get material properties
|
||||
aiColor3D ambient;
|
||||
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_AMBIENT, ambient))
|
||||
sub->material.ambient = aiColor3D2Color(ambient);
|
||||
|
||||
aiColor3D diffuse;
|
||||
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse))
|
||||
sub->material.diffuse = aiColor3D2Color(diffuse);
|
||||
|
||||
aiColor3D specular;
|
||||
if (AI_SUCCESS == material->Get(AI_MATKEY_COLOR_SPECULAR, specular))
|
||||
sub->material.specular = aiColor3D2Color(specular);
|
||||
|
||||
float shininess;
|
||||
if(AI_SUCCESS == material->Get(AI_MATKEY_SHININESS, shininess))
|
||||
sub->material.shininess = (int) shininess;
|
||||
|
||||
return sub;
|
||||
}
|
||||
|
||||
Color Mesh::aiColor3D2Color(aiColor3D aicolor) {
|
||||
Color color;
|
||||
color.r = aicolor.r;
|
||||
color.g = aicolor.g;
|
||||
color.b = aicolor.b;
|
||||
return color;
|
||||
}
|
||||
|
||||
void Mesh::Draw(Shader *shader) {
|
||||
for (SubMesh *sub : submeshes) {
|
||||
sub->Draw(shader);
|
||||
|
||||
@@ -48,7 +48,8 @@ protected:
|
||||
SubMeshList submeshes;
|
||||
virtual void SetupMesh();
|
||||
private:
|
||||
static SubMesh *aiMesh2SubMesh(aiMesh *mesh);
|
||||
static SubMesh *aiMesh2SubMesh(aiMesh *mesh, aiMaterial *material);
|
||||
static Color aiColor3D2Color(aiColor3D aicolor);
|
||||
};
|
||||
|
||||
typedef std::list<Mesh*> MeshList;
|
||||
|
||||
@@ -54,15 +54,16 @@ void Shader::UpdateProjection(Matrix projection) {
|
||||
}
|
||||
|
||||
void Shader::UpdateMaterial(Material material) {
|
||||
glUniform1i(glGetUniformLocation(id, "material.usesColor"), (int) material.usesColor);
|
||||
glUniform3f(glGetUniformLocation(id, "material.color"),
|
||||
material.color.r,
|
||||
material.color.g,
|
||||
material.color.b);
|
||||
glUniform1i(glGetUniformLocation(id, "material.usesTex"), (int) material.usesTex);
|
||||
if (material.usesTex) {
|
||||
glBindTexture(GL_TEXTURE_2D, material.tex.id);
|
||||
}
|
||||
|
||||
glUniform3fv(glGetUniformLocation(id, "material.ambient"), 1, (cfloat*) &material.ambient);
|
||||
glUniform3fv(glGetUniformLocation(id, "material.diffuse"), 1, (cfloat*) &material.diffuse);
|
||||
glUniform3fv(glGetUniformLocation(id, "material.specular"), 1, (cfloat*) &material.specular);
|
||||
glUniform1i(glGetUniformLocation(id, "material.shininess"), material.shininess);
|
||||
|
||||
glUniform1f(glGetUniformLocation(id, "material.alphaScissor"), material.alphaScissor);
|
||||
glUniform1i(glGetUniformLocation(id, "material.unshaded"), (int) material.unshaded);
|
||||
glUniform1i(glGetUniformLocation(id, "material.cullBack"), (int) material.cullBack);
|
||||
|
||||
Reference in New Issue
Block a user