Mercurial > projects > ldc
annotate demos/ray.d @ 100:5071469303d4 trunk
[svn r104] TONS OF FIXES.
Split up declaration, constant initializer gen and definition for globals, structs, classes and functions.
Improved ClassInfo support (not complete), not in vtable yet.
Fixed a bunch of forward reference problems.
Much more. Major commit! :)
author | lindquist |
---|---|
date | Fri, 16 Nov 2007 08:21:47 +0100 |
parents | ce7ed8f59b99 |
children | 9922b9982552 |
rev | line source |
---|---|
100 | 1 import std.stdio; |
92 | 2 |
3 int atoi(char[] s) { | |
4 int i, fac=1; | |
5 bool neg = (s.length) && (s[0] == '-'); | |
6 char[] a = neg ? s[1..$] : s; | |
7 foreach_reverse(c; a) { | |
8 i += (c-'0') * fac; | |
9 fac *= 10; | |
10 } | |
11 return !neg ? i : -i; | |
12 } | |
13 | |
14 pragma(LLVM_internal, "intrinsic", "llvm.sqrt.f64") | |
15 double sqrt(double val); | |
16 | |
17 double delta; | |
18 static this() { delta=sqrt(real.epsilon); } | |
19 | |
20 struct Vec { | |
21 double x, y, z; | |
22 Vec opAdd(ref Vec other) { return Vec(x+other.x, y+other.y, z+other.z); } | |
23 Vec opSub(ref Vec other) { return Vec(x-other.x, y-other.y, z-other.z); } | |
24 Vec opMul(double a) { return Vec(x*a, y*a, z*a); } | |
25 double dot(ref Vec other) { return x*other.x+y*other.y+z*other.z; } | |
26 Vec unitise() { return opMul(1.0/sqrt(dot(*this))); } | |
27 } | |
28 | |
29 struct Pair(T, U) { T first; U second; } | |
30 typedef Pair!(double, Vec) Hit; | |
31 | |
32 struct Ray { Vec orig, dir; } | |
33 | |
34 class Scene { | |
35 //abstract void intersect(ref Hit, ref Ray); | |
36 void intersect(ref Hit, ref Ray) {} | |
37 } | |
38 | |
39 class Sphere : Scene { | |
40 Vec center; | |
41 double radius; | |
42 //mixin This!("center, radius"); | |
43 this(ref Vec c, double r) | |
44 { | |
45 center = c; | |
46 radius = r; | |
47 } | |
48 double ray_sphere(ref Ray ray) { | |
49 auto v = center - ray.orig, b = v.dot(ray.dir), disc=b*b - v.dot(v) + radius*radius; | |
50 if (disc < 0) return double.infinity; | |
51 auto d = sqrt(disc), t2 = b + d; | |
52 if (t2 < 0) return double.infinity; | |
53 auto t1 = b - d; | |
54 return (t1 > 0 ? t1 : t2); | |
55 } | |
56 void intersect(ref Hit hit, ref Ray ray) { | |
57 auto lambda = ray_sphere(ray); | |
58 if (lambda < hit.first) | |
59 hit = Hit(lambda, (ray.orig + lambda*ray.dir - center).unitise); | |
60 } | |
61 } | |
62 | |
63 class Group : Scene { | |
64 Sphere bound; | |
65 Scene[] children; | |
66 //mixin This!("bound, children"); | |
67 this (Sphere s, Scene[] c) | |
68 { | |
69 bound = s; | |
70 children = c; | |
71 } | |
72 void intersect(ref Hit hit, ref Ray ray) { | |
73 auto l = bound.ray_sphere(ray); | |
74 if (l < hit.first) foreach (child; children) child.intersect(hit, ray); | |
75 } | |
76 } | |
77 | |
78 double ray_trace(ref Vec light, ref Ray ray, Scene s) { | |
79 auto hit=Hit(double.infinity, Vec(0, 0, 0)); | |
80 s.intersect(hit, ray); | |
81 if (hit.first == double.infinity) return 0.0; | |
82 auto g = hit.second.dot(light); | |
83 if (g >= 0) return 0.0; | |
84 auto p = ray.orig + ray.dir*hit.first + hit.second*delta; | |
85 auto hit2=Hit(double.infinity, Vec(0, 0, 0)); | |
86 s.intersect(hit2, Ray(p, light*-1.0)); | |
87 return (hit2.first < double.infinity ? 0 : -g); | |
88 } | |
89 | |
90 Scene create(int level, ref Vec c, double r) { | |
91 auto s = new Sphere(c, r); | |
92 if (level == 1) return s; | |
94
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
92
diff
changeset
|
93 Scene[] children; |
61615fa85940
[svn r98] Added support for std.c.stdlib.alloca via pragma(LLVM_internal, "alloca").
lindquist
parents:
92
diff
changeset
|
94 children ~= s; |
92 | 95 double rn = 3*r/sqrt(12.0); |
96 for (int dz=-1; dz<=1; dz+=2) | |
97 for (int dx=-1; dx<=1; dx+=2) | |
98 children~=create(level-1, c + Vec(dx, 1, dz)*rn, r/2); | |
99 return new Group(new Sphere(c, 3*r), children); | |
100 } | |
101 | |
102 void main(string[] args) { | |
103 int level = (args.length==3 ? args[1].atoi() : 9), | |
104 n = (args.length==3 ? args[2].atoi() : 512), ss = 4; | |
105 auto light = Vec(-1, -3, 2).unitise(); | |
106 auto s=create(level, Vec(0, -1, 0), 1); | |
100 | 107 writefln("P5\n", n, " ", n, "\n255"); |
92 | 108 for (int y=n-1; y>=0; --y) |
109 for (int x=0; x<n; ++x) { | |
110 double g=0; | |
111 for (int d=0; d<ss*ss; ++d) { | |
112 auto dir=Vec(x+(d%ss)*1.0/ss-n/2.0, y+(d/ss)*1.0/ss-n/2.0, n).unitise(); | |
113 g += ray_trace(light, Ray(Vec(0, 0, -4), dir), s); | |
114 } | |
115 printf("%c", cast(ubyte)(0.5 + 255.0 * g / (ss*ss))); | |
116 } | |
117 } |