Merge branch 'master' into master

This commit is contained in:
Jon Daniel 2025-08-08 02:24:57 +02:00 • committed by GitHub
commit 0842aabe5c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 2557 additions and 769 deletions

View file

@ -0,0 +1,64 @@
#version 100
precision mediump float;
// Input vertex attributes (from vertex shader)
varying vec3 fragPosition;
varying vec2 fragTexCoord;
varying vec3 fragNormal; //used for when normal mapping is toggled off
varying vec4 fragColor;
varying mat3 TBN;
// Input uniform values
uniform sampler2D texture0;
uniform sampler2D normalMap;
uniform vec4 colDiffuse;
uniform vec3 viewPos;
// NOTE: Add your custom variables here
uniform vec3 lightPos;
uniform bool useNormalMap;
uniform float specularExponent;
void main()
{
vec4 texelColor = texture(texture0, vec2(fragTexCoord.x, fragTexCoord.y));
vec3 specular = vec3(0.0);
vec3 viewDir = normalize(viewPos - fragPosition);
vec3 lightDir = normalize(lightPos - fragPosition);
vec3 normal;
if (useNormalMap)
{
normal = texture(normalMap, vec2(fragTexCoord.x, fragTexCoord.y)).rgb;
//Transform normal values to the range -1.0 ... 1.0
normal = normalize(normal * 2.0 - 1.0);
//Transform the normal from tangent-space to world-space for lighting calculation
normal = normalize(normal * TBN);
}
else
{
normal = normalize(fragNormal);
}
vec4 tint = colDiffuse * fragColor;
vec3 lightColor = vec3(1.0, 1.0, 1.0);
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 lightDot = lightColor * NdotL;
float specCo = 0.0;
if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewDir, reflect(-lightDir, normal))), specularExponent); // 16 refers to shine
specular += specCo;
finalColor = (texelColor * ((tint + vec4(specular, 1.0)) * vec4(lightDot, 1.0)));
finalColor += texelColor * (vec4(1.0, 1.0, 1.0, 1.0) / 40.0) * tint;
// Gamma correction
gl_FragColor = pow(finalColor, vec4(1.0 / 2.2));
}

View file

@ -0,0 +1,76 @@
#version 100
// Input vertex attributes
attribute vec3 vertexPosition;
attribute vec2 vertexTexCoord;
attribute vec3 vertexNormal;
attribute vec4 vertexTangent;
attribute vec4 vertexColor;
// Input uniform values
uniform mat4 mvp;
uniform mat4 matModel;
// Output vertex attributes (to fragment shader)
varying vec3 fragPosition;
varying vec2 fragTexCoord;
varying vec3 fragNormal; //used for when normal mapping is toggled off
varying vec4 fragColor;
varying mat3 TBN;
// NOTE: Add your custom variables here
// https://github.com/glslify/glsl-inverse
mat3 inverse(mat3 m)
{
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
float b01 = a22 * a11 - a12 * a21;
float b11 = -a22 * a10 + a12 * a20;
float b21 = a21 * a10 - a11 * a20;
float det = a00 * b01 + a01 * b11 + a02 * b21;
return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
}
// https://github.com/glslify/glsl-transpose
mat3 transpose(mat3 m)
{
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}
void main()
{
// Compute binormal from vertex normal and tangent. W component is the tangent handedness
vec3 vertexBinormal = cross(vertexNormal, vertexTangent.xyz) * vertexTangent.w;
// Compute fragment normal based on normal transformations
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
// Compute fragment position based on model transformations
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
//Create TBN matrix for transforming the normal map values from tangent-space to world-space
fragNormal = normalize(normalMatrix * vertexNormal);
vec3 fragTangent = normalize(normalMatrix * vertexTangent.xyz);
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal) * fragNormal);
vec3 fragBinormal = normalize(normalMatrix * vertexBinormal);
fragBinormal = cross(fragNormal, fragTangent);
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
fragColor = vertexColor;
fragTexCoord = vertexTexCoord;
gl_Position = mvp * vec4(vertexPosition, 1.0);
}

View file

