texture rendering
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [shaders] example - texture rendering
//!
//! Zig-API translation of examples/shaders/shaders_texture_rendering.c (raylib 6.2).
//! Example contributed by Michał Ciesielski (@ciessielski) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2019-2025 Michał Ciesielski (@ciessielski) and Ramon Santamaria (@raysan5)
const std = @import("std");
const rl = @import("raylib");
const glsl_version = 330;
pub fn main() !void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib [shaders] example - texture rendering");
defer rl.closeWindow(); // Close window and OpenGL context
const im_blank = rl.genImageColor(1024, 1024, .blank);
const texture = try rl.loadTextureFromImage(im_blank); // Load blank texture to fill on shader
defer rl.unloadTexture(texture);
rl.unloadImage(im_blank);
// NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
const shader = try rl.loadShader(null, std.fmt.comptimePrint("resources/shaders/glsl{d}/cubes_panning.fs", .{glsl_version}));
defer rl.unloadShader(shader);
var time: f32 = 0.0;
const time_loc = rl.getShaderLocation(shader, "uTime");
rl.setShaderValue(shader, time_loc, time);
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
time = @floatCast(rl.getTime());
rl.setShaderValue(shader, time_loc, time);
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
{
rl.beginShaderMode(shader); // Enable our custom shader for next shapes/textures drawings
defer rl.endShaderMode(); // Disable our custom shader, return to default shader
rl.drawTexture(texture, 0, 0, .white); // Drawing BLANK texture, all rendering magic happens on shader
}
rl.drawText("BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, .maroon);
}
}/*******************************************************************************************
*
* raylib [shaders] example - texture rendering
*
* Example complexity rating: [★★☆☆] 2/4
*
* Example originally created with raylib 2.0, last time updated with raylib 3.7
*
* Example contributed by Michał Ciesielski (@ciessielski) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2019-2025 Michał Ciesielski (@ciessielski) and Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#endif
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - texture rendering");
Image imBlank = GenImageColor(1024, 1024, BLANK);
Texture2D texture = LoadTextureFromImage(imBlank); // Load blank texture to fill on shader
UnloadImage(imBlank);
// NOTE: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version
Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/cubes_panning.fs", GLSL_VERSION));
float time = 0.0f;
int timeLoc = GetShaderLocation(shader, "uTime");
SetShaderValue(shader, timeLoc, &time, SHADER_UNIFORM_FLOAT);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
// -------------------------------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
time = (float)GetTime();
SetShaderValue(shader, timeLoc, &time, SHADER_UNIFORM_FLOAT);
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginShaderMode(shader); // Enable our custom shader for next shapes/textures drawings
DrawTexture(texture, 0, 0, WHITE); // Drawing BLANK texture, all rendering magic happens on shader
EndShaderMode(); // Disable our custom shader, return to default shader
DrawText("BACKGROUND is PAINTED and ANIMATED on SHADER!", 10, 10, 20, MAROON);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadShader(shader);
UnloadTexture(texture);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}From raylib 6.2. The original on GitHub.