-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.zig
195 lines (156 loc) · 6.44 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const std = @import("std");
const glfw = @import("glfw");
const gl = @import("gl");
const vertexShaderSource =
\\ #version 410 core
\\ layout (location = 0) in vec3 aPos;
\\ void main()
\\ {
\\ gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
\\ }
;
const fragmentShaderSource =
\\ #version 410 core
\\ out vec4 FragColor;
\\ void main() {
\\ FragColor = vec4(1.0, 0.5, 0.2, 1.0);
\\ }
;
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);
// Create vertex shader
var vertexShader: c_uint = undefined;
vertexShader = gl.createShader(gl.VERTEX_SHADER);
defer gl.deleteShader(vertexShader);
// Attach the shader source to the vertex shader object and compile it
gl.shaderSource(vertexShader, 1, @ptrCast([*c]const [*c]const u8, &vertexShaderSource), 0);
gl.compileShader(vertexShader);
// Check if vertex shader was compiled successfully
var success: c_int = undefined;
var infoLog: [512]u8 = [_]u8{0} ** 512;
gl.getShaderiv(vertexShader, gl.COMPILE_STATUS, &success);
if (success == 0) {
gl.getShaderInfoLog(vertexShader, 512, 0, &infoLog);
std.log.err("{s}", .{infoLog});
}
// Fragment shader
var fragmentShader: c_uint = undefined;
fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
defer gl.deleteShader(fragmentShader);
gl.shaderSource(fragmentShader, 1, @ptrCast([*c]const [*c]const u8, &fragmentShaderSource), 0);
gl.compileShader(fragmentShader);
gl.getShaderiv(fragmentShader, gl.COMPILE_STATUS, &success);
if (success == 0) {
gl.getShaderInfoLog(fragmentShader, 512, 0, &infoLog);
std.log.err("{s}", .{infoLog});
}
// create a program object
var shaderProgram: c_uint = undefined;
shaderProgram = gl.createProgram();
defer gl.deleteProgram(shaderProgram);
// attach compiled shader objects to the program object and link
gl.attachShader(shaderProgram, vertexShader);
gl.attachShader(shaderProgram, fragmentShader);
gl.linkProgram(shaderProgram);
// check if shader linking was successfull
gl.getProgramiv(shaderProgram, gl.LINK_STATUS, &success);
if (success == 0) {
gl.getProgramInfoLog(shaderProgram, 512, 0, &infoLog);
std.log.err("{s}", .{infoLog});
}
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
// const vertices = [9]f32{ -0.5, -0.5, 0.0, 0.5, -0.5, 0.0, 0.0, 0.5, 0.0 };
const vertices = [12]f32{
0.5, 0.5, 0.0, // top right
0.5, -0.5, 0.0, // bottom right
-0.5, -0.5, 0.0, // bottom left
-0.5, 0.5, 0.0, // top left
};
const indices = [6]c_uint{
// note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3, // second triangle
};
var VBO: c_uint = undefined;
var VAO: c_uint = undefined;
var EBO: c_uint = undefined;
gl.genVertexArrays(1, &VAO);
defer gl.deleteVertexArrays(1, &VAO);
gl.genBuffers(1, &VBO);
defer gl.deleteBuffers(1, &VBO);
gl.genBuffers(1, &EBO);
defer gl.deleteBuffers(1, &EBO);
// 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);
// copy our index array in an element buffer for OpenGL to use
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, EBO);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, 6 * @sizeOf(c_uint), &indices, gl.STATIC_DRAW);
// Specify and link our vertext attribute description
gl.vertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * @sizeOf(f32), null);
gl.enableVertexAttribArray(0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
gl.bindBuffer(gl.ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
gl.bindVertexArray(0);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, 0);
// Wireframe mode
gl.polygonMode(gl.FRONT_AND_BACK, gl.LINE);
while (!window.shouldClose()) {
processInput(window);
gl.clearColor(0.2, 0.3, 0.3, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
// Activate shaderProgram
gl.useProgram(shaderProgram);
gl.bindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, EBO);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, null);
gl.bindVertexArray(0);
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);
}
}