Advertisement
sknthelisper

LearnOpenGL.com - Hello rectangle

Jul 13th, 2023 (edited)
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 5.67 KB | Source Code | 0 0
  1. ;;;; Tutorial 01: Hello OpenGL from - https://learnopengl.com/Getting-started/Hello-Triangle
  2. ;;;; Original C source: https://learnopengl.com/code_viewer_gh.php?code=src/1.getting_started/2.2.hello_triangle_indexed/hello_triangle_indexed.cpp
  3.  
  4. ;;;; Usage; Recommend using quicklisp and create an ASD file that loads this.
  5. ;;;;          Dependencies: #:CL-opengl #:bt-semaphore #:Lispbuilder-sdl
  6. (defpackage #:hello-rectangle
  7.   (:use #:cl)
  8.   (:export :main))
  9.  
  10. (in-package #:hello-rectangle)
  11.  
  12. (defparameter *fps* 60)
  13. (defparameter *Width* 640)
  14. (defparameter *height* 480)
  15.  
  16.  
  17. (defparameter *vertex-shader-source* "
  18. #version 330 core
  19. layout (location = 0) in vec3 position;
  20. void main(){
  21.  gl_Position = vec4(position.x, position.y, position.z, 1.0);
  22. }")
  23.  
  24. (defparameter *fragment-shader-source* "
  25. #version 330 core
  26. out vec4 color;
  27. void main(){
  28.  color = vec4(1.0f, 0.5f, 0.2f, 1.0f);
  29. }")
  30.  
  31. (defparameter *verticies-data* #( 0.5  0.5 0.0  ; Top Right
  32.                   0.5 -0.5 0.0  ; Bottom Right
  33.                  -0.5 -0.5 0.0  ; Bottom Left
  34.                  -0.5  0.5 0.0)); Top Left
  35.  
  36. (defparameter *indices-data* #(0 1 3   ; First Triangle
  37.                 1 2 3)) ; Second Triangle
  38.  
  39. (defparameter *verticies* nil)
  40. (defparameter *indices* nil)
  41. (defparameter *VBO* nil)
  42. (defparameter *VAO* nil)
  43. (defparameter *EBO* nil)
  44.  
  45. (defparameter *shader-program* nil)
  46.  
  47.  
  48. (defun check-shader-error (shader)
  49. "Check for shader compilation errors"
  50.   (let ((success (cffi:foreign-alloc :int :initial-element 0)))
  51.     (unwind-protect
  52.      (progn
  53.        (%gl:get-shader-iv shader :compile-status success)
  54.        (unless (= (cffi:mem-aref success :int) 1)
  55.          (error "OpenGL Shader Error:~%~a~%" (gl:get-shader-info-log shader)))
  56.       (cffi:foreign-free success)))))
  57.  
  58.  
  59. (defun check-program-error (program)
  60. "Check for linking errors"
  61.   (let ((success (cffi:foreign-alloc :int :initial-element 0)))
  62.     (unwind-protect
  63.      (progn
  64.        (%gl:get-program-iv program :link-status success)
  65.        (unless (= (cffi:mem-aref success :int) 1)
  66.          (error "OpenGL Program Error:~%~a~%" (gl:get-program-info-log program))))
  67.       (cffi:foreign-free success))))
  68.  
  69. (defun setup-program (vertex-Shader Fragment-Shader)
  70. "link and attach the shader programs"
  71.   (let ((program (gl:create-program)))
  72.     (gl:attach-shader program vertex-shader)
  73.     (gl:attach-shader program fragment-shader)
  74.     (gl:link-program program)
  75.    
  76.     (check-program-error program)
  77.     (setf *shader-program* program)))
  78.  
  79. (defun setup-shaders ()
  80. "Build and compile the actual shaders"
  81.   (let ((vertex-shader (gl:create-shader :vertex-shader))
  82.     (fragment-shader (gl:create-shader :fragment-shader)))
  83.  
  84.     ;; Vertex shader
  85.     (gl:shader-source vertex-shader *vertex-shader-source*)
  86.     (gl:compile-shader vertex-shader)
  87.  
  88.     (check-shader-error vertex-shader)
  89.  
  90.     ;; Fragment shader
  91.     (gl:shader-source fragment-shader *fragment-shader-source*)
  92.     (gl:compile-shader fragment-shader)
  93.     (check-shader-error fragment-shader)
  94.  
  95.     (setup-program vertex-shader fragment-shader)
  96.     (gl:delete-shader vertex-shader)
  97.     (gl:delete-shader fragment-shader)))
  98.  
  99. (defun setup-vertex-attributes ()
  100. "Set up vertex data (and buffer(s)) and configure vertex attributes"
  101.   (setf *VBO* (gl:gen-buffer))
  102.   (setf *vao* (gl:gen-vertex-array))
  103.   (setf *EBO* (gl:gen-buffer))
  104.  
  105.  
  106.   ;; Convert CL arrays into an C arrays
  107.   (setf *verticies*
  108.     (loop :with gl-array = (gl:alloc-gl-array :float (length *verticies-data*))
  109.           :for i :below (length *verticies-data*)
  110.           :do (setf (gl:glaref gl-array i) (aref *verticies-data* i))
  111.           :finally (return gl-array))
  112.    
  113.     *indices*
  114.     (loop :with gl-array = (gl:alloc-gl-array :unsigned-int (length *indices-data*))
  115.           :for i :below (length *indices-data*)
  116.           :do (setf (gl:glaref gl-array i) (aref *indices-data* i))
  117.           :finally (return gl-array)))
  118.  
  119.  
  120.   (gl:bind-vertex-array *vao*)
  121.   (gl:bind-buffer :array-buffer *vbo*)
  122.   (gl:buffer-data :array-buffer :static-draw *verticies*)
  123.  
  124.  
  125.  
  126.   (gl:bind-buffer :element-array-buffer *ebo*)
  127.   (gl:buffer-data :element-array-buffer :static-draw *indices*)
  128.  
  129.   (gl:vertex-attrib-pointer 0 3 :float nil (* 3 (cffi:foreign-type-size :float)) 0)
  130.   (gl:enable-vertex-attrib-array 0)
  131.   (gl:bind-buffer :array-buffer 0)
  132.   (gl:bind-vertex-array 0))
  133.  
  134. (defun init-openGL ()
  135. "Gets openGL ready"
  136.   (gl:clear-color .2 .3 .3 1.0)
  137.   ;;(gl:polygon-mode :front-and-back :line)
  138.  
  139.   (setup-shaders)
  140.   (setup-vertex-attributes)
  141.   )
  142.  
  143. (defun rendering ()
  144. "Displays the actual rectangle"
  145.   (gl:clear :color-buffer-bit)
  146.  
  147.   (gl:use-program *shader-program*)
  148.  
  149.   (gl:bind-vertex-array *vao*)
  150.   (%gl:draw-elements :triangles 6 :unsigned-int 0)
  151.   (gl:bind-vertex-array 0)
  152.  
  153.   (sdl:update-display))
  154.  
  155. (defun freeing-memory ()
  156. "Clear allocated memory"
  157.   (gl:free-gl-array *verticies*)
  158.   (gl:free-gl-array *indices*)
  159.  
  160.   (gl:delete-vertex-arrays '(*vao*))
  161.   (gl:delete-buffers '(*vbo* *ebo* ))
  162.  
  163.   (gl:delete-program *shader-program*))
  164.  
  165.  
  166. (defun main (&aux (title "Hello Rectangle"))
  167.   (bt:make-thread
  168.     (lambda ()
  169.       (sdl:with-init ()
  170.         (unwind-protect ; ensures proper error handling in-case of crash
  171.           (sdl:window *width* *height* :title-caption title :flags '(sdl:sdl-opengl)) ; Window creation with opengl rendering
  172.           (setf (sdl:frame-rate) *fps*)
  173.           (sdl:enable-key-repeat 100 1)
  174.  
  175.           (setf cl-opengl-bindings:*gl-get-proc-address* #'sdl-cffi::sdl-gl-get-proc-address) ; overwrite SDL's rendering with CL-opengl's.
  176.  
  177.           (init-openGL)
  178.  
  179.           (sdl:with-events ()
  180.             (:quit-event () t)
  181.      
  182.             (:idle ()
  183.             (rendering)))
  184.              
  185.           (freeing-memory)))) ;; Freeing the memory after program terminates
  186.    :name title))
  187.  
Tags: opengl
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement