Skip to content

Instantly share code, notes, and snippets.

@Lightnet
Created June 23, 2026 00:31
Show Gist options
  • Select an option

  • Save Lightnet/91119ca0fd8af01fc6f8a9439bbceac9 to your computer and use it in GitHub Desktop.

Select an option

Save Lightnet/91119ca0fd8af01fc6f8a9439bbceac9 to your computer and use it in GitHub Desktop.
Text 3d render using rlgl raylib. zig.
// working
const rl = @import("raylib");
// https://github.com/raylib-zig/raylib-zig
const rlgl = rl.gl; // raylib rlgl
pub fn main() anyerror!void {
// Initialization
const screen_width = 800;
const screen_height = 450;
rl.initWindow(screen_width, screen_height, "raylib-zig - 2D Text on 3D Plane");
defer rl.closeWindow();
rl.disableCursor(); // Keeps the mouse locked to the window center
// 1. Create a Render Texture to hold our text "canvas"
const tex_width = 256;
const tex_height = 64;
const target = try rl.loadRenderTexture(tex_width, tex_height);
defer rl.unloadRenderTexture(target);
// 2. Set up a basic 3D Camera
var camera = rl.Camera3D{
.position = rl.Vector3{ .x = 0.0, .y = 2.0, .z = 5.0 },
.target = rl.Vector3{ .x = 0.0, .y = 0.0, .z = 0.0 },
.up = rl.Vector3{ .x = 0.0, .y = 1.0, .z = 0.0 },
.fovy = 45.0,
.projection = .perspective,
};
rl.setTargetFPS(60);
// Main game loop
while (!rl.windowShouldClose()) {
// Update Camera (allows you to orbit around to see the 3D effect)
// rl.updateCamera(&camera, .orbital);
rl.updateCamera(&camera, .first_person);
// 3. Render the 2D text onto our Render Texture
rl.beginTextureMode(target);
rl.clearBackground(.blank); // Transparent background
rl.drawText("Hello 3D Plane!", 10, 20, 24, .blue);
rl.endTextureMode();
// 4. Draw the 3D Scene
rl.beginDrawing();
rl.clearBackground(.ray_white);
rl.beginMode3D(camera);
// Draw a grid for spatial reference
rl.drawGrid(10, 1.0);
// --- Draw Flat Static 3D Text Quad ---
// 1. Bind our text texture ID directly to the GPU shader context
rlgl.rlSetTexture(target.texture.id);
// 1. Save current matrix state
rlgl.rlPushMatrix();
// 2. Position the text plane in your 3D world (X, Y, Z)
rlgl.rlTranslatef(0.0, 1.0, 0.0);
// 3. Rotate 90 degrees around the X-axis to stand the text upright
rlgl.rlRotatef(90.0, 1.0, 0.0, 0.0);
// 2. Start drawing a Quad. Zig enums map this to .quads
// rlgl.rlBegin(rlgl.quads);
rlgl.rlBegin(rlgl.rl_quads);
rlgl.rlColor4ub(255, 255, 255, 255); // Keep original texture colors
// Corner 1: Top-Left (Texture mappings use f32 ranges from 0.0 to 1.0)
rlgl.rlTexCoord2f(0.0, 1.0);
rlgl.rlVertex3f(-1.5, 0.01, -0.5);
// Corner 2: Bottom-Left
rlgl.rlTexCoord2f(0.0, 0.0);
rlgl.rlVertex3f(-1.5, 0.01, 0.5);
// Corner 3: Bottom-Right
rlgl.rlTexCoord2f(1.0, 0.0);
rlgl.rlVertex3f(1.5, 0.01, 0.5);
// Corner 4: Top-Right
rlgl.rlTexCoord2f(1.0, 1.0);
rlgl.rlVertex3f(1.5, 0.01, -0.5);
// 3. Finalize drawing geometry
rlgl.rlEnd();
// 4. Reset texture state back to 0 so other 3D items draw correctly
rlgl.rlSetTexture(0);
rlgl.rlPopMatrix();
// -------------------------------------
rl.endMode3D();
// Optional: Draw instructions on the 2D UI screen layer
rl.drawText("Hold right-click & drag mouse to rotate camera", 10, 10, 10, .dark_gray);
rl.endDrawing();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment