example resize fixes

This commit is contained in:
Jon Daniel 2025-02-26 19:12:32 +01:00
parent e4993c5557
commit 48f9c9c8ff
32 changed files with 323 additions and 234 deletions

View file

@ -12,19 +12,15 @@ uniform vec4 colDiffuse;
// NOTE: Add your custom variables here
// NOTE: Render size values should be passed from code
const float renderWidth = 800.0;
const float renderHeight = 450.0;
float radius = 250.0;
float angle = 0.8;
uniform vec2 size = { 800.0, 450.0 };
uniform vec2 center;
void main()
{
vec2 texSize = vec2(renderWidth, renderHeight);
vec2 tc = fragTexCoord*texSize;
vec2 tc = fragTexCoord*size;
tc -= center;
float dist = length(tc);
@ -40,7 +36,7 @@ void main()
}
tc += center;
vec4 color = texture2D(texture0, tc/texSize)*colDiffuse*fragColor;;
vec4 color = texture2D(texture0, tc/size)*colDiffuse*fragColor;;
gl_FragColor = vec4(color.rgb, 1.0);
}

View file

@ -10,19 +10,15 @@ uniform vec4 colDiffuse;
// NOTE: Add your custom variables here
// NOTE: Render size values should be passed from code
const float renderWidth = 800;
const float renderHeight = 450;
float radius = 250.0;
float angle = 0.8;
uniform vec2 size = vec2(800.0, 450.0);
uniform vec2 center;
void main()
{
vec2 texSize = vec2(renderWidth, renderHeight);
vec2 tc = fragTexCoord*texSize;
vec2 tc = fragTexCoord*size;
tc -= center;
float dist = length(tc);
@ -38,7 +34,7 @@ void main()
}
tc += center;
vec4 color = texture2D(texture0, tc/texSize)*colDiffuse*fragColor;;
vec4 color = texture2D(texture0, tc/size)*colDiffuse*fragColor;;
gl_FragColor = vec4(color.rgb, 1.0);;
}
}

View file

@ -13,19 +13,15 @@ out vec4 finalColor;
// NOTE: Add your custom variables here
// NOTE: Render size values should be passed from code
const float renderWidth = 800;
const float renderHeight = 450;
float radius = 250.0;
float angle = 0.8;
uniform vec2 size = vec2(800.0, 450.0);
uniform vec2 center = vec2(200.0, 200.0);
void main()
{
vec2 texSize = vec2(renderWidth, renderHeight);
vec2 tc = fragTexCoord*texSize;
vec2 tc = fragTexCoord*size;
tc -= center;
float dist = length(tc);
@ -41,7 +37,7 @@ void main()
}
tc += center;
vec4 color = texture(texture0, tc/texSize)*colDiffuse*fragColor;;
vec4 color = texture(texture0, tc/size)*colDiffuse*fragColor;;
finalColor = vec4(color.rgb, 1.0);;
}
}

View file

@ -253,7 +253,7 @@ int main()
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);
DrawText("(c) Old Rusty Car model by Renafox (https://skfb.ly/LxRy)", GetScreenWidth() - 320, GetScreenHeight() - 20, 10, LIGHTGRAY);
DrawFPS(10, 10);

View file

