2
0
mirror of https://github.com/raylib-cs/raylib-cs synced 2025-06-30 19:03:42 -04:00

Merge pull request #14 from mikaelssen/master

example update, missing lights still but progress is forwards
This commit is contained in:
2018-10-24 21:05:52 +01:00
committed by GitHub
13 changed files with 91376 additions and 261 deletions

1
.gitignore vendored
View File

@ -61,7 +61,6 @@ artifacts/
*_i.h *_i.h
*.ilk *.ilk
*.meta *.meta
*.obj
*.pch *.pch
*.pdb *.pdb
*.pgc *.pgc

View File

@ -29,34 +29,36 @@ public struct Light
public partial class models_material_pbr public partial class models_material_pbr
{ {
/*******************************************************************************************
*
* raylib [models] example - PBR material
*
* This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
*
********************************************************************************************/ /*******************************************************************************************
* *
* raylib [models] example - PBR material * raylib [models] example - PBR material
* *
* This example has been created using raylib 1.8 (www.raylib.com) * This example has been created using raylib 1.8 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2017 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
public const int CUBEMAP_SIZE = 512; public const int CUBEMAP_SIZE = 512;
public const int IRRADIANCE_SIZE = 32; public const int IRRADIANCE_SIZE = 32;
public const int PREFILTERED_SIZE = 256; public const int PREFILTERED_SIZE = 256;
public static int Main() public const int BRDF_SIZE = 512;
public const int MAX_LIGHTS = 4; public const int MAX_LIGHTS = 4;
public static int lightsCount = 0; public static int lightsCount = 0;
public const float LIGHT_DISTANCE = 3.5f; public const float LIGHT_DISTANCE = 3.5f;
public const float LIGHT_HEIGHT = 1.0f; public const float LIGHT_HEIGHT = 1.0f;
// PBR material loading // PBR material loading
float LIGHT_DISTANCE = 3.5f;
float LIGHT_HEIGHT = 1.0f;
//private static Material LoadMaterialPBR(Color albedo, float metalness, float roughness); //private static Material LoadMaterialPBR(Color albedo, float metalness, float roughness);
public unsafe static int Main() public unsafe static int Main()
@ -75,8 +77,7 @@ public partial class models_material_pbr
// Load model and PBR material // Load model and PBR material
Model model = LoadModel("resources/pbr/trooper.obj"); Model model = LoadModel("resources/pbr/trooper.obj");
CreateLight(LightType.LIGHT_POINT, new Vector3( -LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f ), new Vector3( 0.0f, 0.0f, 0.0f ),new Color( 0, 0, 255, 255 ), model.material.shader), MeshTangents(ref model.mesh);
CreateLight(LightType.LIGHT_DIRECTIONAL, new Vector3(0.0f, LIGHT_HEIGHT * 2.0f, -LIGHT_DISTANCE ), new Vector3( 0.0f, 0.0f, 0.0f ), new Color(255, 0, 255, 255 ), model.material.shader)
model.material = LoadMaterialPBR(new Color(255, 255, 255, 255), 1.0f, 1.0f); model.material = LoadMaterialPBR(new Color(255, 255, 255, 255), 1.0f, 1.0f);
// Define lights attributes // Define lights attributes
@ -93,7 +94,8 @@ public partial class models_material_pbr
SetTargetFPS(60); // Set our game to run at 60 frames-per-second SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
SetShaderValue(model.material.shader, 0, cameraPos, 3); //(int)model.material.shader.locs, cameraPos, 3);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
// Update // Update
@ -121,54 +123,7 @@ public partial class models_material_pbr
DrawFPS(10, 10); DrawFPS(10, 10);
return 0; EndDrawing();
}
public static Light CreateLight(LightType type, Vector3 pos, Vector3 targ, Color color, Shader shader)
{
Light light = new Light() {
enabled = true,
type = type,
position = pos,
target = targ,
color = color,
};
string enabledName = "lights[x].enabled\0";
string typeName = "lights[x].type\0";
string posName = "lights[x].position\0";
string targetName = "lights[x].target\0";
string colorName = "lights[x].color\0";
light.enabledLoc = GetShaderLocation(shader, enabledName);
light.typeLoc = GetShaderLocation(shader, typeName);
light.posLoc = GetShaderLocation(shader, posName);
light.targetLoc = GetShaderLocation(shader, targetName);
light.colorLoc = GetShaderLocation(shader, colorName);
UpdateLightValues(shader, light);
return light;
}
public static void UpdateLightValues(Shader shader, Light light)
{
// Send to shader light enabled state and type
SetShaderValuei(shader, light.enabledLoc, new int[] { Convert.ToInt32(light.enabled) }, 1);
SetShaderValuei(shader, light.typeLoc, new int[] { Convert.ToInt32(light.type) }, 1);
// Send to shader light position values
float[] position = { light.position.x, light.position.y, light.position.z };
SetShaderValue(shader, light.posLoc, position, 3);
// Send to shader light target position values
float[] target = { light.target.x, light.target.y, light.target.z };
SetShaderValue(shader, light.targetLoc, target, 3);
// Send to shader light color values
float[] diff = { light.color.r / 255, light.color.g / 255, light.color.b / 255, light.color.a / 255 };
SetShaderValue(shader, light.colorLoc, diff, 4);
}
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
} }
@ -208,13 +163,13 @@ public partial class models_material_pbr
mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT"); mat.shader.locs[(int)ShaderLocationIndex.LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
// Set view matrix location // Set view matrix location
const string PATH_CUBEMAP_VS = "resources/shaders/cubemap.vs"; mat.shader.locs[(int)ShaderLocationIndex.LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel");
const string PATH_CUBEMAP_FS = "resources/shaders/cubemap.fs"; mat.shader.locs[(int)ShaderLocationIndex.LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
const string PATH_SKYBOX_VS = "resources/shaders/skybox.vs"; mat.shader.locs[(int)ShaderLocationIndex.LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
const string PATH_IRRADIANCE_FS = "resources/shaders/irradiance.fs";
const string PATH_PREFILTER_FS = "resources/shaders/prefilter.fs"; // Set PBR standard maps
const string PATH_BRDF_VS = "resources/shaders/brdf.vs"; mat.maps[(int)TexmapIndex.MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
const string PATH_BRDF_FS = "resources/shaders/brdf.fs"; mat.maps[(int)TexmapIndex.MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png");
mat.maps[(int)TexmapIndex.MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png"); mat.maps[(int)TexmapIndex.MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png"); mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
mat.maps[(int)TexmapIndex.MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png"); mat.maps[(int)TexmapIndex.MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
@ -222,9 +177,7 @@ public partial class models_material_pbr
// Set environment maps // Set environment maps
const string PATH_CUBEMAP_VS = "resources/shaders/cubemap.vs"; // Path to equirectangular to cubemap vertex shader const string PATH_CUBEMAP_VS = "resources/shaders/cubemap.vs"; // Path to equirectangular to cubemap vertex shader
const string PATH_CUBEMAP_FS = "resources/shaders/cubemap.fs"; // Path to equirectangular to cubemap fragment shader const string PATH_CUBEMAP_FS = "resources/shaders/cubemap.fs"; // Path to equirectangular to cubemap fragment shader
SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), new int[] { 0 }, 1); const string PATH_SKYBOX_VS = "resources/shaders/skybox.vs"; // Path to skybox vertex shader
SetShaderValuei(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), new int[] { 0 }, 1);
SetShaderValuei(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), new int[] { 0 }, 1);
const string PATH_IRRADIANCE_FS = "resources/shaders/irradiance.fs"; // Path to irradiance (GI) calculation fragment shader const string PATH_IRRADIANCE_FS = "resources/shaders/irradiance.fs"; // Path to irradiance (GI) calculation fragment shader
const string PATH_PREFILTER_FS = "resources/shaders/prefilter.fs"; // Path to reflection prefilter calculation fragment shader const string PATH_PREFILTER_FS = "resources/shaders/prefilter.fs"; // Path to reflection prefilter calculation fragment shader
const string PATH_BRDF_VS = "resources/shaders/brdf.vs"; // Path to bidirectional reflectance distribution function vertex shader const string PATH_BRDF_VS = "resources/shaders/brdf.vs"; // Path to bidirectional reflectance distribution function vertex shader
@ -248,26 +201,67 @@ public partial class models_material_pbr
UnloadTexture(cubemap); UnloadTexture(cubemap);
UnloadTexture(texHDR); UnloadTexture(texHDR);
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), new int[] { 1 }, 1); // Unload already used shaders (to create specific textures)
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), new int[] { 1 }, 1);
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), new int[] { 1 }, 1);
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), new int[] { 1 }, 1);
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), new int[] { 1 }, 1);
UnloadShader(shdrCubemap); UnloadShader(shdrCubemap);
UnloadShader(shdrIrradiance); UnloadShader(shdrIrradiance);
UnloadShader(shdrPrefilter); UnloadShader(shdrPrefilter);
UnloadShader(shdrBRDF); UnloadShader(shdrBRDF);
mat.maps[(int)TexmapIndex.MAP_ALBEDO].color = albedo; // Set textures filtering for better quality
mat.maps[(int)TexmapIndex.MAP_NORMAL].color = new Color( 128, 128, 255, 255 ); SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_ALBEDO].texture, (int)TextureFilterMode.FILTER_BILINEAR);
mat.maps[(int)TexmapIndex.MAP_METALNESS].value = metalness;
mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].value = roughness;
mat.maps[(int)TexmapIndex.MAP_OCCLUSION].value = 1.0f;
mat.maps[(int)TexmapIndex.MAP_EMISSION].value = 0.5f;
mat.maps[(int)TexmapIndex.MAP_HEIGHT].value = 0.5f;
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_NORMAL].texture, (int)TextureFilterMode.FILTER_BILINEAR); SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_NORMAL].texture, (int)TextureFilterMode.FILTER_BILINEAR);
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_METALNESS].texture, (int)TextureFilterMode.FILTER_BILINEAR); SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_METALNESS].texture, (int)TextureFilterMode.FILTER_BILINEAR);
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].texture, (int)TextureFilterMode.FILTER_BILINEAR); SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].texture, (int)TextureFilterMode.FILTER_BILINEAR);
SetTextureFilter(mat.maps[(int)TexmapIndex.MAP_OCCLUSION].texture, (int)TextureFilterMode.FILTER_BILINEAR);
// Enable sample usage in shader for assigned textures
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), new int[] { 1 }, 1);
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), new int[] { 1 }, 1);
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), new int[] { 1 }, 1);
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), new int[] { 1 }, 1);
SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), new int[] { 1 }, 1);
int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
SetShaderValuei(mat.shader, renderModeLoc, new int[] { 0 }, 1);
// Set up material properties color
mat.maps[(int)TexmapIndex.MAP_ALBEDO].color = albedo;
mat.maps[(int)TexmapIndex.MAP_NORMAL].color = new Color(128, 128, 255, 255);
mat.maps[(int)TexmapIndex.MAP_METALNESS].value = metalness;
mat.maps[(int)TexmapIndex.MAP_ROUGHNESS].value = roughness;
mat.maps[(int)TexmapIndex.MAP_OCCLUSION].value = 1.0f;
mat.maps[(int)TexmapIndex.MAP_EMISSION].value = 0.5f;
mat.maps[(int)TexmapIndex.MAP_HEIGHT].value = 0.5f;
return mat;
}
public static Light CreateLight(LightType type, Vector3 pos, Vector3 targ, Color color, Shader shader)
{
Light light = new Light();
if (lightsCount < MAX_LIGHTS)
{
light.enabled = true;
light.type = type;
light.position = pos;
light.target = targ;
light.color = color;
string enabledName = $"lights[{lightsCount}].enabled\0";
string typeName = $"lights[{lightsCount}].type\0";
string posName = $"lights[{lightsCount}].position\0";
string targetName = $"lights[{lightsCount}].target\0";
string colorName = $"lights[{lightsCount}].color\0";
light.enabledLoc = GetShaderLocation(shader, enabledName);
light.typeLoc = GetShaderLocation(shader, typeName);
light.posLoc = GetShaderLocation(shader, posName);
light.targetLoc = GetShaderLocation(shader, targetName);
light.colorLoc = GetShaderLocation(shader, colorName);
UpdateLightValues(shader, light);
lightsCount++; lightsCount++;
} }
return light; return light;

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff