initial commit
This commit is contained in:
commit
00a88f0dca
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
zig-cache
|
||||||
|
zig-out
|
8
README.md
Normal file
8
README.md
Normal file
@ -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
|
23
build.zig
Normal file
23
build.zig
Normal file
@ -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);
|
||||||
|
}
|
93
main.c
Normal file
93
main.c
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#define GLFW_DLL
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user