@ -0,0 +1,28 @@
#version 120
// Input vertex attributes (from vertex shader)
varying vec2 fragTexCoord;
// Input uniform values
uniform sampler2D depthTexture;
uniform bool flipY;
float nearPlane = 0.1;
float farPlane = 100.0;
void main()
{
// Handle potential Y-flipping
vec2 texCoord = fragTexCoord;
if (flipY)
texCoord.y = 1.0 - texCoord.y;
// Sample depth texture
float depth = texture2D(depthTexture, texCoord).r;
// Linearize depth
float linearDepth = (2.0*nearPlane)/(farPlane + nearPlane - depth*(farPlane - nearPlane));
// Output final color
gl_FragColor = vec4(vec3(linearDepth), 1.0);
}

View file

@ -0,0 +1,62 @@
#version 120
// Input vertex attributes (from vertex shader)
varying vec3 fragPosition;
varying vec2 fragTexCoord;
varying vec3 fragNormal; //used for when normal mapping is toggled off
varying vec4 fragColor;
varying mat3 TBN;
// Input uniform values
uniform sampler2D texture0;
uniform sampler2D normalMap;
uniform vec4 colDiffuse;
uniform vec3 viewPos;
// NOTE: Add your custom variables here
uniform vec3 lightPos;
uniform bool useNormalMap;
uniform float specularExponent;
void main()
{
vec4 texelColor = texture(texture0, vec2(fragTexCoord.x, fragTexCoord.y));
vec3 specular = vec3(0.0);
vec3 viewDir = normalize(viewPos - fragPosition);
vec3 lightDir = normalize(lightPos - fragPosition);
vec3 normal;
if (useNormalMap)
{
normal = texture(normalMap, vec2(fragTexCoord.x, fragTexCoord.y)).rgb;
//Transform normal values to the range -1.0 ... 1.0
normal = normalize(normal * 2.0 - 1.0);
//Transform the normal from tangent-space to world-space for lighting calculation
normal = normalize(normal * TBN);
}
else
{
normal = normalize(fragNormal);
}
vec4 tint = colDiffuse * fragColor;
vec3 lightColor = vec3(1.0, 1.0, 1.0);
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 lightDot = lightColor * NdotL;
float specCo = 0.0;
if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewDir, reflect(-lightDir, normal))), specularExponent); // 16 refers to shine
specular += specCo;
finalColor = (texelColor * ((tint + vec4(specular, 1.0)) * vec4(lightDot, 1.0)));
finalColor += texelColor * (vec4(1.0, 1.0, 1.0, 1.0) / 40.0) * tint;
// Gamma correction
gl_FragColor = pow(finalColor, vec4(1.0 / 2.2));
}

View file

@ -0,0 +1,76 @@
#version 120
// Input vertex attributes
attribute vec3 vertexPosition;
attribute vec2 vertexTexCoord;
attribute vec3 vertexNormal;
attribute vec4 vertexTangent;
attribute vec4 vertexColor;
// Input uniform values
uniform mat4 mvp;
uniform mat4 matModel;
// Output vertex attributes (to fragment shader)
varying vec3 fragPosition;
varying vec2 fragTexCoord;
varying vec3 fragNormal; //used for when normal mapping is toggled off
varying vec4 fragColor;
varying mat3 TBN;
// NOTE: Add your custom variables here
// https://github.com/glslify/glsl-inverse
mat3 inverse(mat3 m)
{
float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2];
float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2];
float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2];
float b01 = a22 * a11 - a12 * a21;
float b11 = -a22 * a10 + a12 * a20;
float b21 = a21 * a10 - a11 * a20;
float det = a00 * b01 + a01 * b11 + a02 * b21;
return mat3(b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10)) / det;
}
// https://github.com/glslify/glsl-transpose
mat3 transpose(mat3 m)
{
return mat3(m[0][0], m[1][0], m[2][0],
m[0][1], m[1][1], m[2][1],
m[0][2], m[1][2], m[2][2]);
}
void main()
{
// Compute binormal from vertex normal and tangent. W component is the tangent handedness
vec3 vertexBinormal = cross(vertexNormal, vertexTangent.xyz) * vertexTangent.w;
// Compute fragment normal based on normal transformations
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
// Compute fragment position based on model transformations
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
//Create TBN matrix for transforming the normal map values from tangent-space to world-space
fragNormal = normalize(normalMatrix * vertexNormal);
vec3 fragTangent = normalize(normalMatrix * vertexTangent.xyz);
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal) * fragNormal);
vec3 fragBinormal = normalize(normalMatrix * vertexBinormal);
fragBinormal = cross(fragNormal, fragTangent);
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
fragColor = vertexColor;
fragTexCoord = vertexTexCoord;
gl_Position = mvp * vec4(vertexPosition, 1.0);
}