@ -35,8 +35,10 @@ int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
int screenWidth = 800;
int screenHeight = 450;
float swirlSize[2] = { (float)screenWidth, (float)screenHeight };
float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 };
SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
@ -62,12 +64,11 @@ int main(void)
// Get variable (uniform) location on the shader to connect with the program
// NOTE: If uniform variable could not be found in the shader, function returns -1
int swirlSizeLoc = GetShaderLocation(shader, "size");
int swirlCenterLoc = GetShaderLocation(shader, "center");
float swirlCenter[2] = { (float)screenWidth/2, (float)screenHeight/2 };
// Create a RenderTexture2D to be used for render to texture
RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
RenderTexture2D target;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -75,15 +76,24 @@ int main(void)
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
// Create a RenderTexture2D to be used for render to texture
UnloadRenderTexture(target);
target = LoadRenderTexture(screenWidth, screenHeight);
// Update
//----------------------------------------------------------------------------------
UpdateCamera(&camera, CAMERA_ORBITAL);
Vector2 mousePosition = GetMousePosition();
swirlSize[0] = screenWidth;
swirlSize[1] = screenHeight;
SetShaderValue(shader, swirlSizeLoc, swirlSize, SHADER_UNIFORM_VEC2);
swirlCenter[0] = mousePosition.x;
swirlCenter[1] = screenHeight - mousePosition.y;
// Send new value to the shader to be used on drawing
SetShaderValue(shader, swirlCenterLoc, swirlCenter, SHADER_UNIFORM_VEC2);
//----------------------------------------------------------------------------------
@ -98,7 +108,7 @@ int main(void)
DrawGrid(10, 1.0f); // Draw a grid
EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode
DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED);
DrawText("TEXT DRAWN IN RENDER TEXTURE", screenWidth - 600, 10, 30, RED);
EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
BeginDrawing();
@ -107,7 +117,7 @@ int main(void)
// Enable shader using the custom uniform
BeginShaderMode(shader);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE);
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2){ 0, 0 }, WHITE);
EndShaderMode();
// Draw some 2d text over drawn texture
@ -128,4 +138,4 @@ int main(void)
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -51,9 +51,6 @@ int main(void)
int mouseLoc = GetShaderLocation(shader, "mouse");
int timeLoc = GetShaderLocation(shader, "time");
float resolution[2] = { (float)screenWidth, (float)screenHeight };
SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
float totalTime = 0.0f;
bool shaderAutoReloading = false;
@ -65,6 +62,9 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
float resolution[2] = { (float)GetScreenWidth(), (float)GetScreenHeight() };
SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
totalTime += GetFrameTime();
Vector2 mouse = GetMousePosition();
float mousePos[2] = { mouse.x, mouse.y };
@ -113,14 +113,14 @@ int main(void)
// We only draw a white full-screen rectangle, frame is generated in shader
BeginShaderMode(shader);
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), WHITE);
EndShaderMode();
DrawText(TextFormat("PRESS [A] to TOGGLE SHADER AUTOLOADING: %s",
shaderAutoReloading? "AUTO" : "MANUAL"), 10, 10, 10, shaderAutoReloading? RED : BLACK);
if (!shaderAutoReloading) DrawText("MOUSE CLICK to SHADER RE-LOADING", 10, 30, 10, BLACK);
DrawText(TextFormat("Shader last modification: %s", asctime(localtime(&fragShaderFileModTime))), 10, 430, 10, BLACK);
DrawText(TextFormat("Shader last modification: %s", asctime(localtime(&fragShaderFileModTime))), 10, GetScreenHeight() - 20, 10, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------

View file

@ -69,12 +69,6 @@ int main(void)
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 = {
@ -87,6 +81,7 @@ int main(void)
// Camera FOV is pre-calculated in the camera Distance.
float camDist = 1.0f/(tanf(camera.fovy*0.5f*DEG2RAD));
RenderTexture2D target;
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
@ -96,6 +91,14 @@ int main(void)
{
// Update
//----------------------------------------------------------------------------------
// Use Customized function to create writable depth texture buffer
// Transfer screenCenter position to shader. Which is used to calculate ray direction.
UnloadRenderTexture(target);
target = LoadRenderTextureDepthTex(GetScreenWidth(), GetScreenHeight());
Vector2 screenCenter = {.x = GetScreenWidth()/2.0f, .y = GetScreenHeight()/2.0f};
SetShaderValue(shdrRaymarch, marchLocs.screenCenter , &screenCenter , SHADER_UNIFORM_VEC2);
UpdateCamera(&camera, CAMERA_ORBITAL);
// Update Camera Postion in the ray march shader.
@ -115,7 +118,7 @@ int main(void)
// Raymarch Scene
rlEnableDepthTest(); //Manually enable Depth Test to handle multiple rendering methods.
BeginShaderMode(shdrRaymarch);
DrawRectangleRec((Rectangle){0,0, (float)screenWidth, (float)screenHeight},WHITE);
DrawRectangleRec((Rectangle){0,0, (float)GetScreenWidth(), (float)GetScreenHeight()},WHITE);
EndShaderMode();
// Rasterize Scene
@ -134,7 +137,7 @@ int main(void)
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)GetScreenWidth(), (float)-GetScreenHeight() }, (Vector2) { 0, 0 }, WHITE);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------

