Last active
October 10, 2024 15:07
-
-
Save skorianez/f8903d61f7963cf3e02dd2692e182fac to your computer and use it in GitHub Desktop.
OpenGL in Odin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package new | |
import gl "vendor:OpenGL" | |
import "vendor:glfw" | |
main :: proc() { | |
// glfw: initialize and configure | |
glfw.Init() | |
glfw.WindowHint(glfw.CONTEXT_VERSION_MAJOR, 3) | |
glfw.WindowHint(glfw.CONTEXT_VERSION_MINOR, 3) | |
glfw.WindowHint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) | |
// glfw window creation | |
window := glfw.CreateWindow(800,600,"OpenGL", nil, nil) | |
assert(window != nil, "Failed to create GLFW window") | |
glfw.MakeContextCurrent(window) | |
// gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) | |
gl.load_up_to(3, 3, glfw.gl_set_proc_address) | |
gl.Viewport(0, 0, 800,600) | |
// build and compile our shader program | |
vertexShader := gl.CreateShader(gl.VERTEX_SHADER) | |
gl.ShaderSource(vertexShader, 1, &vertexShaderSource, nil) | |
fragmentShader := gl.CreateShader(gl.FRAGMENT_SHADER) | |
gl.ShaderSource(fragmentShader, 1, &fragmentShaderSource, nil) | |
// link shaders | |
shaderProgram := gl.CreateProgram() | |
gl.AttachShader(shaderProgram, vertexShader) | |
gl.AttachShader(shaderProgram, fragmentShader) | |
gl.LinkProgram(shaderProgram) | |
gl.DeleteShader(vertexShader) | |
gl.DeleteShader(fragmentShader) | |
// Set up vertex data | |
vertices := []f32{ | |
0.5, 0.5, 0.0, // top right | |
0.5, -0.5, 0.0, // bottom right | |
-0.5, -0.5, 0.0, // bottom left | |
-0.5, 0.5, 0.0, // top left | |
} | |
indicies := []i32 { | |
0, 1, 3, | |
1, 2, 3, | |
} | |
VBO, VAO, EBO : u32 | |
gl.GenVertexArrays(1, &VAO) | |
gl.GenBuffers(1, &VBO) | |
gl.GenBuffers(1, &EBO) | |
gl.BindVertexArray(VAO) | |
gl.BindBuffer(gl.ARRAY_BUFFER, VBO) | |
gl.BufferData(gl.ARRAY_BUFFER, 12 * size_of(f32), raw_data(vertices), gl.STATIC_DRAW) | |
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, EBO) | |
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, 6 * size_of(i32, raw_data(indicies), gl.STATIC_DRAW) | |
gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 3 * size_of(f32), 0) | |
gl.EnableVertexAttribArray(0) | |
gl.BindVertexArray(0) | |
// render loop | |
for ! glfw.WindowShouldClose(window) { | |
processInput(&window) | |
gl.ClearColor(0.2, 0.3, 0.3, 1) | |
gl.Clear(gl.COLOR_BUFFER_BIT) | |
gl.UseProgram(shaderProgram) | |
gl.BindVertexArray(VAO) | |
gl.DrawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, nil) | |
glfw.SwapBuffers(window) | |
glfw.PollEvents() | |
} | |
glfw.Terminate() | |
} | |
processInput :: proc(window : ^glfw.WindowHandle) { | |
if glfw.GetKey(window^, glfw.KEY_ESCAPE) == glfw.PRESS { | |
glfw.SetWindowShouldClose(window^, true) | |
} | |
} | |
vertexShaderSource : cstring = `#version 330 core | |
layout (location = 0) in vec3 aPos; | |
void main() | |
{ | |
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); | |
} | |
` | |
fragmentShaderSource: cstring = `#version 330 core | |
out vec4 FragColor; | |
void main() | |
{ | |
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); | |
} | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment