opengl/learn_opengl/1_getting_started/3_shaders

Licence This example has its own licence, rather than Odin's: opengl/learn_opengl/LICENSE
LearnOpenGL.com © 2025 by Joey de Vries is licensed under CC BY-NC 4.0. To view a copy of this license, visit https://creativecommons.org/licenses/by-nc/4.0/ https://learnopengl.com https://x.com/JoeyDeVriez

Code

main.odin ¶
99 linesSource

1package main
2
3import gl "vendor:OpenGL"
4import "vendor:glfw"
5import "core:fmt"
6import "core:os"
7
8framebuffer_size_callback :: proc "c" (window: glfw.WindowHandle, width: i32, height: i32) {
9	gl.Viewport(0, 0, width, height)
10}
11
12processInput :: proc "c" (window: glfw.WindowHandle) {
13	if glfw.GetKey(window, glfw.KEY_ESCAPE) == glfw.PRESS {
14		glfw.SetWindowShouldClose(window, true)
15	}
16}
17
18shader_set_bool :: proc(id: u32, name: cstring, value: bool) {
19	gl.Uniform1i(gl.GetUniformLocation(id, name), i32(value))
20}
21
22shader_set_int :: proc(id: u32, name: cstring, value: i32) {
23	gl.Uniform1i(gl.GetUniformLocation(id, name), value)
24}
25
26shader_set_float :: proc(id: u32, name: cstring, value: f32) {
27	gl.Uniform1f(gl.GetUniformLocation(id, name), value)
28}
29
30SCR_WIDTH :: 800
31SCR_HEIGHT :: 600
32
33main :: proc() {
34	glfw.Init()
35	glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
36	glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3)
37	glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
38
39	window := glfw.CreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nil, nil)
40	if window == nil {
41		fmt.println("Failed to create GLFW window")
42		glfw.Terminate()
43		os.exit(-1)
44	}
45	glfw.MakeContextCurrent(window)
46
47	gl.load_up_to(3, 3, glfw.gl_set_proc_address)
48
49	gl.Viewport(0, 0, SCR_WIDTH, SCR_HEIGHT)
50
51	glfw.SetFramebufferSizeCallback(window, framebuffer_size_callback)
52
53	vertices := [?]f32 {
54         0.5, -0.5, 0.0,  1.0, 0.0, 0.0,
55        -0.5, -0.5, 0.0,  0.0, 1.0, 0.0,
56         0.0,  0.5, 0.0,  0.0, 0.0, 1.0,
57	}
58
59	// The shader loading they create can be replaced with just this
60	shaderProgram, loaded_ok := gl.load_shaders_file("./res/vertex.vs", "./res/fragment.fs")
61	if !loaded_ok {
62		os.exit(-1)
63	}
64
65	VBO, VAO: u32
66	gl.GenVertexArrays(1, &VAO)
67	gl.GenBuffers(1, &VBO)
68
69	gl.BindVertexArray(VAO)
70
71	gl.BindBuffer(gl.ARRAY_BUFFER, VBO)
72	gl.BufferData(gl.ARRAY_BUFFER, size_of(vertices), raw_data(&vertices), gl.STATIC_DRAW)
73
74	gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 0)
75	gl.EnableVertexAttribArray(0)
76
77	gl.VertexAttribPointer(1, 3, gl.FLOAT, gl.FALSE, 6 * size_of(f32), 3 * size_of(f32))
78	gl.EnableVertexAttribArray(1)
79
80	for !glfw.WindowShouldClose(window) {
81		processInput(window)
82
83		gl.ClearColor(0.2, 0.3, 0.3, 1.0)
84		gl.Clear(gl.COLOR_BUFFER_BIT)
85
86		gl.UseProgram(shaderProgram)
87		gl.BindVertexArray(VAO)
88		gl.DrawArrays(gl.TRIANGLES, 0, 3)
89
90		glfw.SwapBuffers(window)
91		glfw.PollEvents()
92	}
93
94	gl.DeleteVertexArrays(1, &VAO)
95	gl.DeleteBuffers(1, &VBO)
96	gl.DeleteProgram(shaderProgram)
97
98	glfw.Terminate()
99}

Shaders

res/fragment.fs ¶
8 linesSource

#version 330 core
out vec4 FragColor;  
in vec3 ourColor;
  
void main()
{
    FragColor = vec4(ourColor, 1.0);
}

res/vertex.vs ¶
11 linesSource

#version 330 core
layout (location = 0) in vec3 aPos;   // the position variable has attribute position 0
layout (location = 1) in vec3 aColor; // the color variable has attribute position 1
  
out vec3 ourColor; // output a color to the fragment shader

void main()
{
    gl_Position = vec4(aPos, 1.0);
    ourColor = aColor; // set ourColor to the input color we got from the vertex data
} 

Declarations Used 47