Advertisement
sknthelisper

LearnOpenGL.com - Hello triangle

Jul 13th, 2023 (edited)
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 5.19 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.1.hello_triangle/hello_triangle.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-triangle
  7.   (:use #:cl)
  8.   (:export :main))
  9.  
  10. (in-package #:hello-triangle)
  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.                                   ;x   ;y   ;z
  32. (defparameter *verticies-data* #(-0.5 -0.5  0.0 ; Bottom Left
  33.                       0.5 -0.5  0.0 ; Bottom Right
  34.                       0.0  0.5  0.0)) ; Top
  35.  
  36. (defparameter *verticies* nil)
  37.  
  38. (defparameter *VBO* nil)
  39. (defparameter *VAO* nil)
  40.  
  41. (defparameter *shader-program* nil)
  42.  
  43.  
  44. (defun check-shader-error (shader)
  45. "Check for shader compilation errors"
  46.   (let ((success (cffi:foreign-alloc :int :initial-element 0)))
  47.     (unwind-protect
  48.      (progn
  49.        (%gl:get-shader-iv shader :compile-status success)
  50.        (unless (= (cffi:mem-aref success :int) 1)
  51.          (error "OpenGL Shader Error:~%~a~%" (gl:get-shader-info-log shader)))
  52.       (cffi:foreign-free success)))))
  53.  
  54.  
  55. (defun check-program-error (program)
  56. "Check for linking errors"
  57.   (let ((success (cffi:foreign-alloc :int :initial-element 0)))
  58.     (unwind-protect
  59.      (progn
  60.        (%gl:get-program-iv program :link-status success)
  61.        (unless (= (cffi:mem-aref success :int) 1)
  62.          (error "OpenGL Program Error:~%~a~%" (gl:get-program-info-log program))))
  63.       (cffi:foreign-free success))))
  64.  
  65. (defun setup-program (vertex-Shader Fragment-Shader)
  66. "link and attach the shader programs"
  67.   (let ((program (gl:create-program)))
  68.     (gl:attach-shader program vertex-shader)
  69.     (gl:attach-shader program fragment-shader)
  70.     (gl:link-program program)
  71.    
  72.     (check-program-error program)
  73.     (setf *shader-program* program)))
  74.  
  75. (defun setup-shaders ()
  76. "Build and compile the actual shaders"
  77.   (let ((vertex-shader (gl:create-shader :vertex-shader))
  78.     (fragment-shader (gl:create-shader :fragment-shader)))
  79.  
  80.     ;; Vertex shader
  81.     (gl:shader-source vertex-shader *vertex-shader-source*)
  82.     (gl:compile-shader vertex-shader)
  83.  
  84.     (check-shader-error vertex-shader)
  85.  
  86.     ;; Fragment shader
  87.     (gl:shader-source fragment-shader *fragment-shader-source*)
  88.     (gl:compile-shader fragment-shader)
  89.     (check-shader-error fragment-shader)
  90.    
  91.     (setup-program vertex-shader fragment-shader)
  92.  
  93.     (gl:delete-shader vertex-shader)
  94.     (gl:delete-shader fragment-shader)))
  95.  
  96. (defun setup-vertex-attributes ()
  97. "Set up vertex data (and buffer(s)) and configure vertex attributes"
  98.   ;; Bind the vertex array object first, then bind and set vertex buffer(s), and then configure vertex attribute(s).
  99.   (setf *VBO* (gl:gen-buffer))
  100.   (setf *vao* (gl:gen-vertex-array))
  101.  
  102.   (gl:bind-vertex-array *vao*)
  103.   (gl:bind-buffer :array-buffer *vbo*)
  104.  
  105.   ;; Convert CL array into an C array
  106.   (setf *verticies*
  107.     (loop :with gl-array = (gl:alloc-gl-array :float (length *verticies-data*))
  108.           :for i :below (length *verticies-data*)
  109.           :do (setf (gl:glaref gl-array i) (aref *verticies-data* i))
  110.           :finally (return gl-array)))
  111.  
  112.   (gl:buffer-data :array-buffer :static-draw *verticies*)
  113.  
  114.  
  115.   (gl:vertex-attrib-pointer 0 3 :float 0 (* 3 (cffi:foreign-type-size :float)) 0)
  116.   (gl:enable-vertex-attrib-array 0))
  117.  
  118.  
  119. (defun init-openGL ()
  120. "Gets openGL ready"
  121.   (gl:clear-color .2 .3 .3 1.0)
  122.   ;;(gl:polygon-mode :front-and-back :line)
  123.  
  124.   (setup-shaders)
  125.   (setup-vertex-attributes)
  126.   )
  127.  
  128. (defun rendering ()
  129. "Displays the actual triangle"
  130.   (gl:clear :color-buffer-bit)
  131.  
  132.   (gl:use-program *shader-program*)
  133.   (gl:bind-vertex-array *vao*)
  134.   (gl:draw-arrays :triangles 0 3)
  135.  
  136.   (sdl:update-display))
  137.  
  138. ;;; Since OpenGL doesn't have an garbage collector, have to manually do it.
  139. (defun freeing-memory ()
  140. "Clear allocated memory"
  141.   (gl:free-gl-array *verticies*)
  142.  
  143.   (gl:delete-vertex-arrays '(*vao*))
  144.   (gl:delete-buffers '(*vbo*))
  145.  
  146.   (gl:delete-program *shader-program*))
  147.  
  148.  
  149. (defun main (&aux (title "Hello Triangle"))
  150.   (bt:make-thread
  151.     (lambda ()
  152.       (sdl:with-init ()
  153.         (unwind-protect ; ensures proper error handling in-case of crash
  154.           (sdl:window *width* *height* :title-caption title :flags '(sdl:sdl-opengl)) ; Window creation with opengl rendering
  155.           (setf (sdl:frame-rate) *fps*)
  156.           (sdl:enable-key-repeat 100 1)
  157.  
  158.           (setf cl-opengl-bindings:*gl-get-proc-address* #'sdl-cffi::sdl-gl-get-proc-address) ; overwrite SDL's rendering with CL-opengl's.
  159.  
  160.           (init-openGL)
  161.  
  162.           (sdl:with-events ()
  163.             (:quit-event () t)
  164.      
  165.             (:idle ()
  166.             (rendering)))
  167.              
  168.           (freeing-memory)))) ;; Freeing the memory after program terminates
  169.    :name title))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement