diff --git a/src/renderer.lisp b/src/renderer.lisp index 16714a7..07df1f0 100644 --- a/src/renderer.lisp +++ b/src/renderer.lisp @@ -5,9 +5,12 @@ #version 330 core layout (location = 0) in vec3 aPos; +out vec4 vertexColor; + void main() { gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); + vertexColor = vec4(0.5, 0.0, 0.0, 1.0); } ")) @@ -15,15 +18,19 @@ void main() #version 330 core out vec4 FragColor; +// in vec4 vertexColor; +uniform vec4 ourColor; + void main() { - FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); + FragColor = ourColor; } ")) (defparameter *vbo* 0) (defparameter *vao* 0) +(defparameter *ebo* 0) (defparameter *shader-program* 0) (defun init-renderer () @@ -40,23 +47,47 @@ void main() (gl:delete-shader vertex-shader) (gl:delete-shader fragment-shader) (setf *vbo* (gl:gen-buffer)) + (setf *ebo* (gl:gen-buffer)) (setf *vao* (gl:gen-vertex-array)) (gl:bind-vertex-array *vao*) (gl:bind-buffer :array-buffer *vbo*) + (gl:bind-buffer :element-array-buffer *ebo*) - (let ((arr (gl:alloc-gl-array :float 12)) - (vertices #(-0.5 -0.5 0.0 + (let* ((vertices #( 0.5 0.5 0.0 0.5 -0.5 0.0 - 0.0 0.5 0.0))) + -0.5 -0.5 0.0 + -0.5 0.5 0.0)) + (arr (gl:alloc-gl-array :float (length vertices)))) (dotimes (i (length vertices)) (setf (gl:glaref arr i) (aref vertices i))) (gl:buffer-data :array-buffer :static-draw arr) (gl:free-gl-array arr)) + (let* ((indices #(0 1 3 + 1 2 3)) + (arr (gl:alloc-gl-array :int (length indices)))) + (dotimes (i (length indices)) + (setf (gl:glaref arr i) (aref indices i))) + (gl:buffer-data :element-array-buffer :static-draw arr) + (gl:free-gl-array arr)) + (gl:vertex-attrib-pointer 0 3 :float nil 0 (cffi:null-pointer)) (gl:enable-vertex-attrib-array 0))) +(defun enable-wireframe () + (gl:polygon-mode :front-and-back :line)) + +(defun disable-wireframe () + (gl:polygon-mode :front-and-back :fill)) + +(let ((wireframe nil)) + (defun toggle-wireframe () + (if wireframe + (disable-wireframe) + (enable-wireframe)) + (setf wireframe (not wireframe)))) + (defun render () (gl:clear :color-buffer) ;; (gl:with-pushed-matrix @@ -64,7 +95,10 @@ void main() ;; (gl:rect -25 -25 25 25)) (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:draw-arrays :triangles 0 3) + (gl:draw-elements :triangles (gl:make-null-gl-array :unsigned-int) :count 6) + (gl:bind-vertex-array 0) (glfw:swap-buffers)) diff --git a/src/window.lisp b/src/window.lisp index 331008a..00d9787 100644 --- a/src/window.lisp +++ b/src/window.lisp @@ -17,7 +17,9 @@ (glfw:def-key-callback key-callback (window key scancode action mod-keys) (declare (ignore window scancode mod-keys)) (when (and (eq key :escape) (eq action :press)) - (glfw:set-window-should-close))) + (glfw:set-window-should-close)) + (when (and (eq key :space) (eq action :press)) + (toggle-wireframe))) (glfw:def-mouse-button-callback mouse-button-callback (window button action mod-keys) (declare (ignore window button action mod-keys)))