View file

@ -0,0 +1,67 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec3 fragPosition;
in vec2 fragTexCoord;
in vec3 fragNormal; //used for when normal mapping is toggled off
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform sampler2D normalMap;
uniform vec4 colDiffuse;
uniform vec3 viewPos;
uniform vec4 tintColor;
uniform vec3 lightPos;
uniform bool useNormalMap;
uniform float specularExponent;
// Output fragment color
out vec4 finalColor;
in mat3 TBN;
void main()
{
vec4 texelColor = texture(texture0, vec2(fragTexCoord.x, fragTexCoord.y));
vec3 specular = vec3(0.0);
vec3 viewDir = normalize(viewPos - fragPosition);
vec3 lightDir = normalize(lightPos - fragPosition);
vec3 normal;
if (useNormalMap)
{
normal = texture(normalMap, vec2(fragTexCoord.x, fragTexCoord.y)).rgb;
//Transform normal values to the range -1.0 ... 1.0
normal = normalize(normal * 2.0 - 1.0);
//Transform the normal from tangent-space to world-space for lighting calculation
normal = normalize(normal * TBN);
}
else
{
normal = normalize(fragNormal);
}
vec4 tint = colDiffuse * fragColor;
vec3 lightColor = vec3(1.0, 1.0, 1.0);
float NdotL = max(dot(normal, lightDir), 0.0);
vec3 lightDot = lightColor * NdotL;
float specCo = 0.0;
if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewDir, reflect(-lightDir, normal))), specularExponent); // 16 refers to shine
specular += specCo;
finalColor = (texelColor * ((tint + vec4(specular, 1.0)) * vec4(lightDot, 1.0)));
finalColor += texelColor * (vec4(1.0, 1.0, 1.0, 1.0) / 40.0) * tint;
// Gamma correction
finalColor = pow(finalColor, vec4(1.0 / 2.2));
//finalColor = vec4(normal, 1.0);
}

View file

