Merge branch 'master' into master

This commit is contained in:
Jon Daniel 2025-04-13 23:59:57 +02:00 committed by GitHub
commit db4efbce74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 113 additions and 81 deletions

View file

@ -15,40 +15,12 @@
*
********************************************************************************************/
#include <raylib.h>
#include <raymath.h>
#include "raylib.h"
#include "raymath.h"
//------------------------------------------------------------------------------------
// Sound positioning function
//------------------------------------------------------------------------------------
static void SetSoundPosition(Camera listener, Sound sound, Vector3 position, float maxDist)
{
// Calculate direction vector and distance between listener and sound source
Vector3 direction = Vector3Subtract(position, listener.position);
float distance = Vector3Length(direction);
// Apply logarithmic distance attenuation and clamp between 0-1
float attenuation = 1.0f / (1.0f + (distance / maxDist));
attenuation = Clamp(attenuation, 0.0f, 1.0f);
// Calculate normalized vectors for spatial positioning
Vector3 normalizedDirection = Vector3Normalize(direction);
Vector3 forward = Vector3Normalize(Vector3Subtract(listener.target, listener.position));
Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, listener.up));
// Reduce volume for sounds behind the listener
float dotProduct = Vector3DotProduct(forward, normalizedDirection);
if (dotProduct < 0.0f) {
attenuation *= (1.0f + dotProduct * 0.5f);
}
// Set stereo panning based on sound position relative to listener
float pan = 0.5f + 0.5f * Vector3DotProduct(normalizedDirection, right);
// Apply final sound properties
SetSoundVolume(sound, attenuation);
SetSoundPan(sound, pan);
}
static void SetSoundPosition(Camera listener, Sound sound, Vector3 position, float maxDist);
//------------------------------------------------------------------------------------
// Program main entry point
@ -57,12 +29,13 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
InitWindow(800, 600, "Quick Spatial Sound");
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [audio] example - Playing spatialized 3D sound");
InitAudioDevice();
SetTargetFPS(60);
DisableCursor();
Sound sound = LoadSound("resources/coin.wav");
Camera camera = {
@ -70,7 +43,12 @@ int main(void)
.target = (Vector3) { 0, 0, 0 },
.up = (Vector3) { 0, 1, 0 },
.fovy = 60,
.projection = CAMERA_PERSPECTIVE
};
DisableCursor();
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main game loop
@ -83,9 +61,9 @@ int main(void)
float th = GetTime();
Vector3 spherePos = {
.x = 5.0f * cosf(th),
.x = 5.0f*cosf(th),
.y = 0.0f,
.z = 5.0f * sinf(th)
.z = 5.0f*sinf(th)
};
SetSoundPosition(camera, sound, spherePos, 20.0f);
@ -95,14 +73,14 @@ int main(void)
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
{
ClearBackground(BLACK);
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawGrid(10, 2);
DrawSphere(spherePos, 0.5f, RED);
EndMode3D();
}
EndDrawing();
//----------------------------------------------------------------------------------
}
@ -110,6 +88,36 @@ int main(void)
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadSound(sound);
CloseAudioDevice();
CloseWindow();
CloseAudioDevice(); // Close audio device
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
}
// Sound positioning function
static void SetSoundPosition(Camera listener, Sound sound, Vector3 position, float maxDist)
{
// Calculate direction vector and distance between listener and sound source
Vector3 direction = Vector3Subtract(position, listener.position);
float distance = Vector3Length(direction);
// Apply logarithmic distance attenuation and clamp between 0-1
float attenuation = 1.0f/(1.0f + (distance/maxDist));
attenuation = Clamp(attenuation, 0.0f, 1.0f);
// Calculate normalized vectors for spatial positioning
Vector3 normalizedDirection = Vector3Normalize(direction);
Vector3 forward = Vector3Normalize(Vector3Subtract(listener.target, listener.position));
Vector3 right = Vector3Normalize(Vector3CrossProduct(forward, listener.up));
// Reduce volume for sounds behind the listener
float dotProduct = Vector3DotProduct(forward, normalizedDirection);
if (dotProduct < 0.0f) attenuation *= (1.0f + dotProduct*0.5f);
// Set stereo panning based on sound position relative to listener
float pan = 0.5f + 0.5f*Vector3DotProduct(normalizedDirection, right);
// Apply final sound properties
SetSoundVolume(sound, attenuation);
SetSoundPan(sound, pan);
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 26 KiB

Before After
Before After

View file

@ -10,12 +10,6 @@ uniform bool flipY;
float nearPlane = 0.1;
float farPlane = 100.0;
// Function to linearize depth from non-linear depth buffer
float linearizeDepth(float depth)
{
return (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane));
}
void main()
{
// Handle potential Y-flipping
@ -27,7 +21,7 @@ void main()
float depth = texture2D(depthTexture, texCoord).r;
// Linearize depth
float linearDepth = linearizeDepth(depth);
float linearDepth = (2.0*nearPlane)/(farPlane + nearPlane - depth*(farPlane - nearPlane));
// Output final color
gl_FragColor = vec4(vec3(linearDepth), 1.0);

View file

@ -13,24 +13,17 @@ const float farPlane = 100.0;
// Output fragment color
out vec4 finalColor;
// Linearizes the depth buffer value
float linearizeDepth(float depth)
{
return (2.0 * nearPlane) / (farPlane + nearPlane - depth * (farPlane - nearPlane));
}
void main()
{
// Handle potential Y-flipping
vec2 texCoord = fragTexCoord;
if (flipY)
texCoord.y = 1.0 - texCoord.y;
if (flipY) texCoord.y = 1.0 - texCoord.y;
// Sample depth
float depth = texture(depthTexture, texCoord).r;
// Linearize depth value
float linearDepth = linearizeDepth(depth);
float linearDepth = (2.0*nearPlane)/(farPlane + nearPlane - depth*(farPlane - nearPlane));
// Output final color
finalColor = vec4(vec3(linearDepth), 1.0);

View file

@ -34,11 +34,11 @@ int main(void)
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 600;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shader] example - render depth texture");
// Load camera
// Init camera
Camera camera = { 0 };
camera.position = (Vector3){ 4.0f, 1.0f, 5.0f };
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
@ -56,7 +56,7 @@ int main(void)
SetShaderValue(depthShader, flipTextureLoc, (int[]){ 1 }, SHADER_UNIFORM_INT); // Flip Y texture
// Load models
Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
Model floor = LoadModelFromMesh(GenMeshPlane(20.0f, 20.0f, 1, 1));
DisableCursor(); // Limit cursor to relative movement inside the window
@ -75,25 +75,19 @@ int main(void)
// Draw
//----------------------------------------------------------------------------------
BeginTextureMode(target);
ClearBackground(WHITE);
BeginMode3D(camera);
DrawModel(cube, (Vector3){ 0.0f, 0.0f, 0.0f }, 3.0f, YELLOW);
DrawModel(floor, (Vector3){ 10.0f, 0.0f, 2.0f }, 2.0f, RED);
EndMode3D();
EndTextureMode();
BeginDrawing();
BeginShaderMode(depthShader);
SetShaderValueTexture(depthShader, depthLoc, target.depth);
DrawTexture(target.depth, 0, 0, WHITE);
EndShaderMode();
DrawRectangle( 10, 10, 320, 93, Fade(SKYBLUE, 0.5f));

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 30 KiB

Before After
Before After

View file

@ -74,9 +74,8 @@ int main(void)
// Draw
//----------------------------------------------------------------------------------
UnloadRenderTexture(target);
target = LoadRenderTextureDepthTex(GetScreenWidth(), GetScreenHeight());
target = LoadRenderTextureDepthTex(GetScreenWidth(), GetScreenHeight());
// Draw into our custom render texture (framebuffer)
BeginTextureMode(target);
ClearBackground(WHITE);
@ -94,8 +93,7 @@ int main(void)
// Draw into screen our custom render texture
BeginDrawing();
ClearBackground(RAYWHITE);
ClearBackground(RAYWHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)GetScreenWidth(), (float)-GetScreenHeight() }, (Vector2) { 0, 0 }, WHITE);
DrawFPS(10, 10);
EndDrawing();