Browse Source

OpenGL texture experiment

master
Macoy Madson 8 months ago
parent
commit
70eccd978f
  1. 96
      src/OpenGL.cake

96
src/OpenGL.cake

@ -45,12 +45,16 @@
(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
}")
#"##version 460 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec2 textureCoord;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(position.x, position.y, position.z, 1.0);
TexCoord = textureCoord;
}#"#)
(glShaderSource vertex-shader 1 (addr vertex-shader-code) null)
(glCompileShader vertex-shader)
(unless (opengl-shader-was-compiled-sucessfully vertex-shader)
@ -61,12 +65,14 @@ void main()\n
(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
}")
#"##version 460 core
out vec4 FragmentColor;
in vec2 TexCoord;
uniform sampler2D textureSampler;
void main()
{
FragmentColor = texture(textureSampler, TexCoord);
}#"#)
(glShaderSource fragment-shader 1 (addr fragment-shader-code) null)
(glCompileShader fragment-shader)
(unless (opengl-shader-was-compiled-sucessfully fragment-shader)
@ -86,20 +92,28 @@ void main()\n
(glDeleteShader fragment-shader)))
(var mesh-array-object gl-id)
(var texture gl-id)
(glGenVertexArrays 1 (addr mesh-array-object))
(var start-indices int 0)
(var num-indices int 0)
(scope
(var-static vertices ([] float)
(defstruct vertex-data
;; Position
x float
y float
z float
;; Texture
s float
t float)
(var-static vertices ([] vertex-data)
(array
0.25f 0.25f 0.25f
0.75f 0.25f 0.25f
0.75f 0.75f 0.25f
0.25f 0.75f 0.25f))
(array 0.25f 0.25f 0.25f 0.f 0.f)
(array 0.75f 0.25f 0.25f 1.f 0.f)
(array 0.75f 0.75f 0.25f 1.f 1.f)
(array 0.25f 0.75f 0.25f 0.f 1.f)))
(var-static indices ([] int)
(array 0 1 3
1 2 3))
(set num-indices (array-size indices))
;; From this point on, configuration will be saved in the vertex array object
@ -109,14 +123,13 @@ void main()\n
(var vertex-buffer-id gl-id)
(glGenBuffers 1 (addr vertex-buffer-id))
(glBindBuffer GL_ARRAY_BUFFER vertex-buffer-id)
(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))
(sizeof (type vertex-data))
;; Buffer start offset
(type-cast 0 (* void)))
(glEnableVertexAttribArray layout-location)
@ -125,8 +138,46 @@ void main()\n
(var index-buffer-id gl-id)
(glGenBuffers 1 (addr index-buffer-id))
(glBindBuffer GL_ELEMENT_ARRAY_BUFFER index-buffer-id)
(glBufferData GL_ELEMENT_ARRAY_BUFFER (sizeof indices) indices GL_STATIC_DRAW))
(glBufferData GL_ELEMENT_ARRAY_BUFFER (sizeof indices) indices GL_STATIC_DRAW)
;; Textures
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_REPEAT)
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_REPEAT)
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_NEAREST_MIPMAP_NEAREST)
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_NEAREST)
;; Generate a texture
(var width (const int) 256)
(var height (const int) 256)
(var checker-image ([] (* width height 3) (unsigned char)) (array 0))
(each-in-range height y
(each-in-range width x
(set (at (+ (* y width 3) (* 3 x) 0) checker-image) (? (or (= 0 (mod (/ x 64) 2))
(= 0 (mod (/ y 64) 2)))
255 0))
(set (at (+ (* y width 3) (* 3 x) 1) checker-image) 163)
(set (at (+ (* y width 3) (* 3 x) 2) checker-image) 0)))
(glGenTextures 1 (addr texture))
(glBindTexture GL_TEXTURE_2D texture)
(glTexImage2D GL_TEXTURE_2D
0 ;; Num mips we are providing
GL_RGB ;; Destination format
width
height
0 ;; Legacy
GL_RGB ;; Source format
GL_UNSIGNED_BYTE
checker-image)
(glGenerateMipmap GL_TEXTURE_2D)
(var texture-coord-layout-location int 1) ;; Should match vertex shader layout
(glVertexAttribPointer texture-coord-layout-location
2 ;; Two texture coordinates
GL_FLOAT
GL_FALSE ;; Normalized?
(sizeof (type vertex-data)) ;; stride
;; Buffer start offset
(type-cast (offsetof (type vertex-data) s) (* void)))
(glEnableVertexAttribArray texture-coord-layout-location))
(var exit-reason (* (const char)) null)
(while (not exit-reason)
@ -142,6 +193,7 @@ void main()\n
(glClear GL_COLOR_BUFFER_BIT)
(glUseProgram shader-program)
(glBindTexture GL_TEXTURE_2D texture)
(glBindVertexArray mesh-array-object)
(glDrawElements GL_TRIANGLES num-indices GL_UNSIGNED_INT
(type-cast 0 (* void)))

Loading…
Cancel
Save