From 3d97ade42e3ba607f2753e77429571453aab47b8 Mon Sep 17 00:00:00 2001 From: Kenneth van Ewijk Date: Thu, 12 May 2016 13:17:03 +0200 Subject: [PATCH] Parse all lighting --- Main.cpp | 10 +++++----- Model.cpp | 30 +++++++++++++++++++++++++++--- Model.h | 5 +++++ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Main.cpp b/Main.cpp index 0906373..df1af15 100644 --- a/Main.cpp +++ b/Main.cpp @@ -151,10 +151,10 @@ void configureOpenGL() //Lighting GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; - GLfloat mat_shininess[] = { 50.0 }; - GLfloat light_position[] = { 30.0, 30.0, 30.0, 0.0 }; - GLfloat light_diffuse[] = { 1.7, 1.7, 1.7, 1.0 }; - GLfloat light_ambient[] = { 1.7, 1.7, 1.7, 1.0 }; + GLfloat mat_shininess[] = { 20.0 }; + GLfloat light_position[] = { 30.0, 30.0, 30.0, 1.0 }; + GLfloat light_diffuse[] = { 2.0, 2.0, 2.0, 1.0 }; + GLfloat light_ambient[] = { 2.0, 2.0, 2.0, 1.0 }; glClearColor(0.0, 0.0, 0.0, 0.0); glShadeModel(GL_SMOOTH); @@ -174,5 +174,5 @@ void configureOpenGL() void loadModels() { - models.push_back(new Model("models/ship/Zwaard.obj")); + models.push_back(new Model("models/ship/shipA_OBJ.obj")); } \ No newline at end of file diff --git a/Model.cpp b/Model.cpp index 38abd82..b25e3d4 100644 --- a/Model.cpp +++ b/Model.cpp @@ -134,9 +134,22 @@ void Model::draw() glDisable(GL_TEXTURE_2D); float color[4] = { 1, 0, 0, 1 }; - memcpy(color, materials[g->materialIndex]->diffuseColor.v, 3 * sizeof(float)); - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color); + + if (materials[g->materialIndex]->hasDiffuse) + { + memcpy(color, materials[g->materialIndex]->diffuseColor.v, 3 * sizeof(float)); + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color); + } + else if (materials[g->materialIndex]->hasAmbient) + { + memcpy(color, materials[g->materialIndex]->ambientColor.v, 3 * sizeof(float)); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color); + } + else if (materials[g->materialIndex]->hasSpecular) + { + memcpy(color, materials[g->materialIndex]->specularColor.v, 3 * sizeof(float)); + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, color); + } } glBegin(GL_TRIANGLES); @@ -204,8 +217,19 @@ void Model::loadMaterialFile(std::string fileName, std::string dirName) } else if (params[0] == "kd") { + currentMaterial->hasDiffuse = true; currentMaterial->diffuseColor = Vec3f(atof(params[1].c_str()), atof(params[2].c_str()), atof(params[3].c_str())); } + else if (params[0] == "ka") + { + currentMaterial->hasAmbient = true; + currentMaterial->ambientColor = Vec3f(atof(params[1].c_str()), atof(params[2].c_str()), atof(params[3].c_str())); + } + else if (params[0] == "ks") + { + currentMaterial->hasSpecular = true; + currentMaterial->specularColor = Vec3f(atof(params[1].c_str()), atof(params[2].c_str()), atof(params[3].c_str())); + } else std::cout << "Didn't parse " << params[0] << " in material file" << std::endl; } diff --git a/Model.h b/Model.h index d5e2ab6..77ea091 100644 --- a/Model.h +++ b/Model.h @@ -36,7 +36,12 @@ private: Texture* texture; bool hasTexture; + bool hasDiffuse; Vec3f diffuseColor; + bool hasAmbient; + Vec3f ambientColor; + bool hasSpecular; + Vec3f specularColor; };