|
2 | 2 |
|
3 | 3 | (require :cl-opengl)
|
4 | 4 | (require :sdl2-ttf)
|
| 5 | +(require :mathkit) |
5 | 6 |
|
6 | 7 | (defun create-gl-array (type lisp-array)
|
7 | 8 | (let ((gl-array (gl:alloc-gl-array type (length lisp-array))))
|
|
10 | 11 | gl-array))
|
11 | 12 |
|
12 | 13 | ;;Text, as texutres, are loaded upside down and mirrored. Plan accordingly!
|
13 |
| -(defparameter *vertex-attribute-array* (create-gl-array :float #(-0.5 -0.5 1.0 1.0 1.0 0.0 1.0 |
14 |
| - 0.5 -0.5 1.0 1.0 1.0 1.0 1.0 |
15 |
| - -0.5 0.5 1.0 1.0 1.0 0.0 0.0 |
16 |
| - 0.5 0.5 1.0 1.0 1.0 1.0 0.0))) |
| 14 | +(defparameter *vertex-color-texture-array* (create-gl-array :float #(1.0 1.0 1.0 0.0 1.0 |
| 15 | + 1.0 1.0 1.0 1.0 1.0 |
| 16 | + 1.0 1.0 1.0 0.0 0.0 |
| 17 | + 1.0 1.0 1.0 1.0 0.0))) |
| 18 | + |
| 19 | +;;since we're using an orthographic projection we need the width and height of our texture |
| 20 | +(defparameter *vertex-position-array* 'nil) |
17 | 21 |
|
18 | 22 | (defparameter *element-attribute-array* (create-gl-array :unsigned-short #(0 1 2 3)))
|
19 | 23 |
|
| 24 | +(defparameter *projection-matrix* (kit.math:ortho-matrix 0 300 0 300 -10 10)) |
| 25 | + |
20 | 26 | (defun gl-example ()
|
21 | 27 | (with-init (:everything)
|
22 | 28 | (sdl2-ttf:init)
|
|
34 | 40 | 255
|
35 | 41 | 0))
|
36 | 42 | ;;The first buffer is our verticies, the second is our elements
|
37 |
| - (buffers (gl:gen-buffers 2)) |
| 43 | + (buffers (gl:gen-buffers 3)) |
38 | 44 | (vao (car (gl:gen-vertex-arrays 1)))
|
39 | 45 | (texture (car (gl:gen-textures 1)))
|
40 | 46 | (vertex-shader (gl:create-shader :vertex-shader))
|
|
48 | 54 | "examples/texture-vertex-shader.glsl")))
|
49 | 55 | (gl:compile-shader vertex-shader)
|
50 | 56 | (print (gl:get-shader-info-log vertex-shader))
|
51 |
| - |
52 | 57 | (gl:shader-source fragment-shader (read-file-into-string (asdf:system-relative-pathname 'sdl2-ttf-examples
|
53 | 58 | "examples/texture-fragment-shader.glsl")))
|
54 | 59 | (gl:compile-shader fragment-shader)
|
|
59 | 64 | (gl:link-program shader-program)
|
60 | 65 | (gl:use-program shader-program)
|
61 | 66 |
|
| 67 | + (let ((width (/ (surface-width texture-surface) 2.0)) |
| 68 | + (height (/ (surface-height texture-surface) 2.0))) |
| 69 | + (setf *vertex-position-array* (create-gl-array :float (make-array 8 |
| 70 | + :initial-contents `(,(- width) ,(- height) |
| 71 | + ,width ,(- height) |
| 72 | + ,(- width) ,height |
| 73 | + ,width ,height))))) |
62 | 74 | (gl:bind-vertex-array vao)
|
63 |
| - |
64 | 75 | (gl:bind-buffer :array-buffer (first buffers))
|
65 |
| - (gl:buffer-data :array-buffer :static-draw *vertex-attribute-array*) |
| 76 | + (gl:buffer-data :array-buffer :static-draw *vertex-position-array*) |
66 | 77 |
|
67 | 78 | (gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "position")
|
68 | 79 | 2
|
69 | 80 | :float
|
70 | 81 | :false
|
71 |
| - (* 7 (cffi:foreign-type-size :float)) |
| 82 | + (* 2 (cffi:foreign-type-size :float)) |
72 | 83 | (cffi:null-pointer))
|
| 84 | + |
| 85 | + (gl:bind-buffer :array-buffer (second buffers)) |
| 86 | + (gl:buffer-data :array-buffer :static-draw *vertex-color-texture-array*) |
73 | 87 | (gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
|
74 | 88 |
|
75 | 89 | (gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "input_color")
|
76 | 90 | 3
|
77 | 91 | :float
|
78 | 92 | :false
|
79 |
| - (* 7 (cffi:foreign-type-size :float)) |
80 |
| - (* 2 (cffi:foreign-type-size :float))) |
| 93 | + (* 5 (cffi:foreign-type-size :float)) |
| 94 | + (cffi:null-pointer)) |
81 | 95 | (gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "input_color"))
|
82 | 96 |
|
83 | 97 | ;;Texture coordinates
|
84 | 98 | (gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "tex_coord")
|
85 | 99 | 2
|
86 | 100 | :float
|
87 | 101 | :false
|
88 |
| - (* 7 (cffi:foreign-type-size :float)) |
89 |
| - (* 5 (cffi:foreign-type-size :float))) |
| 102 | + (* 5 (cffi:foreign-type-size :float)) |
| 103 | + (* 3 (cffi:foreign-type-size :float))) |
90 | 104 | (gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "tex_coord"))
|
91 | 105 |
|
| 106 | + ;;Bind the projection matrix |
| 107 | + (gl:uniform-matrix (gl:get-uniform-location shader-program "projection_matrix") |
| 108 | + 4 |
| 109 | + (make-array 1 :initial-element *projection-matrix*)) |
| 110 | + |
92 | 111 | ;;Binding the texture object for configuration
|
93 | 112 | (gl:bind-texture :texture-2d texture)
|
94 | 113 | (gl:tex-parameter :texture-2d :texture-wrap-s :clamp-to-border)
|
|
106 | 125 | :unsigned-byte
|
107 | 126 | ;;Note this does NOT need to be freed because it's a dereferenced pointer belonging to struct, not a pointer to a pointer! It will be freed when free-surface is called later
|
108 | 127 | (surface-pixels texture-surface))
|
109 |
| - (gl:bind-buffer :element-array-buffer (second buffers)) |
| 128 | + (gl:bind-buffer :element-array-buffer (third buffers)) |
110 | 129 | (gl:buffer-data :element-array-buffer :static-draw *element-attribute-array*)
|
111 | 130 |
|
112 | 131 | (with-event-loop (:method :poll)
|
|
122 | 141 | (sdl2-ttf:close-font font)
|
123 | 142 | (free-surface texture-surface)
|
124 | 143 | (sdl2-ttf:quit))
|
| 144 | + (gl:free-gl-array *vertex-position-array*) |
| 145 | + (gl:free-gl-array *vertex-color-texture-array*) |
| 146 | + (gl:free-gl-array *element-attribute-array*) |
125 | 147 | (gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
|
126 | 148 | t)))))))
|
0 commit comments