commit 00a88f0dca3ba2986a4d6ea1d62b7a2395222a61 Author: Rokas Puzonas Date: Sun Jan 14 12:11:49 2024 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c82b07 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-cache +zig-out diff --git a/README.md b/README.md new file mode 100644 index 0000000..5e4ba63 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# OpenGL "Hello, Triangle" example + +To run this example you need to have glfw, opengl and glew installed on your system. +``` +zig build run +``` + +Followed this tutorial: https://antongerdelan.net/opengl/hellotriangle.html diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..fffd70b --- /dev/null +++ b/build.zig @@ -0,0 +1,23 @@ +const std = @import("std"); + +pub fn build(b: *std.build.Builder) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "opengl-example", + .root_source_file = .{ .path = "main.c" }, + .optimize = optimize, + .target = target + }); + exe.linkLibC(); + exe.linkSystemLibrary("GL"); + exe.linkSystemLibrary("glfw"); + exe.linkSystemLibrary("GLEW"); + + const run_cmd = b.addRunArtifact(exe); + const run_step = b.step("run", "Run simple program"); + run_step.dependOn(&run_cmd.step); + + b.installArtifact(exe); +} diff --git a/main.c b/main.c new file mode 100644 index 0000000..58b2bdc --- /dev/null +++ b/main.c @@ -0,0 +1,93 @@ +#include +#include +#define GLFW_DLL +#include + +#define eprint(fmt, ...) fprintf(stderr, "ERROR: " fmt "\n" #__VA_ARGS__) + +const char *vertex_shader = + "#version 330 core\n" + "in vec3 vp;\n" + "void main() {\n" + " gl_Position = vec4(vp, 1.0);\n" + "}\n"; + +const char *fragment_shader = + "#version 330 core\n" + "out vec4 fragColor;\n" + "void main() {\n" + " fragColor = vec4(0.5, 0.0, 0.5, 1.0);\n" + "}\n"; + +int main() { + if (!glfwInit()) { + eprint("Could not start glfw"); + return -1; + } + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + GLFWwindow *window = glfwCreateWindow(640, 480, "Hello triangle", NULL, NULL); + if (!window) { + eprint("Could not open window with glfw"); + glfwTerminate(); + return -1; + } + glfwMakeContextCurrent(window); + + glewExperimental = GL_TRUE; + glewInit(); + + printf("Renderer: %s\n", glGetString(GL_RENDERER)); + printf("OpenGL version supported: %s\n", glGetString(GL_VERSION)); + + glEnable(GL_DEPTH_TEST); + glDepthFunc(GL_LESS); + + float points[] = { + 0.0f, 0.5f, 0.0f, + 0.5f, -0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + }; + + GLuint vbo = 0; + glGenBuffers(1, &vbo); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW); + + GLuint vao = 0; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + glEnableVertexAttribArray(0); + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); + + GLuint vs = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vs, 1, &vertex_shader, NULL); + glCompileShader(vs); + + GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fs, 1, &fragment_shader, NULL); + glCompileShader(fs); + + GLuint shader_program = glCreateProgram(); + glAttachShader(shader_program, fs); + glAttachShader(shader_program, vs); + glLinkProgram(shader_program); + + while (!glfwWindowShouldClose(window)) { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glUseProgram(shader_program); + glBindVertexArray(vao); + glDrawArrays(GL_TRIANGLES, 0, 3); + glfwPollEvents(); + glfwSwapBuffers(window); + } + + + glfwTerminate(); + return 0; +}