Skip to content

Commit 68988de

Browse files
Added projection matrix (orthographic). Getting a black screen however
1 parent cde973f commit 68988de

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

examples/gl-example.lisp

+36-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
(require :cl-opengl)
44
(require :sdl2-ttf)
5+
(require :mathkit)
56

67
(defun create-gl-array (type lisp-array)
78
(let ((gl-array (gl:alloc-gl-array type (length lisp-array))))
@@ -10,13 +11,18 @@
1011
gl-array))
1112

1213
;;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)
1721

1822
(defparameter *element-attribute-array* (create-gl-array :unsigned-short #(0 1 2 3)))
1923

24+
(defparameter *projection-matrix* (kit.math:ortho-matrix 0 300 0 300 -10 10))
25+
2026
(defun gl-example ()
2127
(with-init (:everything)
2228
(sdl2-ttf:init)
@@ -34,7 +40,7 @@
3440
255
3541
0))
3642
;;The first buffer is our verticies, the second is our elements
37-
(buffers (gl:gen-buffers 2))
43+
(buffers (gl:gen-buffers 3))
3844
(vao (car (gl:gen-vertex-arrays 1)))
3945
(texture (car (gl:gen-textures 1)))
4046
(vertex-shader (gl:create-shader :vertex-shader))
@@ -48,7 +54,6 @@
4854
"examples/texture-vertex-shader.glsl")))
4955
(gl:compile-shader vertex-shader)
5056
(print (gl:get-shader-info-log vertex-shader))
51-
5257
(gl:shader-source fragment-shader (read-file-into-string (asdf:system-relative-pathname 'sdl2-ttf-examples
5358
"examples/texture-fragment-shader.glsl")))
5459
(gl:compile-shader fragment-shader)
@@ -59,36 +64,50 @@
5964
(gl:link-program shader-program)
6065
(gl:use-program shader-program)
6166

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)))))
6274
(gl:bind-vertex-array vao)
63-
6475
(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*)
6677

6778
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "position")
6879
2
6980
:float
7081
:false
71-
(* 7 (cffi:foreign-type-size :float))
82+
(* 2 (cffi:foreign-type-size :float))
7283
(cffi:null-pointer))
84+
85+
(gl:bind-buffer :array-buffer (second buffers))
86+
(gl:buffer-data :array-buffer :static-draw *vertex-color-texture-array*)
7387
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
7488

7589
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "input_color")
7690
3
7791
:float
7892
: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))
8195
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "input_color"))
8296

8397
;;Texture coordinates
8498
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "tex_coord")
8599
2
86100
:float
87101
: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)))
90104
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "tex_coord"))
91105

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+
92111
;;Binding the texture object for configuration
93112
(gl:bind-texture :texture-2d texture)
94113
(gl:tex-parameter :texture-2d :texture-wrap-s :clamp-to-border)
@@ -106,7 +125,7 @@
106125
:unsigned-byte
107126
;;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
108127
(surface-pixels texture-surface))
109-
(gl:bind-buffer :element-array-buffer (second buffers))
128+
(gl:bind-buffer :element-array-buffer (third buffers))
110129
(gl:buffer-data :element-array-buffer :static-draw *element-attribute-array*)
111130

112131
(with-event-loop (:method :poll)
@@ -122,5 +141,8 @@
122141
(sdl2-ttf:close-font font)
123142
(free-surface texture-surface)
124143
(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*)
125147
(gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
126148
t)))))))

examples/texture-vertex-shader.glsl

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ in vec3 input_color;
77
out vec3 color_output;
88
out vec2 tex_output;
99

10+
uniform mat4 projection_matrix;
11+
1012
void main()
1113
{
1214
tex_output = tex_coord;

sdl2-ttf-examples.asd

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
:author "Bryan Baraoidan"
1010
:license "MIT"
1111
:version "1.0"
12-
:depends-on (:alexandria :sdl2 :sdl2-ttf :cl-opengl)
12+
:depends-on (:alexandria :sdl2 :sdl2-ttf :cl-opengl :mathkit)
1313
:pathname "examples"
1414
:components ((:file "package")
1515
(:file "basic" :depends-on ("package"))

0 commit comments

Comments
 (0)