diff options
| author | git perforce import user <a@b> | 2016-10-25 12:29:14 -0600 |
|---|---|---|
| committer | Sheikh Dawood Abdul Ajees <Sheikh Dawood Abdul Ajees> | 2016-10-25 18:56:37 -0500 |
| commit | 3dfe2108cfab31ba3ee5527e217d0d8e99a51162 (patch) | |
| tree | fa6485c169e50d7415a651bf838f5bcd0fd3bfbd /KaplaDemo/samples/sampleViewer3/Shader.h | |
| download | physx-3.4-3dfe2108cfab31ba3ee5527e217d0d8e99a51162.tar.xz physx-3.4-3dfe2108cfab31ba3ee5527e217d0d8e99a51162.zip | |
Initial commit:
PhysX 3.4.0 Update @ 21294896
APEX 1.4.0 Update @ 21275617
[CL 21300167]
Diffstat (limited to 'KaplaDemo/samples/sampleViewer3/Shader.h')
| -rw-r--r-- | KaplaDemo/samples/sampleViewer3/Shader.h | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/KaplaDemo/samples/sampleViewer3/Shader.h b/KaplaDemo/samples/sampleViewer3/Shader.h new file mode 100644 index 00000000..a61ccc41 --- /dev/null +++ b/KaplaDemo/samples/sampleViewer3/Shader.h @@ -0,0 +1,146 @@ +#ifndef SHADER_H +#define SHADER_H + +#include "foundation/PxVec3.h" +#include "foundation/PxMat33.h" +#include "foundation/PxTransform.h" + +#include <GL/glew.h> +#include <map> +#include <vector> +#include <string> + +using namespace physx; + +#define STRINGIFY(A) #A + +// ---------------------------------------------------------------------------------- +struct ShaderMaterial +{ + void init(unsigned int texId = 0) { + this->texId = texId; + ambientCoeff = 0.0f; + diffuseCoeff = 1.0f; + specularCoeff = 0.0f; + reflectionCoeff = 0.0f; + refractionCoeff = 0.0f; + shadowCoeff = 0.0f; + color[0] = 1.0f; color[1] = 1.0f; color[2] = 1.0f; color[3] = 1.0f; + } + void setColor(float r, float g, float b, float a = 1.0f) { + color[0] = r; color[1] = g; color[2] = b; color[3] = a; + } + bool operator == (const ShaderMaterial &m) const { + if (texId != m.texId) return false; + if (color[0] != m.color[0] || color[1] != m.color[1] || color[2] != m.color[2] || color[3] != m.color[3]) return false; + return true; + } + unsigned int texId; + float color[4]; + float ambientCoeff; + float diffuseCoeff; + float specularCoeff; + float reflectionCoeff; + float refractionCoeff; + float shadowCoeff; + +}; + +// ---------------------------------------------------------------------------------- +class Shader +{ +public: + Shader(); + virtual ~Shader(); + + bool isValid(); + virtual void activate(); + virtual void activate(const ShaderMaterial &mat); + virtual void deactivate(); + operator GLuint () + { + return mShaderProgram; + } + + const std::string &getErrorMessage() { return mErrorMessage; } + GLuint getShaderProgram() {return mShaderProgram;} + +//protected: + bool loadShaders(const char* vertexShaderFile, const char* fragmentShaderFile); + bool loadShaders(const char* vertexShaderFile, const char* geometryShaderFile, const char* fragmentShaderFile); + bool loadShaderCode(const char* vertexShaderCode, const char* fragmentShaderCode); + bool loadShaderCode(const char* vertexShaderCode, const char* geometryShaderCode, const char* fragmentShaderCode); + void deleteShaders(); + + bool compileShaders(); + void getCompileError(GLuint shader); + void getLinkError(GLuint shader); + void findVariables(); + void bindTexture(const char *name, GLuint tex, GLenum target, GLint unit); + + GLuint mShaderProgram; + GLuint mVertexShader; + GLuint mGeometryShader; + GLuint mFragmentShader; + GLuint mGlShaderAttributes; + + char* mVertexShaderSource; + int mVertexShaderSourceLength; + + char* mGeometryShaderSource; + int mGeometryShaderSourceLength; + + char* mFragmentShaderSource; + int mFragmentShaderSourceLength; + + struct UniformVariable + { + GLint size; + GLenum type; + GLint index; + }; + + struct AttributeVariable + { + GLint size; + GLenum type; + GLint index; + }; + + enum glShaderAttribute + { + gl_VERTEX = (1 << 0), + gl_NORMAL = (1 << 1), + gl_COLOR = (1 << 2), + gl_TEXTURE = (1 << 3), + }; + + typedef std::map<std::string, UniformVariable> tUniforms; + tUniforms mUniforms; + typedef std::map<std::string, AttributeVariable> tAttributes; + tAttributes mAttributes; + + std::string mErrorMessage; + +public: + virtual bool setUniform(const char* name, const PxMat33& value); + virtual bool setUniform(const char* name, const PxTransform& value); + virtual bool setUniform(const char *name, PxU32 size, const PxVec3* value); + + virtual bool setUniform1(const char* name, float val); + virtual bool setUniform2(const char* name, float val0, float val1); + virtual bool setUniform3(const char* name, float val0, float val1, float val2); + virtual bool setUniform4(const char* name, float val0, float val1, float val2, float val3); + virtual bool setUniformfv(const GLchar *name, GLfloat *v, int elementSize, int count=1); + + virtual bool setUniform(const char* name, float value); + virtual bool setUniform(const char* name, int value); + virtual bool setUniformMatrix4fv(const GLchar *name, const GLfloat *m, bool transpose); + + virtual GLint getUniformCommon(const char* name); + + virtual bool setAttribute(const char* name, PxU32 size, PxU32 stride, GLenum type, void* data); + bool loadFile(const char* filename, char*& destination, int& destinationLength); +}; + +#endif
\ No newline at end of file |