kick
/Users/morten/Programmering/cpp/kick/src/kick/scene/mesh_renderer.cpp
00001 //
00002 //  mesh_renderer.cpp
00003 //  KickCPP
00004 //
00005 //  Created by Morten Nobel-Jørgensen on 12/12/13.
00006 //  Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved.
00007 //
00008 
00009 #include "kick/scene/mesh_renderer.h"
00010 #include "kick/scene/game_object.h"
00011 #include "kick/mesh/mesh.h"
00012 #include "kick/scene/game_object.h"
00013 #include "kick/core/debug.h"
00014 
00015 namespace kick {
00016     MeshRenderer::MeshRenderer(GameObject *gameObject)
00017     :ComponentRenderable{gameObject} {
00018         mTransform = gameObject->transform();
00019     }
00020     
00021     MeshRenderer::~MeshRenderer(){
00022     }
00023 
00024     void MeshRenderer::render(EngineUniforms *engineUniforms, Material* replacementMaterial){
00025         if (!enabled()){
00026             return;
00027         }
00028         if (mMesh == nullptr){
00029             logWarning("Cannot render mesh is null");
00030         }
00031         for (unsigned int i=0;i< mMaterials.size();i++){
00032             auto material = replacementMaterial ? replacementMaterial : mMaterials[i];
00033             auto shader = material->shader().get();
00034             mMesh->bind(shader);
00035             shader->bind_uniforms(material, engineUniforms, mTransform.get());
00036             mMesh->render(i);
00037         }
00038     }
00039     
00040     void MeshRenderer::setMesh(std::shared_ptr<Mesh> mesh){
00041         this->mMesh = mesh;
00042     }
00043 
00044     std::shared_ptr<Mesh>  MeshRenderer::mesh(){
00045         return mMesh;
00046     }
00047     
00048     void MeshRenderer::setMaterials(const std::vector<Material *> &mats){
00049         isInstanced = false;
00050         this->mMaterials = mats;
00051     }
00052     
00053     const std::vector<Material*> & MeshRenderer::materials(){
00054         return mMaterials;
00055     }
00056     
00057     void MeshRenderer::setMaterial(Material *material){
00058         isInstanced = false;
00059         if (mMaterials.size()==0){
00060             mMaterials.push_back(material);
00061         } else {
00062             mMaterials[0] = material;
00063         }
00064     }
00065     
00066     Material* MeshRenderer::material(){
00067         if (mMaterials.size()==0){
00068             return nullptr;
00069         }
00070         return mMaterials[0];
00071     }
00072 
00073     int MeshRenderer::renderOrder() {
00074         if (mMaterials.size() == 0){
00075             return 0;
00076         }
00077         return mMaterials[0]->renderOrder();
00078     }
00079 
00080     Material *MeshRenderer::instancedMaterial() {
00081         if (!isInstanced){
00082             isInstanced = true;
00083             for (int i=0;i<mMaterials.size();i++){
00084                 mMaterials[i] = new Material(*mMaterials[i]); // memory leak
00085             }
00086         }
00087         return material();
00088     }
00089 
00090     const std::vector<Material *> &MeshRenderer::instancedMaterials() {
00091         instancedMaterial();
00092         return materials();
00093     }
00094 }
 All Classes Functions Variables