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

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

Issue 12021025: Merge the openglui samples into one. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix emulator sample build Created 7 years, 11 months 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_driver.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 // Note: The first line of the fragment shader ("precision mediump float")
17 // is not portable. It is required for WebGL and OpenGL ES. Desktop OpenGL
18 // does not use precision specifiers. Some desktop systems treat this as a
19 // no-op and some treat it as a syntax error. In particular, this line needs
20 // to be removed to this this shader on a MAc.
21 const FRAGMENT_PROGRAM = """
22 precision mediump float;
23
24 const vec3 lightDir = vec3(0.577350269, 0.577350269, -0.577350269);
25 varying vec3 vPosition;
26 uniform vec3 cameraPos;
27 uniform vec3 sphere1Center;
28 uniform vec3 sphere2Center;
29 uniform vec3 sphere3Center;
30
31 bool intersectSphere(vec3 center, vec3 lStart, vec3 lDir,
32 out float dist) {
33 vec3 c = center - lStart;
34 float b = dot(lDir, c);
35 float d = b*b - dot(c, c) + 1.0;
36 if (d < 0.0) {
37 dist = 10000.0;
38 return false;
39 }
40
41 dist = b - sqrt(d);
42 if (dist < 0.0) {
43 dist = 10000.0;
44 return false;
45 }
46
47 return true;
48 }
49
50 vec3 lightAt(vec3 N, vec3 V, vec3 color) {
51 vec3 L = lightDir;
52 vec3 R = reflect(-L, N);
53
54 float c = 0.3 + 0.4 * pow(max(dot(R, V), 0.0), 30.0) + 0.7 * dot(L, N);
55
56 if (c > 1.0) {
57 return mix(color, vec3(1.6, 1.6, 1.6), c - 1.0);
58 }
59
60 return c * color;
61 }
62
63 bool intersectWorld(vec3 lStart, vec3 lDir, out vec3 pos,
64 out vec3 normal, out vec3 color) {
65 float d1, d2, d3;
66 bool h1, h2, h3;
67
68 h1 = intersectSphere(sphere1Center, lStart, lDir, d1);
69 h2 = intersectSphere(sphere2Center, lStart, lDir, d2);
70 h3 = intersectSphere(sphere3Center, lStart, lDir, d3);
71
72 if (h1 && d1 < d2 && d1 < d3) {
73 pos = lStart + d1 * lDir;
74 normal = pos - sphere1Center;
75 color = vec3(0.0, 0.0, 0.9);
76 if (fract(pos.x / 1.5) > 0.5 ^^
77 fract(pos.y / 1.5) > 0.5 ^^
78 fract(pos.z / 1.5) > 0.5) {
79 color = vec3(1.0, 0.0, 1.0);
80 }
81 else {
82 color = vec3(1.0, 1.0, 0.0);
83 }
84 }
85 else if (h2 && d2 < d3) {
86 pos = lStart + d2 * lDir;
87 normal = pos - sphere2Center;
88 color = vec3(0.9, mod(normal.y * 2.5, 1.0), 0.0);
89 }
90 else if (h3) {
91 pos = lStart + d3 * lDir;
92 normal = pos - sphere3Center;
93 color = vec3(0.0, clamp(sphere3Center.y/1.5, 0.0, 0.9),
94 clamp(0.9 - sphere3Center.y/1.5, 0.0, 0.9));
95 }
96 else if (lDir.y < -0.01) {
97 pos = lStart + ((lStart.y + 2.7) / -lDir.y) * lDir;
98 if (pos.x*pos.x + pos.z*pos.z > 30.0) {
99 return false;
100 }
101 normal = vec3(0.0, 1.0, 0.0);
102 if (fract(pos.x / 5.0) > 0.5 == fract(pos.z / 5.0) > 0.5) {
103 color = vec3(1.0);
104 }
105 else {
106 color = vec3(0.0);
107 }
108 }
109 else {
110 return false;
111 }
112
113 return true;
114 }
115
116 void main(void)
117 {
118 vec3 cameraDir = normalize(vPosition - cameraPos);
119
120 vec3 p1, norm, p2;
121 vec3 col, colT, colM, col3;
122 if (intersectWorld(cameraPos, cameraDir, p1,
123 norm, colT)) {
124 col = lightAt(norm, -cameraDir, colT);
125 colM = (colT + vec3(0.2)) / 1.2;
126 cameraDir = reflect(cameraDir, norm);
127 if (intersectWorld(p1, cameraDir, p2, norm, colT)) {
128 col += lightAt(norm, -cameraDir, colT) * colM;
129 colM *= (colT + vec3(0.2)) / 1.2;
130 cameraDir = reflect(cameraDir, norm);
131 if (intersectWorld(p2, cameraDir, p1, norm, colT)) {
132 col += lightAt(norm, -cameraDir, colT) * colM;
133 }
134 }
135
136 gl_FragColor = vec4(col, 1.0);
137 }
138 else {
139 gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);
140 }
141 }
142 """;
143
144 const VERTEX_PROGRAM = """
145 attribute vec2 aVertexPosition;
146 attribute vec3 aPlotPosition;
147
148 varying vec3 vPosition;
149
150 void main(void)
151 {
152 gl_Position = vec4(aVertexPosition, 1.0, 1.0);
153 vPosition = aPlotPosition;
154 }
155 """;
156
157 loadShader(final type, final program) {
158 final shader = gl.createShader(type);
159 gl.shaderSource(shader, program);
160 gl.compileShader(shader);
161 if (gl.getShaderParameter(shader, WebGLRenderingContext.COMPILE_STATUS) != tru e) {
162 final error = gl.getShaderInfoLog(shader);
163 log(error);
164 throw new Exception("Shader compilation error: $error");
165 }
166
167 return shader;
168 }
169
170 var shaderProgram;
171 var aVertexPosition;
172 var aPlotPosition;
173 var cameraPos;
174 var sphere1Center;
175 var sphere2Center;
176 var sphere3Center;
177 var ratio;
178
179 void initShaders() {
180 var vertexShader = loadShader(WebGLRenderingContext.VERTEX_SHADER,
181 VERTEX_PROGRAM);
182 var fragmentShader = loadShader(WebGLRenderingContext.FRAGMENT_SHADER,
183 FRAGMENT_PROGRAM);
184
185 shaderProgram = gl.createProgram();
186 if (shaderProgram == 0) {
187 throw new Exception("Could not create program.");
188 }
189
190 gl.attachShader(shaderProgram, vertexShader);
191 gl.attachShader(shaderProgram, fragmentShader);
192 gl.linkProgram(shaderProgram);
193
194 if (gl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) ! = true) {
195 final error = gl.getProgramInfoLog(shaderProgram);
196 throw new Exception("Program compilation error: $error");
197 }
198
199 gl.useProgram(shaderProgram);
200
201 aVertexPosition = gl.getAttribLocation(shaderProgram, "aVertexPosition");
202 gl.enableVertexAttribArray(aVertexPosition);
203
204 aPlotPosition = gl.getAttribLocation(shaderProgram, "aPlotPosition");
205 gl.enableVertexAttribArray(aPlotPosition);
206
207 cameraPos = gl.getUniformLocation(shaderProgram, "cameraPos");
208 sphere1Center = gl.getUniformLocation(shaderProgram, "sphere1Center");
209 sphere2Center = gl.getUniformLocation(shaderProgram, "sphere2Center");
210 sphere3Center = gl.getUniformLocation(shaderProgram, "sphere3Center");
211 }
212
213 // TODO(gram): This should go away at some point. For now it is a kludge
214 // to allow us to run same .dart file with WebGL and natively; the WebGL
215 // version will set this to true.
216 var wrapVertexArray = false;
217
218 wrapVertices(a) => wrapVertexArray ? (new Float32Array.fromList(a)) : a;
219
220 void initBuffers() {
221 var vertexPositionBuffer = gl.createBuffer();
222 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer);
223 var vertices = [
224 1.0, 1.0,
225 -1.0, 1.0,
226 1.0, -1.0,
227 -1.0, -1.0,
228 ];
229
230 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, wrapVertices(vertices),
231 WebGLRenderingContext.STATIC_DRAW);
232 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, vertexPositionBuffer);
233 gl.vertexAttribPointer(aVertexPosition, 2, WebGLRenderingContext.FLOAT,
234 false,0, 0);
235
236 var plotPositionBuffer = gl.createBuffer();
237 gl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, plotPositionBuffer);
238 gl.vertexAttribPointer(aPlotPosition, 3, WebGLRenderingContext.FLOAT,
239 false, 0, 0);
240 }
241
242 class Vector {
243 var x;
244 var y;
245 var z;
246 Vector(this.x, this.y, this.z);
247 }
248
249 // TODO(gram): This should be using vector_math.
250 crossProd(v1, v2) =>
251 new Vector(v1.y*v2.z - v2.y*v1.z,
252 v1.z*v2.x - v2.z*v1.x,
253 v1.x*v2.y - v2.x*v1.y);
254
255 Vector normalize(v) {
256 var l = 1 / Math.sqrt(v.x*v.x + v.y*v.y + v.z*v.z);
257 return new Vector( v.x*l, v.y*l, v.z*l );
258 }
259
260 vectAdd(v1, v2) => new Vector( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z );
261
262 vectSub(v1, v2) => new Vector( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z );
263
264 vectMul(v, l) => new Vector( v.x*l, v.y*l, v.z*l );
265
266 void pushVec(v, arr) {
267 arr.addAll([v.x, v.y, v.z]);
268 }
269
270 var t = 0;
271
272 void drawScene() {
273 var x1 = Math.sin(t * 1.1) * 1.5;
274 var y1 = Math.cos(t * 1.3) * 1.5;
275 var z1 = Math.sin(t + Math.PI/3) * 1.5;
276 var x2 = Math.cos(t * 1.2) * 1.5;
277 var y2 = Math.sin(t * 1.4) * 1.5;
278 var z2 = Math.sin(t*1.25 - Math.PI/3) * 1.5;
279 var x3 = Math.cos(t * 1.15) * 1.5;
280 var y3 = Math.sin(t * 1.37) * 1.5;
281 var z3 = Math.sin(t*1.27) * 1.5;
282
283 var cameraFrom = new Vector(
284 Math.sin(t * 0.4) * 18,
285 Math.sin(t * 0.13) * 5 + 5,
286 Math.cos(t * 0.4) * 18 );
287 var cameraTo = new Vector(0.0, 0.0, 0.0);
288 var cameraPersp = 6.0;
289 var up = new Vector(0.0, 1.0, 0.0);
290 var cameraDir = normalize(vectSub(cameraTo, cameraFrom));
291
292 var cameraLeft = normalize(crossProd(cameraDir, up));
293 var cameraUp = normalize(crossProd(cameraLeft, cameraDir));
294 // cameraFrom + cameraDir * cameraPersp
295 var cameraCenter = vectAdd(cameraFrom, vectMul(cameraDir, cameraPersp));
296
297 // cameraCenter + cameraUp + cameraLeft * ratio
298 var cameraTopLeft = vectAdd(vectAdd(cameraCenter, cameraUp),
299 vectMul(cameraLeft, ratio));
300 var cameraBotLeft = vectAdd(vectSub(cameraCenter, cameraUp),
301 vectMul(cameraLeft, ratio));
302 var cameraTopRight = vectSub(vectAdd(cameraCenter, cameraUp),
303 vectMul(cameraLeft, ratio));
304 var cameraBotRight = vectSub(vectSub(cameraCenter, cameraUp),
305 vectMul(cameraLeft, ratio));
306
307 var corners = [];
308 pushVec(cameraTopRight, corners);
309 pushVec(cameraTopLeft, corners);
310 pushVec(cameraBotRight, corners);
311 pushVec(cameraBotLeft, corners);
312
313 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, wrapVertices(corners),
314 WebGLRenderingContext.STATIC_DRAW);
315
316 gl.uniform3f(cameraPos, cameraFrom.x, cameraFrom.y, cameraFrom.z);
317 gl.uniform3f(sphere1Center, x1, y1, z1);
318 gl.uniform3f(sphere2Center, x2, y2, z2);
319 gl.uniform3f(sphere3Center, x3, y3, z3);
320
321 gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4);
322
323 t += 0.03;
324 if (t > Math.PI * 200) {
325 t -= Math.PI * 200;
326 }
327 }
328
329 void setup(int width, int height) {
330 initShaders();
331 gl.clearColor(0.0, 0.0, 0.0, 1.0);
332 gl.clearDepth(1.0);
333 initBuffers();
334 resize(width, height);
335 }
336
337 void resize(int width, int height) {
338 ratio = width / height;
339 gl.viewport(0, 0, width, height);
340 t -= 0.03;
341 drawScene();
342 }
343
344 void update() {
345 drawScene();
346 }
347
348 /*
349 TODO(gram): below are the current entry points for input events for the
350 native code version. We need to integrate these somehow with the WebGL
351 version:
352
353 onMotionDown(num when, num x, num y) {}
354 onMotionUp(num when, num x, num y) {}
355 onMotionMove(num when, num x, num y) {}
356 onMotionCancel(num when, num x, num y) {}
357 onMotionOutside(num when, num x, num y) {}
358 onMotionPointerDown(num when, num x, num y) {}
359 onMotionPointerUp(num when, num x, num y) {}
360 onKeyDown(num when, int flags, int keycode, int metastate, int repeat) {}
361 onKeyUp(num when, int flags, int keycode, int metastate, int repeat) {}
362 onKeyMultiple(num when, int flags, int keycode, int metastate, int repeat) {
363 }
364 */
365
OLDNEW
« no previous file with comments | « samples/simplegl/web/gl_driver.dart ('k') | samples/simplegl/web/raytrace_driver.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698