From 9d95bc980b13787bb96cfd56e79f75e44ed15ebf Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Wed, 27 Mar 2013 22:04:40 -0400 Subject: [PATCH] Actual blob shading yaaay --- res/shader/blob.frag | 24 ++++++++++++++++++++---- res/shader/blob.vert | 7 ++++--- src/celldiv/blob.clj | 38 ++++++++++++++++++++++++++++++++++++++ src/hottub/core.clj | 14 +++++++------- 4 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 src/celldiv/blob.clj diff --git a/res/shader/blob.frag b/res/shader/blob.frag index 266135c..bfbda90 100644 --- a/res/shader/blob.frag +++ b/res/shader/blob.frag @@ -1,5 +1,21 @@ -void main() -{ - // Setting Each Pixel To Red - gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); +varying vec2 xy; +uniform vec4 blobs[10]; + +float doBlob(vec4 blob) { + vec2 s = pow(blob.xy - xy, vec2(2.0, 2.0)); + return 1.0 / ((s.x / blob.z) + (s.y / blob.w)); +} +void main() { + float v = 0.0; + for (int i = 0; i < 10; i ++) { + v += doBlob(blobs[i]/*vec4(0.3, 0.3, 0.1, 0.1)*/); + } + float celledge = min(fract(xy.x), fract(xy.y)); + float majorxy = (fract(xy.x) < fract(xy.y)) ? xy.x : xy.y; + float cell = (mod(abs(floor(majorxy)), 2.0) + 1.0) / 2.0; + + gl_FragColor = v > 0.32 ? vec4(1.0, 1.0, 1.0, 1.0) : + (v > 0.3 ? vec4(v, v, v, 1.0) : + (celledge < 0.01 ? vec4(cell, cell, cell, 1.0) + : vec4(0.0, 0.0, 0.0, 0.0))); } diff --git a/res/shader/blob.vert b/res/shader/blob.vert index 6116fa3..e3a3ab8 100644 --- a/res/shader/blob.vert +++ b/res/shader/blob.vert @@ -1,5 +1,6 @@ -void main() -{ - // Transforming The Vertex +varying vec2 xy; + +void main() { gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; + xy = vec2(gl_MultiTexCoord0); } diff --git a/src/celldiv/blob.clj b/src/celldiv/blob.clj new file mode 100644 index 0000000..cede98e --- /dev/null +++ b/src/celldiv/blob.clj @@ -0,0 +1,38 @@ +(ns celldiv.blob + (:import [org.newdawn.slick.opengl.renderer Renderer SGL] + [org.lwjgl BufferUtils] + [org.lwjgl.opengl ARBShaderObjects]) + (:require [hottub.resource :as res])) + +(def minx -3) +(def maxx 3) +(def miny -3) +(def maxy 3) +(def GL (Renderer/get)) +(def blobbuf (BufferUtils/createFloatBuffer (* 4 10))) +(def emptyblob {:x 0 :y 0 :scalex 0 :scaley 0}) + +(defn with-blob-shader [f & args] + (let [shader (res/get-shader "res/shader/blob")] + (.bind shader) + (apply f shader args) + (.unbind shader))) + +(defn draw-blobs [shader blobs x y w h] + (.glBegin GL SGL/GL_QUADS) + (let [blobs (take 10 (concat blobs (cycle [emptyblob]))) + floats (flatten (map (fn [{:keys [x y scalex scaley]}] [x y scalex scaley]) blobs))] + (.rewind blobbuf) + (doseq [v floats] (.put blobbuf (float v))) + (.rewind blobbuf) + (ARBShaderObjects/glUniform4ARB (.getUniformID shader "blobs") blobbuf)) + (.glTexCoord2f GL minx miny) + (.glVertex3f GL x y 0) + (.glTexCoord2f GL minx maxy) + (.glVertex3f GL x (+ y h) 0) + (.glTexCoord2f GL maxx maxy) + (.glVertex3f GL (+ x w) (+ y h) 0) + (.glTexCoord2f GL maxx miny) + (.glVertex3f GL (+ x w) y 0) + (.glEnd GL)) + diff --git a/src/hottub/core.clj b/src/hottub/core.clj index 1d6aae9..11e6122 100644 --- a/src/hottub/core.clj +++ b/src/hottub/core.clj @@ -4,7 +4,8 @@ [hottub.resource :as res] [hottub.timeline :as tln] [hottub.screen :as scr] - [hottub.util :as u])) + [hottub.util :as u] + [celldiv.blob :as blob])) (defn -main [& args] (res/start-resource-expiry-thread) @@ -41,13 +42,12 @@ (defn render-gs [gs g] (gs/with-gs gs - (let [shader (res/get-shader "res/shader/blob")] - (.bind shader) - (.fillRect g 0 0 100 100) - (.unbind shader)) (doseq [sprite (gs/q :type :sprite)] (if-let [image (res/get-image (:image sprite))] - (.draw image (:x sprite) (:y sprite)))))) + (.draw image (:x sprite) (:y sprite)))) + (blob/with-blob-shader blob/draw-blobs + [{:x 0.3 :y 0.4 :scalex 0.2 :scaley 0.3} + {:x -0.3 :y -0.4 :scalex 0.5 :scaley 0.3}] 0 0 300 300))) (defmethod slick/update-game :game [screen inputtln delta] (if-let [input (tln/timeline-last-value inputtln)] @@ -89,7 +89,7 @@ tln (:tln subscreen) ts (tln/time-nearest tln (:ts screen)) gs (last (get tln ts))] - (render-gs gs))) + (render-gs gs graphics))) ;;; repl helpers (def ^:dynamic g nil)