View file

@ -88,7 +88,7 @@ int main(void)
EndMode3D();
DrawText("(c) Watermill 3D model by Alberto Cano", screenWidth - 210, screenHeight - 20, 10, GRAY);
DrawText("(c) Watermill 3D model by Alberto Cano", GetScreenWidth() - 210, GetScreenHeight() - 20, 10, GRAY);
DrawFPS(10, 10);
@ -106,4 +106,4 @@ int main(void)
//--------------------------------------------------------------------------------------
return 0;
}
}

View file

@ -133,6 +133,8 @@ int main(void)
// Draw
//----------------------------------------------------------------------------------
UnloadRenderTexture(target);
target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
BeginTextureMode(target); // Enable drawing to texture
ClearBackground(RAYWHITE); // Clear texture background
@ -148,17 +150,17 @@ int main(void)
// Render generated texture using selected postprocessing shader
BeginShaderMode(shaders[currentShader]);
// NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE);
DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)GetScreenWidth(), (float)-GetScreenHeight() }, (Vector2){0, 0}, WHITE);
EndShaderMode();
// Draw 2d shapes and text over drawn texture
DrawRectangle(0, 9, 580, 30, Fade(LIGHTGRAY, 0.7f));
DrawText("(c) Church 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
DrawText("(c) Church 3D model by Alberto Cano", GetScreenWidth() - 200, GetScreenHeight() - 20, 10, GRAY);
DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK);
DrawText(postproShaderText[currentShader], 330, 15, 20, RED);
DrawText("< >", 540, 10, 30, DARKBLUE);
DrawFPS(700, 15);
DrawFPS(GetScreenWidth() - 100, 15);
EndDrawing();
//----------------------------------------------------------------------------------
}

View file

