Browse Source

Got first triangle drawing

windows-imgui
Macoy Madson 8 months ago
parent
commit
0a5d159ff2
  1. 103
      src/OpenGL.cake

103
src/OpenGL.cake

@ -7,6 +7,28 @@
;; Use galogen-generated header. See (generate-gl-header)
"gl46.h") ;; "GL/gl.h"
(def-type-alias-global gl-id (unsigned int))
(defun opengl-shader-was-compiled-sucessfully (shader gl-id &return bool)
(var success int)
(var output-buffer ([] 512 char))
(glGetShaderiv shader GL_COMPILE_STATUS (addr success))
(unless success
(glGetShaderInfoLog shader (sizeof output-buffer) null output-buffer)
(fprintf stderr "error: Shader compilation failed:\n%s\n" output-buffer)
(return false))
(return true))
(defun opengl-program-was-linked-sucessfully (program gl-id &return bool)
(var success int)
(var output-buffer ([] 512 char))
(glGetProgramiv program GL_LINK_STATUS (addr success))
(unless success
(glGetProgramInfoLog program (sizeof output-buffer) null output-buffer)
(fprintf stderr "error: Program link failed:\n%s\n" output-buffer)
(return false))
(return true))
(comptime-cond
('auto-test
(defun test--opengl (&return int)
@ -17,17 +39,80 @@
;; TODO: Support resizing with window resize callback
(glViewport 0 0 640 480)
(scope ;; Make our triangle vertex array object
(var vertex-buffer-id (unsigned int))
(var shader-program gl-id (glCreateProgram))
(scope ;; Prepare program
(var vertex-shader gl-id (glCreateShader GL_VERTEX_SHADER))
(scope ;; Vertex shader
;; TODO: Generate this via a new Cakelisp language/dialect feature?
(var vertex-shader-code (* (const char))
"#version 460 core\n
layout (location = 0) in vec3 position;\n
void main()\n
{\n
gl_Position = vec4(position.x, position.y, position.z, 1.0);\n
}")
(glShaderSource vertex-shader 1 (addr vertex-shader-code) null)
(glCompileShader vertex-shader)
(unless (opengl-shader-was-compiled-sucessfully vertex-shader)
(sdl-shutdown window)
(return 1)))
(var fragment-shader gl-id (glCreateShader GL_FRAGMENT_SHADER))
(scope ;; Fragment shader
;; TODO: Generate this via a new Cakelisp language/dialect feature?
(var fragment-shader-code (* (const char))
"#version 460 core\n
out vec4 FragmentColor;\n
void main()\n
{\n
FragmentColor = vec4(1.f, 0.5f, 0.2f, 1.0f);\n
}")
(glShaderSource fragment-shader 1 (addr fragment-shader-code) null)
(glCompileShader fragment-shader)
(unless (opengl-shader-was-compiled-sucessfully fragment-shader)
(sdl-shutdown window)
(return 1)))
(scope ;; Link vertex and fragment shader
(glAttachShader shader-program vertex-shader)
(glAttachShader shader-program fragment-shader)
(glLinkProgram shader-program)
(unless (opengl-program-was-linked-sucessfully shader-program)
(sdl-shutdown window)
(return 1)))
(scope ;; Clean up shaders
(glDeleteShader vertex-shader)
(glDeleteShader fragment-shader)))
(var triangle-array-object gl-id)
(glGenVertexArrays 1 (addr triangle-array-object))
(var start-triangles int 0)
(var num-triangles int 3)
(scope
;; From this point on, configuration will be saved in the vertex array object
(glBindVertexArray triangle-array-object)
;; Make our triangle vertex array
(var vertex-buffer-id gl-id)
(glGenBuffers 1 (addr vertex-buffer-id))
(glBindBuffer GL_ARRAY_BUFFER vertex-buffer-id)
(var vertices ([] float)
(var-static vertices ([] float)
(array
0.25f 0.25f 0.25f
0.5f 0.5f 0.5f
0.75f 0.75f 0.75f))
(glBufferData GL_ARRAY_BUFFER (sizeof vertices) vertices GL_STATIC_DRAW))
0.5f 0.25f 0.25f
0.5f 0.75f 0.25f))
(glBufferData GL_ARRAY_BUFFER (sizeof vertices) vertices GL_STATIC_DRAW)
(var layout-location int 0) ;; Should match vertex shader layout
(glVertexAttribPointer layout-location 3 GL_FLOAT
GL_FALSE ;; Normalize?
;; Stride
(* 3 (sizeof float))
;; Buffer start offset
(type-cast 0 (* void)))
(glEnableVertexAttribArray layout-location))
(var exit-reason (* (const char)) null)
(while (not exit-reason)
@ -39,9 +124,13 @@
(when (at SDL_SCANCODE_ESCAPE currentKeyStates)
(set exit-reason "Escape pressed"))
(glClearColor 0.2f 0.3f 0.3f 1.f)
(glClearColor 0.2f 0.2f 0.2f 1.f)
(glClear GL_COLOR_BUFFER_BIT)
(glUseProgram shader-program)
(glBindVertexArray triangle-array-object)
(glDrawArrays GL_TRIANGLES start-triangles num-triangles)
(SDL_GL_SwapWindow window))
(when exit-reason

Loading…
Cancel
Save