Skip to content

Commit 9c6477c

Browse files
authored
Merge branch 'odin-lang:master' into master
2 parents c038dc4 + 6539959 commit 9c6477c

File tree

25 files changed

+1487
-3
lines changed

25 files changed

+1487
-3
lines changed

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ Checklist before submitting:
44
- [ ] This example has been added to `.github/workflows/check.yml` (for automatic testing)
55
- [ ] This example compiles cleanly with flags `-vet -strict-style -vet-tabs -disallow-do -warnings-as-errors`
66
- [ ] This example the naming and style convention: https://github.com/odin-lang/examples/wiki/Naming-and-style-convention (exceptions can be made for ports of examples that need to match 1:1 to the original source).
7-
- [ ] By submitting, I understand that this example is made available under these licenses: [Public Domain](https://unlicense.org) and [Odin's BSD-3 license](https://github.com/odin-lang/Odin/blob/master/LICENSE). Only for third-party dependencies are other licenses allowed.
7+
- [ ] By submitting, I understand that this example is made available under these licenses: [Public Domain](https://unlicense.org) and [Odin's zlib license](https://github.com/odin-lang/Odin/blob/master/LICENSE). Only for third-party dependencies are other licenses allowed.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ Clone this repository. Most examples can be run by navigating into the folder an
1818
Some examples have a `README` with additional information and instructions.
1919

2020
## License
21-
The contents of this repository is available under two licenses. Choose the one that you prefer:
21+
The contents of this repository are available under two licenses. Choose the one that you prefer:
2222

2323
- [Public Domain](https://unlicense.org)
2424
or
25-
- [Odin's BSD-3 license](https://github.com/odin-lang/Odin/blob/master/LICENSE)
25+
- [Odin's zlib license](https://github.com/odin-lang/Odin/blob/master/LICENSE)
2626

2727
Assets and third-party libraries are provided under their own license. If in doubt, check the `LICENSE*` and `COPYING*` file(s) in a particular directory for clarification.
2828

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import gl "vendor:OpenGL"
4+
import "vendor:glfw"
5+
import "core:fmt"
6+
import "core:os"
7+
8+
framebuffer_size_callback :: proc "c" (window: glfw.WindowHandle, width: i32, height: i32) {
9+
gl.Viewport(0, 0, width, height)
10+
}
11+
12+
processInput :: proc "c" (window: glfw.WindowHandle) {
13+
if glfw.GetKey(window, glfw.KEY_ESCAPE) == glfw.PRESS {
14+
glfw.SetWindowShouldClose(window, true)
15+
}
16+
}
17+
18+
main :: proc() {
19+
glfw.Init()
20+
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
21+
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3)
22+
glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
23+
24+
window := glfw.CreateWindow(800, 600, "LearnOpenGL", nil, nil)
25+
if window == nil {
26+
fmt.println("Failed to create GLFW window")
27+
glfw.Terminate()
28+
os.exit(-1)
29+
}
30+
glfw.MakeContextCurrent(window)
31+
32+
gl.load_up_to(3, 3, glfw.gl_set_proc_address)
33+
34+
gl.Viewport(0, 0, 800, 600)
35+
36+
glfw.SetFramebufferSizeCallback(window, framebuffer_size_callback)
37+
38+
for !glfw.WindowShouldClose(window) {
39+
processInput(window)
40+
41+
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
42+
gl.Clear(gl.COLOR_BUFFER_BIT)
43+
44+
glfw.SwapBuffers(window)
45+
glfw.PollEvents()
46+
}
47+
48+
glfw.Terminate()
49+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package main
2+
3+
import gl "vendor:OpenGL"
4+
import "vendor:glfw"
5+
import "core:fmt"
6+
import "core:os"
7+
import "core:c"
8+
9+
framebuffer_size_callback :: proc "c" (window: glfw.WindowHandle, width: i32, height: i32) {
10+
gl.Viewport(0, 0, width, height)
11+
}
12+
13+
processInput :: proc "c" (window: glfw.WindowHandle) {
14+
if glfw.GetKey(window, glfw.KEY_ESCAPE) == glfw.PRESS {
15+
glfw.SetWindowShouldClose(window, true)
16+
}
17+
}
18+
19+
vertexShaderSource: cstring =
20+
`#version 330 core
21+
layout (location = 0) in vec3 aPos;
22+
void main()
23+
{
24+
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
25+
}`
26+
27+
fragmentShaderSource: cstring =
28+
`#version 330 core
29+
out vec4 FragColor;
30+
31+
void main()
32+
{
33+
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
34+
}`
35+
36+
SCR_WIDTH :: 800
37+
SCR_HEIGHT :: 600
38+
39+
main :: proc() {
40+
glfw.Init()
41+
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3)
42+
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3)
43+
glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
44+
45+
window := glfw.CreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nil, nil)
46+
if window == nil {
47+
fmt.println("Failed to create GLFW window")
48+
glfw.Terminate()
49+
os.exit(-1)
50+
}
51+
glfw.MakeContextCurrent(window)
52+
53+
gl.load_up_to(3, 3, glfw.gl_set_proc_address)
54+
55+
gl.Viewport(0, 0, SCR_WIDTH, SCR_HEIGHT)
56+
57+
glfw.SetFramebufferSizeCallback(window, framebuffer_size_callback)
58+
59+
success: i32
60+
infoLog: [512]c.char
61+
62+
vertexShader := gl.CreateShader(gl.VERTEX_SHADER)
63+
gl.ShaderSource(vertexShader, 1, &vertexShaderSource, nil)
64+
gl.CompileShader(vertexShader)
65+
66+
gl.GetShaderiv(vertexShader, gl.COMPILE_STATUS, &success)
67+
if success != 1 {
68+
gl.GetShaderInfoLog(vertexShader, 512, nil, raw_data(&infoLog))
69+
}
70+
71+
fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER)
72+
gl.ShaderSource(fragmentShader, 1, &fragmentShaderSource, nil)
73+
gl.CompileShader(fragmentShader)
74+
75+
gl.GetShaderiv(fragmentShader, gl.COMPILE_STATUS, &success)
76+
if success != 1 {
77+
gl.GetShaderInfoLog(fragmentShader, 512, nil, raw_data(&infoLog))
78+
os.exit(-1)
79+
}
80+
81+
shaderProgram := gl.CreateProgram()
82+
gl.AttachShader(shaderProgram, vertexShader)
83+
gl.AttachShader(shaderProgram, fragmentShader)
84+
gl.LinkProgram(shaderProgram)
85+
86+
gl.GetProgramiv(fragmentShader, gl.LINK_STATUS, &success)
87+
if success != 1 {
88+
gl.GetProgramInfoLog(fragmentShader, 512, nil, raw_data(&infoLog))
89+
os.exit(-1)
90+
}
91+
92+
gl.DeleteShader(vertexShader)
93+
gl.DeleteShader(fragmentShader)
94+
95+
vertices := [?]f32 {
96+
0.5, 0.5, 0.0,
97+
0.5, -0.5, 0.0,
98+
-0.5, -0.5, 0.0,
99+
-0.5, 0.5, 0.0,
100+
}
101+
102+
indices := [?]u32 {
103+
0, 1, 3,
104+
1, 2, 3,
105+
}
106+
107+
VBO, VAO, EBO: u32
108+
gl.GenVertexArrays(1, &VAO)
109+
gl.GenBuffers(1, &VBO)
110+
gl.GenBuffers(1, &EBO)
111+
112+
gl.BindVertexArray(VAO)
113+
114+
gl.BindBuffer(gl.ARRAY_BUFFER, VBO)
115+
gl.BufferData(gl.ARRAY_BUFFER, size_of(vertices), raw_data(&vertices), gl.STATIC_DRAW)
116+
117+
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, EBO)
118+
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, size_of(indices), raw_data(&indices), gl.STATIC_DRAW)
119+
120+
gl.VertexAttribPointer(0, 3, gl.FLOAT, gl.FALSE, 3 * size_of(f32), 0)
121+
gl.EnableVertexAttribArray(0)
122+
123+
gl.BindBuffer(gl.ARRAY_BUFFER, 0)
124+
gl.BindVertexArray(0)
125+
126+
for !glfw.WindowShouldClose(window) {
127+
processInput(window)
128+
129+
gl.ClearColor(0.2, 0.3, 0.3, 1.0)
130+
gl.Clear(gl.COLOR_BUFFER_BIT)
131+
132+
gl.UseProgram(shaderProgram)
133+
gl.BindVertexArray(VAO)
134+
gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil)
135+
136+
glfw.SwapBuffers(window)
137+
glfw.PollEvents()
138+
}
139+
140+
gl.DeleteVertexArrays(1, &VAO)
141+
gl.DeleteBuffers(1, &VBO)
142+
gl.DeleteBuffers(1, &EBO)
143+
gl.DeleteProgram(shaderProgram)
144+
145+
glfw.Terminate()
146+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import gl "vendor:OpenGL"
4+
import "vendor:glfw"
5+
import "core:fmt"
6+
import "core:os"
7+
8+
framebuffer_size_callback :: proc "c" (window: glfw.WindowHandle, width: i32, height: i32) {
9+
gl.Viewport(0, 0, width, height)
10+
}
11+
12+
processInput :: proc "c" (window: glfw.WindowHandle) {
13+
if glfw.GetKey(window, glfw.KEY_ESCAPE) == glfw.PRESS {
14+
glfw.SetWindowShouldClose(window, true)
15+
}
16+
}
17+
18+
shader_set_bool :: proc(id: u32, name: cstring, value: bool) {
19+
gl.Uniform1i(gl.GetUniformLocation(id, name), i32(value))
20+
}
21+
22+
shader_set_int :: proc(id: u32, name: cstring, value: i32) {
23+
gl.Uniform1i(gl.GetUniformLocation(id, name), value)
24+
}
25+
26+
shader_set_float :: proc(id: u32, name: cstring, value: f32) {
27+
gl.Uniform1f(gl.GetUniformLocation(id, name), value)
28+
}
29+
30+
SCR_WIDTH :: 800
31+
SCR_HEIGHT :: 600
32+
33+
main :: 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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#version 330 core
2+
out vec4 FragColor;
3+
in vec3 ourColor;
4+
5+
void main()
6+
{
7+
FragColor = vec4(ourColor, 1.0);
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#version 330 core
2+
layout (location = 0) in vec3 aPos; // the position variable has attribute position 0
3+
layout (location = 1) in vec3 aColor; // the color variable has attribute position 1
4+
5+
out vec3 ourColor; // output a color to the fragment shader
6+
7+
void main()
8+
{
9+
gl_Position = vec4(aPos, 1.0);
10+
ourColor = aColor; // set ourColor to the input color we got from the vertex data
11+
}

0 commit comments

Comments
 (0)