| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // | 4 // |
| 5 // This sample is based upon the Ray Tracer sample by Jonas Sicking at: | 5 // This sample is based upon the Ray Tracer sample by Jonas Sicking at: |
| 6 // http://www.khronos.org/webgl/wiki/Demo_Repository | 6 // http://www.khronos.org/webgl/wiki/Demo_Repository |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * A sample GL application. | 9 * A sample GL application. |
| 10 */ | 10 */ |
| 11 library raytrace; | 11 library openglui_raytrace; |
| 12 | 12 |
| 13 import 'gl.dart'; | 13 import 'gl.dart'; |
| 14 import 'dart:math' as Math; | 14 import 'dart:math' as Math; |
| 15 | 15 |
| 16 WebGLRenderingContext gl = null; |
| 17 |
| 16 // Note: The first line of the fragment shader ("precision mediump float") | 18 // 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 | 19 // 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 | 20 // 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 | 21 // 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. | 22 // to be removed to this this shader on a MAc. |
| 21 const FRAGMENT_PROGRAM = """ | 23 const FRAGMENT_PROGRAM = """ |
| 22 // TODO(vsm): Remove this ifdef. It's a temporary workaround to get | 24 // TODO(vsm): Remove this ifdef. It's a temporary workaround to get |
| 23 // this sample running on the Mac desktop emulator. | 25 // this sample running on the Mac desktop emulator. |
| 24 #ifdef GL_ES | 26 #ifdef GL_ES |
| 25 precision mediump float; | 27 precision mediump float; |
| (...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 292 var cameraPersp = 6.0; | 294 var cameraPersp = 6.0; |
| 293 var up = new Vector(0.0, 1.0, 0.0); | 295 var up = new Vector(0.0, 1.0, 0.0); |
| 294 var cameraDir = normalize(vectSub(cameraTo, cameraFrom)); | 296 var cameraDir = normalize(vectSub(cameraTo, cameraFrom)); |
| 295 | 297 |
| 296 var cameraLeft = normalize(crossProd(cameraDir, up)); | 298 var cameraLeft = normalize(crossProd(cameraDir, up)); |
| 297 var cameraUp = normalize(crossProd(cameraLeft, cameraDir)); | 299 var cameraUp = normalize(crossProd(cameraLeft, cameraDir)); |
| 298 // cameraFrom + cameraDir * cameraPersp | 300 // cameraFrom + cameraDir * cameraPersp |
| 299 var cameraCenter = vectAdd(cameraFrom, vectMul(cameraDir, cameraPersp)); | 301 var cameraCenter = vectAdd(cameraFrom, vectMul(cameraDir, cameraPersp)); |
| 300 | 302 |
| 301 // cameraCenter + cameraUp + cameraLeft * ratio | 303 // cameraCenter + cameraUp + cameraLeft * ratio |
| 302 var cameraTopLeft = vectAdd(vectAdd(cameraCenter, cameraUp), | 304 log("cameraLeft = ${cameraLeft}"); |
| 303 vectMul(cameraLeft, ratio)); | 305 log("ratio = ${ratio}"); |
| 304 var cameraBotLeft = vectAdd(vectSub(cameraCenter, cameraUp), | 306 var vl = vectMul(cameraLeft, ratio); |
| 305 vectMul(cameraLeft, ratio)); | 307 var cameraTopLeft = vectAdd(vectAdd(cameraCenter, cameraUp), vl); |
| 306 var cameraTopRight = vectSub(vectAdd(cameraCenter, cameraUp), | 308 //vectMul(cameraLeft, ratio)); |
| 307 vectMul(cameraLeft, ratio)); | 309 var cameraBotLeft = vectAdd(vectSub(cameraCenter, cameraUp), vl); |
| 308 var cameraBotRight = vectSub(vectSub(cameraCenter, cameraUp), | 310 //vectMul(cameraLeft, ratio)); |
| 309 vectMul(cameraLeft, ratio)); | 311 var cameraTopRight = vectSub(vectAdd(cameraCenter, cameraUp), vl); |
| 312 //vectMul(cameraLeft, ratio)); |
| 313 var cameraBotRight = vectSub(vectSub(cameraCenter, cameraUp), vl); |
| 314 //vectMul(cameraLeft, ratio)); |
| 310 | 315 |
| 311 var corners = []; | 316 var corners = []; |
| 312 pushVec(cameraTopRight, corners); | 317 pushVec(cameraTopRight, corners); |
| 313 pushVec(cameraTopLeft, corners); | 318 pushVec(cameraTopLeft, corners); |
| 314 pushVec(cameraBotRight, corners); | 319 pushVec(cameraBotRight, corners); |
| 315 pushVec(cameraBotLeft, corners); | 320 pushVec(cameraBotLeft, corners); |
| 316 | 321 |
| 317 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, wrapVertices(corners), | 322 gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, wrapVertices(corners), |
| 318 WebGLRenderingContext.STATIC_DRAW); | 323 WebGLRenderingContext.STATIC_DRAW); |
| 319 | 324 |
| 320 gl.uniform3f(cameraPos, cameraFrom.x, cameraFrom.y, cameraFrom.z); | 325 gl.uniform3f(cameraPos, cameraFrom.x, cameraFrom.y, cameraFrom.z); |
| 321 gl.uniform3f(sphere1Center, x1, y1, z1); | 326 gl.uniform3f(sphere1Center, x1, y1, z1); |
| 322 gl.uniform3f(sphere2Center, x2, y2, z2); | 327 gl.uniform3f(sphere2Center, x2, y2, z2); |
| 323 gl.uniform3f(sphere3Center, x3, y3, z3); | 328 gl.uniform3f(sphere3Center, x3, y3, z3); |
| 324 | 329 |
| 325 gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4); | 330 gl.drawArrays(WebGLRenderingContext.TRIANGLE_STRIP, 0, 4); |
| 326 | 331 |
| 327 t += 0.03; | 332 t += 0.03; |
| 328 if (t > Math.PI * 200) { | 333 if (t > Math.PI * 200) { |
| 329 t -= Math.PI * 200; | 334 t -= Math.PI * 200; |
| 330 } | 335 } |
| 331 } | 336 } |
| 332 | 337 |
| 333 void setup(int width, int height) { | 338 void setup(canvas, int w, int h) { |
| 339 if (canvas == null) { |
| 340 canvas = new CanvasElement(width: w, height: h); |
| 341 } |
| 342 gl = canvas.getContext("experimental-webgl"); |
| 334 initShaders(); | 343 initShaders(); |
| 335 gl.clearColor(0.0, 0.0, 0.0, 1.0); | 344 gl.clearColor(0.0, 0.0, 0.0, 1.0); |
| 336 gl.clearDepth(1.0); | 345 gl.clearDepth(1.0); |
| 337 initBuffers(); | 346 initBuffers(); |
| 338 resize(width, height); | 347 resize(w, h); |
| 348 log("Done setup"); |
| 339 } | 349 } |
| 340 | 350 |
| 341 void resize(int width, int height) { | 351 void resize(int width, int height) { |
| 352 gl.viewport(0, 0, width, height); |
| 342 ratio = width / height; | 353 ratio = width / height; |
| 343 gl.viewport(0, 0, width, height); | |
| 344 t -= 0.03; | 354 t -= 0.03; |
| 345 drawScene(); | 355 drawScene(); |
| 346 } | 356 } |
| 347 | 357 |
| 348 void update() { | 358 void update() { |
| 349 drawScene(); | 359 drawScene(); |
| 350 } | 360 } |
| 351 | 361 |
| 352 /* | 362 onMotionDown(num when, num x, num y) { |
| 353 TODO(gram): below are the current entry points for input events for the | 363 } |
| 354 native code version. We need to integrate these somehow with the WebGL | |
| 355 version: | |
| 356 | 364 |
| 357 onMotionDown(num when, num x, num y) {} | |
| 358 onMotionUp(num when, num x, num y) {} | |
| 359 onMotionMove(num when, num x, num y) {} | |
| 360 onMotionCancel(num when, num x, num y) {} | |
| 361 onMotionOutside(num when, num x, num y) {} | |
| 362 onMotionPointerDown(num when, num x, num y) {} | |
| 363 onMotionPointerUp(num when, num x, num y) {} | |
| 364 onKeyDown(num when, int flags, int keycode, int metastate, int repeat) {} | |
| 365 onKeyUp(num when, int flags, int keycode, int metastate, int repeat) {} | |
| 366 onKeyMultiple(num when, int flags, int keycode, int metastate, int repeat) { | |
| 367 } | |
| 368 */ | |
| 369 | |
| OLD | NEW |