opengl/learn_opengl/1_getting_started/5_transformations

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 ¶
159 linesSource

1package main
2
3import "core:math/linalg/glsl"
4import gl "vendor:OpenGL"
5import "vendor:glfw"
6import "core:fmt"
7import "core:os"
8import stbi "vendor:stb/image"
9
10framebuffer_size_callback :: proc "c" (window: glfw.WindowHandle, width: i32, height: i32) {
11	gl.Viewport(0, 0, width, height)
12}
13
14processInput :: proc "c" (window: glfw.WindowHandle) {
15	if glfw.GetKey(window, glfw.KEY_ESCAPE) == glfw.PRESS {
16		glfw.SetWindowShouldClose(window, true)
17	}
18}
19
20SCR_WIDTH :: 800
21SCR_HEIGHT :: 600
22
23main :: proc() {
24	glfw.Init()
25	glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
26	glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3)
27	glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
28
29	window := glfw.CreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nil, nil)
30	if window == nil {
31		fmt.println("Failed to create GLFW window")
32		glfw.Terminate()
33		os.exit(-1)
34	}
35	glfw.MakeContextCurrent(window)
36
37	gl.load_up_to(3, 3, glfw.gl_set_proc_address)
38
39	gl.Viewport(0, 0, SCR_WIDTH, SCR_HEIGHT)
40
41	glfw.SetFramebufferSizeCallback(window, framebuffer_size_callback)
42
43	vertices := [?]f32 {
44         0.5,  0.5, 0.0,  1.0, 1.0,
45         0.5, -0.5, 0.0,  1.0, 0.0, 
46        -0.5, -0.5, 0.0,  0.0, 0.0, 
47        -0.5,  0.5, 0.0,  0.0, 1.0, 
48	}
49
50	indices := [?]u32 {
51		0, 1, 3,
52		1, 2, 3,
53	}
54
55	// The shader loading they create can be replaced with just this
56	shaderProgram, loaded_ok := gl.load_shaders_file("./res/vertex.vs", "./res/fragment.fs")
57	if !loaded_ok {
58		os.exit(-1)
59	}
60
61	VBO, VAO, EBO: u32
62	gl.GenVertexArrays(1, &VAO)
63	gl.GenBuffers(1, &VBO)
64	gl.GenBuffers(1, &EBO)
65
66	gl.BindVertexArray(VAO)
67
68	gl.BindBuffer(gl.ARRAY_BUFFER, VBO)
69	gl.BufferData(gl.ARRAY_BUFFER, size_of(vertices), raw_data(&vertices), gl.STATIC_DRAW)
70
71	gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, EBO)
72	gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size_of(indices), raw_data(&indices), gl.STATIC_DRAW)
73
74	gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 5 * size_of(f32), 0)
75	gl.EnableVertexAttribArray(0)
76
77	gl.VertexAttribPointer(1, 2, gl.FLOAT, gl.FALSE, 5 * size_of(f32), 3 * size_of(f32))
78	gl.EnableVertexAttribArray(1)
79
80	texture1, texture2: u32
81	gl.GenTextures(1, &texture1)
82	gl.BindTexture(gl.TEXTURE_2D, texture1)
83
84	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
85	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
86
87	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
88	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
89
90	width, height, nrChannels: i32
91	stbi.set_flip_vertically_on_load(1)
92
93	data := stbi.load("../res/container.jpg", &width, &height, &nrChannels, 0)
94	if data == nil {
95		fmt.println("Failed to load texture")
96		os.exit(-1)
97	}
98
99	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGB, width, height, 0, gl.RGB, gl.UNSIGNED_BYTE, data)
100	gl.GenerateMipmap(gl.TEXTURE_2D)
101
102	stbi.image_free(data)
103
104	gl.GenTextures(1, &texture2)
105	gl.BindTexture(gl.TEXTURE_2D, texture2)
106
107	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT)
108	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT)
109
110	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
111	gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
112
113	data = stbi.load("../res/awesomeface.png", &width, &height, &nrChannels, 0)
114	if data == nil {
115		fmt.println("Failed to load texture")
116		os.exit(-1)
117	}
118
119	gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, width, height, 0, gl.RGBA, gl.UNSIGNED_BYTE, data)
120	gl.GenerateMipmap(gl.TEXTURE_2D)
121
122	stbi.image_free(data)
123
124	gl.UseProgram(shaderProgram)
125	shader_set_int(shaderProgram, "texture1", 0)
126	shader_set_int(shaderProgram, "texture2", 1)
127
128	for !glfw.WindowShouldClose(window) {
129		processInput(window)
130
131		gl.ClearColor(0.2, 0.3, 0.3, 1.0)
132		gl.Clear(gl.COLOR_BUFFER_BIT)
133
134		gl.ActiveTexture(gl.TEXTURE0)
135		gl.BindTexture(gl.TEXTURE_2D, texture1)
136		gl.ActiveTexture(gl.TEXTURE1)
137		gl.BindTexture(gl.TEXTURE_2D, texture2)
138
139		transform: glsl.mat4 = 1
140		transform *= glsl.mat4Translate({0.5, -0.5, 0.0})
141		transform *= glsl.mat4Rotate({0, 0, 1}, f32(glfw.GetTime()))
142
143		gl.UseProgram(shaderProgram)
144		shader_set_mat4(shaderProgram, "transform", transform)
145
146		gl.BindVertexArray(VAO)
147		gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil)
148
149		glfw.SwapBuffers(window)
150		glfw.PollEvents()
151	}
152
153	gl.DeleteVertexArrays(1, &VAO)
154	gl.DeleteBuffers(1, &VBO)
155	gl.DeleteBuffers(1, &EBO)
156	gl.DeleteProgram(shaderProgram)
157
158	glfw.Terminate()
159}

