Skip to content

Commit

Permalink
Added chapter 7
Browse files Browse the repository at this point in the history
  • Loading branch information
Christophe Riccio committed Jul 14, 2012
1 parent f0d261f commit 24cc2df
Show file tree
Hide file tree
Showing 150 changed files with 41,557 additions and 0 deletions.
8,643 changes: 8,643 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/GL/glext.h

Large diffs are not rendered by default.

486 changes: 486 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/GL/glfw.h

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
This software is provided under the permissive "MIT license",
as stated below. The license is officially approved by OSI as an
open source license. It is less restrictive than e.g. the GPL in
that it is compatible with closed-source commercial use without
charge and without sharing modifications you make, but you need
to give credit to the original authors wherever you use the code
or derivatives of it. Credit does not necessarily have to be
presented to every end-user of the software, but it should be
present in the source code and in "about" pop-ups, program
documentation and the like. You must also include a copy of the
license and the copyright notice below in all your distributions.

The code is specifically not public domain, and you may not claim
that you wrote it yourself, either explicitly by putting yourself
down as the sole author of derivative work, or implicitly by
removing the names of the original authors or the license terms.

You may share this code freely, but you must not sub-license it
under a different license, neither a more nor a less restrictive
one. If the MIT license does not suit your needs, you should
contact the original authors to negotiate a different license.

----

Copyright (C) 2011 by Stefan Gustavson and Ian McEwan
([email protected], [email protected])

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

----
21 changes: 21 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#Makefile for Windows mingw32, Linux and MacOSX (gcc environments)

Usage:
@echo "Usage: make Win32 | Linux | MacOSX | clean | distclean"

Win32:
gcc.exe -c shaderbench.c -o shaderbench.o -I. -IC:/Dev-Cpp/include -Wall -O3 -ffast-math -g3
gcc.exe shaderbench.o -o shaderbench.exe -L. -mwindows -lglfw -lopengl32 -lglu32 -mconsole -g3

Linux:
gcc -I. -I/usr/include shaderbench.c -lglfw -lGLU -o shaderbench

MacOSX:
./bundle.sh shaderbench
gcc -I. -I/usr/X11/include shaderbench.c -o shaderbench.app/Contents/MacOS/shaderbench -lglfw -framework Cocoa -framework OpenGL

clean:
rm -f shaderbench.o

distclean:
rm -rf shaderbench.o shaderbench shaderbench.exe shaderbench.app
30 changes: 30 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
This is the program used to benchmark various procedural shaders
for the chapter "Procedural Textures in GLSL" for the book
"OpenGL Insights" published by AK Peters in 2012.
The book is copyrighted works subject to its own license
from the publisher, but this source code is open and free,
distributed under the MIT license. See LICENSE.txt for details.

A Makefile for GCC is included. Type "make" for instructions.
Both the code and the Makefile were designed to work under
Windows, MacOS X and Linux without changes, but the Makefile
was written for GNU Make and GCC, even under Windows. If you
want to use a different compiler or a different build system,
you are on your own.

Compilation requires the free GLFW library. A GCC library
archive file "libglfw.a" is distributed with the source, as
allowed by the GLFW license. For details on GLFW, see
http://http://www.glfw.org.

When running the program, press the number keys '1' through '8'
to switch between different shaders. Shaders 1 and 2 are not
procedural, but provide a baseline for comparison. Shaders 3
through 7 are purely procedural. Shader 8 uses a texture as
input, and creates a strongly procedural pattern based on it.
The view can be manipulated by the mouse. Drag to move,
shift-drag to zoom and ctrl-drag to rotate.
The program launches with shader 8 active, to look nice.

----
Stefan Gustavson ([email protected]), 2011-10-31
10 changes: 10 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/cellular.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#version 120

varying vec2 vTexCoord2D;
varying vec3 vTexCoord3D;

void main(void) {
vTexCoord2D = gl_MultiTexCoord0.xy * 16.0;
vTexCoord3D = gl_Vertex.xyz * 8.0;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}
12 changes: 12 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/fragment1.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Procedural shading demo, Stefan Gustavson 2011.

uniform float time;
uniform sampler2D teximage;
varying vec2 dims;
varying vec2 one;
varying vec2 st;

void main( void )
{
gl_FragColor = vec4(1.0, 0.5, 0.0, 1.0);
}
12 changes: 12 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/fragment2.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Procedural shading demo, Stefan Gustavson 2011.

uniform float time;
uniform sampler2D teximage;
varying vec2 dims;
varying vec2 one;
varying vec2 st;

void main( void )
{
gl_FragColor = texture2D(teximage, st);
}
13 changes: 13 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/fragment3.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Procedural shading demo, Stefan Gustavson 2011.

uniform float time;
uniform sampler2D teximage;
varying vec2 dims;
varying vec2 one;
varying vec2 st;

void main( void )
{
float pattern = smoothstep(0.3, 0.32, length(fract(5.0*st)-0.5));
gl_FragColor = vec4(pattern.xxx, 1.0);
}
93 changes: 93 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/fragment4.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Procedural shading demo, Stefan Gustavson 2011.

