Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1084)

Side by Side Diff: samples/simplegl/web/raytrace.dart

Issue 11362256: Added raytracer to gl samples. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/simplegl/web/gl_html.dart ('k') | samples/simplegl/web/raytrace_driver.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 //
5 // This sample is based upon the Ray Tracer sample by Jonas Sicking at:
6 // http://www.khronos.org/webgl/wiki/Demo_Repository
7
8 /**
9 * A sample GL application.
10 */
11 library raytrace;
12
13 import 'gl.dart';
14 import 'dart:math' as Math;
15
16 const FRAGMENT_PROGRAM = """
17 precision mediump float;
18
19 const vec3 lightDir = vec3(0.577350269, 0.577350269, -0.577350269);
20 varying vec3 vPosition;
21 uniform vec3 cameraPos;
22 uniform vec3 sphere1Center;
23 uniform vec3 sphere2Center;
24 uniform vec3 sphere3Center;
25
26 bool intersectSphere(vec3 center, vec3 lStart, vec3 lDir,
27 out float dist) {
28 vec3 c = center - lStart;
29 float b = dot(lDir, c);
30 float d = b*b - dot(c, c) + 1.0;
31 if (d < 0.0) {
32 dist = 10000.0;
33 return false;
34 }
35
36 dist = b - sqrt(d);
37 if (dist < 0.0) {
38 dist = 10000.0;
39 return false;
40 }
41
42 return true;
43 }
44
45 vec3 lightAt(vec3 N, vec3 V, vec3 color) {
46 vec3 L = lightDir;
47 vec3 R = reflect(-L, N);
48
49 float c = 0.3 + 0.4 * pow(max(dot(R, V), 0.0), 30.0) + 0.7 * dot(L, N);
50
51 if (c > 1.0) {
52 return mix(color, vec3(1.6, 1.6, 1.6), c - 1.0);
53 }
54
55 return c * color;
56 }
57
58 bool intersectWorld(vec3 lStart, vec3 lDir, out vec3 pos,
59 out vec3 normal, out vec3 color) {
60 float d1, d2, d3;
61 bool h1, h2, h3;
62
63 h1 = intersectSphere(sphere1Center, lStart, lDir, d1);
64 h2 = intersectSphere(sphere2Center, lStart, lDir, d2);
65 h3 = intersectSphere(sphere3Center, lStart, lDir, d3);
66
67 if (h1 && d1 < d2 && d1 < d3) {
68 pos = lStart + d1 * lDir;
69 normal = pos - sphere1Center;
70 color = vec3(0.0, 0.0, 0.9);
71 if (fract(pos.x / 1.5) > 0.5 ^^
72 fract(pos.y / 1.5) > 0.5 ^^
73 fract(pos.z / 1.5) > 0.5) {
74 color = vec3(1.0, 0.0, 1.0);
75 }
76 else {
77 color = vec3(1.0, 1.0, 0.0);
78 }
79 }
80 else if (h2 && d2 < d3) {
81 pos = lStart + d2 * lDir;
82 normal = pos - sphere2Center;
83 color = vec3(0.9, mod(normal.y * 2.5, 1.0), 0.0);
84 }
85 else if (h3) {
86 pos = lStart + d3 * lDir;
87 normal = pos - sphere3Center;
88 color = vec3(0.0, clamp(sphere3Center.y/1.5, 0.0, 0.9),
89 clamp(0.9 - sphere3Center.y/1.5, 0.0, 0.9));
90 }
91 else if (lDir.y < -0.01) {
92 pos = lStart + ((lStart.y + 2.7) / -lDir.y) * lDir;
93 if (pos.x*pos.x + pos.z*pos.z > 30.0) {
94 return false;
95 }
96 normal = vec3(0.0, 1.0, 0.0);
97 if (fract(pos.x / 5.0) > 0.5 == fract(pos.z / 5.0) > 0.5) {
98 color = vec3(1.0);
99 }
100 else {
101 color = vec3(0.0);
102 }
103 }
104 else {
105 return false;
106 }
107
108 return true;
109 }
110
111 void main(void)
112 {
113 vec3 cameraDir = normalize(vPosition - cameraPos);
114
115 vec3 p1, norm, p2;
116 vec3 col, colT, colM, col3;
117 if (intersectWorld(cameraPos, cameraDir, p1,
118 norm, colT)) {
119 col = lightAt(norm, -cameraDir, colT);
120 colM = (colT + vec3(0.2)) / 1.2;
121 cameraDir = reflect(cameraDir, norm);
122 if (intersectWorld(p1, cameraDir, p2, norm, colT)) {
123 col += lightAt(norm, -cameraDir, colT) * colM;
124 colM *= (colT + vec3(0.2)) / 1.2;
125 cameraDir = reflect(cameraDir, norm);
126 if (intersectWorld(p2, cameraDir, p1, norm, colT)) {
127 col += lightAt(norm, -cameraDir, colT) * colM;
128 }
129 }
130
131 gl_FragColor = vec4(col, 1.0);
132 }
133 else {
134 gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
135 }
136 }
137 """;
138
139 const VERTEX_PROGRAM = """
140 attribute vec2 aVertexPosition;
141 attribute vec3 aPlotPosition;
142
143 varying vec3 vPosition;
144
145 void main(void)
146 {
147 gl_Position = vec4(aVertexPosition, 1.0, 1.0);
148 vPosition = aPlotPosition;
149 }
150 """;
151
152 loadShader(final type, final program) {
153 final shader = gl.createShader(type);
154 gl.shaderSource(shader, program);
155 gl.compileShader(shader);
156 if (gl.getShaderParameter(shader, WebGLRenderingContext.COMPILE_STATUS) != tru e) {
157 throw new Exception("Could not compile shader");
Cutch 2012/11/15 15:06:13 How about calling getshaderinfolog and include the
vsm 2012/11/15 20:22:09 Done.
158 }
159
160 return shader;
161 }
162
163 var shaderProgram;
164 var aVertexPosition;
165 var aPlotPosition;
166 var cameraPos;
167 var sphere1Center;
168 var sphere2Center;
169 var sphere3Center;
170 var ratio;
171
172 void initShaders() {
173 var vertexShader = loadShader(WebGLRenderingContext.VERTEX_SHADER, VERTEX_PROG RAM);
174 var fragmentShader = loadShader(WebGLRenderingContext.FRAGMENT_SHADER, FRAGMEN T_PROGRAM);
175
176 shaderProgram = gl.createProgram();
177 if (shaderProgram == 0) {
178 throw new Exception("Could not create program.");
179 }
180
181 gl.attachShader(shaderProgram, vertexShader);
182 gl.attachShader(shaderProgram, fragmentShader);
183 gl.linkProgram(shaderProgram);
184
185 if (gl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) ! = true) {
186 throw new Exception("Could not initialize shaders");
Cutch 2012/11/15 15:06:13 Same here but with getprograminfolog
vsm 2012/11/15 20:22:09 Done.
187 }
188
189 gl.useProgram(shaderProgram);
190
191 aVertexPosition = gl.getAttribLocation(shaderProgram, "aVertexPosition");
192 gl.enableVertexAttribArray(aVertexPosition);
193
194 aPlotPosition = gl.getAttribLocation(shaderProgram, "aPlotPosition");
195 gl.enableVertexAttribArray(aPlotPosition);
196
197 cameraPos = gl.getUniformLocation(shaderProgram, "cameraPos");
198 sphere1Center = gl.getUniformLocation(shaderProgram, "sphere1Center");
199 sphere2Center = gl.getUniformLocation(shaderProgram, "sphere2Center");
200 sphere3Center = gl.getUniformLocation(shaderProgram, "sphere3Center");
201 }
202
203 void initBuffers() {
204 var vertexPositionBuffer = gl.createBuffer();
205 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer);
206 var vertices = [
207 1.0, 1.0,
208 -1.0, 1.0,
209 1.0, -1.0,
210 -1.0, -1.0,
211 ];
212
213 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, new Float32Array.fromList(ve rtices), WebGLRenderingContext.STATIC_DRAW);
214 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer);
Cutch 2012/11/15 15:06:13 This bind is unnecessary.
vsm 2012/11/15 20:22:09 Done. Thanks for the catch. On 2012/11/15 15:06:
215 gl.vertexAttribPointer(aVertexPosition, 2, WebGLRenderingContext.FLOAT, false, 0, 0);
216
217 var plotPositionBuffer = gl.createBuffer();
218 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, plotPositionBuffer);
219 gl.vertexAttribPointer(aPlotPosition, 3, WebGLRenderingContext.FLOAT, false, 0 , 0);
220 }
221
222 class Vector {
223 var x;
224 var y;
225 var z;
226
227 Vector(this.x, this.y, this.z);
228 }
229
230 crossProd(v1, v2) =>
231 new Vector(v1.y*v2.z - v2.y*v1.z,
232 v1.z*v2.x - v2.z*v1.x,
233 v1.x*v2.y - v2.x*v1.y);
234
235 Vector normalize(v) {
236 var l = Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
237 return new Vector( v.x/l, v.y/l, v.z/l );
Cutch 2012/11/15 15:06:13 Do the division once: var lr = 1.0 / l; return ne
vsm 2012/11/15 20:22:09 Done.
238 }
239
240 vectAdd(v1, v2) => new Vector( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z );
241
242 vectSub(v1, v2) => new Vector( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z );
243
244 vectMul(v, l) => new Vector( v.x*l, v.y*l, v.z*l );
245
246 void pushVec(v, arr) {
247 arr.addAll([v.x, v.y, v.z]);
248 }
249
250 var t = 0;
251
252 void drawScene() {
253 var x1 = Math.sin(t * 1.1) * 1.5;
254 var y1 = Math.cos(t * 1.3) * 1.5;
255 var z1 = Math.sin(t + Math.PI/3) * 1.5;
256 var x2 = Math.cos(t * 1.2) * 1.5;
257 var y2 = Math.sin(t * 1.4) * 1.5;
258 var z2 = Math.sin(t*1.25 - Math.PI/3) * 1.5;
259 var x3 = Math.cos(t * 1.15) * 1.5;
260 var y3 = Math.sin(t * 1.37) * 1.5;
261 var z3 = Math.sin(t*1.27) * 1.5;
262
263 var cameraFrom = new Vector(
264 Math.sin(t * 0.4) * 18,
265 Math.sin(t * 0.13) * 5 + 5,
266 Math.cos(t * 0.4) * 18 );
267 var cameraTo = new Vector(0.0, 0.0, 0.0);
268 var cameraPersp = 6.0;
269 var up = new Vector(0.0, 1.0, 0.0);
270 var cameraDir = normalize(vectSub(cameraTo, cameraFrom));
271
272 var cameraLeft = normalize(crossProd(cameraDir, up));
273 var cameraUp = normalize(crossProd(cameraLeft, cameraDir));
274 // cameraFrom + cameraDir * cameraPersp
275 var cameraCenter = vectAdd(cameraFrom, vectMul(cameraDir, cameraPersp));
276
277 // cameraCenter + cameraUp + cameraLeft * ratio
278 var cameraTopLeft = vectAdd(vectAdd(cameraCenter, cameraUp),
279 vectMul(cameraLeft, ratio));
280 var cameraBotLeft = vectAdd(vectSub(cameraCenter, cameraUp),
281 vectMul(cameraLeft, ratio));
282 var cameraTopRight = vectSub(vectAdd(cameraCenter, cameraUp),
283 vectMul(cameraLeft, ratio));
284 var cameraBotRight = vectSub(vectSub(cameraCenter, cameraUp),
285 vectMul(cameraLeft, ratio));
286
287 // corners = [1.2, 1, -12, -1.2, 1, -12, 1.2, -1, -12, -1.2, -1, -12];
288 var corners = [];
289 pushVec(cameraTopRight, corners);
290 pushVec(cameraTopLeft, corners);
291 pushVec(cameraBotRight, corners);
292 pushVec(cameraBotLeft, corners);
293 var cornersArray = new Float32Array.fromList(corners);
294 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, cornersArray,
295 WebGLRenderingContext.STATIC_DRAW);
296
297 gl.uniform3f(cameraPos, cameraFrom.x, cameraFrom.y, cameraFrom.z);
298 gl.uniform3f(sphere1Center, x1, y1, z1);
299 gl.uniform3f(sphere2Center, x2, y2, z2);
300 gl.uniform3f(sphere3Center, x3, y3, z3);
301
302 gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4);
303
304 t += 0.03;
305 if (t > Math.PI * 200) {
306 t -= Math.PI * 200;
307 }
308 }
309
310 void setup() {
311 initShaders();
312 gl.clearColor(0.0, 0.0, 0.0, 1.0);
313 gl.clearDepth(1.0);
314 initBuffers();
315 }
316
317 void resize(int width, int height) {
318 ratio = width / height;
319 gl.viewport(0, 0, width, height);
320 t -= 0.03;
321 drawScene();
322 }
323
324 void draw() {
325 drawScene();
326 }
OLDNEW
« no previous file with comments | « samples/simplegl/web/gl_html.dart ('k') | samples/simplegl/web/raytrace_driver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698