shader.odin ¶
73 linesSource

1package main
2
3import gl "vendor:OpenGL"
4import "core:math/linalg/glsl"
5
6shader_set_bool :: proc(id: u32, name: cstring, value: bool) {
7	gl.Uniform1i(gl.GetUniformLocation(id, name), i32(value))
8}
9
10shader_set_int :: proc(id: u32, name: cstring, value: i32) {
11	gl.Uniform1i(gl.GetUniformLocation(id, name), value)
12}
13
14shader_set_float :: proc(id: u32, name: cstring, value: f32) {
15	gl.Uniform1f(gl.GetUniformLocation(id, name), value)
16}
17
18shader_set_vec2_v :: proc(id: u32, name: cstring, value: glsl.vec2) {
19	lc := value
20	gl.Uniform2fv(gl.GetUniformLocation(id, name), 1, raw_data(&lc))
21}
22
23shader_set_vec2_f :: proc(id: u32, name: cstring, x, y: f32) {
24	gl.Uniform2f(gl.GetUniformLocation(id, name), x, y)
25}
26
27shader_set_vec2 :: proc {
28	shader_set_vec2_v,
29	shader_set_vec2_f,
30}
31
32shader_set_vec3_v :: proc(id: u32, name: cstring, value: glsl.vec3) {
33	lc := value
34	gl.Uniform3fv(gl.GetUniformLocation(id, name), 1, raw_data(&lc))
35}
36
37shader_set_vec3_f :: proc(id: u32, name: cstring, x, y, z: f32) {
38	gl.Uniform3f(gl.GetUniformLocation(id, name), x, y, z)
39}
40
41shader_set_vec3 :: proc {
42	shader_set_vec3_v,
43	shader_set_vec3_f,
44}
45
46shader_set_vec4_v :: proc(id: u32, name: cstring, value: glsl.vec4) {
47	lc := value
48	gl.Uniform4fv(gl.GetUniformLocation(id, name), 1, raw_data(&lc))
49}
50
51shader_set_vec4_f :: proc(id: u32, name: cstring, x, y, z, w: f32) {
52	gl.Uniform4f(gl.GetUniformLocation(id, name), x, y, z, w)
53}
54
55shader_set_vec4 :: proc {
56	shader_set_vec4_v,
57	shader_set_vec4_f,
58}
59
60shader_set_mat2 :: proc(id: u32, name: cstring, mat: glsl.mat2) {
61	lc := mat
62	gl.UniformMatrix2fv(gl.GetUniformLocation(id, name), 1, gl.FALSE, raw_data(&lc))
63}
64
65shader_set_mat3 :: proc(id: u32, name: cstring, mat: glsl.mat3) {
66	lc := mat
67	gl.UniformMatrix3fv(gl.GetUniformLocation(id, name), 1, gl.FALSE, raw_data(&lc))
68}
69
70shader_set_mat4 :: proc(id: u32, name: cstring, mat: glsl.mat4) {
71	lc := mat
72	gl.UniformMatrix4fv(gl.GetUniformLocation(id, name), 1, gl.FALSE, raw_data(&lc))
73}

Shaders

res/fragment.fs ¶
14 linesSource

#version 330 core
out vec4 FragColor;

in vec2 TexCoord;

// texture samplers
uniform sampler2D texture1;
uniform sampler2D texture2;

void main()
{
	// linearly interpolate between both textures (80% container, 20% awesomeface)
	FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), 0.2);
}

res/vertex.vs ¶
13 linesSource

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;

out vec2 TexCoord;

uniform mat4 transform;

void main()
{
	gl_Position = transform * vec4(aPos, 1.0);
	TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}

Declarations Used 88