Browse Source

Reviewed DrawLight() function and some tweaks

Ray 9 years ago
parent
commit
caa7bc366b
4 changed files with 51 additions and 46 deletions
  1. 6 6
      examples/shaders_standard_lighting.c
  2. 29 0
      src/models.c
  3. 9 9
      src/raylib.h
  4. 7 31
      src/rlgl.c

+ 6 - 6
examples/shaders_standard_lighting.c

@@ -33,12 +33,12 @@ int main()
     Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
     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
     Vector3 position = { 0.0f, 0.0f, 0.0f };   // Set model position
     
     
-    Model dwarf = LoadModel("resources/model/dwarf.obj");                   // Load OBJ model
+    Model dwarf = LoadModel("resources/model/dwarf.obj");                     // Load OBJ model
 
 
     Material material = LoadStandardMaterial();
     Material material = LoadStandardMaterial();
     
     
-    material.texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model diffuse texture
-    material.texNormal = LoadTexture("resources/model/dwarf_normal.png"); // Load model normal texture
+    material.texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png");   // Load model diffuse texture
+    material.texNormal = LoadTexture("resources/model/dwarf_normal.png");     // Load model normal texture
     material.texSpecular = LoadTexture("resources/model/dwarf_specular.png"); // Load model specular texture
     material.texSpecular = LoadTexture("resources/model/dwarf_specular.png"); // Load model specular texture
     material.colDiffuse = (Color){255, 255, 255, 255};
     material.colDiffuse = (Color){255, 255, 255, 255};
     material.colAmbient = (Color){0, 0, 10, 255};
     material.colAmbient = (Color){0, 0, 10, 255};
@@ -46,8 +46,6 @@ int main()
     material.glossiness = 50.0f;
     material.glossiness = 50.0f;
     
     
     dwarf.material = material;      // Apply material to model
     dwarf.material = material;      // Apply material to model
-    
-    Model dwarf2 = LoadModel("resources/model/dwarf.obj");                   // Load OBJ model
 
 
     Light spotLight = CreateLight(LIGHT_SPOT, (Vector3){3.0f, 5.0f, 2.0f}, (Color){255, 255, 255, 255});
     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->target = (Vector3){0.0f, 0.0f, 0.0f};
@@ -91,7 +89,9 @@ int main()
                 
                 
                 DrawModel(dwarf, position, 2.0f, WHITE);   // Draw 3d model with texture
                 DrawModel(dwarf, position, 2.0f, WHITE);   // Draw 3d model with texture
                 
                 
-                DrawLights();   // Draw all created lights in 3D world
+                DrawLight(spotLight);   // Draw spot light
+                DrawLight(dirLight);    // Draw directional light
+                DrawLight(pointLight);  // Draw point light
 
 
                 DrawGrid(10, 1.0f);     // Draw a grid
                 DrawGrid(10, 1.0f);     // Draw a grid
 
 

+ 29 - 0
src/models.c

@@ -569,6 +569,35 @@ void DrawGizmo(Vector3 position)
     rlPopMatrix();
     rlPopMatrix();
 }
 }
 
 
