Resize the viewport and recompute the projection when needed

This commit is contained in:
Renaud Casenave-Péré 2015-08-28 15:04:00 +02:00
parent cdfd5f6bb5
commit bf6352369b
2 changed files with 20 additions and 3 deletions

View file

@ -8,6 +8,7 @@
:stoe/core/utils
:stoe/core/thread
:stoe/engine/gl-utils
:stoe/engine/viewport
:stoe/engine/mesh)
(:export #:world #:world-scene #:world-camera #:scene-lock #:scenes #:world-initialize
#:get-world #:get-current-scene #:get-current-camera
@ -106,6 +107,10 @@
(defun update-view (camera)
"Compute the world to view matrix from the position and the direction of CAMERA."
(when (need-resize-p)
(with-slots (fovy aspect near far) camera
(setf aspect (/ (viewport-width) (viewport-height)))
(setf (projection camera) (mperspective fovy aspect near far))))
(with-accessors ((pos pos) (dir dir) (view view)) camera
(setf view (m* (transpose (quat-to-mat4 dir)) (mtranslate (v- pos))))))

View file

@ -7,7 +7,8 @@
(:use :cl :glop
:stoe/core/utils
:stoe/engine/input)
(:export #:glsl-version
(:export #:viewport-width #:viewport-height #:need-resize-p
#:glsl-version
#:support-gl-version-p
#:viewport-configure
#:viewport-initialize
@ -25,6 +26,12 @@
(defvar *main-conf* nil)
(defvar *main-window* nil)
(defun viewport-width () (window-width *main-window*))
(defun viewport-height () (window-height *main-window*))
(defun need-resize-p () (and *main-window*
(or (/= (window-width *main-window*) (viewport-config-width *main-conf*))
(/= (window-height *main-window*) (viewport-config-height *main-conf*)))))
(defun support-gl-version-p (version)
(and *gl-config*
(multiple-value-bind (maj min) (floor version 10)
@ -97,6 +104,8 @@
"Poll events from the window manager.
This needs to be called once per frame, at the beginning of the loop."
(when *main-window*
(setf (viewport-config-width *main-conf*) (window-width *main-window*)
(viewport-config-height *main-conf*) (window-height *main-window*))
(dispatch-events *main-window* :blocking nil :on-foo nil)))
(defmethod on-event (window event)
@ -107,9 +116,12 @@ This needs to be called once per frame, at the beginning of the loop."
(button-press-event (on-button-event t (button event)))
(button-release-event (on-button-event nil (button event)))
(mouse-motion-event (on-motion-event (x event) (y event) (dx event) (dy event)))
;; (resize-event (on-resize-event (width event) (height event)))
;; (expose-event)
(resize-event (on-resize-event (width event) (height event)))
(expose-event (on-resize-event (width event) (height event)))
;; (visibility-event)
;; (focus-event)
;; (close-event)
(t (format t "Unhandled event type: ~s~%" (type-of event)))))
(defun on-resize-event (width height)
(gl:viewport 0 0 width height))