kick
/Users/morten/Programmering/cpp/kick/src/kick/scene/camera.h
00001 //
00002 //  camera.h
00003 //  KickCPP
00004 //
00005 //  Created by morten on 8/12/13.
00006 //  Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved.
00007 //
00008 
00009 #pragma once
00010 #include "component.h"
00011 #include <vector>
00012 
00013 #include <glm/glm.hpp>
00014 #include "kick/core/event_listener.h"
00015 #include "kick/math/ray.h"
00016 #include "component_renderable.h"
00017 #include <utility>
00018 #include <functional>
00019 #include "kick/core/kickgl.h"
00020 
00021 namespace kick {
00022     class GameObject;
00023     class Shader;
00024     class Material;
00025     class TextureRenderTarget;
00026     class Texture2D;
00027 
00028     struct PickEntry {
00029         glm::ivec2 point;
00030         glm::ivec2 size;
00031         std::function<void(GameObject*, int)> onPicked;
00032         bool returnNullptrOnNoHit;
00033     };
00034 
00035     class Camera : public Component {
00036     public:
00037         Camera(GameObject *gameObject);
00038         ~Camera();
00039         virtual void deactivated();
00040         virtual void render(EngineUniforms *engineUniforms);
00041 
00042         // reset matrix if used parameters (if any)
00043         virtual void resetProjectionMatrix();
00044         glm::vec4 clearColor();
00045         void setClearColor(glm::vec4 clearColor);
00046         void setClearColorBuffer(bool clear);
00047         bool clearColorBuffer();
00048         void setClearDepthBuffer(bool clear);
00049         bool clearDepthBuffer();
00050         void setClearStencilBuffer(bool clear);
00051         bool clearStencilBuffer();
00052         void setupCamera(EngineUniforms *engineUniforms);
00053         glm::mat4 projectionMatrix();
00054         glm::mat4 viewMatrix();
00055         // override projection matrix
00056         void setProjectionMatrix(glm::mat4 projectionMatrix);
00057         bool shadow() const;
00058         void setShadow(bool renderShadow);
00059         int cullingMask() const;
00060         void setCullingMask(int cullingMask);
00061         TextureRenderTarget *target() const;
00062         void setTarget(TextureRenderTarget *target);
00063 
00064         // callback function when hit (object hit, number of hits)
00065         void pick(glm::ivec2 point, std::function<void(GameObject*,int)> onPicked, glm::ivec2 size = glm::ivec2{1,1}, bool returnNullptrOnNoHit = false);
00066 
00067         std::shared_ptr<Material> const &replacementMaterial() const;
00068         void setReplacementMaterial(std::shared_ptr<Material> const &replacementMaterial);
00069 
00070         glm::vec2 const &viewportOffset() const;
00071         void setViewportOffset(glm::vec2 const &normalizedViewportOffset);
00072         glm::vec2 const &viewportDim() const;
00073         void setViewportDim(glm::vec2 const &normalizedViewportDim);
00074         Ray screenPointToRay(glm::vec2 point);
00075         // Return true if the camera is marked as a mainCamera
00076         bool main();
00077         // Mark the camera as a main camera. The scene should have a single main camera
00078         void setMain(bool main);
00079 
00080         // Set the camera index, which determine the render order
00081         // (lowest index are rendered first)
00082         void setIndex(int index);
00083         int index();
00084 
00085         // Return the main camera (first camera flagged as main) in the active scene.
00086         static std::shared_ptr<Camera> mainCamera();
00087 
00088     protected:
00089         glm::mat4 mProjectionMatrix = glm::mat4{1};
00090         glm::vec2 mNormalizedViewportOffset = glm::vec2(0,0);
00091         glm::vec2 mNormalizedViewportDim = glm::vec2(1,1);
00092     private:
00093         std::vector<ComponentRenderable*> cull();
00094         void initShadowMap();
00095         void destroyShadowMap();
00096         void renderShadowMap(Light* directionalLight);
00097         void createComponentList();
00098         void handleObjectPicking(EngineUniforms *engineUniforms, std::vector<ComponentRenderable*>& components);
00099         TextureRenderTarget*mPickingRenderTarget = nullptr;
00100         std::shared_ptr<Texture2D> mPickingTexture;
00101         std::shared_ptr<Material> mPickingMaterial;
00102         std::shared_ptr<Shader> mShadowMapShader;
00103         std::shared_ptr<Material> mReplacementMaterial;
00104         Material*mShadowMapMaterial;
00105         EventListener<std::pair<std::shared_ptr<Component>, ComponentUpdateStatus>> componentListener;
00106         void setupViewport(glm::vec2 &offset, glm::vec2 &dim);
00107         std::vector<std::shared_ptr<ComponentRenderable>> mRenderableComponents;
00108         glm::vec4 mClearColor = glm::vec4(0,0,0,1);
00109         int mCullingMask = 0xFFFFFEFF;
00110         int mClearFlag  = GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT; // default clear color clear depth
00111         bool mShadow = false;
00112         TextureRenderTarget*mTarget = nullptr;
00113         std::vector<PickEntry> mPickQueue;
00114         bool mMainCamera = true;
00115         int mIndex = 0;
00116     };
00117 }
 All Classes Functions Variables