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

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: Address comments 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 final error = gl.getShaderInfoLog(shader);
158 throw new Exception("Shader compilation error: $error");
159 }
160
161 return shader;
162 }
163
164 var shaderProgram;
165 var aVertexPosition;
166 var aPlotPosition;
167 var cameraPos;
168 var sphere1Center;
169 var sphere2Center;
170 var sphere3Center;
171 var ratio;
172
173 void initShaders() {
174 var vertexShader = loadShader(WebGLRenderingContext.VERTEX_SHADER, VERTEX_PROG RAM);
175 var fragmentShader = loadShader(WebGLRenderingContext.FRAGMENT_SHADER, FRAGMEN T_PROGRAM);
176
177 shaderProgram = gl.createProgram();
178 if (shaderProgram == 0) {
179 throw new Exception("Could not create program.");
180 }
181
182 gl.attachShader(shaderProgram, vertexShader);
183 gl.attachShader(shaderProgram, fragmentShader);
184 gl.linkProgram(shaderProgram);
185
186 if (gl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) ! = true) {
187 final error = gl.getProgramInfoLog(shaderProgram);
188 throw new Exception("Program compilation error: $error");
189 }
190
191 gl.useProgram(shaderProgram);
192
193 aVertexPosition = gl.getAttribLocation(shaderProgram, "aVertexPosition");
194 gl.enableVertexAttribArray(aVertexPosition);
195
196 aPlotPosition = gl.getAttribLocation(shaderProgram, "aPlotPosition");
197 gl.enableVertexAttribArray(aPlotPosition);
198
199 cameraPos = gl.getUniformLocation(shaderProgram, "cameraPos");
200 sphere1Center = gl.getUniformLocation(shaderProgram, "sphere1Center");
201 sphere2Center = gl.getUniformLocation(shaderProgram, "sphere2Center");
202 sphere3Center = gl.getUniformLocation(shaderProgram, "sphere3Center");
203 }
204
205 void initBuffers() {
206 var vertexPositionBuffer = gl.createBuffer();
207 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer);
208 var vertices = [
209 1.0, 1.0,
210 -1.0, 1.0,
211 1.0, -1.0,
212 -1.0, -1.0,
213 ];
214
215 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, new Float32Array.fromList(ve rtices), WebGLRenderingContext.STATIC_DRAW);
216 gl.vertexAttribPointer(aVertexPosition, 2, WebGLRenderingContext.FLOAT, false, 0, 0);
217
218 var plotPositionBuffer = gl.createBuffer();
219 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, plotPositionBuffer);
220 gl.vertexAttribPointer(aPlotPosition, 3, WebGLRenderingContext.FLOAT, false, 0 , 0);
221 }
222
223 class Vector {
224 var x;
225 var y;
226 var z;
227
228 Vector(this.x, this.y, this.z);
229 }
230
231 crossProd(v1, v2) =>
232 new Vector(v1.y*v2.z - v2.y*v1.z,
233 v1.z*v2.x - v2.z*v1.x,
234 v1.x*v2.y - v2.x*v1.y);
235
236 Vector normalize(v) {
237 var l = 1 / Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
238 return new Vector( v.x*l, v.y*l, v.z*l );
239 }
240
241 vectAdd(v1, v2) => new Vector( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z );
242
243 vectSub(v1, v2) => new Vector( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z );
244
245 vectMul(v, l) => new Vector( v.x*l, v.y*l, v.z*l );
246
247 void pushVec(v, arr) {
248 arr.addAll([v.x, v.y, v.z]);
249 }
250
251 var t = 0;
252
253 void drawScene() {
254 var x1 = Math.sin(t * 1.1) * 1.5;
255 var y1 = Math.cos(t * 1.3) * 1.5;
256 var z1 = Math.sin(t + Math.PI/3) * 1.5;
257 var x2 = Math.cos(t * 1.2) * 1.5;
258 var y2 = Math.sin(t * 1.4) * 1.5;
259 var z2 = Math.sin(t*1.25 - Math.PI/3) * 1.5;
260 var x3 = Math.cos(t * 1.15) * 1.5;
261 var y3 = Math.sin(t * 1.37) * 1.5;
262 var z3 = Math.sin(t*1.27) * 1.5;
263
264 var cameraFrom = new Vector(
265 Math.sin(t * 0.4) * 18,
266 Math.sin(t * 0.13) * 5 + 5,
267 Math.cos(t * 0.4) * 18 );
268 var cameraTo = new Vector(0.0, 0.0, 0.0);
269 var cameraPersp = 6.0;
270 var up = new Vector(0.0, 1.0, 0.0);
271 var cameraDir = normalize(vectSub(cameraTo, cameraFrom));
272
273 var cameraLeft = normalize(crossProd(cameraDir, up));
274 var cameraUp = normalize(crossProd(cameraLeft, cameraDir));
275 // cameraFrom + cameraDir * cameraPersp
276 var cameraCenter = vectAdd(cameraFrom, vectMul(cameraDir, cameraPersp));
277
278 // cameraCenter + cameraUp + cameraLeft * ratio
279 var cameraTopLeft = vectAdd(vectAdd(cameraCenter, cameraUp),
280 vectMul(cameraLeft, ratio));
281 var cameraBotLeft = vectAdd(vectSub(cameraCenter, cameraUp),
282 vectMul(cameraLeft, ratio));
283 var cameraTopRight = vectSub(vectAdd(cameraCenter, cameraUp),
284 vectMul(cameraLeft, ratio));
285 var cameraBotRight = vectSub(vectSub(cameraCenter, cameraUp),
286 vectMul(cameraLeft, ratio));
287
288 // corners = [1.2, 1, -12, -1.2, 1, -12, 1.2, -1, -12, -1.2, -1, -12];
289 var corners = [];
290 pushVec(cameraTopRight, corners);
291 pushVec(cameraTopLeft, corners);
292 pushVec(cameraBotRight, corners);
293 pushVec(cameraBotLeft, corners);
294 var cornersArray = new Float32Array.fromList(corners);
295 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, cornersArray,
296 WebGLRenderingContext.STATIC_DRAW);
297
298 gl.uniform3f(cameraPos, cameraFrom.x, cameraFrom.y, cameraFrom.z);
299 gl.uniform3f(sphere1Center, x1, y1, z1);
300 gl.uniform3f(sphere2Center, x2, y2, z2);
301 gl.uniform3f(sphere3Center, x3, y3, z3);
302
303 gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4);
304
305 t += 0.03;
306 if (t > Math.PI * 200) {
307 t -= Math.PI * 200;
308 }
309 }
310
311 void setup() {
312 initShaders();
313 gl.clearColor(0.0, 0.0, 0.0, 1.0);
314 gl.clearDepth(1.0);
315 initBuffers();
316 }
317
318 void resize(int width, int height) {
319 ratio = width / height;
320 gl.viewport(0, 0, width, height);
321 t -= 0.03;
322 drawScene();
323 }
324
325 void draw() {
326 drawScene();
327 }
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