@ -0,0 +1,48 @@
#version 330
// Input vertex attributes
in vec3 vertexPosition;
in vec2 vertexTexCoord;
in vec3 vertexNormal;
in vec4 vertexTangent;
in vec4 vertexColor;
// Input uniform values
uniform mat4 mvp;
uniform mat4 matModel;
// Output vertex attributes (to fragment shader)
out vec3 fragPosition;
out vec2 fragTexCoord;
out vec3 fragNormal; //used for when normal mapping is toggled off
out vec4 fragColor;
out mat3 TBN;
void main()
{
// Compute binormal from vertex normal and tangent. W component is the tangent handedness
vec3 vertexBinormal = cross(vertexNormal, vertexTangent.xyz) * vertexTangent.w;
// Compute fragment normal based on normal transformations
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
// Compute fragment position based on model transformations
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
//Create TBN matrix for transforming the normal map values from tangent-space to world-space
fragNormal = normalize(normalMatrix * vertexNormal);
vec3 fragTangent = normalize(normalMatrix * vertexTangent.xyz);
fragTangent = normalize(fragTangent - dot(fragTangent, fragNormal) * fragNormal);
vec3 fragBinormal = normalize(normalMatrix * vertexBinormal);
fragBinormal = cross(fragNormal, fragTangent);
TBN = transpose(mat3(fragTangent, fragBinormal, fragNormal));
fragColor = vertexColor;
fragTexCoord = vertexTexCoord;
gl_Position = mvp * vec4(vertexPosition, 1.0);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 528 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 663 KiB

View file

@ -5,9 +5,9 @@
* Example complexity rating: [★★★★] 4/4
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
*
* Example originally created with raylib 3.0, last time updated with raylib 4.2
*
@ -59,10 +59,10 @@ int main(void)
TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
// Get some required shader locations
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
// NOTE: "matModel" location name is automatically assigned on shader loading,
// NOTE: "matModel" location name is automatically assigned on shader loading,
// no need to get the location again if using that uniform name
//shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
// Ambient light level (some basic lighting)
int ambientLoc = GetShaderLocation(shader, "ambient");
SetShaderValue(shader, ambientLoc, (float[4]){ 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
@ -87,13 +87,13 @@ int main(void)
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
// Check key inputs to enable/disable lights
if (IsKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; }
if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; }
if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
// Update light values (actually, only enable/disable them)
for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(shader, lights[i]);
//----------------------------------------------------------------------------------

View file

@ -13,8 +13,8 @@
*
* Copyright (c) 2023-2025 Afan OLOVCIC (@_DevDad)
*
* Model: "Old Rusty Car" (https://skfb.ly/LxRy) by Renafox,
* licensed under Creative Commons Attribution-NonCommercial
* Model: "Old Rusty Car" (https://skfb.ly/LxRy) by Renafox,
* licensed under Creative Commons Attribution-NonCommercial
* (http://creativecommons.org/licenses/by-nc/4.0/)
*
********************************************************************************************/
@ -105,7 +105,7 @@ int main()
// shader already takes care of it accordingly
shader.locs[SHADER_LOC_MAP_METALNESS] = GetShaderLocation(shader, "mraMap");
shader.locs[SHADER_LOC_MAP_NORMAL] = GetShaderLocation(shader, "normalMap");
// WARNING: Similar to the MRA map, the emissive map packs different information
// WARNING: Similar to the MRA map, the emissive map packs different information
// into a single texture: it stores height and emission data
// It is binded to SHADER_LOC_MAP_EMISSION location an properly processed on shader
shader.locs[SHADER_LOC_MAP_EMISSION] = GetShaderLocation(shader, "emissiveMap");
@ -153,7 +153,7 @@ int main()
car.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTexture("resources/old_car_mra.png");
car.materials[0].maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("resources/old_car_n.png");
car.materials[0].maps[MATERIAL_MAP_EMISSION].texture = LoadTexture("resources/old_car_e.png");
// Load floor model mesh and assign material parameters
// NOTE: A basic plane shape can be generated instead of being loaded from a model file
Model floor = LoadModel("resources/models/plane.glb");
@ -161,9 +161,9 @@ int main()
//GenMeshTangents(&floorMesh); // TODO: Review tangents generation
//Model floor = LoadModelFromMesh(floorMesh);
// Assign material shader for our floor model, same PBR shader
// Assign material shader for our floor model, same PBR shader
floor.materials[0].shader = shader;
floor.materials[0].maps[MATERIAL_MAP_ALBEDO].color = WHITE;
floor.materials[0].maps[MATERIAL_MAP_METALNESS].value = 0.8f;
floor.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value = 0.1f;
@ -193,7 +193,7 @@ int main()
SetShaderValue(shader, GetShaderLocation(shader, "useTexNormal"), &usage, SHADER_UNIFORM_INT);
SetShaderValue(shader, GetShaderLocation(shader, "useTexMRA"), &usage, SHADER_UNIFORM_INT);
SetShaderValue(shader, GetShaderLocation(shader, "useTexEmissive"), &usage, SHADER_UNIFORM_INT);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//---------------------------------------------------------------------------------------
@ -227,11 +227,11 @@ int main()
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(BLACK);
BeginMode3D(camera);
// Set floor model texture tiling and emissive color parameters on shader
SetShaderValue(shader, textureTilingLoc, &floorTextureTiling, SHADER_UNIFORM_VEC2);
Vector4 floorEmissiveColor = ColorNormalize(floor.materials[0].maps[MATERIAL_MAP_EMISSION].color);
@ -240,7 +240,7 @@ int main()
// Set floor metallic and roughness values
SetShaderValue(shader, metallicValueLoc, &floor.materials[0].maps[MATERIAL_MAP_METALNESS].value, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader, roughnessValueLoc, &floor.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value, SHADER_UNIFORM_FLOAT);
DrawModel(floor, (Vector3){ 0.0f, 0.0f, 0.0f }, 5.0f, WHITE); // Draw floor model
// Set old car model texture tiling, emissive color and emissive intensity parameters on shader
@ -253,24 +253,24 @@ int main()
// Set old car metallic and roughness values
SetShaderValue(shader, metallicValueLoc, &car.materials[0].maps[MATERIAL_MAP_METALNESS].value, SHADER_UNIFORM_FLOAT);
SetShaderValue(shader, roughnessValueLoc, &car.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value, SHADER_UNIFORM_FLOAT);
DrawModel(car, (Vector3){ 0.0f, 0.0f, 0.0f }, 0.25f, WHITE); // Draw car model
// Draw spheres to show the lights positions
for (int i = 0; i < MAX_LIGHTS; i++)
{
Color lightColor = (Color){ lights[i].color[0]*255, lights[i].color[1]*255, lights[i].color[2]*255, lights[i].color[3]*255 };
if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lightColor);
else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lightColor, 0.3f));
}
EndMode3D();
DrawText("Toggle lights: [1][2][3][4]", 10, 40, 20, LIGHTGRAY);
DrawText("(c) Old Rusty Car model by Renafox (https://skfb.ly/LxRy)", screenWidth - 320, screenHeight - 20, 10, LIGHTGRAY);
DrawFPS(10, 10);
EndDrawing();
@ -279,20 +279,20 @@ int main()
// De-Initialization
//--------------------------------------------------------------------------------------
// Unbind (disconnect) shader from car.material[0]
// Unbind (disconnect) shader from car.material[0]
// to avoid UnloadMaterial() trying to unload it automatically
car.materials[0].shader = (Shader){ 0 };
UnloadMaterial(car.materials[0]);
car.materials[0].maps = NULL;
UnloadModel(car);
floor.materials[0].shader = (Shader){ 0 };
UnloadMaterial(floor.materials[0]);
floor.materials[0].maps = NULL;
UnloadModel(floor);
UnloadShader(shader); // Unload Shader
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
@ -316,7 +316,7 @@ static Light CreateLight(int type, Vector3 position, Vector3 target, Color color
light.color[2] = (float)color.b/255.0f;
light.color[3] = (float)color.a/255.0f;
light.intensity = intensity;
// NOTE: Shader parameters names for lights must match the requested ones
light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightCount));
light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightCount));
@ -324,7 +324,7 @@ static Light CreateLight(int type, Vector3 position, Vector3 target, Color color
light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightCount));
light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightCount));
light.intensityLoc = GetShaderLocation(shader, TextFormat("lights[%i].intensity", lightCount));
UpdateLight(shader, light);
lightCount++;
@ -339,7 +339,7 @@ static void UpdateLight(Shader shader, Light light)
{
SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT);
SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT);
// Send to shader light position values
float position[3] = { light.position.x, light.position.y, light.position.z };
SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3);

