Sfoglia il codice sorgente

added extra shader example.

naelstrof 7 anni fa
parent
commit
1dd26ac8fe
2 ha cambiato i file con 71 aggiunte e 0 eliminazioni
  1. 59
    0
      shaderexamples/boxzoom.frag
  2. 12
    0
      shaderexamples/boxzoom.vert

+ 59
- 0
shaderexamples/boxzoom.frag Vedi File

@@ -0,0 +1,59 @@
1
+#version 120
2
+
3
+uniform sampler2D texture;
4
+uniform sampler2D desktop;
5
+uniform vec2 screenSize;
6
+uniform vec2 mouse;
7
+
8
+varying vec2 uvCoord;
9
+
10
+void main()
11
+{
12
+    // Adjustable parameters -------
13
+    vec2 boxOffset = vec2(-64,-64);
14
+    vec2 boxSize = vec2(128,128);
15
+    float magstrength = 5;
16
+    vec2 borderSize = vec2(1,1);
17
+    vec4 borderColor = vec4(0,0,0,1);
18
+    bool crosshair = true;
19
+    //------------------------------
20
+
21
+    // actual code (don't touch unless you're brave)
22
+
23
+    // convert to UV space
24
+    boxOffset = boxOffset/screenSize;
25
+    boxSize = boxSize/screenSize;
26
+    borderSize = borderSize/screenSize;
27
+    // get mouse position in UV space
28
+    vec2 mpos = vec2(mouse.x, -mouse.y)/screenSize + vec2(0,1);
29
+    vec4 color;
30
+
31
+    // Check if our current UV is inside our box.
32
+    if ( uvCoord.x < mpos.x+boxOffset.x+boxSize.x &&
33
+         uvCoord.x > mpos.x+boxOffset.x &&
34
+         uvCoord.y > mpos.y+boxOffset.y &&
35
+         uvCoord.y < mpos.y+boxOffset.y+boxSize.y ) {
36
+      // Check if we're actually inside the crosshair area.
37
+      if ( (distance(uvCoord.x, mpos.x+boxOffset.x+boxSize.x/2) <= borderSize.x ||
38
+            distance(uvCoord.y, mpos.y+boxOffset.y+boxSize.y/2) <= borderSize.y) && crosshair ) {
39
+        color = borderColor;
40
+      } else {
41
+        // Calculate where the UV should be.
42
+        vec2 zoomedUV = ((uvCoord-mpos)-(boxOffset+boxSize/2))/magstrength+mpos;
43
+        // The desktop texture is upside-down due to X11
44
+        vec2 zoomedUVFlipped = vec2( zoomedUV.x, -zoomedUV.y );
45
+        // Then change the color to the desktop color to draw, then add on our rectangle on top.
46
+        color = texture2D( desktop, zoomedUVFlipped ) + texture2D( texture, zoomedUV );
47
+      }
48
+    // Then check if we're in our border size.
49
+    } else if( uvCoord.x <= mpos.x+boxOffset.x+boxSize.x+borderSize.x &&
50
+         uvCoord.x >= mpos.x+boxOffset.x-borderSize.x &&
51
+         uvCoord.y >= mpos.y+boxOffset.y-borderSize.y &&
52
+         uvCoord.y <= mpos.y+boxOffset.y+boxSize.y+borderSize.y ) {
53
+      color = borderColor;
54
+    } else {
55
+      color = texture2D( texture, uvCoord );
56
+    }
57
+
58
+    gl_FragColor = color;
59
+}

+ 12
- 0
shaderexamples/boxzoom.vert Vedi File

@@ -0,0 +1,12 @@
1
+#version 120
2
+
3
+attribute vec2 position;
4
+attribute vec2 uv;
5
+
6
+varying vec2 uvCoord;
7
+
8
+void main()
9
+{
10
+	uvCoord = uv;
11
+	gl_Position = vec4(position,0,1);
12
+}