-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.zig
116 lines (92 loc) · 3.72 KB
/
main.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const std = @import("std");
const glfw = @import("glfw");
const gl = @import("gl");
const Shader = @import("Shader");
const WindowSize = struct {
pub const width: u32 = 800;
pub const height: u32 = 600;
};
pub fn main() !void {
// glfw: initialize and configure
// ------------------------------
if (!glfw.init(.{})) {
std.log.err("GLFW initialization failed", .{});
return;
}
defer glfw.terminate();
// glfw window creation
// --------------------
const window = glfw.Window.create(WindowSize.width, WindowSize.height, "mach-glfw + zig-opengl", null, null, .{
.opengl_profile = .opengl_core_profile,
.context_version_major = 4,
.context_version_minor = 1,
}) orelse {
std.log.err("GLFW Window creation failed", .{});
return;
};
defer window.destroy();
glfw.makeContextCurrent(window);
glfw.Window.setFramebufferSizeCallback(window, framebuffer_size_callback);
// Load all OpenGL function pointers
// ---------------------------------------
const proc: glfw.GLProc = undefined;
try gl.load(proc, glGetProcAddress);
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
var allocator = gpa.allocator();
var arena_allocator_state = std.heap.ArenaAllocator.init(allocator);
defer arena_allocator_state.deinit();
const arena_allocator = arena_allocator_state.allocator();
// create shader program
var shader_program: Shader = Shader.create(arena_allocator, "shaders/shader_ex3.vs", "shaders/shader_ex3.fs");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
const vertices = [_]f32{
-0.5, -0.5, 0.0, 1.0, 0.0, 0.0,
0.5, -0.5, 0.0, 0.0, 1.0, 0.0,
0.0, 0.5, 0.0, 0.0, 0.0, 1.0,
};
var VBO: c_uint = undefined;
var VAO: c_uint = undefined;
gl.genVertexArrays(1, &VAO);
defer gl.deleteVertexArrays(1, &VAO);
gl.genBuffers(1, &VBO);
defer gl.deleteBuffers(1, &VBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
gl.bindVertexArray(VAO);
gl.bindBuffer(gl.ARRAY_BUFFER, VBO);
// Fill our buffer with the vertex data
gl.bufferData(gl.ARRAY_BUFFER, @sizeOf(f32) * vertices.len, &vertices, gl.STATIC_DRAW);
// Specify and link our vertext attribute description
gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 6 * @sizeOf(f32), null);
gl.enableVertexAttribArray(0);
const offset: [*c]c_uint = (3 * @sizeOf(f32));
gl.vertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 6 * @sizeOf(f32), offset);
gl.enableVertexAttribArray(1);
while (!window.shouldClose()) {
processInput(window);
gl.clearColor(0.0, 0.0, 0.0, 0.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// update the uniform color
const timeValue = glfw.getTime();
const offsetValue = @floatCast(f32, @sin(timeValue * 2) / 2.0 + 0.5);
shader_program.setVec3f("offset", [3]f32{ offsetValue, offsetValue, offsetValue });
shader_program.use();
gl.bindVertexArray(VAO);
gl.drawArrays(gl.TRIANGLES, 0, 3);
window.swapBuffers();
glfw.pollEvents();
}
}
fn glGetProcAddress(p: glfw.GLProc, proc: [:0]const u8) ?gl.FunctionPointer {
_ = p;
return glfw.getProcAddress(proc);
}
fn framebuffer_size_callback(window: glfw.Window, width: u32, height: u32) void {
_ = window;
gl.viewport(0, 0, @intCast(c_int, width), @intCast(c_int, height));
}
fn processInput(window: glfw.Window) void {
if (glfw.Window.getKey(window, glfw.Key.escape) == glfw.Action.press) {
_ = glfw.Window.setShouldClose(window, true);
}
}