From c6ab0eb6500946c1f2098a072cca64c3867d934b Mon Sep 17 00:00:00 2001 From: LockedLunatic Date: Tue, 28 Apr 2015 16:25:59 +0200 Subject: [PATCH] materials added --- Weave/Game.cpp | 8 ++++---- Weave/Graphix/Model/IMesh.cpp | 6 ++++++ Weave/Graphix/Model/IMesh.h | 1 + Weave/Graphix/Model/IMetaMesh.cpp | 4 ++-- Weave/Graphix/Model/IMetaMesh.h | 2 +- Weave/Graphix/Model/Model.cpp | 14 ++++++++++++-- Weave/Graphix/Model/Model.h | 3 ++- Weave/Graphix/Model/SkyBox.cpp | 8 ++++++-- Weave/Graphix/Model/SkyBox.h | 3 ++- Weave/Scene/Marvin.cpp | 2 +- Weave/Scene/SceneObject.cpp | 12 +++++++++--- Weave/Scene/SceneObject.h | 6 ++++-- Weave/Scene/Sky.cpp | 2 +- shader/lightingTexture_FS.hlsl | 13 +++++++------ 14 files changed, 58 insertions(+), 26 deletions(-) diff --git a/Weave/Game.cpp b/Weave/Game.cpp index 8ee7620..8d079d0 100644 --- a/Weave/Game.cpp +++ b/Weave/Game.cpp @@ -52,9 +52,9 @@ Game::Game() : playing(true) // Shader* shaderSky = new Shader("skybox_VS.hlsl", "skybox_FS.hlsl"); // load LVL - current_world->addObject(new SceneObject(shader1, scale(3.f * vec3(1.f,.3f,1.f)), "level_test.dae", "model_levelTest_2D.png")); + current_world->addObject(new SceneObject(shader1, scale(3.f * vec3(1.f, .3f, 1.f)), vec4(4.0f, 1.0f, 1.0f, 2.0f), "level_test.dae", "model_levelTest_2D.png")); - current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f,1.f,0.f)), new BBox() , "")); + current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, 1.f, 0.f)), vec4(1.0f, 1.0f, 1.0f, 3.0f), new BBox(), "")); //Player SceneObject* tmp_playerObject = new Marvin(shader1, translate(vec3(1.f, 3.f, 1.f))*rotate((float)M_PI_4,vec3(0.f,1.f,0.f))); @@ -97,7 +97,7 @@ Game::Game() : playing(true) //ein Gegner //current_world->addObject(new SceneObject(shader1, glm::mat4(1.0f), "Player.dae", "model_player.png")); //current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(3.f)), "cow/cow.dae", "model_cow_2D.jpg")); - current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(.4f)), "duck.dae", "model_duck_2D.png")); + current_world->addObject(new SceneObject(shader1, translate(vec3(-3.f, .4f, 0.f))*scale(vec3(.4f)), vec4(3.0f, 0.5f, 0.4f, 1.5f), "duck.dae", "model_duck_2D.png")); //import("level_test.dae", current_world, shader1); @@ -127,7 +127,7 @@ void Game::play() if (sleep_time < 0) sleep_time = 0; - Sleep(sleep_time); + Sleep(sleep_time); Events::processEvents(); diff --git a/Weave/Graphix/Model/IMesh.cpp b/Weave/Graphix/Model/IMesh.cpp index 37264f5..9adaad2 100644 --- a/Weave/Graphix/Model/IMesh.cpp +++ b/Weave/Graphix/Model/IMesh.cpp @@ -67,6 +67,12 @@ void IMesh::useModelMat(const mat4& _model, Shader* _shader) const Model::useModelMat(_model * modelMat, _shader); } +void IMesh::useMaterial(const vec4& _material, Shader* _shader) const +{ + // Model::useMaterial(_material, _shader); + Model::useMaterial(_material, _shader); +} + //void IMesh::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const //{ // diff --git a/Weave/Graphix/Model/IMesh.h b/Weave/Graphix/Model/IMesh.h index 51117f9..b36d740 100644 --- a/Weave/Graphix/Model/IMesh.h +++ b/Weave/Graphix/Model/IMesh.h @@ -20,6 +20,7 @@ public: virtual ~IMesh(); void useModelMat(const mat4& model, Shader* shader) const; + void useMaterial(const vec4& material, Shader* shader) const; //void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; diff --git a/Weave/Graphix/Model/IMetaMesh.cpp b/Weave/Graphix/Model/IMetaMesh.cpp index e95314e..9ef7481 100644 --- a/Weave/Graphix/Model/IMetaMesh.cpp +++ b/Weave/Graphix/Model/IMetaMesh.cpp @@ -80,11 +80,11 @@ void IMetaMesh::bindShader(Shader* _shader) } } -void IMetaMesh::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const +void IMetaMesh::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat, const vec4& _material) const { for (auto i = models.begin(); i != models.end(); ++i) { - (*i)->drawModel(_shader,_texture,_modelMat); + (*i)->drawModel(_shader, _texture, _modelMat, _material); } } diff --git a/Weave/Graphix/Model/IMetaMesh.h b/Weave/Graphix/Model/IMetaMesh.h index 0f20711..8641306 100644 --- a/Weave/Graphix/Model/IMetaMesh.h +++ b/Weave/Graphix/Model/IMetaMesh.h @@ -20,7 +20,7 @@ public: void bindShader(Shader* shader); - void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; + void drawModel(Shader* shader, Texture* texture, const mat4& modelMat, const vec4& material) const; void drawBBox(Shader* shader, const mat4& modelMat) const; diff --git a/Weave/Graphix/Model/Model.cpp b/Weave/Graphix/Model/Model.cpp index 08f087e..f12b67d 100644 --- a/Weave/Graphix/Model/Model.cpp +++ b/Weave/Graphix/Model/Model.cpp @@ -81,7 +81,7 @@ void Model::drawModel() const glBindVertexArray(0); } -void Model::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const +void Model::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat, const vec4& _material) const { //Message::info("Error from before?"); //Graphix::getGlError(); @@ -94,6 +94,9 @@ void Model::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) useModelMat(_modelMat, _shader); //Message::info("IMesh loading MMatrix"); //Graphix::getGlError(); + useMaterial(_material, _shader); + //Message::info("IMesh loading MMatrix"); + //Graphix::getGlError(); drawModel(); //Message::info("IMesh drawing Elements"); //Graphix::getGlError(); @@ -114,7 +117,7 @@ void Model::drawBBox(Shader* _shader,const mat4& _modelMat) const } // BoundingBox->bindShader(_shader); - BoundingBox->drawModel(Graphix::shader_BBox, (Texture*)nullptr, _modelMat*translate(BBposition)*scale(BBsize)); + BoundingBox->drawModel(Graphix::shader_BBox, (Texture*)nullptr, _modelMat*translate(BBposition)*scale(BBsize), vec4(1.0f, 1.0f, 1.0f, 3.0f)); } void Model::updateBB(const vec3& _min, const vec3& _max) @@ -233,6 +236,13 @@ void Model::useModelMat(const mat4& _model, Shader* _shader) const glUniformMatrix4fv(tmp, 1, GL_FALSE, value_ptr(_model)); } +void Model::useMaterial(const vec4& _material, Shader* _shader) const +{ + int tmp = _shader->getUniformLocation("material"); + if (tmp >= 0) + glUniform4fv(tmp, 1, value_ptr(_material)); +} + Model* Model::BoundingBox = nullptr; Shader* Model::BBoxShader = nullptr; bool Model::exBBox = false; \ No newline at end of file diff --git a/Weave/Graphix/Model/Model.h b/Weave/Graphix/Model/Model.h index af8a43e..9a83e93 100644 --- a/Weave/Graphix/Model/Model.h +++ b/Weave/Graphix/Model/Model.h @@ -18,7 +18,7 @@ public: virtual void bindShader(Shader* shader); /* Draws Model */ - virtual void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; + virtual void drawModel(Shader* shader, Texture* texture, const mat4& modelMat, const vec4& material) const; /* Draws a BoundingBox around the Model */ virtual void drawBBox(Shader* shader, const mat4& modelMat) const; @@ -42,6 +42,7 @@ protected: virtual void useModel(Shader* shader) const; virtual void useTexture(Texture* texture, Shader* shader) const; virtual void useModelMat(const mat4& model, Shader* shader) const; + virtual void useMaterial(const vec4& material, Shader* shader) const; virtual void drawModel() const; diff --git a/Weave/Graphix/Model/SkyBox.cpp b/Weave/Graphix/Model/SkyBox.cpp index 9c37e0c..ff7cfd5 100644 --- a/Weave/Graphix/Model/SkyBox.cpp +++ b/Weave/Graphix/Model/SkyBox.cpp @@ -25,10 +25,10 @@ SkyBox::~SkyBox() { } -void SkyBox::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat) const +void SkyBox::drawModel(Shader* _shader, Texture* _texture, const mat4& _modelMat, const vec4& _material) const { glDisable(GL_DEPTH_TEST); - Model::drawModel(_shader, _texture, _modelMat); + Model::drawModel(_shader, _texture, _modelMat, _material); glEnable(GL_DEPTH_TEST); } @@ -36,6 +36,10 @@ void SkyBox::useModelMat(const mat4& _model, Shader* _shader) const { } +void SkyBox::useMaterial(const vec4& _material, Shader* _shader) const +{ +} + void SkyBox::useTexture(Texture* _texture, Shader* _shader) const { } diff --git a/Weave/Graphix/Model/SkyBox.h b/Weave/Graphix/Model/SkyBox.h index 5bf1552..14da4b9 100644 --- a/Weave/Graphix/Model/SkyBox.h +++ b/Weave/Graphix/Model/SkyBox.h @@ -9,10 +9,11 @@ public: SkyBox(); ~SkyBox(); - void drawModel(Shader* shader, Texture* texture, const mat4& modelMat) const; + void drawModel(Shader* shader, Texture* texture, const mat4& modelMat, const vec4& material) const; //uncommend following lines to use default Loaders void useModelMat(const mat4& model, Shader* shader) const; + void useMaterial(const vec4& material, Shader* shader) const; void useTexture(Texture* texture, Shader* shader) const; void drawBBox(Shader* shader, const mat4& modelMat) const; diff --git a/Weave/Scene/Marvin.cpp b/Weave/Scene/Marvin.cpp index 3759689..e0ec59d 100644 --- a/Weave/Scene/Marvin.cpp +++ b/Weave/Scene/Marvin.cpp @@ -3,7 +3,7 @@ #include "../Graphix/Shader.h" -Marvin::Marvin(Shader* _shader, const mat4& _modelMat) : SceneObject(_shader,_modelMat,"Player.dae","model_player_2D.png") +Marvin::Marvin(Shader* _shader, const mat4& _modelMat) : SceneObject(_shader, _modelMat, vec4(7.0f, 0.7f, 1.0f, 3.0f), "Player.dae", "model_player_2D.png") { } diff --git a/Weave/Scene/SceneObject.cpp b/Weave/Scene/SceneObject.cpp index 2845187..51f8b14 100644 --- a/Weave/Scene/SceneObject.cpp +++ b/Weave/Scene/SceneObject.cpp @@ -20,9 +20,10 @@ using std::string; #define YFALL_SPEED 9.8f -SceneObject::SceneObject(Shader* _shader, const mat4& _modelMat, string _modelpath, string texturepath, unsigned int _model_index) : +SceneObject::SceneObject(Shader* _shader, const mat4& _modelMat, const vec4& _material, string _modelpath, string texturepath, unsigned int _model_index) : model(new IMetaMesh(_modelpath)), modelMat(_modelMat), +material(_material), shader(_shader), mainScene(NULL), collision_ignore(false), @@ -48,9 +49,10 @@ yStatic(true) } -SceneObject::SceneObject(Shader* _shader, const mat4& _modelMat, Model* _model, string texturepath) : +SceneObject::SceneObject(Shader* _shader, const mat4& _modelMat, const vec4& _material, Model* _model, string texturepath) : model(_model), modelMat(_modelMat), +material(_material), shader(_shader), mainScene(NULL), collision_ignore(false), @@ -140,7 +142,7 @@ void SceneObject::draw() const // drawModel(); - model->drawModel(shader, texture, modelMat); + model->drawModel(shader, texture, modelMat, material); if (Events::isKToggleActive(SDLK_F6)) model->drawBBox(shader, modelMat); } @@ -168,6 +170,10 @@ void SceneObject::setModel(mat4& _modelMat){ modelMat = _modelMat; } +void SceneObject::setMaterial(vec4& _material){ + material = _material; +} + void SceneObject::turn(float angle, vec3& axis){ //vec3 pos = getPosition(); //modelMat = rotate(degree, axis)*modelMat; diff --git a/Weave/Scene/SceneObject.h b/Weave/Scene/SceneObject.h index a2e7e74..5014fbe 100644 --- a/Weave/Scene/SceneObject.h +++ b/Weave/Scene/SceneObject.h @@ -18,8 +18,8 @@ class SceneObject public: //SceneObject(Shader* _shader, mat4& model); - SceneObject(Shader* _shader, const mat4& model, string modelpath, string texturepath, unsigned int model_index=0); - SceneObject(Shader* _shader, const mat4& modelMat, Model* model, string texturepath); + SceneObject(Shader* _shader, const mat4& model, const vec4& material, string modelpath, string texturepath, unsigned int model_index=0); + SceneObject(Shader* _shader, const mat4& modelMat, const vec4& material, Model* model, string texturepath); virtual ~SceneObject(); @@ -32,6 +32,7 @@ public: __declspec(deprecated) virtual void setModel(mat4&); + virtual void setMaterial(vec4&); __declspec(deprecated) virtual void turn(float angle, vec3& axis); virtual void turnTo(vec3& direction, float speed = 1); @@ -62,6 +63,7 @@ public: protected: mat4 modelMat; + vec4 material; Shader* shader; Texture* texture; Model* model; diff --git a/Weave/Scene/Sky.cpp b/Weave/Scene/Sky.cpp index 2d2b3ca..cedb4cd 100644 --- a/Weave/Scene/Sky.cpp +++ b/Weave/Scene/Sky.cpp @@ -4,7 +4,7 @@ #include "../Graphix/Model.h" -Sky::Sky() : SceneObject(new Shader("skybox_VS.hlsl","skybox_color_FS.hlsl"), mat4(1.f), new SkyBox(), "") +Sky::Sky() : SceneObject(new Shader("skybox_VS.hlsl","skybox_color_FS.hlsl"), mat4(1.f), vec4(1.f), new SkyBox(), "") { } diff --git a/shader/lightingTexture_FS.hlsl b/shader/lightingTexture_FS.hlsl index 24faf93..bf33ed5 100644 --- a/shader/lightingTexture_FS.hlsl +++ b/shader/lightingTexture_FS.hlsl @@ -7,23 +7,24 @@ in float visNormal, SpecularCosPoint1, SpecularCosDirection1; out vec4 FragmentColor; uniform sampler2D uColorTexture; +uniform vec4 material; //vec4 in the form (ambient, point, directional, glossyness); so far it was (1, 1, 1, 3) void main() { - float specularConst = 3.0f; + float specularConst = material[3]; vec3 normal = normalize(worldNormal); vec4 uvColor = texture(uColorTexture, fUVs); + vec3 AmbientLightColor = vec3(0.1f, 0.1f, 0.1f); + vec3 PointLightDirection1 = normalize(worldLightPoint1); vec3 PointLightColor1 = vec3(30.0f, 30.0f, 30.0f); vec3 DirectionalLightDirection1 = normalize(vec3(-2.0f, -2.0f, -2.0f)); vec3 DirectionalLightColor1 = vec3(1.0f, 1.0f, 1.0f); - vec3 AmbientLightColor = vec3(0.1f, 0.1f, 0.1f); - float cosThetaPoint1 = clamp(dot(normal, PointLightDirection1), 0, 1); float cosThetaDirection1 = clamp(dot(normal, DirectionalLightDirection1), 0, 1); @@ -34,9 +35,9 @@ void main() // discard; - FragmentColor = vec4(uvColor.rgb * (AmbientLightColor - + PointLightColor1 * (cosThetaPoint1 + pow(SpecularCosPoint1, specularConst)) / squaredist1 - + DirectionalLightColor1 * (cosThetaDirection1 + pow(SpecularCosDirection1, specularConst)) + FragmentColor = vec4(uvColor.rgb * (AmbientLightColor * material[0] + + PointLightColor1 * material[1] * (cosThetaPoint1 + pow(SpecularCosPoint1, specularConst)) / squaredist1 + + DirectionalLightColor1 * material[2] * (cosThetaDirection1 + pow(SpecularCosDirection1, specularConst)) ), uvColor.a); -- 2.47.3