Reviewed DrawLight() function and some tweaks

This commit is contained in:
Ray 2016-05-31 00:51:55 +02:00
parent 8a4e28f81d
commit caa7bc366b
4 changed files with 51 additions and 46 deletions

View file

@ -47,8 +47,6 @@ int main()
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};
spotLight->intensity = 2.0f; spotLight->intensity = 2.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

View file

@ -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)
{ {

View file

@ -431,18 +431,18 @@ typedef struct Model {
// Light type // Light type
typedef struct LightData { typedef struct LightData {
int id; unsigned int id; // Light id
int type; // LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT int type; // Light type: LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
bool enabled; bool enabled; // Light enabled
Vector3 position; Vector3 position; // Light position
Vector3 target; // Used on LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target) Vector3 target; // Light target: LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target)
float radius; // Lost of light intensity with distance (world distance) 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
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------

View file

@ -1070,6 +1070,13 @@ void rlglClose(void)
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)
{ {