-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesigned lighting system and added lighting example
- Loading branch information
Showing
19 changed files
with
774 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
; Resources | ||
resources.shaders=../assets/shaders3d | ||
resources.textures=../assets/textures | ||
; Window | ||
window.title=Lighting | ||
window.width=800 | ||
window.height=600 | ||
window.fullscreen=false | ||
; Camera | ||
camera.nearclip=0.1 | ||
camera.farclip=1000 | ||
camera.fov=45 | ||
; Input | ||
input.mouse.lock=false | ||
input.joystick.support=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// | ||
// Phong lighting model implementation. | ||
/// | ||
// | ||
// Uniforms: | ||
// u_material | ||
// | ||
|
||
// Compute directional light. | ||
// light - Directional light | ||
// normal - Normalized fragment normal in camera space. | ||
// view_dir - Normalized vector from camera to fragment. | ||
// color - Fragment color | ||
vec3 dir_light(Light light, vec3 normal, vec3 view_dir, vec4 color) | ||
{ | ||
vec3 light_dir = normalize(-light.position.xyz); | ||
// diffuse shading | ||
float diff = max(dot(normal, light_dir), 0.0); | ||
// specular shading | ||
vec3 reflect_dir = reflect(-light_dir, normal); | ||
float spec = 0.0; | ||
if (diff > 0.0) | ||
{ | ||
spec = pow(max(dot(view_dir, reflect_dir), 0.0), u_material.shininess); | ||
} | ||
// combine results | ||
vec3 ambient = light.ambient.rgb * u_material.ambient.rgb * color.rgb; | ||
vec3 diffuse = light.diffuse.rgb * diff * u_material.diffuse.rgb * color.rgb; | ||
vec3 specular = light.specular.rgb * spec * u_material.specular.rgb * color.rgb; | ||
return (ambient + diffuse + specular); | ||
} | ||
|
||
// Compute point light. | ||
// light - Point light | ||
// normal - Normalized fragment normal in camera space. | ||
// frag_pos - Fragment position in camera space. | ||
// view_dir - Normalized vector from camera to fragment. | ||
// color - Fragment color | ||
vec3 point_light(Light light, vec3 normal, vec3 frag_pos, vec3 view_dir, vec4 color) | ||
{ | ||
vec3 light_dir = normalize(light.position.xyz - frag_pos); | ||
// diffuse shading | ||
float diff = max(dot(normal, light_dir), 0.0); | ||
// specular shading | ||
vec3 reflect_dir = reflect(-light_dir, normal); | ||
float spec = 0.0; | ||
if (diff > 0.0) | ||
{ | ||
spec = pow(max(dot(view_dir, reflect_dir), 0.0), u_material.shininess); | ||
} | ||
// attenuation | ||
float distance = length(light.position.xyz - frag_pos); | ||
float attenuation = 1.0 / (light.params[0] + light.params[1] * distance + light.params[2] * (distance * distance)); | ||
// combine results | ||
vec3 ambient = light.ambient.rgb * u_material.ambient.rgb * color.rgb; | ||
vec3 diffuse = light.diffuse.rgb * diff * u_material.diffuse.rgb * color.rgb; | ||
vec3 specular = light.specular.rgb * spec * u_material.specular.rgb * color.rgb; | ||
ambient *= attenuation; | ||
diffuse *= attenuation; | ||
specular *= attenuation; | ||
return (ambient + diffuse + specular); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#version 150 | ||
/// | ||
// Textured and lit fragment shader. | ||
/// | ||
in vec3 v_position; | ||
in vec3 v_normal; | ||
in vec2 v_tex_coord; | ||
|
||
layout(std140) uniform Camera | ||
{ | ||
mat4 proj_pers; | ||
mat4 proj_orth; | ||
mat4 view; | ||
mat4 view_inv; | ||
vec4 position; | ||
vec4 dir; | ||
vec4 up; | ||
vec4 left; | ||
} u_cam; | ||
|
||
layout(std140) uniform Material | ||
{ | ||
vec4 ambient; | ||
vec4 diffuse; | ||
vec4 specular; | ||
float shininess; | ||
vec3 custom; | ||
} u_material; | ||
|
||
struct Light { | ||
vec4 position; | ||
vec4 ambient; | ||
vec4 diffuse; | ||
vec4 specular; | ||
vec4 params; | ||
}; | ||
|
||
layout(std140) uniform Lights { | ||
Light l[5]; | ||
} u_lights; | ||
|
||
uniform sampler2D u_tex0; | ||
|
||
out vec4 o_color; | ||
|
||
#include "lib_phong.fsh" | ||
|
||
void main() | ||
{ | ||
vec3 normal = normalize(v_normal.xyz); | ||
vec3 view_dir = normalize(u_cam.position.xyz - v_position); | ||
vec4 surface = texture(u_tex0, v_tex_coord); | ||
|
||
// Phase 1: directional light | ||
vec3 color = dir_light(u_lights.l[0], normal, view_dir, surface); | ||
// Phase 2: point lights | ||
color += point_light(u_lights.l[1], normal, v_position, view_dir, surface); | ||
color += point_light(u_lights.l[2], normal, v_position, view_dir, surface); | ||
color += point_light(u_lights.l[3], normal, v_position, view_dir, surface); | ||
color += point_light(u_lights.l[4], normal, v_position, view_dir, surface); | ||
|
||
o_color = vec4(color, surface.a); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#version 150 | ||
/// | ||
// Textured and lit vertex shader. | ||
/// | ||
in vec4 a_position; | ||
in vec4 a_normal; | ||
in vec2 a_tex_coord; | ||
|
||
layout(std140) uniform Camera | ||
{ | ||
mat4 proj_pers; | ||
mat4 proj_orth; | ||
mat4 view; | ||
mat4 view_inv; | ||
vec4 position; | ||
vec4 dir; | ||
vec4 up; | ||
vec4 left; | ||
} u_cam; | ||
|
||
uniform mat4 u_model; | ||
|
||
out vec3 v_position; | ||
out vec3 v_normal; | ||
out vec2 v_tex_coord; | ||
|
||
void main() | ||
{ | ||
v_position = vec3(u_model * a_position); | ||
v_normal = mat3(transpose(inverse(u_model))) * a_normal.xyz; | ||
v_tex_coord = a_tex_coord; | ||
gl_Position = u_cam.proj_pers * u_cam.view * vec4(v_position, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
include_directories(../../src) | ||
|
||
add_executable(lighting stdafx.cpp lightingapp.cpp) | ||
target_link_libraries(lighting dukat ${SDL2_LIBRARY} ${SDL2_IMAGE_LIBRARIES} | ||
${GLEW_LIBRARIES} ${OPENGL_LIBRARIES} ${X11_Xext_LIB}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<ItemGroup Label="ProjectConfigurations"> | ||
<ProjectConfiguration Include="Debug|Win32"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|Win32"> | ||
<Configuration>Release</Configuration> | ||
<Platform>Win32</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Debug|x64"> | ||
<Configuration>Debug</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
<ProjectConfiguration Include="Release|x64"> | ||
<Configuration>Release</Configuration> | ||
<Platform>x64</Platform> | ||
</ProjectConfiguration> | ||
</ItemGroup> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>{FC0AD009-FFB5-44ED-A5DD-59113CC0CB74}</ProjectGuid> | ||
<Keyword>Win32Proj</Keyword> | ||
<RootNamespace>lighting</RootNamespace> | ||
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>true</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> | ||
<ConfigurationType>Application</ConfigurationType> | ||
<UseDebugLibraries>false</UseDebugLibraries> | ||
<PlatformToolset>v140</PlatformToolset> | ||
<WholeProgramOptimization>true</WholeProgramOptimization> | ||
<CharacterSet>Unicode</CharacterSet> | ||
</PropertyGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> | ||
<ImportGroup Label="ExtensionSettings"> | ||
</ImportGroup> | ||
<ImportGroup Label="Shared"> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> | ||
</ImportGroup> | ||
<PropertyGroup Label="UserMacros" /> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
<OutDir>$(SolutionDir)..\bin\$(PlatformTarget)\</OutDir> | ||
<IncludePath>$(SolutionDir)..\include\;$(SolutionDir)..\src\;$(IncludePath)</IncludePath> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<LinkIncremental>true</LinkIncremental> | ||
<OutDir>$(SolutionDir)..\bin\$(PlatformTarget)\</OutDir> | ||
<IncludePath>$(SolutionDir)..\include\;$(SolutionDir)..\src\;$(IncludePath)</IncludePath> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
<OutDir>$(SolutionDir)..\bin\$(PlatformTarget)\</OutDir> | ||
<IncludePath>$(SolutionDir)..\include\;$(SolutionDir)..\src\;$(IncludePath)</IncludePath> | ||
</PropertyGroup> | ||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<LinkIncremental>false</LinkIncremental> | ||
<OutDir>$(SolutionDir)..\bin\$(PlatformTarget)\</OutDir> | ||
<IncludePath>$(SolutionDir)..\include\;$(SolutionDir)..\src\;$(IncludePath)</IncludePath> | ||
</PropertyGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> | ||
<ClCompile> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
<AdditionalLibraryDirectories>$(SolutionDir)..\lib\$(PlatformTarget)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;glew32.lib;opengl32.lib;Xinput9_1_0.lib;dukat.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||
<ClCompile> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<WarningLevel>Level3</WarningLevel> | ||
<Optimization>Disabled</Optimization> | ||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
<AdditionalLibraryDirectories>$(SolutionDir)..\lib\$(PlatformTarget)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;glew32.lib;opengl32.lib;Xinput9_1_0.lib;dukat.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
<AdditionalLibraryDirectories>$(SolutionDir)..\lib\$(PlatformTarget)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;glew32.lib;opengl32.lib;Xinput9_1_0.lib;dukat.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> | ||
<ClCompile> | ||
<WarningLevel>Level3</WarningLevel> | ||
<PrecompiledHeader>Use</PrecompiledHeader> | ||
<Optimization>MaxSpeed</Optimization> | ||
<FunctionLevelLinking>true</FunctionLevelLinking> | ||
<IntrinsicFunctions>true</IntrinsicFunctions> | ||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||
<SDLCheck>true</SDLCheck> | ||
</ClCompile> | ||
<Link> | ||
<SubSystem>Console</SubSystem> | ||
<EnableCOMDATFolding>true</EnableCOMDATFolding> | ||
<OptimizeReferences>true</OptimizeReferences> | ||
<GenerateDebugInformation>true</GenerateDebugInformation> | ||
<AdditionalLibraryDirectories>$(SolutionDir)..\lib\$(PlatformTarget)\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;glew32.lib;opengl32.lib;Xinput9_1_0.lib;dukat.lib;%(AdditionalDependencies)</AdditionalDependencies> | ||
</Link> | ||
</ItemDefinitionGroup> | ||
<ItemGroup> | ||
<ClInclude Include="stdafx.h" /> | ||
<ClInclude Include="lightingapp.h" /> | ||
<ClInclude Include="targetver.h" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ClCompile Include="stdafx.cpp"> | ||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader> | ||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader> | ||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader> | ||
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader> | ||
</ClCompile> | ||
<ClCompile Include="lightingapp.cpp" /> | ||
</ItemGroup> | ||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> | ||
<ImportGroup Label="ExtensionTargets"> | ||
</ImportGroup> | ||
</Project> |
Oops, something went wrong.