Mercurial > projects > ldc
comparison demos/ray.d @ 165:9922b9982552 trunk
[svn r181] Updated the raytracer demo by downs to work with tango :) My quick
benchmarking shows it to be around 65% faster than DMD :D
author | lindquist |
---|---|
date | Mon, 05 May 2008 22:20:07 +0200 |
parents | 5071469303d4 |
children | a2c4dc388d5e |
comparison
equal
deleted
inserted
replaced
164:a64becf2a702 | 165:9922b9982552 |
---|---|
1 import std.stdio; | 1 import tango.stdc.stdio; |
2 | |
3 alias char[] string; | |
2 | 4 |
3 int atoi(char[] s) { | 5 int atoi(char[] s) { |
4 int i, fac=1; | 6 int i, fac=1; |
5 bool neg = (s.length) && (s[0] == '-'); | 7 bool neg = (s.length) && (s[0] == '-'); |
6 char[] a = neg ? s[1..$] : s; | 8 char[] a = neg ? s[1..$] : s; |
9 fac *= 10; | 11 fac *= 10; |
10 } | 12 } |
11 return !neg ? i : -i; | 13 return !neg ? i : -i; |
12 } | 14 } |
13 | 15 |
16 version(LLVMDC) | |
17 { | |
14 pragma(LLVM_internal, "intrinsic", "llvm.sqrt.f64") | 18 pragma(LLVM_internal, "intrinsic", "llvm.sqrt.f64") |
15 double sqrt(double val); | 19 double sqrt(double val); |
20 } | |
21 else | |
22 { | |
23 import tango.stdc.math; | |
24 } | |
16 | 25 |
17 double delta; | 26 double delta; |
18 static this() { delta=sqrt(real.epsilon); } | 27 static this() { delta=sqrt(real.epsilon); } |
19 | 28 |
20 struct Vec { | 29 struct Vec { |
102 void main(string[] args) { | 111 void main(string[] args) { |
103 int level = (args.length==3 ? args[1].atoi() : 9), | 112 int level = (args.length==3 ? args[1].atoi() : 9), |
104 n = (args.length==3 ? args[2].atoi() : 512), ss = 4; | 113 n = (args.length==3 ? args[2].atoi() : 512), ss = 4; |
105 auto light = Vec(-1, -3, 2).unitise(); | 114 auto light = Vec(-1, -3, 2).unitise(); |
106 auto s=create(level, Vec(0, -1, 0), 1); | 115 auto s=create(level, Vec(0, -1, 0), 1); |
107 writefln("P5\n", n, " ", n, "\n255"); | 116 printf("P5\n%d %d\n255", n,n); |
108 for (int y=n-1; y>=0; --y) | 117 for (int y=n-1; y>=0; --y) |
109 for (int x=0; x<n; ++x) { | 118 for (int x=0; x<n; ++x) { |
110 double g=0; | 119 double g=0; |
111 for (int d=0; d<ss*ss; ++d) { | 120 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(); | 121 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); | 122 g += ray_trace(light, Ray(Vec(0, 0, -4), dir), s); |
114 } | 123 } |
115 printf("%c", cast(ubyte)(0.5 + 255.0 * g / (ss*ss))); | 124 printf("%c", cast(ubyte)(0.5 + 255.0 * g / (ss*ss))); |
116 } | 125 } |
117 } | 126 } |