Skip to content

Commit

Permalink
Added projection matrix (orthographic). Getting a black screen however
Browse files Browse the repository at this point in the history
  • Loading branch information
Failproofshark committed Jul 4, 2015
1 parent cde973f commit 68988de
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
50 changes: 36 additions & 14 deletions examples/gl-example.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

(require :cl-opengl)
(require :sdl2-ttf)
(require :mathkit)

(defun create-gl-array (type lisp-array)
(let ((gl-array (gl:alloc-gl-array type (length lisp-array))))
Expand All @@ -10,13 +11,18 @@
gl-array))

;;Text, as texutres, are loaded upside down and mirrored. Plan accordingly!
(defparameter *vertex-attribute-array* (create-gl-array :float #(-0.5 -0.5 1.0 1.0 1.0 0.0 1.0
0.5 -0.5 1.0 1.0 1.0 1.0 1.0
-0.5 0.5 1.0 1.0 1.0 0.0 0.0
0.5 0.5 1.0 1.0 1.0 1.0 0.0)))
(defparameter *vertex-color-texture-array* (create-gl-array :float #(1.0 1.0 1.0 0.0 1.0
1.0 1.0 1.0 1.0 1.0
1.0 1.0 1.0 0.0 0.0
1.0 1.0 1.0 1.0 0.0)))

;;since we're using an orthographic projection we need the width and height of our texture
(defparameter *vertex-position-array* 'nil)

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

(defparameter *projection-matrix* (kit.math:ortho-matrix 0 300 0 300 -10 10))

(defun gl-example ()
(with-init (:everything)
(sdl2-ttf:init)
Expand All @@ -34,7 +40,7 @@
255
0))
;;The first buffer is our verticies, the second is our elements
(buffers (gl:gen-buffers 2))
(buffers (gl:gen-buffers 3))
(vao (car (gl:gen-vertex-arrays 1)))
(texture (car (gl:gen-textures 1)))
(vertex-shader (gl:create-shader :vertex-shader))
Expand All @@ -48,7 +54,6 @@
"examples/texture-vertex-shader.glsl")))
(gl:compile-shader vertex-shader)
(print (gl:get-shader-info-log vertex-shader))

(gl:shader-source fragment-shader (read-file-into-string (asdf:system-relative-pathname 'sdl2-ttf-examples
"examples/texture-fragment-shader.glsl")))
(gl:compile-shader fragment-shader)
Expand All @@ -59,36 +64,50 @@
(gl:link-program shader-program)
(gl:use-program shader-program)

(let ((width (/ (surface-width texture-surface) 2.0))
(height (/ (surface-height texture-surface) 2.0)))
(setf *vertex-position-array* (create-gl-array :float (make-array 8
:initial-contents `(,(- width) ,(- height)
,width ,(- height)
,(- width) ,height
,width ,height)))))
(gl:bind-vertex-array vao)

(gl:bind-buffer :array-buffer (first buffers))
(gl:buffer-data :array-buffer :static-draw *vertex-attribute-array*)
(gl:buffer-data :array-buffer :static-draw *vertex-position-array*)

(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "position")
2
:float
:false
(* 7 (cffi:foreign-type-size :float))
(* 2 (cffi:foreign-type-size :float))
(cffi:null-pointer))

(gl:bind-buffer :array-buffer (second buffers))
(gl:buffer-data :array-buffer :static-draw *vertex-color-texture-array*)
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))

(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "input_color")
3
:float
:false
(* 7 (cffi:foreign-type-size :float))
(* 2 (cffi:foreign-type-size :float)))
(* 5 (cffi:foreign-type-size :float))
(cffi:null-pointer))
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "input_color"))

;;Texture coordinates
(gl:vertex-attrib-pointer (gl:get-attrib-location shader-program "tex_coord")
2
:float
:false
(* 7 (cffi:foreign-type-size :float))
(* 5 (cffi:foreign-type-size :float)))
(* 5 (cffi:foreign-type-size :float))
(* 3 (cffi:foreign-type-size :float)))
(gl:enable-vertex-attrib-array (gl:get-attrib-location shader-program "tex_coord"))

;;Bind the projection matrix
(gl:uniform-matrix (gl:get-uniform-location shader-program "projection_matrix")
4
(make-array 1 :initial-element *projection-matrix*))

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

(with-event-loop (:method :poll)
Expand All @@ -122,5 +141,8 @@
(sdl2-ttf:close-font font)
(free-surface texture-surface)
(sdl2-ttf:quit))
(gl:free-gl-array *vertex-position-array*)
(gl:free-gl-array *vertex-color-texture-array*)
(gl:free-gl-array *element-attribute-array*)
(gl:disable-vertex-attrib-array (gl:get-attrib-location shader-program "position"))
t)))))))
2 changes: 2 additions & 0 deletions examples/texture-vertex-shader.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ in vec3 input_color;
out vec3 color_output;
out vec2 tex_output;

uniform mat4 projection_matrix;

void main()
{
tex_output = tex_coord;
Expand Down
2 changes: 1 addition & 1 deletion sdl2-ttf-examples.asd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
:author "Bryan Baraoidan"
:license "MIT"
:version "1.0"
:depends-on (:alexandria :sdl2 :sdl2-ttf :cl-opengl)
:depends-on (:alexandria :sdl2 :sdl2-ttf :cl-opengl :mathkit)
:pathname "examples"
:components ((:file "package")
(:file "basic" :depends-on ("package"))
Expand Down

0 comments on commit 68988de

Please sign in to comment.