+
+// Draw light in 3D world
+void DrawLight(Light light)
+{
+    switch (light->type)
+    {
+        case LIGHT_POINT:
+        {
+            DrawSphereWires(light->position, 0.3f*light->intensity, 4, 8, (light->enabled ? light->diffuse : BLACK));
+            Draw3DCircle(light->position, light->radius, 0.0f, (Vector3){ 0, 0, 0 }, (light->enabled ? light->diffuse : BLACK));
+            Draw3DCircle(light->position, light->radius, 90.0f, (Vector3){ 1, 0, 0 }, (light->enabled ? light->diffuse : BLACK));
+            Draw3DCircle(light->position, light->radius, 90.0f, (Vector3){ 0, 1, 0 }, (light->enabled ? light->diffuse : BLACK));
+        } break;
+        case LIGHT_DIRECTIONAL:
+        {                
+            Draw3DLine(light->position, light->target, (light->enabled ? light->diffuse : BLACK));
+            DrawSphereWires(light->position, 0.3f*light->intensity, 4, 8, (light->enabled ? light->diffuse : BLACK));
+            DrawCubeWires(light->target, 0.3f, 0.3f, 0.3f, (light->enabled ? light->diffuse : BLACK));
+        } break;
+        case LIGHT_SPOT:
+        {                
+            Draw3DLine(light->position, light->target, (light->enabled ? light->diffuse : BLACK));
+            DrawCylinderWires(light->position, 0.0f, 0.3f*light->coneAngle/50, 0.6f, 5, (light->enabled ? light->diffuse : BLACK));
+            DrawCubeWires(light->target, 0.3f, 0.3f, 0.3f, (light->enabled ? light->diffuse : BLACK));
+        } break;
+        default: break;
+    }
+}
+
 // Load a 3d model (from file)
 // Load a 3d model (from file)
 Model LoadModel(const char *fileName)
 Model LoadModel(const char *fileName)
 {
 {

+ 9 - 9
src/raylib.h

@@ -431,18 +431,18 @@ typedef struct Model {
 
 
 // Light type
 // Light type
 typedef struct LightData {
 typedef struct LightData {
-    int id;
-    int type;           // LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
-    bool enabled;
+    unsigned int id;    // Light id
+    int type;           // Light type: LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
+    bool enabled;       // Light enabled
     
     
-    Vector3 position;
-    Vector3 target;     // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target)
-    float radius;       // Lost of light intensity with distance (world distance)
+    Vector3 position;   // Light position
+    Vector3 target;     // Light target: LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target)
+    float radius;       // Light attenuation radius light intensity reduced with distance (world distance)
     
     
-    Color diffuse;      // Light color
+    Color diffuse;      // Light diffuse color
     float intensity;    // Light intensity level
     float intensity;    // Light intensity level
     
     
-    float coneAngle;    // Spot light max angle
+    float coneAngle;    // Light cone max angle: LIGHT_SPOT
 } LightData, *Light;
 } LightData, *Light;
 
 
 // Light types
 // Light types
@@ -817,6 +817,7 @@ void DrawPlane(Vector3 centerPos, Vector2 size, Color color);
 void DrawRay(Ray ray, Color color);                                                                // Draw a ray line
 void DrawRay(Ray ray, Color color);                                                                // Draw a ray line
 void DrawGrid(int slices, float spacing);                                                          // Draw a grid (centered at (0, 0, 0))
 void DrawGrid(int slices, float spacing);                                                          // Draw a grid (centered at (0, 0, 0))
 void DrawGizmo(Vector3 position);                                                                  // Draw simple gizmo
 void DrawGizmo(Vector3 position);                                                                  // Draw simple gizmo
+void DrawLight(Light light);                                                                       // Draw light in 3D world
 //DrawTorus(), DrawTeapot() are useless...
 //DrawTorus(), DrawTeapot() are useless...
 
 
 //------------------------------------------------------------------------------------
 //------------------------------------------------------------------------------------
@@ -873,7 +874,6 @@ void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat);       // S
 void SetBlendMode(int mode);                                        // Set blending mode (alpha, additive, multiplied)
 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
 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
 void DestroyLight(Light light);                                     // Destroy a light and take it out of the list
 
 
 //----------------------------------------------------------------------------------
 //----------------------------------------------------------------------------------

+ 7 - 31
src/rlgl.c

@@ -1069,6 +1069,13 @@ void rlglClose(void)
     // Delete default white texture
     // Delete default white texture
     glDeleteTextures(1, &whiteTexture);
     glDeleteTextures(1, &whiteTexture);
     TraceLog(INFO, "[TEX ID %i] Unloaded texture data (base white texture) from VRAM", whiteTexture);
     TraceLog(INFO, "[TEX ID %i] Unloaded texture data (base white texture) from VRAM", whiteTexture);
+    
+    // Unload lights
+    if (lightsCount > 0)
+    {
+        for (int i = 0; i < lightsCount; i++) free(lights[i]);
+        lightsCount = 0;
+    }
 
 
     free(draws);
     free(draws);
 #endif
 #endif
@@ -2292,37 +2299,6 @@ Light CreateLight(int type, Vector3 position, Color diffuse)
     return light;
     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));
-                Draw3DCircle(lights[i]->position, lights[i]->radius, 0.0f, (Vector3){ 0, 0, 0 }, (lights[i]->enabled ? lights[i]->diffuse : BLACK));
-                Draw3DCircle(lights[i]->position, lights[i]->radius, 90.0f, (Vector3){ 1, 0, 0 }, (lights[i]->enabled ? lights[i]->diffuse : BLACK));
-                Draw3DCircle(lights[i]->position, lights[i]->radius, 90.0f, (Vector3){ 0, 1, 0 }, (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));
-            } break;
-            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
 // Destroy a light and take it out of the list
 void DestroyLight(Light light)
 void DestroyLight(Light light)
 {
 {