kick
/Users/morten/Programmering/cpp/kick/src/kick/scene/line_renderer.cpp
00001 //
00002 // Created by Morten Nobel-Jørgensen on 25/08/14.
00003 //
00004 
00005 #include "line_renderer.h"
00006 #include "kick/core/project.h"
00007 #include "kick/mesh/mesh.h"
00008 #include "kick/core/debug.h"
00009 
00010 using namespace std;
00011 using namespace glm;
00012 
00013 namespace kick {
00014     LineRenderer::LineRenderer(GameObject *gameObject) : ComponentRenderable(gameObject) {
00015         mMesh = make_shared<Mesh>();
00016         auto meshData = make_shared<MeshData>();
00017         auto shader = Project::loadShader("assets/shaders/unlit.shader");
00018         mMaterial = new Material();
00019         mMaterial->setShader(shader);
00020         mMesh->setMeshData(meshData);
00021         mTransform = gameObject->transform();
00022     }
00023 
00024     LineRenderer::~LineRenderer() {
00025     }
00026 
00027     std::vector<glm::vec3> const &LineRenderer::points() const {
00028         return mPoints;
00029     }
00030 
00031     void LineRenderer::setPoints(const std::vector<glm::vec3> &points, MeshType meshType, const std::vector<GLushort> &indices) {
00032         if (meshType != MeshType::Lines && meshType != MeshType::LineLoop && meshType != MeshType::LineStrip){
00033             logWarning("Invalid MeshType for line.");
00034         }
00035         LineRenderer::mMeshType = meshType;
00036         LineRenderer::mPoints = points;
00037 
00038         LineRenderer::mIndices = indices;
00039 
00040         rebuildMesh();
00041     }
00042 
00043     void LineRenderer::render(EngineUniforms *engineUniforms, Material* replacementMaterial) {
00044         if (!enabled()){
00045             return;
00046         }
00047 #ifndef KICK_CONTEXT_ES2
00048         if (mSmoothLine){
00049             glEnable(GL_LINE_SMOOTH);
00050         } else {
00051             glDisable(GL_LINE_SMOOTH);
00052         }
00053 #endif
00054         auto mat = replacementMaterial?replacementMaterial : mMaterial;
00055         auto shader = mat->shader().get();
00056         mMesh->bind(shader);
00057         shader->bind_uniforms(mat, engineUniforms, mTransform.get());
00058         mMesh->render(0);
00059     }
00060 
00061     void LineRenderer::setMaterial(Material *material) {
00062         this->mMaterial = material;
00063     }
00064 
00065     Material *LineRenderer::material() {
00066         return mMaterial;
00067     }
00068 
00069     int LineRenderer::renderOrder() {
00070         return mMaterial->renderOrder();
00071     }
00072 
00073     void LineRenderer::rebuildMesh() {
00074         auto meshData = mMesh->meshData();
00075         meshData->setPosition(mPoints);
00076         meshData->recomputeBounds();
00077         meshData->setSubmesh(0, mIndices, mMeshType);
00078         mMesh->setMeshData(meshData);
00079     }
00080 
00081     bool LineRenderer::smoothLine() const {
00082         return mSmoothLine;
00083     }
00084 
00085     void LineRenderer::setSmoothLine(bool smoothLine) {
00086         mSmoothLine = smoothLine;
00087     }
00088 
00089     std::vector<GLushort> const &LineRenderer::indices() const {
00090         return mIndices;
00091     }
00092 }
 All Classes Functions Variables