Added lighting engine module
- New lighting engine module which contains new data types Light and Material. These data types and functions facilitates making a basic 3D iluminated program with a light and a model. - Added lighting engine module example (currently included in raylib.h; it might be compiled by separate and include lighting.h in game source C file). - Corrected some opengl defines control structures and added some TODO to fix raylib-opengl 1.1 source build (note: now source can be compiled without errors, but rlglReadPixels() won't work properly). Note: most of functions of phong version 330 shader are not in v100 shaders, so I couldn't write a version 100 phong shader. These functions are included from version 150.
This commit is contained in:
parent
4db2da9185
commit
1bcb5ddd50
12 changed files with 7078 additions and 1 deletions
180
examples/lighting_blinn_phong.c
Normal file
180
examples/lighting_blinn_phong.c
Normal file
|
@ -0,0 +1,180 @@
|
|||
/*******************************************************************************************
|
||||
*
|
||||
* raylib - Phong lighting shader example
|
||||
*
|
||||
* This example has been created using raylib v1.3.0 (www.raylib.com)
|
||||
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
*
|
||||
* Copyright (c) 2015 Ramon Santamaria (Ray San - raysan@raysanweb.com)
|
||||
*
|
||||
********************************************************************************************/
|
||||
|
||||
#include "raylib.h"
|
||||
|
||||
#define SHININESS_SPEED 1.0f
|
||||
#define LIGHT_SPEED 0.25f
|
||||
|
||||
int main()
|
||||
{
|
||||
// Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
const int screenWidth = 800;
|
||||
const int screenHeight = 450;
|
||||
|
||||
SetConfigFlags(FLAG_MSAA_4X_HINT);
|
||||
InitWindow(screenWidth, screenHeight, "raylib [lighting] example - basic blinn-phong lighting");
|
||||
SetTargetFPS(60);
|
||||
|
||||
// Camera initialization
|
||||
Camera camera = {{ 10.0, 8.0, 10.0 }, { 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }};
|
||||
|
||||
// Model initialization
|
||||
Vector3 position = { 0.0, 0.0, 0.0 };
|
||||
Model model = LoadModel("resources/model/dwarf.obj");
|
||||
// Shader shader = LoadShader("resources/shaders/phong.vs", "resources/shaders/phong.fs");
|
||||
SetModelShader(&model, shader);
|
||||
|
||||
// Shader locations initialization
|
||||
int lIntensityLoc = GetShaderLocation(shader, "light_intensity");
|
||||
int lAmbientLoc = GetShaderLocation(shader, "light_ambientColor");
|
||||
int lDiffuseLoc = GetShaderLocation(shader, "light_diffuseColor");
|
||||
int lSpecularLoc = GetShaderLocation(shader, "light_specularColor");
|
||||
int lSpecIntensityLoc = GetShaderLocation(shader, "light_specIntensity");
|
||||
|
||||
int mAmbientLoc = GetShaderLocation(shader, "mat_ambientColor");
|
||||
int mSpecularLoc = GetShaderLocation(shader, "mat_specularColor");
|
||||
int mGlossLoc = GetShaderLocation(shader, "mat_glossiness");
|
||||
|
||||
// Camera and light vectors shader locations
|
||||
int cameraLoc = GetShaderLocation(shader, "cameraPos");
|
||||
int lightLoc = GetShaderLocation(shader, "lightPos");
|
||||
|
||||
// Light and material definitions
|
||||
Light directionalLight;
|
||||
Material blinnMaterial;
|
||||
|
||||
// Light initialization
|
||||
SetLightPosition(&directionalLight, (Vector3){5.0f, 1.0f, 1.0f});
|
||||
SetLightRotation(&directionalLight, (Vector3){5.0f, 1.0f, 1.0f});
|
||||
SetLightIntensity(&directionalLight, 1);
|
||||
SetLightAmbientColor(&directionalLight, (Vector3){0.6f, 0.3f, 0});
|
||||
SetLightDiffuseColor(&directionalLight, (Vector3){1, 1, 1});
|
||||
SetLightSpecularColor(&directionalLight, (Vector3){1, 1, 1});
|
||||
SetLightSpecIntensity(&directionalLight, 1);
|
||||
|
||||
// Material initialization
|
||||
SetMaterialAmbientColor(&blinnMaterial, (Vector3){0.2f, 0.2f, 0.2f});
|
||||
SetMaterialDiffuseColor(&blinnMaterial, (Vector3){1.0f, 1.0f, 1.0f});
|
||||
SetMaterialSpecularColor(&blinnMaterial, (Vector3){1.0f, 1.0f, 1.0f});
|
||||
SetMaterialGlossiness(&blinnMaterial, 50);
|
||||
|
||||
// Setup camera
|
||||
SetCameraMode(CAMERA_FREE); // Set 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
|
||||
float cameraPosition[3] = { camera.position.x, camera.position.y, camera.position.z }; // Camera position vector in float array
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
// Main game loop
|
||||
while (!WindowShouldClose()) // Detect window close button or ESC key
|
||||
{
|
||||
// Update
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Update camera position and its float array for shader
|
||||
UpdateCamera(&camera);
|
||||
cameraPosition[0] = camera.position.x;
|
||||
cameraPosition[1] = camera.position.y;
|
||||
cameraPosition[2] = camera.position.z;
|
||||
|
||||
// Glossiness input control
|
||||
if(IsKeyDown(KEY_UP))
|
||||
{
|
||||
blinnMaterial.glossiness[0] += SHININESS_SPEED;
|
||||
}
|
||||
else if(IsKeyDown(KEY_DOWN))
|
||||
{
|
||||
blinnMaterial.glossiness[0] -= SHININESS_SPEED;
|
||||
|
||||
if(blinnMaterial.glossiness[0] < 0) blinnMaterial.glossiness[0] = 0;
|
||||
}
|
||||
|
||||
// Light X movement
|
||||
if(IsKeyDown(KEY_D))
|
||||
{
|
||||
directionalLight.position[0] += LIGHT_SPEED;
|
||||
}
|
||||
else if(IsKeyDown(KEY_A))
|
||||
{
|
||||
directionalLight.position[0] -= LIGHT_SPEED;
|
||||
}
|
||||
|
||||
// Light Y movement
|
||||
if(IsKeyDown(KEY_LEFT_SHIFT))
|
||||
{
|
||||
directionalLight.position[1] += LIGHT_SPEED;
|
||||
}
|
||||
else if(IsKeyDown(KEY_LEFT_CONTROL))
|
||||
{
|
||||
directionalLight.position[1] -= LIGHT_SPEED;
|
||||
}
|
||||
|
||||
// Light Z movement
|
||||
if(IsKeyDown(KEY_S))
|
||||
{
|
||||
directionalLight.position[2] += LIGHT_SPEED;
|
||||
}
|
||||
else if(IsKeyDown(KEY_W))
|
||||
{
|
||||
directionalLight.position[2] -= LIGHT_SPEED;
|
||||
}
|
||||
|
||||
// Send light values to shader
|
||||
SetShaderValue(shader, lIntensityLoc, directionalLight.intensity, 1);
|
||||
SetShaderValue(shader, lAmbientLoc, directionalLight.ambientColor, 3);
|
||||
SetShaderValue(shader, lDiffuseLoc, directionalLight.diffuseColor, 3);
|
||||
SetShaderValue(shader, lSpecularLoc, directionalLight.specularColor, 3);
|
||||
SetShaderValue(shader, lSpecIntensityLoc, directionalLight.specularIntensity, 1);
|
||||
|
||||
// Send material values to shader
|
||||
SetShaderValue(shader, mAmbientLoc, blinnMaterial.ambientColor, 3);
|
||||
SetShaderValue(shader, mSpecularLoc, blinnMaterial.specularColor, 3);
|
||||
SetShaderValue(shader, mGlossLoc, blinnMaterial.glossiness, 1);
|
||||
|
||||
// Send camera and light transform values to shader
|
||||
SetShaderValue(shader, cameraLoc, cameraPosition, 3);
|
||||
SetShaderValue(shader, lightLoc, directionalLight.position, 3);
|
||||
//----------------------------------------------------------------------------------
|
||||
|
||||
// Draw
|
||||
//----------------------------------------------------------------------------------
|
||||
BeginDrawing();
|
||||
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
Begin3dMode(camera);
|
||||
|
||||
DrawModel(model, position, 0.1f, (Color){255 * blinnMaterial.diffuseColor[0], 255 * blinnMaterial.diffuseColor[1], 255 * blinnMaterial.diffuseColor[2], 255});
|
||||
|
||||
DrawSphere((Vector3){directionalLight.position[0], directionalLight.position[1], directionalLight.position[2]}, 1, YELLOW);
|
||||
|
||||
End3dMode();
|
||||
|
||||
// Draw FPS
|
||||
DrawFPS(10, 10);
|
||||
|
||||
EndDrawing();
|
||||
//----------------------------------------------------------------------------------
|
||||
}
|
||||
|
||||
// De-Initialization
|
||||
//--------------------------------------------------------------------------------------
|
||||
// Unload all loaded data
|
||||
UnloadShader(shader);
|
||||
UnloadModel(model);
|
||||
|
||||
CloseWindow(); // Close window and OpenGL context
|
||||
//--------------------------------------------------------------------------------------
|
||||
|
||||
return 0;
|
||||
}
|
BIN
examples/lighting_blinn_phong.png
Normal file
BIN
examples/lighting_blinn_phong.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 49 KiB |
6433
examples/resources/model/shapes.obj
Normal file
6433
examples/resources/model/shapes.obj
Normal file
File diff suppressed because it is too large
Load diff
76
examples/resources/shaders/phong.fs
Normal file
76
examples/resources/shaders/phong.fs
Normal file
|
@ -0,0 +1,76 @@
|
|||
#version 330
|
||||
|
||||
// Vertex shader input data
|
||||
in vec2 fragTexCoord;
|
||||
in vec3 fragNormal;
|
||||
|
||||
// Diffuse data
|
||||
uniform sampler2D texture0;
|
||||
uniform vec4 tintColor;
|
||||
|
||||
// Light attributes
|
||||
uniform vec3 light_ambientColor = vec3(0.6, 0.3, 0);
|
||||
uniform vec3 light_diffuseColor = vec3(1, 0.5, 0);
|
||||
uniform vec3 light_specularColor = vec3(0, 1, 0);
|
||||
uniform float light_intensity = 1;
|
||||
uniform float light_specIntensity = 1;
|
||||
|
||||
// Material attributes
|
||||
uniform vec3 mat_ambientColor = vec3(1, 1, 1);
|
||||
uniform vec3 mat_specularColor = vec3(1, 1, 1);
|
||||
uniform float mat_glossiness = 50;
|
||||
|
||||
// World attributes
|
||||
uniform vec3 lightPos;
|
||||
uniform vec3 cameraPos;
|
||||
|
||||
// Fragment shader output data
|
||||
out vec4 fragColor;
|
||||
|
||||
vec3 AmbientLighting()
|
||||
{
|
||||
return mat_ambientColor * light_ambientColor;
|
||||
}
|
||||
|
||||
vec3 DiffuseLighting(in vec3 N, in vec3 L)
|
||||
{
|
||||
// Lambertian reflection calculation
|
||||
float diffuse = clamp(dot(N, L), 0, 1);
|
||||
|
||||
return tintColor.xyz * light_diffuseColor * light_intensity * diffuse;
|
||||
}
|
||||
|
||||
vec3 SpecularLighting(in vec3 N, in vec3 L, in vec3 V)
|
||||
{
|
||||
float specular = 0;
|
||||
|
||||
// Calculate specular reflection only if the surface is oriented to the light source
|
||||
if(dot(N, L) > 0)
|
||||
{
|
||||
// Calculate half vector
|
||||
vec3 H = normalize(L + V);
|
||||
|
||||
// Calculate specular intensity
|
||||
specular = pow(dot(N, H), 3 + mat_glossiness);
|
||||
}
|
||||
|
||||
return mat_specularColor * light_specularColor * light_specIntensity * specular;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
// Normalize input vectors
|
||||
vec3 L = normalize(lightPos);
|
||||
vec3 V = normalize(cameraPos);
|
||||
vec3 N = normalize(fragNormal);
|
||||
|
||||
vec3 ambient = AmbientLighting();
|
||||
vec3 diffuse = DiffuseLighting(N, L);
|
||||
vec3 specular = SpecularLighting(N, L, V);
|
||||
|
||||
// Get base color from texture
|
||||
vec4 textureColor = texture(texture0, fragTexCoord);
|
||||
vec3 finalColor = textureColor.rgb;
|
||||
|
||||
fragColor = vec4(finalColor * (ambient + diffuse + specular), textureColor.a);
|
||||
}
|
28
examples/resources/shaders/phong.vs
Normal file
28
examples/resources/shaders/phong.vs
Normal file
|
@ -0,0 +1,28 @@
|
|||
#version 330
|
||||
|
||||
// Vertex input data
|
||||
in vec3 vertexPosition;
|
||||
in vec2 vertexTexCoord;
|
||||
in vec3 vertexNormal;
|
||||
|
||||
// Projection and model data
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform mat4 modelviewMatrix;
|
||||
uniform mat4 modelMatrix;
|
||||
|
||||
// Attributes to fragment shader
|
||||
out vec2 fragTexCoord;
|
||||
out vec3 fragNormal;
|
||||
|
||||
void main()
|
||||
{
|
||||
// Send texture coord to fragment shader
|
||||
fragTexCoord = vertexTexCoord;
|
||||
|
||||
// Calculate view vector normal from model
|
||||
mat3 normalMatrix = transpose(inverse(mat3(modelMatrix)));
|
||||
fragNormal = normalize(normalMatrix * vertexNormal);
|
||||
|
||||
// Calculate final vertex position
|
||||
gl_Position = projectionMatrix * modelviewMatrix * vec4(vertexPosition, 1.0);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue