kick
|
00001 // 00002 // shader.h 00003 // KickCPP 00004 // 00005 // Created by morten on 8/25/13. 00006 // Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved. 00007 // 00008 00009 #pragma once 00010 00011 #include <iostream> 00012 #include <string> 00013 #include <map> 00014 #include <vector> 00015 00016 #include <glm/glm.hpp> 00017 #include "kick/core/event.h" 00018 #include "kick/core/kickgl.h" 00019 #include "kick/core/project_asset.h" 00020 #include "kick/mesh/mesh_data.h" 00021 #include "kick/material/shader_enums.h" 00022 #include "kick/material/material.h" 00023 00024 namespace kick { 00025 00026 class Material; 00027 struct EngineUniforms; 00028 class Transform; 00029 00030 class Project; 00031 class Shader; 00032 00033 class Shader : public ProjectAsset { 00034 public: 00036 Shader(std::string vertexShader = "", std::string fragmentShader = "", std::string geometryShader = ""); 00037 Shader(Shader&& s); 00038 Shader(const Shader&) = delete; 00039 ~Shader(); 00040 Shader& operator=(Shader&& other); 00041 void setShaderSource(ShaderType type, std::string str); 00042 std::string getShaderSource(ShaderType type) const; 00043 std::string getPrecompiledShaderSource(ShaderType type) const; 00045 bool apply(); 00046 void bind(); 00047 const std::vector<AttributeDescriptor >& getShaderAttributes() const; 00048 const std::vector<UniformDescriptor >& getShaderUniforms() const; 00049 const AttributeDescriptor* getShaderAttribute(VertexAttributeSemantic semantic) const; 00050 const UniformDescriptor* getShaderUniform(std::string name) const; 00051 Event<ShaderEvent> shaderChanged; 00052 void setBlend(bool b); 00053 bool blend(); 00054 BlendType blendDFactorAlpha(); 00055 BlendType blendDFactorRGB(); 00056 BlendType blendSFactorAlpha(); 00057 BlendType blendSFactorRGB(); 00058 void setBlendDFactorAlpha(BlendType blendDFactorAlpha); 00059 void setBlendDFactorRGB(BlendType blendDFactorRGB); 00060 void setBlendSFactorAlpha(BlendType blendSFactorAlpha); 00061 void setBlendSFactorRGB(BlendType blendSFactorRGB); 00062 void setDepthWrite(bool depthMask); 00063 bool depthWrite(); 00064 void setFaceCulling(FaceCullingType faceCulling); 00065 FaceCullingType faceCulling(); 00066 void setPolygonOffsetEnabled(bool polygonOffsetEnabled); 00067 bool polygonOffsetEnabled(); 00068 void setPolygonOffsetFactorAndUnit(glm::vec2 polygonOffsetFactorAndUnit); 00069 glm::vec2 polygonOffsetFactorAndUnit(); 00070 void setZTest(ZTestType zTest); 00071 ZTestType zTest(); 00072 const std::vector<AttributeDescriptor>&shaderAttributes() { return mShaderAttributes; } 00073 void bind_uniforms(Material *material, EngineUniforms *engineUniforms, Transform* transform); 00074 GLuint shaderProgram(){ return mShaderProgram; } 00075 00076 void setDefaultUniform(std::string name, int value); 00077 void setDefaultUniform(std::string name, float value); 00078 void setDefaultUniform(std::string name, glm::vec4 value); 00079 void setDefaultUniform(std::string name, glm::mat3 value); 00080 void setDefaultUniform(std::string name, glm::mat4 value); 00081 void setDefaultUniform(std::string name, std::shared_ptr<Texture2D> value); 00082 void setDefaultUniform(std::string name, std::shared_ptr<TextureCube> value); 00083 00084 bool tryGetDefaultUniform(std::string name, MaterialData& value); 00085 static std::string getPrecompiledSource(std::string source, ShaderType type); 00086 00087 int getRenderOrder() const; 00088 void setRenderOrder(int renderOrder); 00089 private: 00090 // Default uniform are assigned to materials where uniforms are not mapped 00091 template <class E> 00092 void setDefaultUniformInternal(std::string name, E value); 00093 static void translateToGLSLES(std::string& s, ShaderType type); 00094 void updateShaderLocation(std::string name, MaterialData& value); 00095 void setDefaultUniformData(std::string name, MaterialData&& value); 00096 void updateDefaultShaderLocation(); 00098 bool linkProgram(); 00100 ShaderObj compileShader(std::string source, ShaderType type); 00101 GLuint mShaderProgram = 0; 00102 std::map<ShaderType, std::string> shaderSources; 00103 std::string outputAttributeName = "fragColor"; 00104 std::vector<AttributeDescriptor> mShaderAttributes; 00105 std::vector<UniformDescriptor> shaderUniforms; 00106 bool mBlend{false}; 00107 BlendType mBlendDFactorAlpha{BlendType::OneMinusSrcAlpha}; 00108 BlendType mBlendDFactorRGB{BlendType::OneMinusSrcAlpha}; 00109 BlendType mBlendSFactorAlpha{BlendType::SrcAlpha}; 00110 BlendType mBlendSFactorRGB{BlendType::SrcAlpha}; 00111 bool mDepthBufferWrite{true}; 00112 FaceCullingType mFaceCulling{FaceCullingType::Back}; 00113 bool mPolygonOffsetEnabled{false}; 00114 glm::vec2 mPolygonOffsetFactorAndUnit{2.5, 10}; 00115 ZTestType mZTest{ZTestType::Less}; 00116 std::map<std::string, MaterialData> defaultUniformData; 00117 int mDenderOrder = 1000; 00118 std::map<std::string, std::shared_ptr<Texture2D>> texture2DRef; 00119 std::map<std::string, std::shared_ptr<TextureCube>> textureCubeRef; 00120 }; 00121 00122 template <class E> 00123 void Shader::setDefaultUniformInternal(std::string name, E value){ 00124 MaterialData data{value}; 00125 setDefaultUniformData(name, std::move(data)); 00126 } 00127 }