├── .gitignore ├── SphereTracing.pdf ├── README.md ├── Makefile ├── util.h ├── sphere-tracing.c ├── util.c ├── sphere-tracing.glsl └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | sphere-tracing 2 | *.o 3 | *~ 4 | -------------------------------------------------------------------------------- /SphereTracing.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/larsbrinkhoff/glsl-sphere-tracing/HEAD/SphereTracing.pdf -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | sphere-tracing.qlsl is a spinning twisting rounded cube. 2 | 3 | See [SphereTracing.pdf](SphereTracing.pdf) for the algorithm. 4 | 5 | ![](https://raw.githubusercontent.com/larsbrinkhoff/glsl-sphere-tracing/gif/cube.gif) 6 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CC = gcc 2 | CFLAGS = -std=c89 -pedantic -Wall 3 | LDFLAGS = -L/usr/lib64/nvidia -L/usr/X11R6/lib -lGL -lGLU -lglut -lm 4 | 5 | .PHONY: all 6 | all: sphere-tracing 7 | 8 | sphere-tracing: sphere-tracing.o util.o 9 | $(CC) -o $@ $^ $(LDFLAGS) 10 | 11 | .PHONY: clean 12 | clean: 13 | rm -f *.o sphere-tracing 14 | -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | #ifndef _UTIL_H_ 2 | #define _UTIL_H_ 3 | 4 | unsigned int setup_shader(const char *fname); 5 | void set_uniform1f(unsigned int prog, const char *name, float val); 6 | void set_uniform2f(unsigned int prog, const char *name, float v1, float v2); 7 | void set_uniform4f(unsigned int prog, const char *name, float v1, float v2, float v3, float v4); 8 | void set_uniform1i(unsigned int prog, const char *name, int val); 9 | 10 | #endif /* _UTIL_H_ */ 11 | 12 | -------------------------------------------------------------------------------- /sphere-tracing.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "util.h" 5 | 6 | void draw(void); 7 | void idle_handler(void); 8 | void key_handler(unsigned char key, int x, int y); 9 | 10 | unsigned int prog; 11 | 12 | int main(int argc, char **argv) { 13 | /* initialize glut */ 14 | glutInitWindowSize(800, 600); 15 | 16 | glutInit(&argc, argv); 17 | glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE); 18 | glutCreateWindow("Sphere Tracing"); 19 | 20 | glutDisplayFunc(draw); 21 | glutIdleFunc(idle_handler); 22 | glutKeyboardFunc(key_handler); 23 | 24 | /* load and set the mandelbrot shader */ 25 | if(!(prog = setup_shader("sphere-tracing.glsl"))) { 26 | return EXIT_FAILURE; 27 | } 28 | 29 | glutPostRedisplay(); 30 | glutMainLoop(); 31 | return 0; 32 | } 33 | 34 | int frames = 0; 35 | float last_time = 0; 36 | 37 | void draw(void) { 38 | float t = glutGet(GLUT_ELAPSED_TIME) / 1000.0 - last_time; 39 | frames++; 40 | if (t > 10) { 41 | printf ("FPS = %f\n", frames / t); 42 | frames = 0; 43 | last_time += t; 44 | } 45 | 46 | set_uniform1f(prog, "time", glutGet(GLUT_ELAPSED_TIME)/10000.0); 47 | 48 | glBegin(GL_QUADS); 49 | glTexCoord2f(0, 0); 50 | glVertex2f(-1, -1); 51 | glTexCoord2f(1, 0); 52 | glVertex2f(1, -1); 53 | glTexCoord2f(1, 1); 54 | glVertex2f(1, 1); 55 | glTexCoord2f(0, 1); 56 | glVertex2f(-1, 1); 57 | glEnd(); 58 | 59 | glutSwapBuffers(); 60 | } 61 | 62 | void idle_handler(void) { 63 | glutPostRedisplay(); 64 | } 65 | 66 | void key_handler(unsigned char key, int x, int y) { 67 | switch(key) { 68 | case 27: 69 | case 'q': 70 | case 'Q': 71 | exit(0); 72 | } 73 | glutPostRedisplay(); 74 | } 75 | -------------------------------------------------------------------------------- /util.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #ifdef __unix__ 6 | GLhandleARB glCreateShaderObjectARB(GLenum); 7 | void glShaderSourceARB(GLhandleARB, int, const char**, int*); 8 | void glCompileShaderARB(GLhandleARB); 9 | GLhandleARB glCreateProgramObjectARB(void); 10 | void glAttachObjectARB(GLhandleARB, GLhandleARB); 11 | void glLinkProgramARB(GLhandleARB); 12 | void glUseProgramObjectARB(GLhandleARB); 13 | void glGetInfoLogARB(GLhandleARB, GLsizei, GLsizei*, GLcharARB*); 14 | void glGetObjectParameterivARB(GLhandleARB, GLenum, int*); 15 | GLint glGetUniformLocationARB(GLhandleARB, const char*); 16 | void glUniform1f(GLint location, GLfloat v0); 17 | #endif 18 | 19 | unsigned int setup_shader(const char *fname) { 20 | FILE *fp; 21 | unsigned int prog, sdr, len; 22 | char *src_buf; 23 | int success, linked; 24 | 25 | if(!(fp = fopen(fname, "r"))) { 26 | fprintf(stderr, "failed to open shader: %s\n", fname); 27 | return 0; 28 | } 29 | fseek(fp, 0, SEEK_END); 30 | len = ftell(fp); 31 | fseek(fp, 0, SEEK_SET); 32 | src_buf = malloc(len + 1); 33 | 34 | fread(src_buf, 1, len, fp); 35 | src_buf[len] = 0; 36 | 37 | sdr = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); 38 | glShaderSourceARB(sdr, 1, (const char**)&src_buf, 0); 39 | free(src_buf); 40 | 41 | glCompileShaderARB(sdr); 42 | glGetObjectParameterivARB(sdr, GL_OBJECT_COMPILE_STATUS_ARB, &success); 43 | if(!success) { 44 | int info_len; 45 | char *info_log; 46 | 47 | glGetObjectParameterivARB(sdr, GL_OBJECT_INFO_LOG_LENGTH_ARB, &info_len); 48 | if(info_len > 0) { 49 | if(!(info_log = malloc(info_len + 1))) { 50 | perror("malloc failed"); 51 | return 0; 52 | } 53 | glGetInfoLogARB(sdr, info_len, 0, info_log); 54 | fprintf(stderr, "shader compilation failed: %s\n", info_log); 55 | free(info_log); 56 | } else { 57 | fprintf(stderr, "shader compilation failed\n"); 58 | } 59 | return 0; 60 | } 61 | 62 | prog = glCreateProgramObjectARB(); 63 | glAttachObjectARB(prog, sdr); 64 | glLinkProgramARB(prog); 65 | glGetObjectParameterivARB(prog, GL_OBJECT_LINK_STATUS_ARB, &linked); 66 | if(!linked) { 67 | fprintf(stderr, "shader linking failed\n"); 68 | return 0; 69 | } 70 | 71 | glUseProgramObjectARB(prog); 72 | return prog; 73 | } 74 | 75 | void set_uniform1f(unsigned int prog, const char *name, float val) { 76 | int loc = glGetUniformLocationARB(prog, name); 77 | if(loc != -1) { 78 | glUniform1f(loc, val); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /sphere-tracing.glsl: -------------------------------------------------------------------------------- 1 | #version 130 2 | 3 | const vec2 resolution = vec2(800.0, 600.0); 4 | const int max_iter = 10; 5 | const float bailout = 4.0; 6 | uniform float time; 7 | 8 | vec4 cccc; 9 | mat4 transform; 10 | mat4 inverse_transform; 11 | 12 | vec4 qmul (const vec4 q1, const vec4 q2) 13 | { 14 | vec4 r; 15 | r.x = q1.x*q2.x - dot(q1.yzw, q2.yzw); 16 | r.yzw = q1.x*q2.yzw + q2.x*q1.yzw + cross(q1.yzw, q2.yzw); 17 | return r; 18 | } 19 | 20 | vec4 qsqr (const vec4 q) 21 | { 22 | vec4 r; 23 | r.x = q.x*q.x - dot(q.yzw, q.yzw); 24 | r.yzw = 2.0*q.x*q.yzw; 25 | return r; 26 | } 27 | 28 | mat4 rotate_xz(float x) 29 | { 30 | return mat4(cos(x), 0.0, -sin(x), 0.0, 31 | 0.0, 1.0, 0.0, 0.0, 32 | sin(x), 0.0, cos(x), 0.0, 33 | 0.0, 0.0, 0.0, 1.0); 34 | } 35 | 36 | mat4 rotate_xy(float x) 37 | { 38 | return mat4(cos(x), -sin(x), 0.0, 0.0, 39 | sin(x), cos(x), 0.0, 0.0, 40 | 0.0, 0.0, 1.0, 0.0, 41 | 0.0, 0.0, 0.0, 1.0); 42 | } 43 | 44 | vec3 rotate(const vec3 p, mat4 m) 45 | { 46 | return vec3(m * vec4(p, 1.0)); 47 | } 48 | 49 | vec3 calcNormal(in vec3 p) 50 | { 51 | vec3 normal; 52 | 53 | vec4 c = cccc; 54 | 55 | vec4 nz,ndz,dz[4]; 56 | 57 | vec4 z=vec4(p,0.0); //(c.y+c.x)*.3); 58 | 59 | dz[0]=vec4(1.0,0.0,0.0,0.0); 60 | dz[1]=vec4(0.0,1.0,0.0,0.0); 61 | dz[2]=vec4(0.0,0.0,1.0,0.0); 62 | //dz[3]=vec4(0.0,0.0,0.0,1.0); 63 | 64 | for(int i=0;i4.0) 79 | break; 80 | } 81 | 82 | normal = vec3(dot(z,dz[0]),dot(z,dz[1]),dot(z,dz[2])); 83 | return normal; 84 | } 85 | 86 | float jinteresct(in vec3 rO, in vec3 rD, out float ao, out vec3 normal) 87 | { 88 | float mz2,md2,dist,t; 89 | float res=1000.0; 90 | vec4 z; 91 | vec4 dz; 92 | 93 | vec4 c = cccc; 94 | 95 | ao = 0.0; 96 | 97 | for(t=0.0;t<50.0;t+=dist) 98 | { 99 | ao += 1.0; 100 | vec3 p=rO+t*rD; 101 | 102 | p = rotate(p, transform); 103 | #if 0 104 | p.xy = mod(p.xy + 2.0, 4.0) - 2.0; 105 | //p.z = mod(p.z + 30.0, 60.0) - 30.0; 106 | #endif 107 | 108 | #if 0 109 | // torus 110 | dist = length(vec2(length(p.xy) - 1.0, p.z)) - 0.4; 111 | 112 | #define COMPUTE_NORMAL { \ 113 | vec2 q = p.xy; \ 114 | q *= 1.0 / length(q); \ 115 | normal = p - vec3(q,0.0); } 116 | #elif 0 117 | // pipe 118 | dist = 0.0; 119 | dist = max(dist, length(p.xy) - 1.0); 120 | dist = max(dist, -length(p.xy - 0.1) + 0.6); 121 | dist = max(dist, -1.0-p.z); 122 | dist = max(dist, -1.0+p.z); 123 | 124 | #define COMPUTE_NORMAL { \ 125 | if (abs(length(p.xy) - 1.0) < 0.001) \ 126 | normal = vec3(p.xy,0.0); \ 127 | else if (abs(-length(p.xy - 0.1) + 0.6) < 0.001) \ 128 | normal = -vec3(p.xy-0.1,0.0); \ 129 | else if (abs(-1.0-p.z) < 0.001) \ 130 | normal = vec3(0.0, 0.0, -1.0); \ 131 | else if (abs(-1.0+p.z) < 0.001) \ 132 | normal = vec3(0.0, 0.0, 1.0); \ 133 | } 134 | #elif 1 135 | // supersphere 136 | float k = 10.0; 137 | vec3 pk = pow(abs(p), vec3(k)); 138 | dist = pow(pk.x + pk.y + pk.z, 1.0/k) - 0.8; 139 | #define COMPUTE_NORMAL normal = pk / p 140 | #elif 0 141 | // Sierpinski tetrahedron 142 | int n = 3; 143 | normal = p; 144 | for (int i = 0; i < n; i++) { 145 | if (p.x+p.y < 0.0) 146 | {p.xy = -p.yx; normal.xy=-normal.yx;} 147 | if (p.x+p.z < 0.0) 148 | {p.xz = -p.zx; normal.xz=-normal.zx;} 149 | if (p.y+p.z < 0.0) 150 | {p.yz = -p.zy; normal.yz=-normal.zy;} 151 | p = p*2.0 - (2.0-1.0); 152 | normal = 2.0*normal - 1.0; 153 | if (dot(p,p) > 1e6) 154 | break; 155 | } 156 | dist = (length(p)-2.0) * pow(2.0, -float(n)); 157 | normal = normal; 158 | #else 159 | // quaternion julia set 160 | z=vec4(p,0.0); //(c.y+c.x)*.3); 161 | md2=1.0; 162 | //dz = vec4(1.0,0.0,0.0,0.0); 163 | mz2=dot(z,z); 164 | 165 | for(int i=0;i 4*|dz|^2 168 | md2*=4.0*mz2; 169 | //dz = 2.0 * qmul(dz, z) + vec4(1.0,0.0,0.0,0.0); 170 | // z -> z2 + c 171 | z = qsqr(z) + c; 172 | 173 | mz2=dot(z,z); 174 | if(mz2>bailout) 175 | break; 176 | } 177 | 178 | //float md2 = dot(dz,dz); 179 | dist=0.2*sqrt(mz2/md2)*log(mz2); 180 | normal = calcNormal(p); 181 | #endif 182 | 183 | if(dist<0.0001) 184 | { 185 | COMPUTE_NORMAL; 186 | res=t; 187 | break; 188 | } 189 | } 190 | 191 | normal = rotate(normalize(normal), inverse_transform); 192 | return res; 193 | } 194 | 195 | vec3 trace(vec3 pos, vec3 dir) 196 | { 197 | vec3 color = vec3(0.0); 198 | vec3 w = vec3(1.0); 199 | 200 | for (int i = 0; i < 10; i++) { 201 | float ao; 202 | vec3 nor; 203 | float t = jinteresct(pos,dir,ao,nor); 204 | if(t<1000.0) 205 | { 206 | vec3 inter = pos + t*dir*.99; 207 | #if 0 208 | if (inter.z < -0.5) { 209 | //return vec3(0.0,1.0,0.0); 210 | inter.z = -1.0; 211 | //nor = vec3(0.0,0.0,1.0); 212 | } 213 | #endif 214 | 215 | vec3 light = normalize(vec3(1.0,5.0,-3.0)); 216 | float dif = .5 + .5*dot( nor, light); //vec3(0.57703) ); 217 | ao = max( 1.0-ao*0.005, 0.0 ); 218 | 219 | #if 0 220 | color = vec3(1.0,.9,.5)*dif + .5*vec3(.6,.7,.8); 221 | color *= ao; 222 | return color; 223 | #endif 224 | 225 | pos = inter; 226 | dir = reflect(dir, nor); 227 | w *= vec3(.9, .88, .85); 228 | } 229 | else 230 | { 231 | float t2 = (pos.y - 100.0) / dir.y; 232 | if (t2 > 0.0) { 233 | vec3 p = pos + t2*dir*.99; 234 | p = rotate (p, rotate_xz(-0.2 * time)); 235 | color = vec3(.5 * smoothstep(sin(p.x / 30.0) * sin(p.z / 30.0), 0.0, 1.0) + .5); 236 | } else { 237 | //color = vec3(0.5,0.51,0.52)+vec3(0.5,0.47,0.45)*p.y; 238 | color = vec3(0.5,0.51,0.52)+vec3(0.5,0.47,0.45)*dir.y; 239 | } 240 | return w * color; 241 | }} 242 | 243 | return vec3(1.0,.2,.2); 244 | } 245 | 246 | mat4 inverse (mat4 a) 247 | { 248 | float s0 = a[0][0] * a[1][1] - a[1][0] * a[0][1]; 249 | float s1 = a[0][0] * a[1][2] - a[1][0] * a[0][2]; 250 | float s2 = a[0][0] * a[1][3] - a[1][0] * a[0][3]; 251 | float s3 = a[0][1] * a[1][2] - a[1][1] * a[0][2]; 252 | float s4 = a[0][1] * a[1][3] - a[1][1] * a[0][3]; 253 | float s5 = a[0][2] * a[1][3] - a[1][2] * a[0][3]; 254 | 255 | float c5 = a[2][2] * a[3][3] - a[3][2] * a[2][3]; 256 | float c4 = a[2][1] * a[3][3] - a[3][1] * a[2][3]; 257 | float c3 = a[2][1] * a[3][2] - a[3][1] * a[2][2]; 258 | float c2 = a[2][0] * a[3][3] - a[3][0] * a[2][3]; 259 | float c1 = a[2][0] * a[3][2] - a[3][0] * a[2][2]; 260 | float c0 = a[2][0] * a[3][1] - a[3][0] * a[2][1]; 261 | 262 | // Should check for 0 determinant 263 | float invdet = 1.0 / (s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0); 264 | 265 | mat4 b; 266 | 267 | b[0][0] = ( a[1][1] * c5 - a[1][2] * c4 + a[1][3] * c3) * invdet; 268 | b[0][1] = (-a[0][1] * c5 + a[0][2] * c4 - a[0][3] * c3) * invdet; 269 | b[0][2] = ( a[3][1] * s5 - a[3][2] * s4 + a[3][3] * s3) * invdet; 270 | b[0][3] = (-a[2][1] * s5 + a[2][2] * s4 - a[2][3] * s3) * invdet; 271 | 272 | b[1][0] = (-a[1][0] * c5 + a[1][2] * c2 - a[1][3] * c1) * invdet; 273 | b[1][1] = ( a[0][0] * c5 - a[0][2] * c2 + a[0][3] * c1) * invdet; 274 | b[1][2] = (-a[3][0] * s5 + a[3][2] * s2 - a[3][3] * s1) * invdet; 275 | b[1][3] = ( a[2][0] * s5 - a[2][2] * s2 + a[2][3] * s1) * invdet; 276 | 277 | b[2][0] = ( a[1][0] * c4 - a[1][1] * c2 + a[1][3] * c0) * invdet; 278 | b[2][1] = (-a[0][0] * c4 + a[0][1] * c2 - a[0][3] * c0) * invdet; 279 | b[2][2] = ( a[3][0] * s4 - a[3][1] * s2 + a[3][3] * s0) * invdet; 280 | b[2][3] = (-a[2][0] * s4 + a[2][1] * s2 - a[2][3] * s0) * invdet; 281 | 282 | b[3][0] = (-a[1][0] * c3 + a[1][1] * c1 - a[1][2] * c0) * invdet; 283 | b[3][1] = ( a[0][0] * c3 - a[0][1] * c1 + a[0][2] * c0) * invdet; 284 | b[3][2] = (-a[3][0] * s3 + a[3][1] * s1 - a[3][2] * s0) * invdet; 285 | b[3][3] = ( a[2][0] * s3 - a[2][1] * s1 + a[2][2] * s0) * invdet; 286 | 287 | return b; 288 | } 289 | 290 | void main(void) 291 | { 292 | vec2 p = vec2(8.0/5,6.0/5) * (gl_FragCoord.xy / resolution.xy - .5); 293 | //cccc = vec4( .7*cos(.5*time), .7*sin(.3*time), .7*cos(1.0*time), .7*cos(2.0*time) ); 294 | cccc = vec4( .7*cos(2.5*time), .7*sin(1.3*time), 0.0, 0.0); 295 | vec3 edir = normalize(vec3(p,1.0)); 296 | vec3 wori = vec3(0.0,0.0,-3.0); 297 | 298 | //cccc = vec4(-.5, .1, 0.0, 0.0); 299 | //cccc = vec4(-1.47409435510635, -0.00043243408203125, 0.00043243408203125, 0.0); 300 | //cccc = vec4(-0.817795, -0.2000035, 0.0, 0.0); 301 | //cccc = vec4(-0.817795, -0.2000035, -0.2000035, 0.0); 302 | //cccc = vec4(-0.75, 0.11, 0.0, 0.0); 303 | 304 | transform = 305 | rotate_xy(2.0*time) * 306 | rotate_xz(3.0*time + 1.5*p.y*cos(10.0*time)*sin(time)) * 307 | rotate_xy(0.5*time); 308 | inverse_transform = inverse(transform); 309 | 310 | vec3 color = trace(wori, edir); 311 | gl_FragColor = vec4(color,1.0); 312 | } 313 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 6 | 7 | Everyone is permitted to copy and distribute verbatim copies of this 8 | license document, but changing it is not allowed. 9 | 10 | Preamble 11 | 12 | The licenses for most software are designed to take away your freedom 13 | to share and change it. By contrast, the GNU General Public License is 14 | intended to guarantee your freedom to share and change free software 15 | --to make sure the software is free for all its users. This General 16 | Public License applies to most of the Free Software Foundation's 17 | software and to any other program whose authors commit to using it. 18 | (Some other Free Software Foundation software is covered by the GNU 19 | Lesser General Public License instead.) You can apply it to your 20 | programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not price. 23 | Our General Public Licenses are designed to make sure that you have the 24 | freedom to distribute copies of free software (and charge for this 25 | service if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs; and that you know you can do these things. 28 | 29 | To protect your rights, we need to make restrictions that forbid anyone 30 | to deny you these rights or to ask you to surrender the rights. These 31 | restrictions translate to certain responsibilities for you if you 32 | distribute copies of the software, or if you modify it. 33 | 34 | For example, if you distribute copies of such a program, whether gratis 35 | or for a fee, you must give the recipients all the rights that you 36 | have. You must make sure that they, too, receive or can get the source 37 | code. And you must show them these terms so they know their rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software patents. 51 | We wish to avoid the danger that redistributors of a free program will 52 | individually obtain patent licenses, in effect making the program 53 | proprietary. To prevent this, we have made it clear that any patent 54 | must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 60 | 61 | 0. This License applies to any program or other work which contains a 62 | notice placed by the copyright holder saying it may be distributed 63 | under the terms of this General Public License. The "Program", below, 64 | refers to any such program or work, and a "work based on the Program" 65 | means either the Program or any derivative work under copyright law: 66 | that is to say, a work containing the Program or a portion of it, 67 | either verbatim or with modifications and/or translated into another 68 | language. (Hereinafter, translation is included without limitation in 69 | the term "modification".) Each licensee is addressed as "you". 70 | 71 | Activities other than copying, distribution and modification are not 72 | covered by this License; they are outside its scope. The act of running 73 | the Program is not restricted, and the output from the Program is 74 | covered only if its contents constitute a work based on the Program 75 | (independent of having been made by running the Program). Whether that 76 | is true depends on what the Program does. 77 | 78 | 1. You may copy and distribute verbatim copies of the Program's source 79 | code as you receive it, in any medium, provided that you conspicuously 80 | and appropriately publish on each copy an appropriate copyright notice 81 | and disclaimer of warranty; keep intact all the notices that refer to 82 | this License and to the absence of any warranty; and give any other 83 | recipients of the Program a copy of this License along with the 84 | Program. 85 | 86 | You may charge a fee for the physical act of transferring a copy, and 87 | you may at your option offer warranty protection in exchange for a fee. 88 | 89 | 2. You may modify your copy or copies of the Program or any portion of 90 | it, thus forming a work based on the Program, and copy and distribute 91 | such modifications or work under the terms of Section 1 above, provided 92 | that you also meet all of these conditions: 93 | 94 | a) You must cause the modified files to carry prominent notices 95 | stating that you changed the files and the date of any change. 96 | 97 | b) You must cause any work that you distribute or publish, that in 98 | whole or in part contains or is derived from the Program or any 99 | part thereof, to be licensed as a whole at no charge to all third 100 | parties under the terms of this License. 101 | 102 | c) If the modified program normally reads commands interactively 103 | when run, you must cause it, when started running for such 104 | interactive use in the most ordinary way, to print or display an 105 | announcement including an appropriate copyright notice and a 106 | notice that there is no warranty (or else, saying that you provide 107 | a warranty) and that users may redistribute the program under 108 | these conditions, and telling the user how to view a copy of this 109 | License. (Exception: if the Program itself is interactive but does 110 | not normally print such an announcement, your work based on the 111 | Program is not required to print an announcement.) 112 | 113 | These requirements apply to the modified work as a whole. If 114 | identifiable sections of that work are not derived from the Program, 115 | and can be reasonably considered independent and separate works in 116 | themselves, then this License, and its terms, do not apply to those 117 | sections when you distribute them as separate works. But when you 118 | distribute the same sections as part of a whole which is a work based 119 | on the Program, the distribution of the whole must be on the terms of 120 | this License, whose permissions for other licensees extend to the 121 | entire whole, and thus to each and every part regardless of who wrote 122 | it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of a 131 | storage or distribution medium does not bring the other work under the 132 | scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software 141 | interchange; or, 142 | 143 | b) Accompany it with a written offer, valid for at least three 144 | years, to give any third party, for a charge no more than your cost 145 | of physically performing source distribution, a complete 146 | machine-readable copy of the corresponding source code, to be 147 | distributed under the terms of Sections 1 and 2 above on a medium 148 | customarily used for software interchange; or, 149 | 150 | c) Accompany it with the information you received as to the offer 151 | to distribute corresponding source code. (This alternative is 152 | allowed only for noncommercial distribution and only if you 153 | received the program in object code or executable form with such 154 | an offer, in accord with Subsection b above.) 155 | 156 | The source code for a work means the preferred form of the work for 157 | making modifications to it. For an executable work, complete source 158 | code means all the source code for all modules it contains, plus any 159 | associated interface definition files, plus the scripts used to control 160 | compilation and installation of the executable. However, as a special 161 | exception, the source code distributed need not include anything that 162 | is normally distributed (in either source or binary form) with the 163 | major components (compiler, kernel, and so on) of the operating system 164 | on which the executable runs, unless that component itself accompanies 165 | the executable. 166 | 167 | If distribution of executable or object code is made by offering access 168 | to copy from a designated place, then offering equivalent access to 169 | copy the source code from the same place counts as distribution of the 170 | source code, even though third parties are not compelled to copy the 171 | source along with the object code. 172 | 173 | 4. You may not copy, modify, sublicense, or distribute the Program 174 | except as expressly provided under this License. Any attempt otherwise 175 | to copy, modify, sublicense or distribute the Program is void, and will 176 | automatically terminate your rights under this License. However, 177 | parties who have received copies, or rights, from you under this License 178 | will not have their licenses terminated so long as such parties remain 179 | in full compliance. 180 | 181 | 5. You are not required to accept this License, since you have not 182 | signed it. However, nothing else grants you permission to modify or 183 | distribute the Program or its derivative works. These actions are 184 | prohibited by law if you do not accept this License. Therefore, by 185 | modifying or distributing the Program (or any work based on the 186 | Program), you indicate your acceptance of this License to do so, and 187 | all its terms and conditions for copying, distributing or modifying 188 | the Program or works based on it. 189 | 190 | 6. Each time you redistribute the Program (or any work based on the 191 | Program), the recipient automatically receives a license from the 192 | original licensor to copy, distribute or modify the Program subject to 193 | these terms and conditions. You may not impose any further restrictions 194 | on the recipients' exercise of the rights granted herein. You are not 195 | responsible for enforcing compliance by third parties to this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent license 205 | would not permit royalty-free redistribution of the Program by all 206 | those who receive copies directly or indirectly through you, then the 207 | only way you could satisfy both it and this License would be to refrain 208 | entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License may 232 | add an explicit geographical distribution limitation excluding those 233 | countries, so that distribution is permitted only in or among countries 234 | not thus excluded. In such case, this License incorporates the limitation 235 | as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail 240 | to address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and 245 | conditions either of that version or of any later version published by 246 | the Free Software Foundation. If the Program does not specify a version 247 | number of this License, you may choose any version ever published by 248 | the Free Software Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the 252 | author to ask for permission. For software which is copyrighted by 253 | the Free Software Foundation, write to the Free Software Foundation; 254 | we sometimes make exceptions for this. Our decision will be guided by 255 | the two goals of preserving the free status of all derivatives of our 256 | free software and of promoting the sharing and reuse of software 257 | generally. 258 | 259 | NO WARRANTY 260 | 261 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO 262 | WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 263 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 264 | OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, 265 | EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 266 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE 267 | ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH 268 | YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL 269 | NECESSARY SERVICING, REPAIR OR CORRECTION. 270 | 271 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 272 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY 273 | MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE 274 | TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 275 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 276 | PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 277 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 278 | FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF 279 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 280 | DAMAGES. 281 | 282 | END OF TERMS AND CONDITIONS 283 | --------------------------------------------------------------------------------