View file

@ -5,7 +5,7 @@
* Example complexity rating: [★★☆☆] 2/4
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
@ -87,7 +87,7 @@ int main(void)
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
Vector2 mousePosition = GetMousePosition();
swirlSize[0] = screenWidth;

View file

@ -42,7 +42,7 @@ typedef struct GBuffer {
unsigned int positionTexture;
unsigned int normalTexture;
unsigned int albedoSpecTexture;
unsigned int depthRenderbuffer;
} GBuffer;
@ -94,49 +94,49 @@ int main(void)
TraceLog(LOG_WARNING, "Failed to create framebuffer");
exit(1);
}
rlEnableFramebuffer(gBuffer.framebuffer);
// NOTE: Vertex positions are stored in a texture for simplicity. A better approach would use a depth texture
// (instead of a detph renderbuffer) to reconstruct world positions in the final render shader via clip-space position,
// depth, and the inverse view/projection matrices.
// (instead of a detph renderbuffer) to reconstruct world positions in the final render shader via clip-space position,
// depth, and the inverse view/projection matrices
// 16-bit precision ensures OpenGL ES 3 compatibility, though it may lack precision for real scenarios.
// 16-bit precision ensures OpenGL ES 3 compatibility, though it may lack precision for real scenarios
// But as mentioned above, the positions could be reconstructed instead of stored. If not targeting OpenGL ES
// and you wish to maintain this approach, consider using `RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32`.
// and you wish to maintain this approach, consider using `RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32`
gBuffer.positionTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, 1);
// Similarly, 16-bit precision is used for normals ensures OpenGL ES 3 compatibility.
// This is generally sufficient, but a 16-bit fixed-point format offer a better uniform precision in all orientations.
// Similarly, 16-bit precision is used for normals ensures OpenGL ES 3 compatibility
// This is generally sufficient, but a 16-bit fixed-point format offer a better uniform precision in all orientations
gBuffer.normalTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, 1);
// Albedo (diffuse color) and specular strength can be combined into one texture.
// The color in RGB, and the specular strength in the alpha channel.
// Albedo (diffuse color) and specular strength can be combined into one texture
// The color in RGB, and the specular strength in the alpha channel
gBuffer.albedoSpecTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
// Activate the draw buffers for our framebuffer
rlActiveDrawBuffers(3);
// Now we attach our textures to the framebuffer.
// Now we attach our textures to the framebuffer
rlFramebufferAttach(gBuffer.framebuffer, gBuffer.positionTexture, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
rlFramebufferAttach(gBuffer.framebuffer, gBuffer.normalTexture, RL_ATTACHMENT_COLOR_CHANNEL1, RL_ATTACHMENT_TEXTURE2D, 0);
rlFramebufferAttach(gBuffer.framebuffer, gBuffer.albedoSpecTexture, RL_ATTACHMENT_COLOR_CHANNEL2, RL_ATTACHMENT_TEXTURE2D, 0);
// Finally we attach the depth buffer.
// Finally we attach the depth buffer
gBuffer.depthRenderbuffer = rlLoadTextureDepth(screenWidth, screenHeight, true);
rlFramebufferAttach(gBuffer.framebuffer, gBuffer.depthRenderbuffer, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0);
// Make sure our framebuffer is complete.
// Make sure our framebuffer is complete
// NOTE: rlFramebufferComplete() automatically unbinds the framebuffer, so we don't have
// to rlDisableFramebuffer() here.
// to rlDisableFramebuffer() here
if (!rlFramebufferComplete(gBuffer.framebuffer))
{
TraceLog(LOG_WARNING, "Framebuffer is not complete");
}
// Now we initialize the sampler2D uniform's in the deferred shader.
// Now we initialize the sampler2D uniform's in the deferred shader
// We do this by setting the uniform's values to the texture units that
// we later bind our g-buffer textures to.
// we later bind our g-buffer textures to
rlEnableShader(deferredShader.id);
int texUnitPosition = 0;
int texUnitNormal = 1;
@ -161,7 +161,7 @@ int main(void)
const float CUBE_SCALE = 0.25;
Vector3 cubePositions[MAX_CUBES] = { 0 };
float cubeRotations[MAX_CUBES] = { 0 };
for (int i = 0; i < MAX_CUBES; i++)
{
cubePositions[i] = (Vector3){
@ -169,7 +169,7 @@ int main(void)
.y = (float)(rand()%5),
.z = (float)(rand()%10) - 5,
};
cubeRotations[i] = (float)(rand()%360);
}
@ -190,7 +190,7 @@ int main(void)
// Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
SetShaderValue(deferredShader, deferredShader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
// Check key inputs to enable/disable lights
if (IsKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; }
if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
@ -215,11 +215,11 @@ int main(void)
rlEnableFramebuffer(gBuffer.framebuffer);
rlClearColor(0, 0, 0, 0);
rlClearScreenBuffers(); // Clear color and depth buffer
rlDisableColorBlend();
BeginMode3D(camera);
// NOTE: We have to use rlEnableShader here. `BeginShaderMode` or thus `rlSetShader`
// will not work, as they won't immediately load the shader program.
// will not work, as they won't immediately load the shader program
rlEnableShader(gbufferShader.id);
// When drawing a model here, make sure that the material's shaders
// are set to the gbuffer shader!
@ -236,7 +236,7 @@ int main(void)
EndMode3D();
rlEnableColorBlend();
// Go back to the default framebuffer (0) and draw our deferred shading.
// Go back to the default framebuffer (0) and draw our deferred shading
rlDisableFramebuffer();
rlClearScreenBuffers(); // Clear color & depth buffer
@ -264,10 +264,10 @@ int main(void)
rlEnableColorBlend();
EndMode3D();
// As a last step, we now copy over the depth buffer from our g-buffer to the default framebuffer.
// As a last step, we now copy over the depth buffer from our g-buffer to the default framebuffer
rlBindFramebuffer(RL_READ_FRAMEBUFFER, gBuffer.framebuffer);
rlBindFramebuffer(RL_DRAW_FRAMEBUFFER, 0);
rlBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, 0x00000100); // GL_DEPTH_BUFFER_BIT
rlBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, 0x00000100); // GL_DEPTH_BUFFER_BIT
rlDisableFramebuffer();
// Since our shader is now done and disabled, we can draw spheres
@ -281,7 +281,7 @@ int main(void)
}
rlDisableShader();
EndMode3D();
DrawText("FINAL RESULT", 10, screenHeight - 30, 20, DARKGREEN);
} break;
case DEFERRED_POSITION:
@ -291,7 +291,7 @@ int main(void)
.width = screenWidth,
.height = screenHeight,
}, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, Vector2Zero(), RAYWHITE);
DrawText("POSITION TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
} break;
case DEFERRED_NORMAL:
@ -301,7 +301,7 @@ int main(void)
.width = screenWidth,
.height = screenHeight,
}, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, Vector2Zero(), RAYWHITE);
DrawText("NORMAL TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
} break;
case DEFERRED_ALBEDO:
@ -311,7 +311,7 @@ int main(void)
.width = screenWidth,
.height = screenHeight,
}, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, Vector2Zero(), RAYWHITE);
DrawText("ALBEDO TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
} break;
default: break;
@ -321,7 +321,7 @@ int main(void)
DrawText("Switch G-buffer textures: [1][2][3][4]", 10, 70, 20, DARKGRAY);
DrawFPS(10, 10);
EndDrawing();
// -----------------------------------------------------------------------------
}