uniform float time;
uniform sampler2D teximage;
varying vec2 dims;
varying vec2 one;
varying vec2 st;

// Array- and textureless GLSL 3D simplex noise.
// Author : Ian McEwan, Ashima Arts. Version: 20110822
// Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise

vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }
vec4 taylorInvSqrt(vec4 r) { return 1.792843 - 0.853735 * r; }

float snoise(vec3 v)
{
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);

// First corner
vec3 i = floor(v + dot(v, C.yyy) );
vec3 x0 = v - i + dot(i, C.xxx) ;

// Other corners
vec3 g = step(x0.yzx, x0.xyz);
vec3 l = 1.0 - g;
vec3 i1 = min( g.xyz, l.zxy );
vec3 i2 = max( g.xyz, l.zxy );

vec3 x1 = x0 - i1 + C.xxx;
vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y

// Permutations
i = mod289(i);
vec4 p = permute( permute( permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));

// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float n_ = 0.142857142857; // 1.0/7.0
vec3 ns = n_ * D.wyz - D.xzx;

vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)

vec4 x_ = floor(j * ns.z);
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)

vec4 x = x_ *ns.x + ns.yyyy;
vec4 y = y_ *ns.x + ns.yyyy;
vec4 h = 1.0 - abs(x) - abs(y);

vec4 b0 = vec4( x.xy, y.xy );
vec4 b1 = vec4( x.zw, y.zw );

vec4 s0 = floor(b0)*2.0 + 1.0;
vec4 s1 = floor(b1)*2.0 + 1.0;
vec4 sh = -step(h, vec4(0.0));

vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;

vec3 p0 = vec3(a0.xy,h.x);
vec3 p1 = vec3(a0.zw,h.y);
vec3 p2 = vec3(a1.xy,h.z);
vec3 p3 = vec3(a1.zw,h.w);

//Normalise gradients
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;

// Mix final noise value
vec4 m = max(0.51 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
m = m * m;
return 93.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
dot(p2,x2), dot(p3,x3) ) );
}

void main( void )
{
float n = snoise(vec3(10.0*st.s,10.0*st.t,60.0));
gl_FragColor = vec4(0.5*n.xxx + 0.5, 1.0);
}
97 changes: 97 additions & 0 deletions Chapter 07 Procedural Textures in GLSL/demo1/fragment5.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Procedural shading demo, Stefan Gustavson 2011.

uniform float time;
uniform sampler2D teximage;
varying vec2 dims;
varying vec2 one;
varying vec2 st;

// Array- and textureless GLSL 3D simplex noise.
// Author : Ian McEwan, Ashima Arts. Version: 20110822
// Copyright (C) 2011 Ashima Arts. All rights reserved.
// Distributed under the MIT License. See LICENSE file.
// https://github.com/ashima/webgl-noise

vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }
vec4 taylorInvSqrt(vec4 r) { return 1.792843 - 0.853735 * r; }

float snoise(vec3 v)
{
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);

// First corner
vec3 i = floor(v + dot(v, C.yyy) );
vec3 x0 = v - i + dot(i, C.xxx) ;

// Other corners
vec3 g = step(x0.yzx, x0.xyz);
vec3 l = 1.0 - g;
vec3 i1 = min( g.xyz, l.zxy );
vec3 i2 = max( g.xyz, l.zxy );

vec3 x1 = x0 - i1 + C.xxx;
vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y

// Permutations
i = mod289(i);
vec4 p = permute( permute( permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));

// Gradients: 7x7 points over a square, mapped onto an octahedron.
// The ring size 17*17 = 289 is close to a multiple of 49 (49*6 = 294)
float n_ = 0.142857142857; // 1.0/7.0
vec3 ns = n_ * D.wyz - D.xzx;

vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)

vec4 x_ = floor(j * ns.z);
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)

vec4 x = x_ *ns.x + ns.yyyy;
vec4 y = y_ *ns.x + ns.yyyy;
vec4 h = 1.0 - abs(x) - abs(y);

vec4 b0 = vec4( x.xy, y.xy );
vec4 b1 = vec4( x.zw, y.zw );

vec4 s0 = floor(b0)*2.0 + 1.0;
vec4 s1 = floor(b1)*2.0 + 1.0;
vec4 sh = -step(h, vec4(0.0));

vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;

vec3 p0 = vec3(a0.xy,h.x);
vec3 p1 = vec3(a0.zw,h.y);
vec3 p2 = vec3(a1.xy,h.z);
vec3 p3 = vec3(a1.zw,h.w);

//Normalise gradients
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;

// Mix final noise value
vec4 m = max(0.51 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
m = m * m;
return 93.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
dot(p2,x2), dot(p3,x3) ) );
}

void main( void )
{
float n = snoise(vec3(5.0*st,50.0));
n += 0.5*snoise(vec3(10.0*st,60.0));
n += 0.25*snoise(vec3(20.0*st,70.0));
n += 0.125*snoise(vec3(40.0*st,80.0));
n += 0.0625*snoise(vec3(80.0*st,90.0));
gl_FragColor = vec4(0.4*vec3(n) + 0.5, 1.0);
}
Loading

0 comments on commit 24cc2df

Please sign in to comment.