Browse Source

Triangle de couleur

master
Gabriel Pariat 3 years ago
parent
commit
eb480bf152
  1. 36
      src/renderer.lisp

36
src/renderer.lisp

@ -4,13 +4,14 @@
(defparameter *vertex-shader-source* '(" (defparameter *vertex-shader-source* '("
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
out vec4 vertexColor; out vec3 ourColor;
void main() void main()
{ {
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); gl_Position = vec4(aPos, 1.0);
vertexColor = vec4(0.5, 0.0, 0.0, 1.0); ourColor = aColor;
} }
")) "))
@ -18,12 +19,11 @@ void main()
#version 330 core #version 330 core
out vec4 FragColor; out vec4 FragColor;
// in vec4 vertexColor; in vec3 ourColor;
uniform vec4 ourColor;
void main() void main()
{ {
FragColor = ourColor; FragColor = vec4(ourColor, 1.0);
} }
")) "))
@ -54,26 +54,30 @@ void main()
(gl:bind-buffer :array-buffer *vbo*) (gl:bind-buffer :array-buffer *vbo*)
(gl:bind-buffer :element-array-buffer *ebo*) (gl:bind-buffer :element-array-buffer *ebo*)
(let* ((vertices #( 0.5 0.5 0.0 (let* ((vertices #( 0.5 -0.5 0.0 1.0 0.0 0.0
0.5 -0.5 0.0 -0.5 -0.5 0.0 0.0 1.0 0.0
-0.5 -0.5 0.0 0.0 0.5 0.0 0.0 0.0 1.0))
-0.5 0.5 0.0))
(arr (gl:alloc-gl-array :float (length vertices)))) (arr (gl:alloc-gl-array :float (length vertices))))
(dotimes (i (length vertices)) (dotimes (i (length vertices))
(setf (gl:glaref arr i) (aref vertices i))) (setf (gl:glaref arr i) (aref vertices i)))
(gl:buffer-data :array-buffer :static-draw arr) (gl:buffer-data :array-buffer :static-draw arr)
(gl:free-gl-array arr)) (gl:free-gl-array arr))
(let* ((indices #(0 1 3 (let* ((indices #(0 1 2))
1 2 3))
(arr (gl:alloc-gl-array :int (length indices)))) (arr (gl:alloc-gl-array :int (length indices))))
(dotimes (i (length indices)) (dotimes (i (length indices))
(setf (gl:glaref arr i) (aref indices i))) (setf (gl:glaref arr i) (aref indices i)))
(gl:buffer-data :element-array-buffer :static-draw arr) (gl:buffer-data :element-array-buffer :static-draw arr)
(gl:free-gl-array arr)) (gl:free-gl-array arr))
(gl:vertex-attrib-pointer 0 3 :float nil 0 (cffi:null-pointer)) (gl:vertex-attrib-pointer 0 3 :float nil
(gl:enable-vertex-attrib-array 0))) (* 6 (cffi:foreign-type-size :float))
(cffi:null-pointer))
(gl:enable-vertex-attrib-array 0)
(gl:vertex-attrib-pointer 1 3 :float nil
(* 6 (cffi:foreign-type-size :float))
(cffi:make-pointer (* 3 (cffi:foreign-type-size :float))))
(gl:enable-vertex-attrib-array 1)))
(defun enable-wireframe () (defun enable-wireframe ()
(gl:polygon-mode :front-and-back :line)) (gl:polygon-mode :front-and-back :line))
@ -95,10 +99,8 @@ void main()
;; (gl:rect -25 -25 25 25)) ;; (gl:rect -25 -25 25 25))
(gl:use-program *shader-program*) (gl:use-program *shader-program*)
(gl:uniformf (gl:get-uniform-location *shader-program* "ourColor")
0.0 (+ (/ (sin (glfw:get-time)) 2) 0.5) 0.0 1.0)
(gl:bind-vertex-array *vao*) (gl:bind-vertex-array *vao*)
(gl:draw-elements :triangles (gl:make-null-gl-array :unsigned-int) :count 6) (gl:draw-elements :triangles (gl:make-null-gl-array :unsigned-int) :count 3)
(gl:bind-vertex-array 0) (gl:bind-vertex-array 0)
(glfw:swap-buffers)) (glfw:swap-buffers))

Loading…
Cancel
Save