(define-shader-entity background (renderable) ((name :initform 'background) (texture :initform (error "BACKGROUND REQUIRED") :accessor texture) (vertex-array :initform (// 'trial:trial 'trial::fullscreen-square) :accessor vertex-array))) (defmethod render ((background background) (program shader-program)) (let ((vao (vertex-array background))) (setf (uniform program "view_size") (vec2 (max 1 (width *context*)) (max 1 (height *context*)))) (setf (uniform program "view_matrix") (minv *view-matrix*)) (setf (uniform program "tex_image") 0) (gl:active-texture :texture0) (gl:bind-texture :texture-2d (gl-name (texture background))) (unwind-protect (%gl:draw-elements :triangles (size vao) :unsigned-int 0) (gl:bind-vertex-array 0)))) (defmethod stage :after ((entity background) (area staging-area)) (stage (vertex-array entity) area) (stage (texture entity) area)) (define-class-shader (background :vertex-shader) "layout (location = 0) in vec3 vertex; layout (location = 1) in vec2 vertex_uv; uniform sampler2D tex_image; uniform mat4 view_matrix; uniform vec2 view_size; uniform vec2 parallax = vec2(0.5,0.5); uniform vec2 offset = vec2(0, 0); uinform vec2 scaling = vec2(0.5, 0.5); out vec2 map_coord; out vec2 world_xy; void main(){ gl_Position = vec4(vertex, 1); world_xy = (view_matrix*vec4(vertex_uv*view_size,0,1)).xy; vec2 size_a = textureSize(tex_image, 0).xy; map_coord = (view_matrix * vec4(vertex_uv*view_size*parallax, 0, 1)).xy; map_coord += size_a/2 + offset; map_coord /= parallax * scaling * size_a; }") (define-class-shader (background :fragment-shader) "uniform sampler2D tex_image; in vec2 map_coord; in vec2 world_xy; out vec4 color; void main(){ color = texture(tex_image, map_coord); }")