View file

@ -1,10 +1,10 @@
/*******************************************************************************************
*
* raylib [shaders] example - Sieve of Eratosthenes
* raylib [shaders] example - sieve of Eratosthenes
*
* Example complexity rating: [★★★☆] 3/4
*
* NOTE: Sieve of Eratosthenes, the earliest known (ancient Greek) prime number sieve.
* NOTE: Sieve of Eratosthenes, the earliest known (ancient Greek) prime number sieve
*
* "Sift the twos and sift the threes,
* The Sieve of Eratosthenes.
@ -12,9 +12,9 @@
* the numbers that are left are prime."
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
*
* Example originally created with raylib 2.5, last time updated with raylib 4.0
*
@ -45,7 +45,7 @@ int main(void)
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - Sieve of Eratosthenes");
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - sieve of Eratosthenes");
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);

View file

@ -5,9 +5,9 @@
* Example complexity rating: [★★★☆] 3/4
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
*
* Example originally created with raylib 2.5, last time updated with raylib 3.7
*

View file

@ -1,11 +1,11 @@
/*******************************************************************************************
*
* raylib [shaders] example - Hot reloading
* raylib [shaders] example - hot reloading
*
* Example complexity rating: [★★★☆] 3/4
*
* NOTE: This example requires raylib OpenGL 3.3 for shaders support and only #version 330
* is currently supported. OpenGL ES 2.0 platforms are not supported at the moment.
* is currently supported. OpenGL ES 2.0 platforms are not supported at the moment
*
* Example originally created with raylib 3.0, last time updated with raylib 3.5
*

View file

@ -1,6 +1,6 @@
/*******************************************************************************************
*
* raylib [shaders] example - Hybrid Rendering
* raylib [shaders] example - hybrid rendering
*
* Example complexity rating: [★★★★] 4/4
*
@ -62,14 +62,20 @@ int main(void)
// You are required to write depth for all shaders if one shader does it
Shader shdrRaster = LoadShader(0, TextFormat("resources/shaders/glsl%i/hybrid_raster.fs", GLSL_VERSION));
// Declare Struct used to store camera locs.
// Declare Struct used to store camera locs
RayLocs marchLocs = {0};
// Fill the struct with shader locs.
// Fill the struct with shader locs
marchLocs.camPos = GetShaderLocation(shdrRaymarch, "camPos");
marchLocs.camDir = GetShaderLocation(shdrRaymarch, "camDir");
marchLocs.screenCenter = GetShaderLocation(shdrRaymarch, "screenCenter");
// Transfer screenCenter position to shader. Which is used to calculate ray direction
Vector2 screenCenter = {.x = screenWidth/2.0f, .y = screenHeight/2.0f};
SetShaderValue(shdrRaymarch, marchLocs.screenCenter , &screenCenter , SHADER_UNIFORM_VEC2);
// Use Customized function to create writable depth texture buffer
RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
// Define the camera to look into our 3d world
Camera camera = {
@ -79,11 +85,10 @@ int main(void)
.fovy = 45.0f, // Camera field-of-view Y
.projection = CAMERA_PERSPECTIVE // Camera projection type
};
// Camera FOV is pre-calculated in the camera Distance.
// Camera FOV is pre-calculated in the camera distance
float camDist = 1.0f/(tanf(camera.fovy*0.5f*DEG2RAD));
RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -106,14 +111,14 @@ int main(void)
SetShaderValue(shdrRaymarch, marchLocs.screenCenter , &screenCenter , SHADER_UNIFORM_VEC2);
UpdateCamera(&camera, CAMERA_ORBITAL);
// Update Camera Postion in the ray march shader.
// Update Camera Postion in the ray march shader
SetShaderValue(shdrRaymarch, marchLocs.camPos, &(camera.position), RL_SHADER_UNIFORM_VEC3);
// Update Camera Looking Vector. Vector length determines FOV.
// Update Camera Looking Vector. Vector length determines FOV
Vector3 camDir = Vector3Scale( Vector3Normalize( Vector3Subtract(camera.target, camera.position)) , camDist);
SetShaderValue(shdrRaymarch, marchLocs.camDir, &(camDir), RL_SHADER_UNIFORM_VEC3);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
// Draw into our custom render texture (framebuffer)
@ -121,11 +126,11 @@ int main(void)
ClearBackground(WHITE);
// Raymarch Scene
rlEnableDepthTest(); //Manually enable Depth Test to handle multiple rendering methods.
rlEnableDepthTest(); // Manually enable Depth Test to handle multiple rendering methods
BeginShaderMode(shdrRaymarch);
DrawRectangleRec((Rectangle){0,0, (float)screenWidth, (float)screenHeight},WHITE);
EndShaderMode();
// Rasterize Scene
BeginMode3D(camera);
BeginShaderMode(shdrRaster);
@ -138,7 +143,7 @@ int main(void)
EndMode3D();
EndTextureMode();
// Draw into screen our custom render texture
// Draw into screen our custom render texture
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);

View file

@ -5,9 +5,9 @@
* Example complexity rating: [★★★☆] 3/4
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
*
* Example originally created with raylib 2.5, last time updated with raylib 4.0
*
@ -109,7 +109,7 @@ int main(void)
SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
}
// If "R" is pressed, reset zoom and offset.
// If "R" is pressed, reset zoom and offset
if (IsKeyPressed(KEY_R))
{
zoom = startingZoom;
@ -125,17 +125,16 @@ int main(void)
if (IsKeyPressed(KEY_RIGHT)) incrementSpeed++;
else if (IsKeyPressed(KEY_LEFT)) incrementSpeed--;
// If either left or right button is pressed, zoom in/out.
// If either left or right button is pressed, zoom in/out
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
{
// Change zoom. If Mouse left -> zoom in. Mouse right -> zoom out.
// Change zoom. If Mouse left -> zoom in. Mouse right -> zoom out
zoom *= IsMouseButtonDown(MOUSE_BUTTON_LEFT)? zoomSpeed : 1.0f/zoomSpeed;
const Vector2 mousePos = GetMousePosition();
Vector2 offsetVelocity;
// Find the velocity at which to change the camera. Take the distance of the mouse
// from the center of the screen as the direction, and adjust magnitude based on
// the current zoom.
// from the center of the screen as the direction, and adjust magnitude based on the current zoom
offsetVelocity.x = (mousePos.x/(float)screenWidth - 0.5f)*offsetSpeedMul/zoom;
offsetVelocity.y = (mousePos.y/(float)screenHeight - 0.5f)*offsetSpeedMul/zoom;
@ -167,7 +166,7 @@ int main(void)
// do not represent full screen coordinates (space where want to apply shader)
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK);
EndTextureMode();
BeginDrawing();
ClearBackground(BLACK); // Clear screen background

View file

@ -5,9 +5,9 @@
* Example complexity rating: [★★★☆] 3/4
*
* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
*
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
*
* Example originally created with raylib 4.5, last time updated with raylib 4.5
*
@ -70,7 +70,7 @@ int main(void)
// Load a new texcoords2 attributes buffer
mesh.vboId[SHADER_LOC_VERTEX_TEXCOORD02] = rlLoadVertexBuffer(mesh.texcoords2, mesh.vertexCount*2*sizeof(float), false);
rlEnableVertexArray(mesh.vaoId);
// Index 5 is for texcoords2
rlSetVertexAttribute(5, 2, RL_FLOAT, 0, 0, 0);
rlEnableVertexAttribute(5);
@ -156,10 +156,10 @@ int main(void)
(Vector2){ 0.0, 0.0 },
0.0,
WHITE);
DrawText("lightmap", GetRenderWidth() - 66, 16 + MAP_SIZE*8, 10, GRAY);
DrawText("10x10 pixels", GetRenderWidth() - 76, 30 + MAP_SIZE*8, 10, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}

View file

@ -64,7 +64,7 @@ int main(void)
Vector3 axis = Vector3Normalize((Vector3){ (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360), (float)GetRandomValue(0, 360) });
float angle = (float)GetRandomValue(0, 180)*DEG2RAD;
Matrix rotation = MatrixRotate(axis, angle);
transforms[i] = MatrixMultiply(rotation, translation);
}

Some files were not shown because too many files have changed in this diff Show more