From 99f99bea470ace9577950db8976aa092a678635d Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 25 Jan 2016 13:54:09 +0100 Subject: [PATCH] Simplified shader matrix uniforms --- src/raylib.h | 42 +++++++++++++++++++++--------------------- src/rlgl.c | 25 ++++++++++++++----------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index 097c8865..48aeda54 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -358,12 +358,31 @@ typedef struct Shader { int mapSpecularLoc; // Specular map texture uniform location point (fragment shader) } Shader; +// Material type +// TODO: Redesign material-shaders-textures system +typedef struct Material { + //Shader shader; + + //Texture2D texDiffuse; // Diffuse texture + //Texture2D texNormal; // Normal texture + //Texture2D texSpecular; // Specular texture + + Color colDiffuse; + Color colAmbient; + Color colSpecular; + + float glossiness; + float normalDepth; +} Material; + // 3d Model type +// TODO: Replace shader/testure by material typedef struct Model { Mesh mesh; Matrix transform; Texture2D texture; // Only for OpenGL 1.1, on newer versions this should be in the shader Shader shader; + //Material material; } Model; // Ray type (useful for raycast) @@ -387,26 +406,6 @@ typedef struct Wave { short channels; } Wave; -// Light type -typedef struct Light { - Vector3 position; - Vector3 direction; - float intensity; - float specIntensity; - Color diffuse; - Color ambient; - Color specular; -} Light; - -// Material type -typedef struct Material { - Color diffuse; - Color ambient; - Color specular; - float glossiness; - float normalDepth; -} Material; - // Texture formats // NOTE: Support depends on OpenGL version and platform typedef enum { @@ -535,11 +534,12 @@ void BeginDrawing(void); // Setup drawing can void BeginDrawingEx(int blendMode, Shader shader, Matrix transform); // Setup drawing canvas with extended parameters void EndDrawing(void); // End canvas drawing and Swap Buffers (Double Buffering) -void Begin3dMode(Camera cam); // Initializes 3D mode for drawing (Camera setup) +void Begin3dMode(Camera camera); // Initializes 3D mode for drawing (Camera setup) void End3dMode(void); // Ends 3D mode and returns to default 2D orthographic mode Ray GetMouseRay(Vector2 mousePosition, Camera camera); // Returns a ray trace from mouse position Vector2 WorldToScreen(Vector3 position, Camera camera); // Returns the screen space position from a 3d world space position +Matrix GetCameraMatrix(Camera camera); // Returns camera transform matrix (view matrix) void SetTargetFPS(int fps); // Set target FPS (maximum) float GetFPS(void); // Returns current FPS diff --git a/src/rlgl.c b/src/rlgl.c index ec909385..49300054 100644 --- a/src/rlgl.c +++ b/src/rlgl.c @@ -1477,8 +1477,8 @@ void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 r // NOTE: Drawing in OpenGL 3.3+, matrices are passed to shader // TODO: Reduce number of matrices passed to shaders, use only matMVP - glUniformMatrix4fv(model.shader.modelLoc, 1, false, MatrixToFloat(matModel)); - glUniformMatrix4fv(model.shader.viewLoc, 1, false, MatrixToFloat(matView)); + //glUniformMatrix4fv(model.material.shader.modelLoc, 1, false, MatrixToFloat(matModel)); + //glUniformMatrix4fv(model.material.shader.viewLoc, 1, false, MatrixToFloat(matView)); glUniformMatrix4fv(model.shader.mvpLoc, 1, false, MatrixToFloat(matMVP)); @@ -2201,9 +2201,6 @@ Shader LoadShader(char *vsFileName, char *fsFileName) // Get handles to GLSL uniform locations (vertex shader) shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix"); - - shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix"); - shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix"); // Get handles to GLSL uniform locations (fragment shader) shader.tintColorLoc = glGetUniformLocation(shader.id, "fragTintColor"); @@ -2503,6 +2500,18 @@ void SetShaderValuei(Shader shader, int uniformLoc, int *value, int size) #endif } +// Set shader uniform value (matrix 4x4) +void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat) +{ +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) + glUseProgram(shader.id); + + glUniformMatrix4fv(uniformLoc, 1, false, MatrixToFloat(mat)); + + glUseProgram(0); +#endif +} + // Default diffuse shader map texture assignment void SetShaderMapDiffuse(Shader *shader, Texture2D texture) { @@ -2741,9 +2750,6 @@ static Shader LoadDefaultShader(void) // Get handles to GLSL uniform locations (vertex shader) shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix"); - - shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix"); - shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix"); // Get handles to GLSL uniform locations (fragment shader) shader.tintColorLoc = -1; @@ -2822,9 +2828,6 @@ static Shader LoadSimpleShader(void) // Get handles to GLSL uniform locations (vertex shader) shader.mvpLoc = glGetUniformLocation(shader.id, "mvpMatrix"); - - shader.modelLoc = glGetUniformLocation(shader.id, "modelMatrix"); - shader.viewLoc = glGetUniformLocation(shader.id, "viewMatrix"); // Get handles to GLSL uniform locations (fragment shader) shader.tintColorLoc = glGetUniformLocation(shader.id, "fragTintColor");