@ -112,7 +112,7 @@ int main(void)
DrawText("Rounded rectangle", rec.x - 20, rec.y - 35, 10, DARKGRAY);
// Flip Y axis to match shader coordinate system
rec.y = screenHeight - rec.y - rec.height;
rec.y = GetScreenHeight() - rec.y - rec.height;
SetShaderValue(shader, roundedRectangle.rectangleLoc, (float[]){ rec.x, rec.y, rec.width, rec.height }, SHADER_UNIFORM_VEC4);
// Only rectangle color
@ -121,7 +121,7 @@ int main(void)
SetShaderValue(shader, roundedRectangle.borderColorLoc, (float[]) { 0.0f, 0.0f, 0.0f, 0.0f }, SHADER_UNIFORM_VEC4);
BeginShaderMode(shader);
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), WHITE);
EndShaderMode();
@ -131,7 +131,7 @@ int main(void)
DrawRectangleLines(rec.x - 20, rec.y - 20, rec.width + 40, rec.height + 40, DARKGRAY);
DrawText("Rounded rectangle shadow", rec.x - 20, rec.y - 35, 10, DARKGRAY);
rec.y = screenHeight - rec.y - rec.height;
rec.y = GetScreenHeight() - rec.y - rec.height;
SetShaderValue(shader, roundedRectangle.rectangleLoc, (float[]){ rec.x, rec.y, rec.width, rec.height }, SHADER_UNIFORM_VEC4);
// Only shadow color
@ -140,7 +140,7 @@ int main(void)
SetShaderValue(shader, roundedRectangle.borderColorLoc, (float[]) { 0.0f, 0.0f, 0.0f, 0.0f }, SHADER_UNIFORM_VEC4);
BeginShaderMode(shader);
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), WHITE);
EndShaderMode();
@ -150,7 +150,7 @@ int main(void)
DrawRectangleLines(rec.x - 20, rec.y - 20, rec.width + 40, rec.height + 40, DARKGRAY);
DrawText("Rounded rectangle border", rec.x - 20, rec.y - 35, 10, DARKGRAY);
rec.y = screenHeight - rec.y - rec.height;
rec.y = GetScreenHeight() - rec.y - rec.height;
SetShaderValue(shader, roundedRectangle.rectangleLoc, (float[]){ rec.x, rec.y, rec.width, rec.height }, SHADER_UNIFORM_VEC4);
// Only border color
@ -159,17 +159,17 @@ int main(void)
SetShaderValue(shader, roundedRectangle.borderColorLoc, (float[]) { borderColor.r/255.0f, borderColor.g/255.0f, borderColor.b/255.0f, borderColor.a/255.0f }, SHADER_UNIFORM_VEC4);
BeginShaderMode(shader);
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), WHITE);
EndShaderMode();
// Draw one more rectangle with all three colors
rec = (Rectangle){ 240, 80, 500, 300 };
rec = (Rectangle){ 240, 80, GetScreenWidth() - 300, GetScreenHeight() - 150 };
DrawRectangleLines(rec.x - 30, rec.y - 30, rec.width + 60, rec.height + 60, DARKGRAY);
DrawText("Rectangle with all three combined", rec.x - 30, rec.y - 45, 10, DARKGRAY);
rec.y = screenHeight - rec.y - rec.height;
rec.y = GetScreenHeight() - rec.y - rec.height;
SetShaderValue(shader, roundedRectangle.rectangleLoc, (float[]){ rec.x, rec.y, rec.width, rec.height }, SHADER_UNIFORM_VEC4);
// All three colors
@ -178,10 +178,10 @@ int main(void)
SetShaderValue(shader, roundedRectangle.borderColorLoc, (float[]) { borderColor.r/255.0f, borderColor.g/255.0f, borderColor.b/255.0f, borderColor.a/255.0f }, SHADER_UNIFORM_VEC4);
BeginShaderMode(shader);
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), WHITE);
EndShaderMode();
DrawText("(c) Rounded rectangle SDF by Iñigo Quilez. MIT License.", screenWidth - 300, screenHeight - 20, 10, BLACK);
DrawText("(c) Rounded rectangle SDF by Iñigo Quilez. MIT License.", GetScreenWidth() - 300, GetScreenHeight() - 20, 10, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------

View file

@ -176,7 +176,7 @@ int main(void)
EndMode3D();
DrawText("Shadows in raylib using the shadowmapping algorithm!", screenWidth - 320, screenHeight - 20, 10, GRAY);
DrawText("Shadows in raylib using the shadowmapping algorithm!", GetScreenWidth() - 320, GetScreenHeight() - 20, 10, GRAY);
DrawText("Use the arrow keys to rotate the light!", 10, 10, 30, RED);
EndDrawing();

View file

@ -85,7 +85,7 @@ int main(void)
for (int n = 0; n < MAX_STARS; n++) ResetStar(&stars[n]);
// Progress all the stars on, so they don't all start in the centre
for (int m = 0; m < screenWidth/2.0; m++)
for (int m = 0; m < GetScreenWidth()/2.0; m++)
{
for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
}
@ -124,8 +124,8 @@ int main(void)
// and initialize the shader locations
for (int i = 0; i < MAX_SPOTS; i++)
{
spots[i].position.x = (float)GetRandomValue(64, screenWidth - 64);
spots[i].position.y = (float)GetRandomValue(64, screenHeight - 64);
spots[i].position.x = (float)GetRandomValue(64, GetScreenWidth() - 64);
spots[i].position.y = (float)GetRandomValue(64, GetScreenHeight() - 64);
spots[i].speed = (Vector2){ 0, 0 };
while ((fabs(spots[i].speed.x) + fabs(spots[i].speed.y)) < 2)
@ -152,6 +152,9 @@ int main(void)
//----------------------------------------------------------------------------------
frameCounter++;
sw = (float)GetScreenWidth();
SetShaderValue(shdrSpot, wLoc, &sw, SHADER_UNIFORM_FLOAT);
// Move the stars, resetting them if the go offscreen
for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
@ -162,7 +165,7 @@ int main(void)
{
Vector2 mp = GetMousePosition();
spots[i].position.x = mp.x;
spots[i].position.y = screenHeight - mp.y;
spots[i].position.y = GetScreenHeight() - mp.y;
}
else
{
@ -170,9 +173,9 @@ int main(void)
spots[i].position.y += spots[i].speed.y;
if (spots[i].position.x < 64) spots[i].speed.x = -spots[i].speed.x;
if (spots[i].position.x > (screenWidth - 64)) spots[i].speed.x = -spots[i].speed.x;
if (spots[i].position.x > (GetScreenWidth() - 64)) spots[i].speed.x = -spots[i].speed.x;
if (spots[i].position.y < 64) spots[i].speed.y = -spots[i].speed.y;
if (spots[i].position.y > (screenHeight - 64)) spots[i].speed.y = -spots[i].speed.y;
if (spots[i].position.y > (GetScreenHeight() - 64)) spots[i].speed.y = -spots[i].speed.y;
}
SetShaderValue(shdrSpot, spots[i].positionLoc, &spots[i].position.x, SHADER_UNIFORM_VEC2);
@ -194,8 +197,8 @@ int main(void)
for (int i = 0; i < 16; i++)
{
DrawTexture(texRay,
(int)((screenWidth/2.0f) + cos((frameCounter + i*8)/51.45f)*(screenWidth/2.2f) - 32),
(int)((screenHeight/2.0f) + sin((frameCounter + i*8)/17.87f)*(screenHeight/4.2f)), WHITE);
(int)((GetScreenWidth()/2.0f) + cos((frameCounter + i*8)/51.45f)*(GetScreenWidth()/2.2f) - 32),
(int)((GetScreenHeight()/2.0f) + sin((frameCounter + i*8)/17.87f)*(GetScreenHeight()/4.2f)), WHITE);
}
// Draw spot lights
@ -204,14 +207,14 @@ int main(void)
// a render texture of the full screen used to do screen
// scaling (slight adjustment to shader would be required
// to actually pay attention to the colour!)
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), WHITE);
EndShaderMode();
DrawFPS(10, 10);
DrawText("Move the mouse!", 10, 30, 20, GREEN);
DrawText("Pitch Black", (int)(screenWidth*0.2f), screenHeight/2, 20, GREEN);
DrawText("Dark", (int)(screenWidth*.66f), screenHeight/2, 20, GREEN);
DrawText("Pitch Black", (int)(GetScreenWidth()*0.2f), GetScreenHeight()/2, 20, GREEN);
DrawText("Dark", (int)(GetScreenWidth()*.66f), GetScreenHeight()/2, 20, GREEN);
EndDrawing();
//----------------------------------------------------------------------------------

View file

@ -75,6 +75,8 @@ int main(void)
// Draw
//----------------------------------------------------------------------------------
UnloadRenderTexture(target);
target = LoadRenderTextureDepthTex(GetScreenWidth(), GetScreenHeight());
// Draw into our custom render texture (framebuffer)
BeginTextureMode(target);
ClearBackground(WHITE);
@ -94,7 +96,7 @@ int main(void)
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)GetScreenWidth(), (float)-GetScreenHeight() }, (Vector2) { 0, 0 }, WHITE);
DrawFPS(10, 10);
EndDrawing();
//----------------------------------------------------------------------------------
@ -166,4 +168,4 @@ void UnloadRenderTextureDepthTex(RenderTexture2D target)
// queried and deleted before deleting framebuffer
rlUnloadFramebuffer(target.id);
}
}
}