use opengl directly to render a triangle

This commit is contained in:
Rokas Puzonas 2024-01-14 18:28:46 +02:00
parent 65d4fdedb7
commit 997d0144f2
5 changed files with 5098 additions and 46 deletions

View File

@ -1,25 +1,12 @@
const std = @import("std");
const raylib = @import("libs/raylib/build.zig");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "opengl-zig",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
@ -27,47 +14,20 @@ pub fn build(b: *std.Build) void {
raylib.addTo(b, exe, target, optimize, .{});
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
exe.addAnonymousModule("opengl", .{
.source_file = .{ .path = "libs/gl3v3.zig" }
});
b.installArtifact(exe);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}

5041
libs/gl3v3.zig Normal file

File diff suppressed because it is too large Load Diff

5
src/fragment.glsl Normal file
View File

@ -0,0 +1,5 @@
#version 330 core
out vec4 fragColor;
void main() {
fragColor = vec4(0.5, 0.0, 0.5, 1.0);
}

View File

@ -1,19 +1,60 @@
const std = @import("std");
const rl = @import("raylib");
const gl = @import("opengl");
const print = std.debug.print;
fn load_opengl_function(opengl_lib: *std.DynLib, func_name: [:0]const u8) ?gl.FunctionPointer {
return opengl_lib.lookup(gl.FunctionPointer, func_name);
}
pub fn main() anyerror!void {
var opengl_lib = try std.DynLib.open("libGL.so");
defer opengl_lib.close();
try gl.load(&opengl_lib, load_opengl_function);
rl.InitWindow(1024, 720, "OpenGL zig");
defer rl.CloseWindow();
print("Renderer: {?s}\n", .{gl.getString(gl.RENDERER)});
print("OpenGL version: {?s}\n", .{gl.getString(gl.VERSION)});
rl.InitAudioDevice();
defer rl.CloseAudioDevice();
rl.SetTargetFPS(60);
const points = [_]f32{
0.0, 0.5, 0.0,
-0.5, -0.5, 0.0,
0.5, -0.5, 0.0
};
var vbo: gl.GLuint = 0;
gl.genBuffers(1, &vbo);
defer gl.deleteBuffers(1, &vbo);
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER, 9 * @sizeOf(f32), &points, gl.STATIC_DRAW);
var vao: gl.GLuint = 0;
gl.genVertexArrays(1, &vao);
defer gl.deleteVertexArrays(1, &vao);
gl.bindVertexArray(vao);
gl.enableVertexAttribArray(0);
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * @sizeOf(f32), null);
const shader = rl.LoadShaderFromMemory(@embedFile("vertex.glsl"), @embedFile("fragment.glsl"));
while (!rl.WindowShouldClose()) {
rl.BeginDrawing();
rl.ClearBackground(rl.RAYWHITE);
rl.DrawText("Hello, World", 10, 10, 12, rl.BLACK);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LESS);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
gl.useProgram(shader.id);
gl.bindVertexArray(vao);
gl.drawArrays(gl.TRIANGLES, 0, 3);
rl.EndDrawing();
}
}

5
src/vertex.glsl Normal file
View File

@ -0,0 +1,5 @@
#version 330 core
in vec3 vp;
void main() {
gl_Position = vec4(vp, 1.0);
}