Skip to content

Instantly share code, notes, and snippets.

@QuantumFractal
Created September 30, 2019 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuantumFractal/a2c4981761130e60e295bdaaba452ec9 to your computer and use it in GitHub Desktop.
Save QuantumFractal/a2c4981761130e60e295bdaaba452ec9 to your computer and use it in GitHub Desktop.
julia opengl
using GLFW
using ModernGL
# NO COPY AND PASTE!!!
# CLEAN CODE IS GOOD CODE
# Create Window with size and title
GLFW.Init()
window = GLFW.CreateWindow(400, 300, "Title")
hints = Dict(GL_SAMPLES => 4,
GL_MAJOR_VERSION => 3,
GL_MINOR_VERSION => 3)
GLFW.MakeContextCurrent(window)
# Register Callbacks
# TODO: Make this a bit nicer :)
GLFW.SetKeyCallback(window, (window, key, scancode, action, mods) -> begin
println("$window, $key, $scancode, $action, $mods")
if key != GLFW.KEY_UNKNOWN
if key == GLFW.KEY_ESCAPE && action == GLFW.PRESS
println("Goodbye!")
GLFW.DestroyWindow(window)
exit()
end
end
end)
# Setup Triangle / Vertex Data
data = GLfloat[
0.0, 0.5,
0.5, 0.0,
0.0, 5.0
]
# Setup VAO
vao = GLuint[0]
glGenVertexArrays(1, vao)
vao = vao[]
println(typeof(vao))
println(glGetError())
# Why do we run this?
glBindVertexArray(vao)
# Create VBO and load the buffer with our vertex data
vbo = GLuint[0]
glGenBuffers(1, vbo)
vbo = vbo[]
# Bind ~= Make Current
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW)
# Define shaders
const vertex_shader = """
in vec2 position;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
}
"""
const frag_shader = """
out vec4 color;
void main() {
outColor = vec4(1.0, 1.0, 1.0, 0.0);
}
"""
# OH GOD MAKING SHADERS IS ALWAYS A CHORE GOD DAMN
vertex_shader_id = glCreateShader(GL_VERTEX_SHADER)::GLuint
fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER)::GLuint
glShaderSource(vertex_shader_id, 1, convert(Ptr{UInt32}), pointer([convert(Ptr{GLchar}), pointer(vertex)]))
while !GLFW.WindowShouldClose(window)
GLFW.SwapBuffers(window)
glClear(GL_COLOR_BUFFER_BIT)
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER, vbo)
glVertexAttribPointer(
0, # ????
3, # Size of array
GL_FLOAT, # Data Type
GL_FALSE, # Normalized?
0, # Stride
C_NULL
)
glDrawArrays(GL_TRIANGLES, 0, 3) # From 0 to 3
glDisableVertexAttribArray(0)
GLFW.PollEvents()
end
GLFW.DestroyWindow(window)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment