Skip to content
Browse files

adding webgl with css

  • Loading branch information...
1 parent 053799a commit b11559bce77f10afdd074815b17f9b8e2a79545b @financeCoding committed
View
1 teapot_css/.children
@@ -0,0 +1 @@
+teapot.dart
View
17 teapot_css/.project
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>teapot</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>com.google.dart.tools.core.dartBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.google.dart.tools.core.dartNature</nature>
+ </natures>
+</projectDescription>
View
56 teapot_css/CameraController.dart
@@ -0,0 +1,56 @@
+class CameraController {
+
+ Function onchange = null;
+ num xRot = 0;
+ num yRot = 0;
+ num scaleFactor = 3.0;
+ bool dragging = false;
+ num curX = 0;
+ num curY = 0;
+ Element element;
+
+ CameraController(this.element) {
+ element.on.mouseDown.add(_onmousedown);
+ element.on.mouseUp.add(_onmouseup);
+ element.on.mouseMove.add(_onmousemove);
+ }
+
+ _onmousedown(MouseEvent event) {
+ curX = event.clientX;
+ curY = event.clientY;
+ dragging = true;
+
+ }
+
+ _onmouseup(MouseEvent event) {
+ dragging = false;
+
+ }
+
+ _onmousemove(MouseEvent event) {
+ if (dragging) {
+ // Determine how far we have moved since the last mouse move
+ // event.
+ var _curX = event.clientX;
+ var _curY = event.clientY;
+ var deltaX = (curX - _curX) / scaleFactor;
+ var deltaY = (curY - _curY) / scaleFactor;
+ curX = _curX;
+ curY = _curY;
+ // Update the X and Y rotation angles based on the mouse motion.
+ yRot = (yRot + deltaX) % 360;
+ xRot = (xRot + deltaY);
+ // Clamp the X rotation to prevent the camera from going upside
+ // down.
+ if (xRot < -90) {
+ xRot = -90;
+ } else if (xRot > 90) {
+ xRot = 90;
+ }
+ // Send the onchange event to any listener.
+ if (onchange != null) {
+ onchange();
+ }
+ }
+ }
+}
View
BIN teapot_css/bump.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
65 teapot_css/gl-matrix-dart/Matrix3.dart
@@ -0,0 +1,65 @@
+class Matrix3 {
+
+ Float32Array _dest;
+
+ Matrix3() {
+ _dest = new Float32Array(9);
+ }
+
+ Matrix3.fromList(List<num> list) {
+ if (list.length != 9) {
+ throw new Exception("Matrix4.fromList requires list of exactly 9 items (${list.length} given)");
+ }
+ _dest = new Float32Array.fromList(list);
+ }
+
+ Matrix3.fromFloat32Array(Float32Array list) {
+ if (list.length != 9) {
+ throw new Exception("Matrix4.fromList requires Float32Array of exactly 9 items (${list.length} given)");
+ }
+ _dest = list;
+ }
+
+ operator [](int index) => _dest[index];
+
+ operator []=(int index, double value) => _dest[index] = value;
+
+ Float32Array get array() => _dest;
+ set array(Float32Array array) => _dest = array;
+
+ /**
+ * Transposes a mat3 (flips the values over the diagonal)
+ *
+ * @param {mat3} mat mat3 to transpose
+ * @param {mat3} [dest] mat3 receiving transposed values. If not specified result is written to mat
+ *
+ * @param {mat3} dest is specified, mat otherwise
+ */
+ void transpose([Matrix3 dest]) {
+ // If we are transposing ourselves we can skip a few steps but have to cache some values
+ if (dest == null) {
+ var a01 = _dest[1], a02 = _dest[2],
+ a12 = _dest[5];
+
+ _dest[1] = _dest[3];
+ _dest[2] = _dest[6];
+ _dest[3] = a01;
+ _dest[5] = _dest[7];
+ _dest[6] = a02;
+ _dest[7] = a12;
+
+ } else {
+
+ dest[0] = _dest[0];
+ dest[1] = _dest[3];
+ dest[2] = _dest[6];
+ dest[3] = _dest[1];
+ dest[4] = _dest[4];
+ dest[5] = _dest[7];
+ dest[6] = _dest[2];
+ dest[7] = _dest[5];
+ dest[8] = _dest[8];
+
+ }
+ }
+}
View
955 teapot_css/gl-matrix-dart/Matrix4.dart
@@ -0,0 +1,955 @@
+
+class Matrix4 {
+
+
+ Float32Array _dest;
+
+ Matrix4() {
+ _dest = new Float32Array(16);
+ }
+
+ Matrix4.fromList(List<num> list) {
+ if (list.length != 16) {
+ throw new Exception("Matrix4.fromList requires list of exactly 16 items (${list.length} given)");
+ }
+ _dest = new Float32Array.fromList(list);
+ }
+
+ Matrix4.fromFloat32Array(Float32Array list) {
+ if (list.length != 16) {
+ throw new Exception("Matrix4.fromList requires Float32Array of exactly 16 items (${list.length} given)");
+ }
+ _dest = list;
+ }
+
+ Matrix4.identity() {
+ _dest = new Float32Array(16);
+ this.identity();
+ }
+
+ operator [](int index) => _dest[index];
+
+ operator []=(int index, num value) => _dest[index] = value;
+
+
+ Float32Array get array() => _dest;
+ set array(Float32Array array) => _dest = array;
+
+ /**
+ * Copies the values of one mat4 to another
+ *
+ * @param {mat4} mat mat4 containing values to copy
+ * @param {mat4} dest mat4 receiving copied values
+ *
+ * @returns {mat4} dest
+ */
+ Matrix4 clone() {
+ return new Matrix4.fromFloat32Array(_dest.subarray(0, _dest.length)); // copy items
+ }
+
+ /**
+ * Sets a mat4 to an identity matrix
+ *
+ * @param {mat4} dest mat4 to set
+ *
+ * @returns {mat4} dest
+ */
+ void identity() {
+ _dest[0] = 1;
+ _dest[1] = 0;
+ _dest[2] = 0;
+ _dest[3] = 0;
+ _dest[4] = 0;
+ _dest[5] = 1;
+ _dest[6] = 0;
+ _dest[7] = 0;
+ _dest[8] = 0;
+ _dest[9] = 0;
+ _dest[10] = 1;
+ _dest[11] = 0;
+ _dest[12] = 0;
+ _dest[13] = 0;
+ _dest[14] = 0;
+ _dest[15] = 1;
+ }
+
+ /**
+ * Transposes a mat4 (flips the values over the diagonal)
+ *
+ * @param {mat4} mat mat4 to transpose
+ * @param {mat4} [dest] mat4 receiving transposed values. If not specified result is written to mat
+ *
+ * @param {mat4} dest is specified, mat otherwise
+ */
+ void transpose([Matrix4 dest]) {
+ // If we are transposing ourselves we can skip a few steps but have to cache some values
+ if (null == dest) {
+ double
+ a01 = _dest[1], a02 = _dest[2], a03 = _dest[3],
+ a12 = _dest[6], a13 = _dest[7],
+ a23 = _dest[11];
+
+ _dest[1] = _dest[4];
+ _dest[2] = _dest[8];
+ _dest[3] = _dest[12];
+ _dest[4] = a01;
+ _dest[6] = _dest[9];
+ _dest[7] = _dest[13];
+ _dest[8] = a02;
+ _dest[9] = a12;
+ _dest[11] = _dest[14];
+ _dest[12] = a03;
+ _dest[13] = a13;
+ _dest[14] = a23;
+ } else {
+ dest[0] = _dest[0];
+ dest[1] = _dest[4];
+ dest[2] = _dest[8];
+ dest[3] = _dest[12];
+ dest[4] = _dest[1];
+ dest[5] = _dest[5];
+ dest[6] = _dest[9];
+ dest[7] = _dest[13];
+ dest[8] = _dest[2];
+ dest[9] = _dest[6];
+ dest[10] = _dest[10];
+ dest[11] = _dest[14];
+ dest[12] = _dest[3];
+ dest[13] = _dest[7];
+ dest[14] = _dest[11];
+ dest[15] = _dest[15];
+ }
+ }
+
+ /**
+ * Calculates the determinant of a mat4
+ *
+ * @param {mat4} mat mat4 to calculate determinant of
+ *
+ * @returns {number} determinant of mat
+ */
+ double determinant() {
+ // Cache the matrix values (makes for huge speed increases!)
+ double a00 = _dest[0], a01 = _dest[1], a02 = _dest[2], a03 = _dest[3],
+ a10 = _dest[4], a11 = _dest[5], a12 = _dest[6], a13 = _dest[7],
+ a20 = _dest[8], a21 = _dest[9], a22 = _dest[10], a23 = _dest[11],
+ a30 = _dest[12], a31 = _dest[13], a32 = _dest[14], a33 = _dest[15];
+
+ return (a30 * a21 * a12 * a03 - a20 * a31 * a12 * a03 - a30 * a11 * a22 * a03 + a10 * a31 * a22 * a03 +
+ a20 * a11 * a32 * a03 - a10 * a21 * a32 * a03 - a30 * a21 * a02 * a13 + a20 * a31 * a02 * a13 +
+ a30 * a01 * a22 * a13 - a00 * a31 * a22 * a13 - a20 * a01 * a32 * a13 + a00 * a21 * a32 * a13 +
+ a30 * a11 * a02 * a23 - a10 * a31 * a02 * a23 - a30 * a01 * a12 * a23 + a00 * a31 * a12 * a23 +
+ a10 * a01 * a32 * a23 - a00 * a11 * a32 * a23 - a20 * a11 * a02 * a33 + a10 * a21 * a02 * a33 +
+ a20 * a01 * a12 * a33 - a00 * a21 * a12 * a33 - a10 * a01 * a22 * a33 + a00 * a11 * a22 * a33);
+ }
+
+ /**
+ * Calculates the inverse matrix of a mat4
+ *
+ * @param {mat4} mat mat4 to calculate inverse of
+ * @param {mat4} [dest] mat4 receiving inverse matrix. If not specified result is written to mat
+ *
+ * @param {mat4} dest is specified, mat otherwise, null if matrix cannot be inverted
+ */
+ double inverse([Matrix4 dest]) {
+ // Cache the matrix values (makes for huge speed increases!)
+ double
+ a00 = _dest[0], a01 = _dest[1], a02 = _dest[2], a03 = _dest[3],
+ a10 = _dest[4], a11 = _dest[5], a12 = _dest[6], a13 = _dest[7],
+ a20 = _dest[8], a21 = _dest[9], a22 = _dest[10], a23 = _dest[11],
+ a30 = _dest[12], a31 = _dest[13], a32 = _dest[14], a33 = _dest[15],
+
+ b00 = a00 * a11 - a01 * a10,
+ b01 = a00 * a12 - a02 * a10,
+ b02 = a00 * a13 - a03 * a10,
+ b03 = a01 * a12 - a02 * a11,
+ b04 = a01 * a13 - a03 * a11,
+ b05 = a02 * a13 - a03 * a12,
+ b06 = a20 * a31 - a21 * a30,
+ b07 = a20 * a32 - a22 * a30,
+ b08 = a20 * a33 - a23 * a30,
+ b09 = a21 * a32 - a22 * a31,
+ b10 = a21 * a33 - a23 * a31,
+ b11 = a22 * a33 - a23 * a32,
+
+ d = (b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06),
+ invDet;
+
+ // Calculate the determinant
+ if (0 == d) {
+ return null;
+ }
+ invDet = 1 / d;
+
+ var out = (null == dest ? _dest : dest );
+
+ out[0] = ( a11 * b11 - a12 * b10 + a13 * b09) * invDet;
+ out[1] = (-a01 * b11 + a02 * b10 - a03 * b09) * invDet;
+ out[2] = ( a31 * b05 - a32 * b04 + a33 * b03) * invDet;
+ out[3] = (-a21 * b05 + a22 * b04 - a23 * b03) * invDet;
+ out[4] = (-a10 * b11 + a12 * b08 - a13 * b07) * invDet;
+ out[5] = ( a00 * b11 - a02 * b08 + a03 * b07) * invDet;
+ out[6] = (-a30 * b05 + a32 * b02 - a33 * b01) * invDet;
+ out[7] = ( a20 * b05 - a22 * b02 + a23 * b01) * invDet;
+ out[8] = ( a10 * b10 - a11 * b08 + a13 * b06) * invDet;
+ out[9] = (-a00 * b10 + a01 * b08 - a03 * b06) * invDet;
+ out[10] = ( a30 * b04 - a31 * b02 + a33 * b00) * invDet;
+ out[11] = (-a20 * b04 + a21 * b02 - a23 * b00) * invDet;
+ out[12] = (-a10 * b09 + a11 * b07 - a12 * b06) * invDet;
+ out[13] = ( a00 * b09 - a01 * b07 + a02 * b06) * invDet;
+ out[14] = (-a30 * b03 + a31 * b01 - a32 * b00) * invDet;
+ out[15] = ( a20 * b03 - a21 * b01 + a22 * b00) * invDet;
+ }
+
+ /**
+ * Copies the upper 3x3 elements of a mat4 into another mat4
+ *
+ * @param {mat4} mat mat4 containing values to copy
+ * @param {mat4} [dest] mat4 receiving copied values
+ *
+ * @returns {mat4} dest is specified, a new mat4 otherwise
+ */
+ Matrix4 toRotationMat([Matrix4 dest]) {
+ //Float32Array list = new Float32Array(16);
+ var out = (null == dest ? _dest : dest );
+
+ out[0] = _dest[0];
+ out[1] = _dest[1];
+ out[2] = _dest[2];
+ out[3] = _dest[3];
+ out[4] = _dest[4];
+ out[5] = _dest[5];
+ out[6] = _dest[6];
+ out[7] = _dest[7];
+ out[8] = _dest[8];
+ out[9] = _dest[9];
+ out[10] = _dest[10];
+ out[11] = _dest[11];
+ out[12] = 0;
+ out[13] = 0;
+ out[14] = 0;
+ out[15] = 1;
+ }
+
+ /**
+ * Copies the upper 3x3 elements of a mat4 into a mat3
+ *
+ * @param {mat4} mat mat4 containing values to copy
+ * @param {mat3} [dest] mat3 receiving copied values
+ *
+ * @returns {mat3} dest is specified, a new mat3 otherwise
+ */
+ Matrix3 toMat3([Matrix3 dest]) {
+ //Float32Array list = new Float32Array(9);
+ if (null == dest) {
+ dest = new Matrix3();
+ }
+
+ dest[0] = _dest[0];
+ dest[1] = _dest[1];
+ dest[2] = _dest[2];
+ dest[3] = _dest[4];
+ dest[4] = _dest[5];
+ dest[5] = _dest[6];
+ dest[6] = _dest[8];
+ dest[7] = _dest[9];
+ dest[8] = _dest[10];
+
+ return dest;
+ }
+
+ /**
+ * Calculates the inverse of the upper 3x3 elements of a mat4 and copies the result into a mat3
+ * The resulting matrix is useful for calculating transformed normals
+ *
+ * Params:
+ * @param {mat4} mat mat4 containing values to invert and copy
+ * @param {mat3} [dest] mat3 receiving values
+ *
+ * @returns {mat3} dest is specified, a new mat3 otherwise, null if the matrix cannot be inverted
+ */
+ Matrix3 toInverseMat3([Matrix3 dest]) {
+ // Cache the matrix values (makes for huge speed increases!)
+ double
+ a00 = _dest[0], a01 = _dest[1], a02 = _dest[2],
+ a10 = _dest[4], a11 = _dest[5], a12 = _dest[6],
+ a20 = _dest[8], a21 = _dest[9], a22 = _dest[10],
+
+ b01 = a22 * a11 - a12 * a21,
+ b11 = -a22 * a10 + a12 * a20,
+ b21 = a21 * a10 - a11 * a20,
+
+ d = a00 * b01 + a01 * b11 + a02 * b21,
+ id;
+
+ if (0 == d) {
+ return null;
+ }
+ id = 1 / d;
+
+ if (null == dest) {
+ dest = new Matrix3();
+ }
+
+ dest[0] = b01 * id;
+ dest[1] = (-a22 * a01 + a02 * a21) * id;
+ dest[2] = (a12 * a01 - a02 * a11) * id;
+ dest[3] = b11 * id;
+ dest[4] = (a22 * a00 - a02 * a20) * id;
+ dest[5] = (-a12 * a00 + a02 * a10) * id;
+ dest[6] = b21 * id;
+ dest[7] = (-a21 * a00 + a01 * a20) * id;
+ dest[8] = (a11 * a00 - a01 * a10) * id;
+
+ return dest;
+ }
+
+ /**
+ * Performs a matrix multiplication
+ *
+ * @param {mat4} mat First operand
+ * @param {mat4} mat2 Second operand
+ * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
+ *
+ * @returns {mat4} dest if specified, mat otherwise
+ */
+ void multiply(Matrix4 mat, [Matrix4 dest]) {
+ //Float32Array list = new Float32Array(16);
+
+ // Cache the matrix values (makes for huge speed increases!)
+ double
+ a00 = _dest[0], a01 = _dest[1], a02 = _dest[2], a03 = _dest[3],
+ a10 = _dest[4], a11 = _dest[5], a12 = _dest[6], a13 = _dest[7],
+ a20 = _dest[8], a21 = _dest[9], a22 = _dest[10], a23 = _dest[11],
+ a30 = _dest[12], a31 = _dest[13], a32 = _dest[14], a33 = _dest[15],
+
+ b00 = mat[0], b01 = mat[1], b02 = mat[2], b03 = mat[3],
+ b10 = mat[4], b11 = mat[5], b12 = mat[6], b13 = mat[7],
+ b20 = mat[8], b21 = mat[9], b22 = mat[10], b23 = mat[11],
+ b30 = mat[12], b31 = mat[13], b32 = mat[14], b33 = mat[15];
+
+ var out = (null == dest ? _dest : dest );
+
+ out[0] = b00 * a00 + b01 * a10 + b02 * a20 + b03 * a30;
+ out[1] = b00 * a01 + b01 * a11 + b02 * a21 + b03 * a31;
+ out[2] = b00 * a02 + b01 * a12 + b02 * a22 + b03 * a32;
+ out[3] = b00 * a03 + b01 * a13 + b02 * a23 + b03 * a33;
+ out[4] = b10 * a00 + b11 * a10 + b12 * a20 + b13 * a30;
+ out[5] = b10 * a01 + b11 * a11 + b12 * a21 + b13 * a31;
+ out[6] = b10 * a02 + b11 * a12 + b12 * a22 + b13 * a32;
+ out[7] = b10 * a03 + b11 * a13 + b12 * a23 + b13 * a33;
+ out[8] = b20 * a00 + b21 * a10 + b22 * a20 + b23 * a30;
+ out[9] = b20 * a01 + b21 * a11 + b22 * a21 + b23 * a31;
+ out[10] = b20 * a02 + b21 * a12 + b22 * a22 + b23 * a32;
+ out[11] = b20 * a03 + b21 * a13 + b22 * a23 + b23 * a33;
+ out[12] = b30 * a00 + b31 * a10 + b32 * a20 + b33 * a30;
+ out[13] = b30 * a01 + b31 * a11 + b32 * a21 + b33 * a31;
+ out[14] = b30 * a02 + b31 * a12 + b32 * a22 + b33 * a32;
+ out[15] = b30 * a03 + b31 * a13 + b32 * a23 + b33 * a33;
+ }
+
+ /**
+ * Transforms a vec3 with the given matrix
+ * 4th vector component is implicitly '1'
+ *
+ * @param {mat4} mat mat4 to transform the vector with
+ * @param {vec3} vec vec3 to transform
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void multiplyVec3(Vector3 vec, [Vector3 dest]) {
+ if (null == dest) {
+ dest = vec;
+ }
+
+ var x = vec[0], y = vec[1], z = vec[2];
+
+ dest[0] = _dest[0] * x + _dest[4] * y + _dest[8] * z + _dest[12];
+ dest[1] = _dest[1] * x + _dest[5] * y + _dest[9] * z + _dest[13];
+ dest[2] = _dest[2] * x + _dest[6] * y + _dest[10] * z + _dest[14];
+ }
+
+ /**
+ * Transforms a vec4 with the given matrix
+ *
+ * @param {mat4} mat mat4 to transform the vector with
+ * @param {vec4} vec vec4 to transform
+ * @param {vec4} [dest] vec4 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec4} dest if specified, vec otherwise
+ */
+ void multiplyVec4(Vector4 vec, [Vector4 dest]) {
+ if (null == dest) {
+ dest = vec;
+ }
+
+ var x = vec[0], y = vec[1], z = vec[2], w = vec[3];
+
+ dest[0] = _dest[0] * x + _dest[4] * y + _dest[8] * z + _dest[12] * w;
+ dest[1] = _dest[1] * x + _dest[5] * y + _dest[9] * z + _dest[13] * w;
+ dest[2] = _dest[2] * x + _dest[6] * y + _dest[10] * z + _dest[14] * w;
+ dest[3] = _dest[3] * x + _dest[7] * y + _dest[11] * z + _dest[15] * w;
+ }
+
+ /**
+ * Translates a matrix by the given vector
+ *
+ * @param {mat4} mat mat4 to translate
+ * @param {vec3} vec vec3 specifying the translation
+ * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
+ *
+ * @returns {mat4} dest if specified, mat otherwise
+ */
+ void translate(Vector3 vec, [Matrix4 dest]) {
+ var x = vec[0], y = vec[1], z = vec[2],
+ a00, a01, a02, a03,
+ a10, a11, a12, a13,
+ a20, a21, a22, a23;
+
+ if (null == dest) {
+ _dest[12] = _dest[0] * x + _dest[4] * y + _dest[8] * z + _dest[12];
+ _dest[13] = _dest[1] * x + _dest[5] * y + _dest[9] * z + _dest[13];
+ _dest[14] = _dest[2] * x + _dest[6] * y + _dest[10] * z + _dest[14];
+ _dest[15] = _dest[3] * x + _dest[7] * y + _dest[11] * z + _dest[15];
+ } else {
+ a00 = _dest[0]; a01 = _dest[1]; a02 = _dest[2]; a03 = _dest[3];
+ a10 = _dest[4]; a11 = _dest[5]; a12 = _dest[6]; a13 = _dest[7];
+ a20 = _dest[8]; a21 = _dest[9]; a22 = _dest[10]; a23 = _dest[11];
+
+ dest[0] = a00; dest[1] = a01; dest[2] = a02; dest[3] = a03;
+ dest[4] = a10; dest[5] = a11; dest[6] = a12; dest[7] = a13;
+ dest[8] = a20; dest[9] = a21; dest[10] = a22; dest[11] = a23;
+
+ dest[12] = a00 * x + a10 * y + a20 * z + _dest[12];
+ dest[13] = a01 * x + a11 * y + a21 * z + _dest[13];
+ dest[14] = a02 * x + a12 * y + a22 * z + _dest[14];
+ dest[15] = a03 * x + a13 * y + a23 * z + _dest[15];
+ }
+ }
+
+ /**
+ * Scales a matrix by the given vector
+ *
+ * @param {mat4} mat mat4 to scale
+ * @param {vec3} vec vec3 specifying the scale for each axis
+ * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
+ *
+ * @param {mat4} dest if specified, mat otherwise
+ */
+ void scale(Vector3 vec, [Matrix4 dest]) {
+ var x = vec[0], y = vec[1], z = vec[2];
+
+ if (null == dest) {
+ _dest[0] *= x;
+ _dest[1] *= x;
+ _dest[2] *= x;
+ _dest[3] *= x;
+ _dest[4] *= y;
+ _dest[5] *= y;
+ _dest[6] *= y;
+ _dest[7] *= y;
+ _dest[8] *= z;
+ _dest[9] *= z;
+ _dest[10] *= z;
+ _dest[11] *= z;
+ } else {
+ dest[0] = _dest[0] * x;
+ dest[1] = _dest[1] * x;
+ dest[2] = _dest[2] * x;
+ dest[3] = _dest[3] * x;
+ dest[4] = _dest[4] * y;
+ dest[5] = _dest[5] * y;
+ dest[6] = _dest[6] * y;
+ dest[7] = _dest[7] * y;
+ dest[8] = _dest[8] * z;
+ dest[9] = _dest[9] * z;
+ dest[10] = _dest[10] * z;
+ dest[11] = _dest[11] * z;
+ dest[12] = _dest[12];
+ dest[13] = _dest[13];
+ dest[14] = _dest[14];
+ dest[15] = _dest[15];
+ }
+ }
+
+ /**
+ * Rotates a matrix by the given angle around the specified axis
+ * If rotating around a primary axis (X,Y,Z) one of the specialized rotation functions should be used instead for performance
+ *
+ * @param {mat4} mat mat4 to rotate
+ * @param {number} angle Angle (in radians) to rotate
+ * @param {vec3} axis vec3 representing the axis to rotate around
+ * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
+ *
+ * @returns {mat4} dest if specified, mat otherwise
+ */
+ void rotate(num angle, Vector3 axis, [Matrix4 dest]) {
+ var x = axis[0], y = axis[1], z = axis[2],
+ len = Math.sqrt(x * x + y * y + z * z),
+ s, c, t,
+ a00, a01, a02, a03,
+ a10, a11, a12, a13,
+ a20, a21, a22, a23,
+ b00, b01, b02,
+ b10, b11, b12,
+ b20, b21, b22;
+
+ if (0 == len) {
+ return null;
+ }
+ if (len != 1) {
+ len = 1 / len;
+ x *= len;
+ y *= len;
+ z *= len;
+ }
+
+ s = Math.sin(angle);
+ c = Math.cos(angle);
+ t = 1 - c;
+
+ a00 = _dest[0]; a01 = _dest[1]; a02 = _dest[2]; a03 = _dest[3];
+ a10 = _dest[4]; a11 = _dest[5]; a12 = _dest[6]; a13 = _dest[7];
+ a20 = _dest[8]; a21 = _dest[9]; a22 = _dest[10]; a23 = _dest[11];
+
+ // Construct the elements of the rotation matrix
+ b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s;
+ b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s;
+ b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c;
+
+ var out;
+ if (null == dest) {
+ out = _dest;
+ } else { // If the source and destination differ, copy the unchanged last row
+ out = dest;
+ out[12] = _dest[12];
+ out[13] = _dest[13];
+ out[14] = _dest[14];
+ out[15] = _dest[15];
+ }
+
+ // Perform rotation-specific matrix multiplication
+ out[0] = a00 * b00 + a10 * b01 + a20 * b02;
+ out[1] = a01 * b00 + a11 * b01 + a21 * b02;
+ out[2] = a02 * b00 + a12 * b01 + a22 * b02;
+ out[3] = a03 * b00 + a13 * b01 + a23 * b02;
+
+ out[4] = a00 * b10 + a10 * b11 + a20 * b12;
+ out[5] = a01 * b10 + a11 * b11 + a21 * b12;
+ out[6] = a02 * b10 + a12 * b11 + a22 * b12;
+ out[7] = a03 * b10 + a13 * b11 + a23 * b12;
+
+ out[8] = a00 * b20 + a10 * b21 + a20 * b22;
+ out[9] = a01 * b20 + a11 * b21 + a21 * b22;
+ out[10] = a02 * b20 + a12 * b21 + a22 * b22;
+ out[11] = a03 * b20 + a13 * b21 + a23 * b22;
+ }
+
+ /**
+ * Rotates a matrix by the given angle around the X axis
+ *
+ * @param {mat4} mat mat4 to rotate
+ * @param {number} angle Angle (in radians) to rotate
+ * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
+ *
+ * @returns {mat4} dest if specified, mat otherwise
+ */
+ void rotateX(num angle, [Matrix4 dest]) {
+ var s = Math.sin(angle),
+ c = Math.cos(angle),
+ a10 = _dest[4],
+ a11 = _dest[5],
+ a12 = _dest[6],
+ a13 = _dest[7],
+ a20 = _dest[8],
+ a21 = _dest[9],
+ a22 = _dest[10],
+ a23 = _dest[11];
+
+ var out;
+ if (null == dest) {
+ out = _dest;
+ } else { // If the source and destination differ, copy the unchanged rows
+ out = _dest;
+ out[0] = _dest[0];
+ out[1] = _dest[1];
+ out[2] = _dest[2];
+ out[3] = _dest[3];
+
+ out[12] = _dest[12];
+ out[13] = _dest[13];
+ out[14] = _dest[14];
+ out[15] = _dest[15];
+ }
+
+ // Perform axis-specific matrix multiplication
+ out[4] = a10 * c + a20 * s;
+ out[5] = a11 * c + a21 * s;
+ out[6] = a12 * c + a22 * s;
+ out[7] = a13 * c + a23 * s;
+
+ out[8] = a10 * -s + a20 * c;
+ out[9] = a11 * -s + a21 * c;
+ out[10] = a12 * -s + a22 * c;
+ out[11] = a13 * -s + a23 * c;
+ }
+
+ /**
+ * Rotates a matrix by the given angle around the Y axis
+ *
+ * @param {mat4} mat mat4 to rotate
+ * @param {number} angle Angle (in radians) to rotate
+ * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
+ *
+ * @returns {mat4} dest if specified, mat otherwise
+ */
+ void rotateY(num angle, [Matrix4 dest]) {
+ var s = Math.sin(angle),
+ c = Math.cos(angle),
+ a00 = _dest[0],
+ a01 = _dest[1],
+ a02 = _dest[2],
+ a03 = _dest[3],
+ a20 = _dest[8],
+ a21 = _dest[9],
+ a22 = _dest[10],
+ a23 = _dest[11];
+
+ var out;
+ if (null == dest) {
+ out = _dest;
+ } else { // If the source and destination differ, copy the unchanged rows
+ out = dest;
+ out[4] = _dest[4];
+ out[5] = _dest[5];
+ out[6] = _dest[6];
+ out[7] = _dest[7];
+
+ out[12] = _dest[12];
+ out[13] = _dest[13];
+ out[14] = _dest[14];
+ out[15] = _dest[15];
+ }
+
+ // Perform axis-specific matrix multiplication
+ out[0] = a00 * c + a20 * -s;
+ out[1] = a01 * c + a21 * -s;
+ out[2] = a02 * c + a22 * -s;
+ out[3] = a03 * c + a23 * -s;
+
+ out[8] = a00 * s + a20 * c;
+ out[9] = a01 * s + a21 * c;
+ out[10] = a02 * s + a22 * c;
+ out[11] = a03 * s + a23 * c;
+ }
+
+ /**
+ * Rotates a matrix by the given angle around the Z axis
+ *
+ * @param {mat4} mat mat4 to rotate
+ * @param {number} angle Angle (in radians) to rotate
+ * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to mat
+ *
+ * @returns {mat4} dest if specified, mat otherwise
+ */
+ void rotateZ(num angle, [Matrix4 dest]) {
+ var s = Math.sin(angle),
+ c = Math.cos(angle),
+ a00 = _dest[0],
+ a01 = _dest[1],
+ a02 = _dest[2],
+ a03 = _dest[3],
+ a10 = _dest[4],
+ a11 = _dest[5],
+ a12 = _dest[6],
+ a13 = _dest[7];
+
+ var out;
+ if (null == dest) {
+ out = _dest;
+ } else { // If the source and destination differ, copy the unchanged last row
+ out = dest;
+ out[8] = _dest[8];
+ out[9] = _dest[9];
+ out[10] = _dest[10];
+ out[11] = _dest[11];
+
+ out[12] = _dest[12];
+ out[13] = _dest[13];
+ out[14] = _dest[14];
+ out[15] = _dest[15];
+ }
+
+ // Perform axis-specific matrix multiplication
+ out[0] = a00 * c + a10 * s;
+ out[1] = a01 * c + a11 * s;
+ out[2] = a02 * c + a12 * s;
+ out[3] = a03 * c + a13 * s;
+
+ out[4] = a00 * -s + a10 * c;
+ out[5] = a01 * -s + a11 * c;
+ out[6] = a02 * -s + a12 * c;
+ out[7] = a03 * -s + a13 * c;
+ }
+
+ /**
+ * Generates a frustum matrix with the given bounds
+ *
+ * @param {number} left Left bound of the frustum
+ * @param {number} right Right bound of the frustum
+ * @param {number} bottom Bottom bound of the frustum
+ * @param {number} top Top bound of the frustum
+ * @param {number} near Near bound of the frustum
+ * @param {number} far Far bound of the frustum
+ * @param {mat4} [dest] mat4 frustum matrix will be written into
+ *
+ * @returns {mat4} dest if specified, a new mat4 otherwise
+ */
+ static Matrix4 frustum(num left, num right, num bottom, num top, num near, num far, [Matrix4 dest]) {
+ var out = (null == dest ? new Float32Array(16) : dest);
+ var rl = (right - left),
+ tb = (top - bottom),
+ fn = (far - near);
+
+ out[0] = (near * 2) / rl;
+ out[1] = 0;
+ out[2] = 0;
+ out[3] = 0;
+ out[4] = 0;
+ out[5] = (near * 2) / tb;
+ out[6] = 0;
+ out[7] = 0;
+ out[8] = (right + left) / rl;
+ out[9] = (top + bottom) / tb;
+ out[10] = -(far + near) / fn;
+ out[11] = -1;
+ out[12] = 0;
+ out[13] = 0;
+ out[14] = -(far * near * 2) / fn;
+ out[15] = 0;
+
+ return (null == dest ? new Matrix4.fromFloat32Array(out) : null);
+ }
+
+ /**
+ * Generates a perspective projection matrix with the given bounds
+ *
+ * @param {number} fovy Vertical field of view
+ * @param {number} aspect Aspect ratio. typically viewport width/height
+ * @param {number} near Near bound of the frustum
+ * @param {number} far Far bound of the frustum
+ * @param {mat4} [dest] mat4 frustum matrix will be written into
+ *
+ * @returns {mat4} dest if specified, a new mat4 otherwise
+ */
+ static Matrix4 perspective(num fovy, num aspect, num near, num far, [Matrix4 dest]) {
+ var top = near * Math.tan(fovy * Math.PI / 360.0),
+ right = top * aspect;
+ return Matrix4.frustum(-right, right, -top, top, near, far, dest);
+ }
+
+ /**
+ * Generates a orthogonal projection matrix with the given bounds
+ *
+ * @param {number} left Left bound of the frustum
+ * @param {number} right Right bound of the frustum
+ * @param {number} bottom Bottom bound of the frustum
+ * @param {number} top Top bound of the frustum
+ * @param {number} near Near bound of the frustum
+ * @param {number} far Far bound of the frustum
+ * @param {mat4} [dest] mat4 frustum matrix will be written into
+ *
+ * @returns {mat4} dest if specified, a new mat4 otherwise
+ */
+ static Matrix4 ortho(num left, num right, num bottom, num top, num near, num far, [Matrix4 dest]) {
+ var out = (null == dest ? new Float32Array(16) : dest);
+ var rl = (right - left),
+ tb = (top - bottom),
+ fn = (far - near);
+
+ out[0] = 2 / rl;
+ out[1] = 0;
+ out[2] = 0;
+ out[3] = 0;
+ out[4] = 0;
+ out[5] = 2 / tb;
+ out[6] = 0;
+ out[7] = 0;
+ out[8] = 0;
+ out[9] = 0;
+ out[10] = -2 / fn;
+ out[11] = 0;
+ out[12] = -(left + right) / rl;
+ out[13] = -(top + bottom) / tb;
+ out[14] = -(far + near) / fn;
+ out[15] = 1;
+ return (null == dest ? new Matrix4.fromFloat32Array(out) : null);
+ }
+
+ /**
+ * Generates a look-at matrix with the given eye position, focal point, and up axis
+ *
+ * @param {vec3} eye Position of the viewer
+ * @param {vec3} center Point the viewer is looking at
+ * @param {vec3} up vec3 pointing "up"
+ * @param {mat4} [dest] mat4 frustum matrix will be written into
+ *
+ * @returns {mat4} dest if specified, a new mat4 otherwise
+ */
+ static Matrix4 lookAt(Vector3 eye, Vector3 center, Vector3 up, [Matrix4 dest]) {
+ var out = (null == dest ? new Float32Array(16) : dest);
+
+ var x0, x1, x2, y0, y1, y2, z0, z1, z2, len,
+ eyex = eye[0],
+ eyey = eye[1],
+ eyez = eye[2],
+ upx = up[0],
+ upy = up[1],
+ upz = up[2],
+ centerx = center[0],
+ centery = center[1],
+ centerz = center[2];
+
+ if (eyex === centerx && eyey === centery && eyez === centerz) {
+ if (null == dest) {
+ return new Matrix4.identity();
+ } else {
+ out.identity();
+ return out;
+ }
+ }
+
+ //vec3.direction(eye, center, z);
+ z0 = eyex - centerx;
+ z1 = eyey - centery;
+ z2 = eyez - centerz;
+
+ // normalize (no check needed for 0 because of early return)
+ len = 1 / Math.sqrt(z0 * z0 + z1 * z1 + z2 * z2);
+ z0 *= len;
+ z1 *= len;
+ z2 *= len;
+
+ //vec3.normalize(vec3.cross(up, z, x));
+ x0 = upy * z2 - upz * z1;
+ x1 = upz * z0 - upx * z2;
+ x2 = upx * z1 - upy * z0;
+ len = Math.sqrt(x0 * x0 + x1 * x1 + x2 * x2);
+ if (!len) {
+ x0 = 0;
+ x1 = 0;
+ x2 = 0;
+ } else {
+ len = 1 / len;
+ x0 *= len;
+ x1 *= len;
+ x2 *= len;
+ }
+
+ //vec3.normalize(vec3.cross(z, x, y));
+ y0 = z1 * x2 - z2 * x1;
+ y1 = z2 * x0 - z0 * x2;
+ y2 = z0 * x1 - z1 * x0;
+
+ len = Math.sqrt(y0 * y0 + y1 * y1 + y2 * y2);
+ if (!len) {
+ y0 = 0;
+ y1 = 0;
+ y2 = 0;
+ } else {
+ len = 1 / len;
+ y0 *= len;
+ y1 *= len;
+ y2 *= len;
+ }
+
+ out[0] = x0;
+ out[1] = y0;
+ out[2] = z0;
+ out[3] = 0;
+ out[4] = x1;
+ out[5] = y1;
+ out[6] = z1;
+ out[7] = 0;
+ out[8] = x2;
+ out[9] = y2;
+ out[10] = z2;
+ out[11] = 0;
+ out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
+ out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
+ out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
+ out[15] = 1;
+
+ return (null == dest ? new Matrix4.fromFloat32Array(out) : null);
+ }
+
+ /**
+ * Creates a matrix from a quaternion rotation and vector translation
+ * This is equivalent to (but much faster than):
+ *
+ * mat4.identity(dest);
+ * mat4.translate(dest, vec);
+ * var quatMat = mat4.create();
+ * quat4.toMat4(quat, quatMat);
+ * mat4.multiply(dest, quatMat);
+ *
+ * @param {quat4} quat Rotation quaternion
+ * @param {vec3} vec Translation vector
+ * @param {mat4} [dest] mat4 receiving operation result. If not specified result is written to a new mat4
+ *
+ * @returns {mat4} dest if specified, a new mat4 otherwise
+ */
+ static Matrix4 fromRotationTranslation(Quat4 quat, Vector3 vec, [Matrix4 dest]) {
+ var out = (null == dest ? new Float32Array(16) : dest);
+
+ // Quaternion math
+ var x = quat[0], y = quat[1], z = quat[2], w = quat[3],
+ x2 = x + x,
+ y2 = y + y,
+ z2 = z + z,
+
+ xx = x * x2,
+ xy = x * y2,
+ xz = x * z2,
+ yy = y * y2,
+ yz = y * z2,
+ zz = z * z2,
+ wx = w * x2,
+ wy = w * y2,
+ wz = w * z2;
+
+ out[0] = 1 - (yy + zz);
+ out[1] = xy + wz;
+ out[2] = xz - wy;
+ out[3] = 0;
+ out[4] = xy - wz;
+ out[5] = 1 - (xx + zz);
+ out[6] = yz + wx;
+ out[7] = 0;
+ out[8] = xz + wy;
+ out[9] = yz - wx;
+ out[10] = 1 - (xx + yy);
+ out[11] = 0;
+ out[12] = vec[0];
+ out[13] = vec[1];
+ out[14] = vec[2];
+ out[15] = 1;
+
+ return (null == dest ? new Matrix4.fromFloat32Array(out) : null);
+ }
+
+ /**
+ * Returns a string representation of a mat4
+ *
+ * @param {mat4} mat mat4 to represent as a string
+ *
+ * @returns {string} String representation of mat
+ */
+ String toString() {
+ return '[' + _dest[0] + ', ' + _dest[1] + ', ' + _dest[2] + ', ' + _dest[3] + "\n" +
+ ' ' + _dest[4] + ', ' + _dest[5] + ', ' + _dest[6] + ', ' + _dest[7] + "\n" +
+ ' ' + _dest[8] + ', ' + _dest[9] + ', ' + _dest[10] + ', ' + _dest[11] + "\n" +
+ ' ' + _dest[12] + ', ' + _dest[13] + ', ' + _dest[14] + ', ' + _dest[15] + ']';
+ }
+}
View
36 teapot_css/gl-matrix-dart/Quat4.dart
@@ -0,0 +1,36 @@
+
+class Quat4 {
+
+ Float32Array _items;
+
+ Quat4() {
+ _items = new Float32Array(4);
+ }
+
+ Quat4.identity() {
+ _items = new Float32Array(4);
+ _items[0] = 0;
+ _items[1] = 0;
+ _items[2] = 0;
+ _items[3] = 1;
+ }
+
+ operator [](int index) => _items[index];
+
+ operator []=(int index, double value) => _items[index] = value;
+
+
+ Float32Array get array() => _items;
+ set array(Float32Array array) => _items = array;
+
+
+ void identity() {
+ _items[0] = 0;
+ _items[1] = 0;
+ _items[2] = 0;
+ _items[3] = 1;
+ }
+
+
+
+}
View
6 teapot_css/gl-matrix-dart/README.md
@@ -0,0 +1,6 @@
+## gl-matrix for Dart
+
+This is port of gl-matrix JavaScript matrix and vector library for high performance WebGL apps ([original repo](https://github.com/toji/gl-matrix)). I'll try to keep it up to date with the original repo.
+
+It's not complete, there are just Vector3 and Matrix4 fully implemented at this moment and I'm not sure I didn't make any bugs while rewriting to Dart (when I see some unit testing library for Dart I'll add tests).
+Also it's performance might not be the same like original pure JavaScript implementation because this port is object-oriented. Feel free to fork and send pull requests.
View
372 teapot_css/gl-matrix-dart/Vector3.dart
@@ -0,0 +1,372 @@
+class Vector3 {
+
+ Float32Array _items;
+
+ Vector3() {
+ _items = new Float32Array(3);
+ }
+
+ Vector3.fromValues(num x, num y, num z) {
+ _items = new Float32Array(3); //.fromList([x, y, z]);
+ _items[0] = x;
+ _items[1] = y;
+ _items[2] = z;
+ }
+
+ Vector3.fromList(List<num> list) {
+ _items = new Float32Array.fromList(list);
+ }
+
+ operator [](int index) => _items[index];
+
+ operator []=(int index, double value) => _items[index] = value;
+
+ Float32Array get array() => _items;
+ set array(Float32Array array) => _items = array;
+
+
+ /**
+ * Performs a vector addition
+ *
+ * @param {vec3} vec First operand
+ * @param {vec3} vec2 Second operand
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void add(Vector3 vec2, [Vector3 dest]) {
+ if (null == dest) {
+ _items[0] += vec2[0];
+ _items[1] += vec2[1];
+ _items[2] += vec2[2];
+ } else {
+ dest[0] = _items[0] + vec2[0];
+ dest[1] = _items[1] + vec2[1];
+ dest[2] = _items[2] + vec2[2];
+ }
+ }
+
+ /**
+ * Performs a vector subtraction
+ *
+ * @param {vec3} vec First operand
+ * @param {vec3} vec2 Second operand
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void subtract(Vector3 vec2, [Vector3 dest]) {
+ if (null == dest) {
+ _items[0] -= vec2[0];
+ _items[1] -= vec2[1];
+ _items[2] -= vec2[2];
+ } else {
+ dest[0] = _items[0] - vec2[0];
+ dest[1] = _items[1] - vec2[1];
+ dest[2] = _items[2] - vec2[2];
+ }
+ }
+
+ /**
+ * Performs a vector multiplication
+ *
+ * @param {vec3} vec First operand
+ * @param {vec3} vec2 Second operand
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void multiply(Vector3 vec2, [Vector3 dest]) {
+ if (dest == null) {
+ _items[0] *= vec2[0];
+ _items[1] *= vec2[1];
+ _items[2] *= vec2[2];
+ } else {
+ dest[0] = _items[0] * vec2[0];
+ dest[1] = _items[1] * vec2[1];
+ dest[2] = _items[2] * vec2[2];
+ }
+ }
+
+ /**
+ * Negates the components of a vec3
+ *
+ * @param {vec3} vec vec3 to negate
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void negate([Vector3 dest]) {
+ var out = (null == dest ? _items : dest);
+
+ out[0] = -_items[0];
+ out[1] = -_items[1];
+ out[2] = -_items[2];
+ }
+
+ /**
+ * Multiplies the components of a vec3 by a scalar value
+ *
+ * @param {vec3} vec vec3 to scale
+ * @param {number} val Value to scale by
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void scale(num val, [Vector3 dest]) {
+ if (null == dest) {
+ _items[0] *= val;
+ _items[1] *= val;
+ _items[2] *= val;
+ } else {
+ dest[0] = _items[0] * val;
+ dest[1] = _items[1] * val;
+ dest[2] = _items[2] * val;
+ }
+ }
+
+ /**
+ * Generates a unit vector of the same direction as the provided vec3
+ * If vector length is 0, returns [0, 0, 0]
+ *
+ * @param {vec3} vec vec3 to normalize
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void normalize([Vector3 dest]) {
+ var out = (null == dest ? _items : dest);
+
+ var x = _items[0], y = _items[1], z = _items[2],
+ len = Math.sqrt(x * x + y * y + z * z);
+
+ if (0 == len) {
+ out[0] = 0;
+ out[1] = 0;
+ out[2] = 0;
+ } else if (len == 1) {
+ out[0] = x;
+ out[1] = y;
+ out[2] = z;
+ } else {
+ len = 1 / len;
+ out[0] = x * len;
+ out[1] = y * len;
+ out[2] = z * len;
+ }
+ }
+
+ /**
+ * Generates the cross product of two vec3s
+ *
+ * @param {vec3} vec First operand
+ * @param {vec3} vec2 Second operand
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void cross(Vector3 vec2, [Vector3 dest]) {
+ var out = (null == dest ? _items : dest);
+
+ double x = _items[0], y = _items[1], z = _items[2],
+ x2 = vec2[0], y2 = vec2[1], z2 = vec2[2];
+
+ out[0] = y * z2 - z * y2;
+ out[1] = z * x2 - x * z2;
+ out[2] = x * y2 - y * x2;
+ }
+
+ /**
+ * Caclulates the length of a vec3
+ *
+ * @param {vec3} vec vec3 to calculate length of
+ *
+ * @returns {number} Length of vec
+ */
+ double length() {
+ double x = _items[0], y = _items[1], z = _items[2];
+ return Math.sqrt(x * x + y * y + z * z);
+ }
+
+ /**
+ * Caclulates the dot product of two vec3s
+ *
+ * @param {vec3} vec First operand
+ * @param {vec3} vec2 Second operand
+ *
+ * @returns {number} Dot product of vec and vec2
+ */
+ double dot(Vector3 vec2) {
+ return _items[0] * vec2[0] + _items[1] * vec2[1] + _items[2] * vec2[2];
+ }
+
+ /**
+ * Generates a unit vector pointing from one vector to another
+ *
+ * @param {vec3} vec Origin vec3
+ * @param {vec3} vec2 vec3 to point to
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void direction(Vector3 vec2, [Vector3 dest]) {
+ var out = (null == dest ? _items : dest);
+
+ double
+ x = _items[0] - vec2[0],
+ y = _items[1] - vec2[1],
+ z = _items[2] - vec2[2],
+ len = Math.sqrt(x * x + y * y + z * z);
+
+ if (0 == len) {
+ out[0] = 0;
+ out[1] = 0;
+ out[2] = 0;
+ } else {
+ len = 1 / len;
+ out[0] = x * len;
+ out[1] = y * len;
+ out[2] = z * len;
+ }
+ }
+
+ /**
+ * Performs a linear interpolation between two vec3
+ *
+ * @param {vec3} vec First vector
+ * @param {vec3} vec2 Second vector
+ * @param {number} lerp Interpolation amount between the two inputs
+ * @param {vec3} [dest] vec3 receiving operation result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void lerp(Vector3 vec2, double coef, [Vector3 dest]) {
+ var out = (null == dest ? _items : dest);
+
+ out[0] = _items[0] + coef * (vec2[0] - _items[0]);
+ out[1] = _items[1] + coef * (vec2[1] - _items[1]);
+ out[2] = _items[2] + coef * (vec2[2] - _items[2]);
+ }
+
+ /**
+ * Calculates the euclidian distance between two vec3
+ *
+ * Params:
+ * @param {vec3} vec First vector
+ * @param {vec3} vec2 Second vector
+ *
+ * @returns {number} Distance between vec and vec2
+ */
+ double dist(Vector3 vec2) {
+ double
+ x = vec2[0] - _items[0],
+ y = vec2[1] - _items[1],
+ z = vec2[2] - _items[2];
+
+ return Math.sqrt(x*x + y*y + z*z);
+ }
+
+ // Pre-allocated to prevent unecessary garbage collection
+ var _unprojectMat = null;
+ var _unprojectVec;
+ /**
+ * Projects the specified vec3 from screen space into object space
+ * Based on the <a href="http://webcvs.freedesktop.org/mesa/Mesa/src/glu/mesa/project.c?revision=1.4&view=markup">Mesa gluUnProject implementation</a>
+ *
+ * @param {vec3} vec Screen-space vector to project
+ * @param {mat4} view View matrix
+ * @param {mat4} proj Projection matrix
+ * @param {vec4} viewport Viewport as given to gl.viewport [x, y, width, height]
+ * @param {vec3} [dest] vec3 receiving unprojected result. If not specified result is written to vec
+ *
+ * @returns {vec3} dest if specified, vec otherwise
+ */
+ void unproject(Matrix4 view, Matrix4 proj, Vector4 viewport, [Vector3 dest]) {
+ var out = (null == dest ? _items : dest);
+
+ _unprojectVec = new Float32Array(4);
+
+ if (null == _unprojectMat) {
+ _unprojectMat = new Matrix4();
+ }
+
+ var m = _unprojectMat;
+ var v = _unprojectVec;
+
+ v[0] = (_items[0] - viewport[0]) * 2.0 / viewport[2] - 1.0;
+ v[1] = (_items[1] - viewport[1]) * 2.0 / viewport[3] - 1.0;
+ v[2] = 2.0 * _items[2] - 1.0;
+ v[3] = 1.0;
+
+ proj.multiply(view, m);
+ if (!m.inverse()) { return null; }
+
+ m.multiplyVec4(v);
+ if (v[3] === 0.0) { return null; }
+
+ out[0] = v[0] / v[3];
+ out[1] = v[1] / v[3];
+ out[2] = v[2] / v[3];
+ }
+
+ /**
+ * Generates a quaternion of rotation between two given normalized vectors
+ *
+ * @param {vec3} a Normalized source vector
+ * @param {vec3} b Normalized target vector
+ * @param {quat4} [dest] quat4 receiving operation result.
+ *
+ * @returns {quat4} dest if specified, a new quat4 otherwise
+ */
+ void rotationTo(Vector3 target, [Quat4 dest]) {
+ var out = (null == dest ? _items : dest);
+
+ Vector3 xUnitVec3 = new Vector3.fromValues(1,0,0);
+ Vector3 yUnitVec3 = new Vector3.fromValues(0,1,0);
+ Vector3 zUnitVec3 = new Vector3.fromValues(0,0,1);
+
+ double d = this.dot(target);
+ Vector3 axis = new Vector3();
+
+ if (d >= 1.0) {
+ dest.identity();
+ } else if (d < (0.000001 - 1.0)) {
+ xUnitVec3.cross(this, axis);
+ if (axis.length() < 0.000001) {
+ yUnitVec3.cross(this, axis);
+ }
+ if (axis.length() < 0.000001) {
+ zUnitVec3.cross(this, axis);
+ }
+ axis.normalize();
+ /** @TODO: check upstream for: quat4.fromAxisAngle(Math.PI, dest); */
+ } else {
+ double s = Math.sqrt((1.0 + d) * 2.0);
+ double sInv = 1.0 / s;
+ this.cross(target, axis);
+
+ out[0] = axis[0] * sInv;
+ out[1] = axis[1] * sInv;
+ out[2] = axis[2] * sInv;
+ out[3] = s * 0.5;
+ out.normalize();
+ }
+ if (_items[3] > 1.0) {
+ _items[3] = 1.0;
+ } else if (_items[3] < -1.0) {
+ _items[3] = -1.0;
+ }
+ }
+
+ /**
+ * Returns a string representation of a vector
+ *
+ * @param {vec3} vec Vector to represent as a string
+ *
+ * @returns {string} String representation of vec
+ */
+ String toString() {
+ return '[' + _items[0] + ', ' + _items[1] + ', ' + _items[2] + ']';
+ }
+
+}
View
18 teapot_css/gl-matrix-dart/Vector4.dart
@@ -0,0 +1,18 @@
+class Vector4 {
+
+ Float32Array _items;
+
+ Vector4() {
+ _items = new Float32Array(4);
+ }
+
+ Vector4.fromList(List<num> list) {
+ _items = new Float32Array.fromList(list);
+ }
+
+ operator [](int index) => _items[index];
+
+ operator []=(int index, double value) => _items[index] = value;
+
+
+}
View
14 teapot_css/gl-matrix-dart/gl-matrix.dart
@@ -0,0 +1,14 @@
+#library('gl-matrix');
+
+#import('dart:html');
+
+#source('Matrix4.dart');
+#source('Matrix3.dart');
+#source('Vector3.dart');
+#source('Vector4.dart');
+#source('Quat4.dart');
+
+/**
+ * All these classes are based on: https://github.com/toji/gl-matrix
+ * commit: 45cea95f2e8baa3ff76d3834e34b3669139a18a5
+ */
View
BIN teapot_css/skybox-negx.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN teapot_css/skybox-negy.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN teapot_css/skybox-negz.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN teapot_css/skybox-posx.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN teapot_css/skybox-posy.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
BIN teapot_css/skybox-posz.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
430 teapot_css/teapot.dart
430 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
5,389 teapot_css/teapot.dart.js
5,389 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
3,777 teapot_css/teapot.dart.js_
3,777 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
85 teapot_css/teapot.html
@@ -0,0 +1,85 @@
+<!DOCTYPE html>
+
+<html>
+<!--
+Copyright (c) 2011 The Chromium Authors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ * Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with the
+distribution.
+ * Neither the name of Google Inc. nor the names of its
+contributors may be used to endorse or promote products derived from
+this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+-->
+ <head>
+ <title>teapot</title>
+ <style type="text/css">
+ #container {
+ -webkit-transform-origin:400px 300px;
+ -webkit-transform: perspective(800) translateX(0) rotateY(0) rotateZ(0) translateZ(0px);
+ -webkit-transition: -webkit-transform 1s;
+ }
+ #container.tilted {
+ -webkit-transform: perspective(800) translateX(200px) rotateY(60deg) rotateZ(320deg) translateZ(0px);
+ }
+
+ @-webkit-keyframes rock {
+ 0% { -webkit-transform: translateX(-250px) rotate3d(0,0,1, -30deg) }
+ 25% { -webkit-transform: translateX(-150px) rotate3d(0,0,1, 30deg) }
+ 50% { -webkit-transform: translateX(-50px) rotate3d(0,0,1, -30deg) }
+ 75% { -webkit-transform: translateX(50px) rotate3d(0,0,1, 30deg) }
+ 100% { -webkit-transform: translateX(150px) rotate3d(0,0,1, -30deg) }
+ }
+ </style>
+ </head>
+ <body>
+ <div id="container" style="position: relative;">
+<P>
+Click and drag to rotate teapot. <br>
+<a href="http://o3d.googlecode.com/svn/trunk/samples/rotatemodel.html">Original demo</a> from <a href="http://code.google.com/apis/o3d/">O3D</a>.
+
+<P>
+Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse est ligula, egestas sed tempor vitae, pellentesque at massa. Nam a aliquet neque. Nulla a mauris eu lectus faucibus consectetur quis sed sem. Quisque posuere malesuada elementum. Etiam tortor purus, eleifend sit amet mattis vitae, ultrices vitae lorem. Nullam sodales risus eu nisi mollis ullamcorper. Morbi at nisl mauris. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Donec et eros lobortis sem lobortis luctus eget at arcu. In feugiat mollis purus in lobortis. Suspendisse sed nulla id augue dictum vulputate. Fusce lectus eros, dictum ac hendrerit sit amet, laoreet ultrices leo.
+
+<P>
+Morbi pellentesque, metus sit amet auctor convallis, massa lectus interdum dui, vitae auctor diam sapien vel metus. Suspendisse faucibus, erat pellentesque tristique egestas, mi justo porta velit, vitae ornare mauris metus sit amet diam. Praesent posuere dapibus eleifend. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean lorem neque, adipiscing eget egestas eget, semper rutrum velit. Praesent blandit tempor convallis. Ut scelerisque, magna pharetra consequat molestie, est arcu convallis justo, sed volutpat sem nisl vel ipsum. Donec euismod risus ut nibh accumsan eget rhoncus tellus vehicula. Etiam vulputate lorem id odio tempus quis suscipit sapien sollicitudin. Duis fringilla hendrerit elit, eget fringilla lectus consequat quis. Nunc suscipit vehicula lorem vel interdum. Sed lectus diam, mattis sed feugiat eget, ullamcorper et velit. Fusce placerat, nisi eget feugiat volutpat, leo diam porta velit, eget volutpat risus mauris sed velit.
+
+<P>
+Vestibulum quam augue, vehicula ut congue nec, pulvinar in risus. Morbi eget odio ac lectus porta pretium. In hac habitasse platea dictumst. Praesent libero turpis, interdum sed ultricies at, tempor ut justo. Suspendisse potenti. Phasellus venenatis, leo et accumsan ullamcorper, orci tortor hendrerit magna, nec consequat tellus metus nec neque. Duis eu dignissim nisi. Nulla convallis aliquam est, eu interdum metus malesuada non. Sed in turpis purus. Phasellus accumsan odio lacinia nisl auctor eget tempor nunc euismod. Vivamus malesuada, nibh ut congue sodales, nulla lacus elementum tellus, et mollis nibh sapien luctus dolor. Praesent egestas purus ullamcorper massa ultricies eget fermentum quam consequat. Proin augue nibh, dignissim eu tempus eget, eleifend eget sapien.
+
+<P>
+Vivamus at mauris velit. Pellentesque feugiat dapibus orci eu gravida. Vestibulum et ipsum lectus, quis accumsan libero. Ut sit amet odio tortor. Donec ullamcorper eleifend magna, sit amet ultricies quam placerat ut. Proin ac ante sem, quis egestas mi. Sed ultricies laoreet elit nec tempus. Curabitur metus velit, fringilla nec congue non, placerat et nibh. Nulla eget massa odio, ut imperdiet lacus. Sed venenatis bibendum faucibus. Vivamus mattis diam sagittis risus volutpat at condimentum ante ultricies. Mauris vitae congue nisi. Aliquam erat volutpat. Curabitur faucibus gravida turpis, vel eleifend risus malesuada consectetur. Ut non massa non neque ullamcorper euismod. Sed dictum leo eu mi egestas tincidunt. Nullam leo magna, interdum adipiscing condimentum ut, ullamcorper id erat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Praesent vestibulum odio sit amet nunc interdum sagittis. Maecenas egestas sodales felis sed placerat.
+
+<P>
+Vestibulum placerat scelerisque lectus eu facilisis. Phasellus eleifend tempus gravida. Vivamus id tortor sapien. Phasellus auctor rutrum iaculis. Phasellus vitae cursus enim. Fusce vitae mi sed nisl rhoncus mollis sed vitae enim. Vivamus arcu justo, commodo ut luctus ac, molestie vitae enim. Curabitur hendrerit, odio adipiscing volutpat cursus, lorem ante congue lectus, non scelerisque nunc tellus eget magna. Vestibulum ac sem augue, ac convallis nibh. Ut sed nisi nec tortor suscipit posuere at quis sapien. Pellentesque at lorem neque, vitae convallis diam. Etiam sagittis consequat augue, porta adipiscing lorem pretium a. Sed tincidunt turpis sem. Nunc vel quam a odio blandit accumsan. Ut porttitor porttitor vestibulum. Quisque sit amet felis blandit erat sollicitudin cursus commodo id eros.
+ <div style="position: absolute; top: 60px; width: 100%;">
+ <!-- Couldn't figure out how to get this effect without using the "center" tag -->
+ <center>
+ <canvas id="c" width="800" height="600" class="centeredcanvas">
+ </canvas>
+ </center>
+ </div>
+ </div>
+ <script type="application/dart" src="teapot.dart"></script>
+ <script src="http://dart.googlecode.com/svn/branches/bleeding_edge/dart/client/dart.js"></script>
+ </body>
+</html>
View
68 teapot_css/utils/webgl-tutorials.css
@@ -0,0 +1,68 @@
+/* Licensed under a BSD license. See ../license.html for license */
+
+html, body {
+ background-color: #aaa;
+ font-family: Sans-Serif;
+}
+
+canvas {
+ background-color: #fff;
+ border: 1px solid black;
+}
+
+#uiContainer {
+ position: absolute;
+ top: 10px;
+ right: 10px;
+ z-index: 3;
+}
+
+.gman-slider-label {
+ float: left;
+ gbackground-color: green;
+}
+.gman-slider-value {
+ float: right;
+ gbackground-color: blue;
+}
+.gman-slider-upper {
+ gbackground-color: yellow;
+ height: 1.5em;
+}
+.gman-slider-outer {
+ gbackground-color: purple;
+ height: 2.5em;
+}
+.gman-slider-slider {
+ font-size: x-small;
+ gbackground-color: red;
+}
+
+/* styles to apply if in an iframe */
+
+body.iframe {
+ width: 100%;
+ height: 100%;
+ margin: 0px;
+ padding: 0px;
+ overflow: hidden;
+}
+
+.iframe>.description {
+ display: none;
+}
+
+.iframe>canvas {
+ width: 100%;
+ height: 100%;
+}
+
+.iframe canvas {
+ border: none;
+}
+
+.iframe>#example {
+ width: 100%;
+ height: 100%;
+}
+
View
92 teapot_css/utils/webglUtils.dart
@@ -0,0 +1,92 @@
+#library('WebGLUtils');
+#import('dart:html');
+
+bool DEBUG=true;
+debugPrint(var p) {
+ if (DEBUG) {
+ print(p);
+ }
+}
+
+debugPrintMethod(f) {
+ if (DEBUG) {
+ f();
+ }
+}
+
+WebGLProgram createProgram(WebGLRenderingContext gl, [List<WebGLShader> shaders]) {
+ // Create program
+ var program = gl.createProgram();
+
+ // Iterate the shaders list
+ if (shaders is List<WebGLShader>) {
+ debugPrint("shaders.length = ${shaders.length}");
+ shaders.forEach((var shader) => gl.attachShader(program, shader));
+ }
+
+ // Link the shader to program
+ gl.linkProgram(program);
+
+ // Check the linked status
+ //var linked = gl.getProgramParameter(program, WebGLRenderingContext.LINK_STATUS);
+ //debugPrint("linked = ${linked}");
+
+ return program;
+}
+
+WebGLShader loadShader(WebGLRenderingContext gl, String shaderSource, int shaderType) {
+ // Create the shader object
+ var shader = gl.createShader(shaderType);
+
+ // Load the shader source
+ gl.shaderSource(shader, shaderSource);
+
+ // Compile the shader
+ gl.compileShader(shader);
+
+ // Check the compile status
+ // NOTE: getShaderParameter maybe borken in minfrog or frog compiler.
+ //var compiled = gl.getShaderParameter(shader, WebGLRenderingContext.COMPILE_STATUS);
+ //debugPrint("compiled = ${compiled}");
+
+ return shader;
+}
+
+WebGLShader createShaderFromScriptElement(WebGLRenderingContext gl, String id) {
+ ScriptElement shaderScript = document.query(id);
+ String shaderSource = shaderScript.text;
+ int shaderType;
+ if (shaderScript.type == "x-shader/x-vertex") {
+ shaderType = WebGLRenderingContext.VERTEX_SHADER;
+ } else if (shaderScript.type == "x-shader/x-fragment") {
+ shaderType = WebGLRenderingContext.FRAGMENT_SHADER;
+ } else {
+ throw new Exception('*** Error: unknown shader type');
+ }
+
+ return loadShader(gl, shaderSource, shaderType);
+}
+
+WebGLRenderingContext getWebGLContext(CanvasElement canvas) {
+ return canvas.getContext("experimental-webgl");
+}
+
+
+// misc functions
+void setRectangle(gl, x, y, width, height) {
+ var x1 = x;
+ var x2 = x + width;
+ var y1 = y;
+ var y2 = y + height;
+ var vertices = [x1, y1,
+ x2, y1,
+ x1, y2,
+ x1, y2,
+ x2, y1,
+ x2, y2];
+ gl.bufferData(WebGLRenderingContext.ARRAY_BUFFER, new Float32Array.fromList(vertices), WebGLRenderingContext.STATIC_DRAW);
+}
+
+int randomInt(int range) {
+ return (Math.random() * range).toInt();
+}

0 comments on commit b11559b

Please sign in to comment.
Something went wrong with that request. Please try again.