aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/resources/shaders/standard.fs136
-rw-r--r--examples/resources/shaders/standard.vs23
-rw-r--r--examples/shaders_standard_lighting.c118
-rw-r--r--src/raylib.h15
-rw-r--r--src/rlgl.c213
-rw-r--r--src/rlgl.h32
6 files changed, 388 insertions, 149 deletions
diff --git a/examples/resources/shaders/standard.fs b/examples/resources/shaders/standard.fs
new file mode 100644
index 00000000..30c841d2
--- /dev/null
+++ b/examples/resources/shaders/standard.fs
@@ -0,0 +1,136 @@
+#version 330
+
+in vec3 fragPosition;
+in vec2 fragTexCoord;
+in vec4 fragColor;
+in vec3 fragNormal;
+
+out vec4 finalColor;
+
+uniform sampler2D texture0;
+
+uniform vec4 colAmbient;
+uniform vec4 colDiffuse;
+uniform vec4 colSpecular;
+uniform float glossiness;
+
+uniform mat4 modelMatrix;
+uniform vec3 viewDir;
+
+struct Light {
+ int enabled;
+ int type;
+ vec3 position;
+ vec3 direction;
+ vec4 diffuse;
+ float intensity;
+ float attenuation;
+ float coneAngle;
+};
+
+const int maxLights = 8;
+uniform int lightsCount;
+uniform Light lights[maxLights];
+
+vec3 CalcPointLight(Light l, vec3 n, vec3 v)
+{
+ vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1));
+ vec3 surfaceToLight = l.position - surfacePos;
+
+ // Diffuse shading
+ float brightness = clamp(dot(n, surfaceToLight)/(length(surfaceToLight)*length(n)), 0, 1);
+ float diff = 1.0/dot(surfaceToLight/l.attenuation, surfaceToLight/l.attenuation)*brightness*l.intensity;
+
+ // Specular shading
+ float spec = 0.0;
+ if(diff > 0.0)
+ {
+ vec3 h = normalize(-l.direction + v);
+ spec = pow(dot(n, h), 3 + glossiness);
+ }
+
+ return (diff*l.diffuse.rgb*colDiffuse.rgb + spec*colSpecular.rgb);
+}
+
+vec3 CalcDirectionalLight(Light l, vec3 n, vec3 v)
+{
+ vec3 lightDir = normalize(-l.direction);
+
+ // Diffuse shading
+ float diff = clamp(dot(n, lightDir), 0.0, 1.0)*l.intensity;
+
+ // Specular shading
+ float spec = 0.0;
+ if(diff > 0.0)
+ {
+ vec3 h = normalize(lightDir + v);
+ spec = pow(dot(n, h), 3 + glossiness);
+ }
+
+ // Combine results
+ return (diff*l.intensity*l.diffuse.rgb*colDiffuse.rgb + spec*colSpecular.rgb);
+}
+
+vec3 CalcSpotLight(Light l, vec3 n, vec3 v)
+{
+ vec3 surfacePos = vec3(modelMatrix*vec4(fragPosition, 1));
+ vec3 lightToSurface = normalize(surfacePos - l.position);
+ vec3 lightDir = normalize(-l.direction);
+
+ // Diffuse shading
+ float diff = clamp(dot(n, lightDir), 0.0, 1.0)*l.intensity;
+
+ // Spot attenuation
+ float attenuation = clamp(dot(n, lightToSurface), 0.0, 1.0);
+ attenuation = dot(lightToSurface, -lightDir);
+ float lightToSurfaceAngle = degrees(acos(attenuation));
+ if(lightToSurfaceAngle > l.coneAngle) attenuation = 0.0;
+ float falloff = (l.coneAngle - lightToSurfaceAngle)/l.coneAngle;
+
+ // Combine diffuse and attenuation
+ float diffAttenuation = diff*attenuation;
+
+ // Specular shading
+ float spec = 0.0;
+ if(diffAttenuation > 0.0)
+ {
+ vec3 h = normalize(lightDir + v);
+ spec = pow(dot(n, h), 3 + glossiness);
+ }
+
+ return falloff*(diffAttenuation*l.diffuse.rgb + spec*colSpecular.rgb);
+}
+
+void main()
+{
+ // Calculate fragment normal in screen space
+ mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
+ vec3 normal = normalize(normalMatrix*fragNormal);
+
+ // Normalize normal and view direction vectors
+ vec3 n = normalize(normal);
+ vec3 v = normalize(viewDir);
+
+ // Calculate diffuse texture color fetching
+ vec4 texelColor = texture(texture0, fragTexCoord);
+ vec3 lighting = colAmbient.rgb;
+
+ for(int i = 0; i < lightsCount; i++)
+ {
+ // Check if light is enabled
+ if(lights[i].enabled == 1)
+ {
+ // Calculate lighting based on light type
+ switch(lights[i].type)
+ {
+ case 0: lighting += CalcPointLight(lights[i], n, v); break;
+ case 1: lighting += CalcDirectionalLight(lights[i], n, v); break;
+ case 2: lighting += CalcSpotLight(lights[i], n, v); break;
+ default: break;
+ }
+ }
+ }
+
+ // Calculate final fragment color
+ finalColor = vec4(texelColor.rgb*lighting, texelColor.a);
+}
diff --git a/examples/resources/shaders/standard.vs b/examples/resources/shaders/standard.vs
new file mode 100644
index 00000000..fc0a5ff4
--- /dev/null
+++ b/examples/resources/shaders/standard.vs
@@ -0,0 +1,23 @@
+#version 330
+
+in vec3 vertexPosition;
+in vec3 vertexNormal;
+in vec2 vertexTexCoord;
+in vec4 vertexColor;
+
+out vec3 fragPosition;
+out vec2 fragTexCoord;
+out vec4 fragColor;
+out vec3 fragNormal;
+
+uniform mat4 mvpMatrix;
+
+void main()
+{
+ fragPosition = vertexPosition;
+ fragTexCoord = vertexTexCoord;
+ fragColor = vertexColor;
+ fragNormal = vertexNormal;
+
+ gl_Position = mvpMatrix*vec4(vertexPosition, 1.0);
+} \ No newline at end of file
diff --git a/examples/shaders_standard_lighting.c b/examples/shaders_standard_lighting.c
new file mode 100644
index 00000000..7a9cc086
--- /dev/null
+++ b/examples/shaders_standard_lighting.c
@@ -0,0 +1,118 @@
+/*******************************************************************************************
+*
+* raylib [shaders] example - Standard lighting (materials and lights)
+*
+* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
+* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
+*
+* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
+* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
+* raylib comes with shaders ready for both versions, check raylib/shaders install folder
+*
+* This example has been created using raylib 1.3 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Copyright (c) 2016 Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+#include "raymath.h"
+
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ int screenWidth = 800;
+ int screenHeight = 450;
+
+ SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
+
+ InitWindow(screenWidth, screenHeight, "raylib [shaders] example - model shader");
+
+ // Define the camera to look into our 3d world
+ Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
+ Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
+
+ Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
+ Texture2D texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model diffuse texture
+
+ Material material = LoadStandardMaterial();
+ material.texDiffuse = texDiffuse;
+ material.colDiffuse = (Color){255, 255, 255, 255};
+ material.colAmbient = (Color){0, 0, 10, 255};
+ material.colSpecular = (Color){255, 255, 255, 255};
+ material.glossiness = 50.0f;
+ dwarf.material = material; // Apply material to model
+
+ Light spotLight = CreateLight(LIGHT_SPOT, (Vector3){3.0f, 5.0f, 2.0f}, (Color){255, 255, 255, 255});
+ spotLight->target = (Vector3){0.0f, 0.0f, 0.0f};
+ spotLight->intensity = 2.0f;
+ spotLight->diffuse = (Color){255, 100, 100, 255};
+ spotLight->coneAngle = 60.0f;
+
+ Light dirLight = CreateLight(LIGHT_DIRECTIONAL, (Vector3){0.0f, -3.0f, -3.0f}, (Color){255, 255, 255, 255});
+ dirLight->target = (Vector3){1.0f, -2.0f, -2.0f};
+ dirLight->intensity = 2.0f;
+ dirLight->diffuse = (Color){100, 255, 100, 255};
+
+ Light pointLight = CreateLight(LIGHT_POINT, (Vector3){0.0f, 4.0f, 5.0f}, (Color){255, 255, 255, 255});
+ pointLight->intensity = 2.0f;
+ pointLight->diffuse = (Color){100, 100, 255, 255};
+ pointLight->attenuation = 3.0f;
+
+ // Setup orbital camera
+ SetCameraMode(CAMERA_ORBITAL); // Set a orbital camera mode
+ SetCameraPosition(camera.position); // Set internal camera position to match our camera position
+ SetCameraTarget(camera.target); // Set internal camera target to match our camera target
+
+ SetTargetFPS(60); // Set our game to run at 60 frames-per-second
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ // Update
+ //----------------------------------------------------------------------------------
+ UpdateCamera(&camera); // Update internal camera and our camera
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ Begin3dMode(camera);
+
+ DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
+
+ DrawLights(); // Draw all created lights in 3D world
+
+ DrawGrid(10, 1.0f); // Draw a grid
+
+ End3dMode();
+
+ DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
+
+ DrawFPS(10, 10);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ UnloadMaterial(material); // Unload material and assigned textures
+ UnloadModel(dwarf); // Unload model
+
+ // Destroy all created lights
+ DestroyLight(pointLight);
+ DestroyLight(dirLight);
+ DestroyLight(spotLight);
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+} \ No newline at end of file
diff --git a/src/raylib.h b/src/raylib.h
index 48534fd6..9cd02fd8 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -418,7 +418,7 @@ typedef struct Material {
Color colAmbient; // Ambient color
Color colSpecular; // Specular color
- float glossiness; // Glossiness level
+ float glossiness; // Glossiness level (Ranges from 0 to 1000)
float normalDepth; // Normal map depth
} Material;
@@ -430,22 +430,19 @@ typedef struct Model {
} Model;
// Light type
-// TODO: Review contained data to support different light types and features
typedef struct LightData {
int id;
int type; // LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
bool enabled;
Vector3 position;
- Vector3 direction; // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction)
- float attenuation; // Lost of light intensity with distance (use radius?)
+ Vector3 target; // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target)
+ float attenuation; // Lost of light intensity with distance (world distance)
- Color diffuse; // Use Vector3 diffuse (including intensities)?
+ Color diffuse; // Use Vector3 diffuse
float intensity;
- Color specular;
-
- float coneAngle; // SpotLight
+ float coneAngle; // Spot light max angle
} LightData, *Light;
// Light types
@@ -805,6 +802,7 @@ const char *SubText(const char *text, int position, int length);
//------------------------------------------------------------------------------------
// Basic 3d Shapes Drawing Functions (Module: models)
//------------------------------------------------------------------------------------
+void Draw3DLine(Vector3 startPos, Vector3 endPos, Color color); // Draw a line in 3D world space
void DrawCube(Vector3 position, float width, float height, float lenght, Color color); // Draw cube
void DrawCubeV(Vector3 position, Vector3 size, Color color); // Draw cube (Vector version)
void DrawCubeWires(Vector3 position, float width, float height, float lenght, Color color); // Draw cube wires
@@ -874,6 +872,7 @@ void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // S
void SetBlendMode(int mode); // Set blending mode (alpha, additive, multiplied)
Light CreateLight(int type, Vector3 position, Color diffuse); // Create a new light, initialize it and add to pool
+void DrawLights(void); // Draw all created lights in 3D world
void DestroyLight(Light light); // Destroy a light and take it out of the list
//----------------------------------------------------------------------------------
diff --git a/src/rlgl.c b/src/rlgl.c
index e2195e4d..55677f3e 100644
--- a/src/rlgl.c
+++ b/src/rlgl.c
@@ -1773,6 +1773,9 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform)
// Send model transformations matrix to shader
glUniformMatrix4fv(glGetUniformLocation(material.shader.id, "modelMatrix"), 1, false, MatrixToFloat(transform));
+ // Send view transformation matrix to shader. View matrix 8, 9 and 10 are view direction vector axis values (target - position)
+ glUniform3f(glGetUniformLocation(material.shader.id, "viewDir"), matView.m8, matView.m9, matView.m10);
+
// Setup shader uniforms for lights
SetShaderLights(material.shader);
@@ -1782,8 +1785,8 @@ void rlglDrawMesh(Mesh mesh, Material material, Matrix transform)
// Upload to shader material.colSpecular
glUniform4f(glGetUniformLocation(material.shader.id, "colSpecular"), (float)material.colSpecular.r/255, (float)material.colSpecular.g/255, (float)material.colSpecular.b/255, (float)material.colSpecular.a/255);
- // TODO: Upload to shader glossiness
- //glUniform1f(???, material.glossiness);
+ // Upload to shader glossiness
+ glUniform1f(glGetUniformLocation(material.shader.id, "glossiness"), material.glossiness);
}
// Set shader textures (diffuse, normal, specular)
@@ -2245,7 +2248,6 @@ void SetBlendMode(int mode)
}
// Create a new light, initialize it and add to pool
-// TODO: Review creation parameters (only generic ones)
Light CreateLight(int type, Vector3 position, Color diffuse)
{
// Allocate dynamic memory
@@ -2257,10 +2259,9 @@ Light CreateLight(int type, Vector3 position, Color diffuse)
light->enabled = true;
light->position = position;
- light->direction = (Vector3){ 0.0f, 0.0f, 0.0f };
+ light->target = (Vector3){ 0.0f, 0.0f, 0.0f };
light->intensity = 1.0f;
light->diffuse = diffuse;
- light->specular = WHITE;
// Add new light to the array
lights[lightsCount] = light;
@@ -2271,6 +2272,31 @@ Light CreateLight(int type, Vector3 position, Color diffuse)
return light;
}
+// Draw all created lights in 3D world
+void DrawLights(void)
+{
+ for (int i = 0; i < lightsCount; i++)
+ {
+ switch (lights[i]->type)
+ {
+ case LIGHT_POINT: DrawSphereWires(lights[i]->position, 0.3f*lights[i]->intensity, 4, 8, (lights[i]->enabled ? lights[i]->diffuse : BLACK)); break;
+ case LIGHT_DIRECTIONAL:
+ {
+ Draw3DLine(lights[i]->position, lights[i]->target, (lights[i]->enabled ? lights[i]->diffuse : BLACK));
+ DrawSphereWires(lights[i]->position, 0.3f*lights[i]->intensity, 4, 8, (lights[i]->enabled ? lights[i]->diffuse : BLACK));
+ DrawCubeWires(lights[i]->target, 0.3f, 0.3f, 0.3f, (lights[i]->enabled ? lights[i]->diffuse : BLACK));
+ }
+ case LIGHT_SPOT:
+ {
+ Draw3DLine(lights[i]->position, lights[i]->target, (lights[i]->enabled ? lights[i]->diffuse : BLACK));
+ DrawCylinderWires(lights[i]->position, 0.0f, 0.3f*lights[i]->coneAngle/50, 0.6f, 5, (lights[i]->enabled ? lights[i]->diffuse : BLACK));
+ DrawCubeWires(lights[i]->target, 0.3f, 0.3f, 0.3f, (lights[i]->enabled ? lights[i]->diffuse : BLACK));
+ } break;
+ default: break;
+ }
+ }
+}
+
// Destroy a light and take it out of the list
void DestroyLight(Light light)
{
@@ -2488,15 +2514,15 @@ static Shader LoadDefaultShader(void)
"varying vec4 fragColor; \n"
#endif
"uniform sampler2D texture0; \n"
- "uniform vec4 fragTintColor; \n"
+ "uniform vec4 colDiffuse; \n"
"void main() \n"
"{ \n"
#if defined(GRAPHICS_API_OPENGL_33)
" vec4 texelColor = texture(texture0, fragTexCoord); \n"
- " finalColor = texelColor*fragTintColor*fragColor; \n"
+ " finalColor = texelColor*colDiffuse*fragColor; \n"
#elif defined(GRAPHICS_API_OPENGL_ES2)
" vec4 texelColor = texture2D(texture0, fragTexCoord); \n" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0
- " gl_FragColor = texelColor*fragTintColor*fragColor; \n"
+ " gl_FragColor = texelColor*colDiffuse*fragColor; \n"
#endif
"} \n";
@@ -2513,87 +2539,17 @@ static Shader LoadDefaultShader(void)
// Load standard shader
// NOTE: This shader supports:
// - Up to 3 different maps: diffuse, normal, specular
-// - Material properties: colDiffuse, colAmbient, colSpecular, glossiness, normalDepth
+// - Material properties: colAmbient, colDiffuse, colSpecular, glossiness, normalDepth
// - Up to 8 lights: Point, Directional or Spot
static Shader LoadStandardShader(void)
{
- Shader shader;
-
- // Vertex shader directly defined, no external file required
-#if defined(GRAPHICS_API_OPENGL_33)
- char vShaderStr[] = "#version 330 \n"
- "in vec3 vertexPosition; \n"
- "in vec3 vertexNormal; \n"
- "in vec2 vertexTexCoord; \n"
- "in vec4 vertexColor; \n"
- "out vec2 fragTexCoord; \n"
- "out vec4 fragColor; \n"
- "out vec3 fragNormal; \n"
-#elif defined(GRAPHICS_API_OPENGL_ES2)
- char vShaderStr[] = "#version 100 \n"
- "attribute vec3 vertexPosition; \n"
- "attribute vec3 vertexNormal; \n"
- "attribute vec2 vertexTexCoord; \n"
- "attribute vec4 vertexColor; \n"
- "varying vec2 fragTexCoord; \n"
- "varying vec4 fragColor; \n"
- "varying vec3 fragNormal; \n"
-#endif
- "uniform mat4 mvpMatrix; \n"
- "uniform mat4 modelMatrix; \n"
- "void main() \n"
- "{ \n"
- " fragTexCoord = vertexTexCoord; \n"
- " fragColor = vertexColor; \n"
- " mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); \n"
- " fragNormal = normalize(normalMatrix*vertexNormal); \n"
- " gl_Position = mvpMatrix*vec4(vertexPosition, 1.0); \n"
- "} \n";
-
- // TODO: add specular calculation, multi-lights structs and light type calculations (directional, point, spot)
- // Fragment shader directly defined, no external file required
-#if defined(GRAPHICS_API_OPENGL_33)
- char fShaderStr[] = "#version 330 \n"
- "in vec2 fragTexCoord; \n"
- "in vec4 fragColor; \n"
- "in vec3 fragNormal; \n"
- "out vec4 finalColor; \n"
-#elif defined(GRAPHICS_API_OPENGL_ES2)
- char fShaderStr[] = "#version 100 \n"
- "precision mediump float; \n" // precision required for OpenGL ES2 (WebGL)
- "varying vec2 fragTexCoord; \n"
- "varying vec4 fragColor; \n"
- "varying vec3 fragNormal; \n"
-#endif
- "uniform sampler2D texture0; \n"
- "uniform vec4 fragTintColor; \n"
- "uniform vec4 colAmbient; \n"
- "uniform vec4 colSpecular; \n"
- "uniform vec3 lightDir; \n"
- "vec3 LambertLighting(in vec3 n, in vec3 l) \n"
- "{ \n"
- " return clamp(dot(n, l), 0, 1)*fragTintColor.rgb; \n"
- "} \n"
-
- "void main() \n"
- "{ \n"
- " vec3 n = normalize(fragNormal); \n"
- " vec3 l = normalize(lightDir); \n"
-#if defined(GRAPHICS_API_OPENGL_33)
- " vec4 texelColor = texture(texture0, fragTexCoord); \n"
- " finalColor = vec4(texelColor.rgb*(colAmbient.rgb + LambertLighting(n, l)) - colSpecular.rgb + colSpecular.rgb, texelColor.a*fragTintColor.a); \n" // Stupid specular color operation to avoid shader location errors
-#elif defined(GRAPHICS_API_OPENGL_ES2)
- " vec4 texelColor = texture2D(texture0, fragTexCoord); \n" // NOTE: texture2D() is deprecated on OpenGL 3.3 and ES 3.0
- " gl_FragColor = texelColor*fragTintColor*fragColor; \n"
-#endif
- "} \n";
-
- shader.id = LoadShaderProgram(vShaderStr, fShaderStr);
+ // Load standard shader (TODO: rewrite as char pointers)
+ Shader shader = LoadShader("resources/shaders/standard.vs", "resources/shaders/standard.fs");
if (shader.id != 0) TraceLog(INFO, "[SHDR ID %i] Standard shader loaded successfully", shader.id);
else TraceLog(WARNING, "[SHDR ID %i] Standard shader could not be loaded", shader.id);
- if (shader.id != 0) LoadDefaultShaderLocations(&shader); // TODO: Review locations fetching
+ if (shader.id != 0) LoadDefaultShaderLocations(&shader);
return shader;
}
@@ -2622,7 +2578,7 @@ static void LoadDefaultShaderLocations(Shader *shader)
shader->mvpLoc = glGetUniformLocation(shader->id, "mvpMatrix");
// Get handles to GLSL uniform locations (fragment shader)
- shader->tintColorLoc = glGetUniformLocation(shader->id, "fragTintColor");
+ shader->tintColorLoc = glGetUniformLocation(shader->id, "colDiffuse");
shader->mapDiffuseLoc = glGetUniformLocation(shader->id, "texture0");
shader->mapNormalLoc = glGetUniformLocation(shader->id, "texture1");
shader->mapSpecularLoc = glGetUniformLocation(shader->id, "texture2");
@@ -3098,62 +3054,75 @@ static void UnloadDefaultBuffers(void)
// Sets shader uniform values for lights array
// NOTE: It would be far easier with shader UBOs but are not supported on OpenGL ES 2.0f
-// TODO: Review memcpy() and parameters pass
static void SetShaderLights(Shader shader)
{
- // Note: currently working with one light (index 0)
- // TODO: add multi-lights feature (http://www.learnopengl.com/#!Lighting/Multiple-lights)
-
- /*
- // NOTE: Standard Shader must include the following data:
-
- // Shader Light struct
- struct Light {
- vec3 position;
- vec3 direction;
-
- vec3 diffuse;
- float intensity;
- }
-
- const int maxLights = 8;
- uniform int lightsCount; // Number of lights
- uniform Light lights[maxLights];
- */
+ int locPoint = glGetUniformLocation(shader.id, "lightsCount");
+ glUniform1i(locPoint, lightsCount);
- /*int locPoint;
char locName[32] = "lights[x].position\0";
-
- glUseProgram(shader.id);
-
- locPoint = glGetUniformLocation(shader.id, "lightsCount");
- glUniform1i(locPoint, lightsCount);
for (int i = 0; i < lightsCount; i++)
{
locName[7] = '0' + i;
- memcpy(&locName[10], "position\0", strlen("position\0"));
- locPoint = glGetUniformLocation(shader.id, locName);
- glUniform3f(locPoint, lights[i]->position.x, lights[i]->position.y, lights[i]->position.z);
+ memcpy(&locName[10], "enabled\0", strlen("enabled\0") + 1);
+ locPoint = GetShaderLocation(shader, locName);
+ glUniform1i(locPoint, lights[i]->enabled);
- memcpy(&locName[10], "direction\0", strlen("direction\0"));
- locPoint = glGetUniformLocation(shader.id, locName);
- glUniform3f(locPoint, lights[i]->direction.x, lights[i]->direction.y, lights[i]->direction.z);
-
- memcpy(&locName[10], "diffuse\0", strlen("diffuse\0"));
+ memcpy(&locName[10], "type\0", strlen("type\0") + 1);
+ locPoint = GetShaderLocation(shader, locName);
+ glUniform1i(locPoint, lights[i]->type);
+
+ memcpy(&locName[10], "diffuse\0", strlen("diffuse\0") + 2);
locPoint = glGetUniformLocation(shader.id, locName);
- glUniform4f(locPoint, (float)lights[i]->diffuse.r/255, (float)lights[i]->diffuse.g/255, (float)lights[i]->diffuse.b/255, (float)lights[i]->diffuse.a/255 );
+ glUniform4f(locPoint, (float)lights[i]->diffuse.r/255, (float)lights[i]->diffuse.g/255, (float)lights[i]->diffuse.b/255, (float)lights[i]->diffuse.a/255);
memcpy(&locName[10], "intensity\0", strlen("intensity\0"));
locPoint = glGetUniformLocation(shader.id, locName);
glUniform1f(locPoint, lights[i]->intensity);
+ switch(lights[i]->type)
+ {
+ case LIGHT_POINT:
+ {
+ memcpy(&locName[10], "position\0", strlen("position\0") + 1);
+ locPoint = GetShaderLocation(shader, locName);
+ glUniform3f(locPoint, lights[i]->position.x, lights[i]->position.y, lights[i]->position.z);
+
+ memcpy(&locName[10], "attenuation\0", strlen("attenuation\0"));
+ locPoint = GetShaderLocation(shader, locName);
+ glUniform1f(locPoint, lights[i]->attenuation);
+ } break;
+ case LIGHT_DIRECTIONAL:
+ {
+ memcpy(&locName[10], "direction\0", strlen("direction\0") + 2);
+ locPoint = GetShaderLocation(shader, locName);
+ Vector3 direction = { lights[i]->target.x - lights[i]->position.x, lights[i]->target.y - lights[i]->position.y, lights[i]->target.z - lights[i]->position.z };
+ VectorNormalize(&direction);
+ glUniform3f(locPoint, direction.x, direction.y, direction.z);
+ } break;
+ case LIGHT_SPOT:
+ {
+ memcpy(&locName[10], "position\0", strlen("position\0") + 1);
+ locPoint = GetShaderLocation(shader, locName);
+ glUniform3f(locPoint, lights[i]->position.x, lights[i]->position.y, lights[i]->position.z);
+
+ memcpy(&locName[10], "direction\0", strlen("direction\0") + 2);
+ locPoint = GetShaderLocation(shader, locName);
+
+ Vector3 direction = { lights[i]->target.x - lights[i]->position.x, lights[i]->target.y - lights[i]->position.y, lights[i]->target.z - lights[i]->position.z };
+ VectorNormalize(&direction);
+ glUniform3f(locPoint, direction.x, direction.y, direction.z);
+
+ memcpy(&locName[10], "coneAngle\0", strlen("coneAngle\0"));
+ locPoint = GetShaderLocation(shader, locName);
+ glUniform1f(locPoint, lights[i]->coneAngle);
+ } break;
+ default: break;
+ }
+
// TODO: Pass to the shader any other required data from LightData struct
- }*/
-
- int locPoint = GetShaderLocation(shader, "lightDir");
- glUniform3f(locPoint, lights[0]->position.x, lights[0]->position.y, lights[0]->position.z);
+ }
}
// Read text data from file
diff --git a/src/rlgl.h b/src/rlgl.h
index 39941b33..0765a8a7 100644
--- a/src/rlgl.h
+++ b/src/rlgl.h
@@ -196,40 +196,34 @@ typedef enum { OPENGL_11 = 1, OPENGL_33, OPENGL_ES_20 } GlVersion;
// Material type
typedef struct Material {
- Shader shader;
+ Shader shader; // Standard shader (supports 3 map types: diffuse, normal, specular)
- Texture2D texDiffuse; // Diffuse texture
- Texture2D texNormal; // Normal texture
- Texture2D texSpecular; // Specular texture
+ Texture2D texDiffuse; // Diffuse texture
+ Texture2D texNormal; // Normal texture
+ Texture2D texSpecular; // Specular texture
- Color colDiffuse;
- Color colAmbient;
- Color colSpecular;
+ Color colDiffuse; // Diffuse color
+ Color colAmbient; // Ambient color
+ Color colSpecular; // Specular color
- float glossiness;
- float normalDepth;
+ float glossiness; // Glossiness level (Ranges from 0 to 1000)
+ float normalDepth; // Normal map depth
} Material;
// Light type
- // TODO: Review contained data to support different light types and features
typedef struct LightData {
int id;
int type; // LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
bool enabled;
Vector3 position;
- Vector3 direction; // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction)
- float attenuation; // Lost of light intensity with distance (use radius?)
+ Vector3 target; // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target)
+ float attenuation; // Lost of light intensity with distance (world distance)
- Color diffuse; // Use Vector3 diffuse (including intensities)?
+ Color diffuse; // Use Vector3 diffuse
float intensity;
- Color specular;
- //float specFactor; // Specular intensity ?
-
- //Color ambient; // Required?
-
- float coneAngle; // SpotLight
+ float coneAngle; // Spot light max angle
} LightData, *Light;
// Color blending modes (pre-defined)