kick
|
00001 // 00002 // Created by morten on 18/05/14. 00003 // 00004 00005 00006 #pragma once 00007 #include "kick/core/kickgl.h" 00008 #include "kick/mesh/mesh_data.h" 00009 #include <string> 00010 00011 namespace kick { 00012 00013 class Shader; 00014 00015 enum class ShaderEventType { 00016 all, 00017 shader, 00018 defaultUniform 00019 }; 00020 00021 struct ShaderEvent { 00022 Shader *shader; 00023 ShaderEventType eventType; 00024 }; 00025 00026 enum class ShaderType { 00027 FragmentShader = GL_FRAGMENT_SHADER, 00028 VertexShader = GL_VERTEX_SHADER, 00029 #ifndef GL_ES_VERSION_2_0 00030 GeometryShader = GL_GEOMETRY_SHADER 00031 #endif 00032 }; 00033 00034 enum class ShaderErrorType { 00035 FragmentShader = GL_FRAGMENT_SHADER, 00036 VertexShader = GL_VERTEX_SHADER, 00037 #ifndef GL_ES_VERSION_2_0 00038 GeometryShader = GL_GEOMETRY_SHADER, 00039 #endif 00040 Linker, 00041 IncompleteShader 00042 }; 00043 00044 enum class BlendType { 00045 Zero = GL_ZERO, 00046 One = GL_ONE, 00047 SrcColor = GL_SRC_COLOR, 00048 OneMinusSrcColor = GL_ONE_MINUS_SRC_COLOR, 00049 DstColor = GL_DST_COLOR, 00050 OneMinusDstColor = GL_ONE_MINUS_DST_COLOR, 00051 SrcAlpha = GL_SRC_ALPHA, 00052 OneMinusSrcAlpha = GL_ONE_MINUS_SRC_ALPHA, 00053 DstAlpha = GL_DST_ALPHA, 00054 OneMinusDstAlpha = GL_ONE_MINUS_DST_ALPHA, 00055 ConstantColor = GL_CONSTANT_COLOR, 00056 OneMinusConstantColor = GL_ONE_MINUS_CONSTANT_COLOR, 00057 ConstantAlpha = GL_CONSTANT_ALPHA, 00058 OneMinusConstantAlpha = GL_ONE_MINUS_CONSTANT_ALPHA 00059 }; 00060 00061 enum class FaceCullingType { 00062 Front = GL_FRONT, 00063 Back = GL_BACK, 00064 FrontAndBack = GL_FRONT_AND_BACK, 00065 None = GL_NONE 00066 }; 00067 00068 enum class ZTestType { 00069 Never = GL_NEVER, 00070 Less = GL_LESS, 00071 Equal = GL_EQUAL, 00072 LEqual = GL_LEQUAL, 00073 Greater = GL_GREATER, 00074 NotEqual = GL_NOTEQUAL, 00075 GEqual = GL_GEQUAL, 00076 Always = GL_ALWAYS 00077 }; 00078 00079 bool toEnum(int value, ShaderType& enumValue); 00080 bool toEnum(int value, ShaderErrorType& enumValue); 00081 bool toEnum(int value, BlendType& enumValue); 00082 bool toEnum(int value, FaceCullingType& enumValue); 00083 bool toEnum(int value, ZTestType& enumValue); 00084 bool toEnum(int value, FaceCullingType& enumValue); 00085 00086 class ShaderObj { 00087 public: 00088 ShaderObj() {} 00089 explicit ShaderObj(ShaderType type); 00090 ~ShaderObj(); 00091 ShaderObj(ShaderObj&&); 00092 ShaderObj(const ShaderObj&) = delete; 00093 ShaderObj& operator=(ShaderObj&& other); 00094 void detach(GLuint shaderProgram); 00095 operator GLuint() { return shader; } 00096 private: 00097 GLuint shader = 0; 00098 }; 00099 00100 00101 struct ShaderBuildException { 00102 std::string errorMessage; 00103 ShaderErrorType errorType; 00104 std::string codeSnippet; 00105 static void logCurrentCompileException(GLuint shader, ShaderErrorType type, std::string source); 00106 static std::string extractLines(std::string errorMessage, std::string source, int extraLines = 2); 00107 }; 00108 00109 struct AttributeDescriptor { 00110 GLuint index; 00111 GLint size; 00112 GLenum type; 00113 std::string name; 00114 VertexAttributeSemantic semantic; 00115 }; 00116 00117 struct UniformDescriptor { 00118 GLuint index; 00119 GLint size; 00120 GLenum type; 00121 std::string name; 00122 }; 00123 00124 namespace UniformNames { 00125 const std::string modelMatrix{"_modelMatrix"}; 00126 const std::string mv{"_mv"}; 00127 const std::string norm{"_norm"}; 00128 const std::string v{"_v"}; 00129 const std::string worldCamPos{"_worldCamPos"}; 00130 const std::string world2object{"_world2object"}; 00131 const std::string mvProj{"_mvProj"}; 00132 const std::string proj{"_proj"}; 00133 const std::string gameObjectUID{"_gameObjectUID"}; 00134 const std::string shadowMapTexture{"_shadowMapTexture"}; 00135 const std::string lightMat{"_lightMat"}; 00136 const std::string ambient{"_ambient"}; 00137 const std::string pointLight{"_pLights[0]"}; 00138 const std::string directionalLight{"_dLight"}; 00139 const std::string directionalLightWorld{"_dLightWorldDir"}; 00140 const std::string time{"_time"}; 00141 const std::string viewport{"_viewport"}; 00142 00143 const static std::string list[] = { 00144 modelMatrix, 00145 mv, 00146 norm, 00147 v, 00148 worldCamPos, 00149 world2object, 00150 mvProj, 00151 proj, 00152 gameObjectUID, 00153 shadowMapTexture, 00154 lightMat, 00155 ambient, 00156 pointLight, 00157 directionalLight, 00158 directionalLightWorld, 00159 time, 00160 viewport 00161 }; 00162 }; 00163 00164 std::string to_string(AttributeDescriptor attDesc); 00165 00166 std::string to_string(UniformDescriptor uniformDesc); 00167 } 00168 00169