ManyPlanetsDeep.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <!DOCTYPE html>
  2. <!--
  3. /*
  4. * Copyright (C) 2009 Apple Inc. All Rights Reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. -->
  28. <html>
  29. <head>
  30. <title>Many Planets Deep</title>
  31. <script src="resources/J3DI.js"> </script>
  32. <script src="resources/J3DIMath.js" type="text/javascript"> </script>
  33. <script id="vshader" type="x-shader/x-vertex">
  34. uniform mat4 u_modelViewProjMatrix;
  35. uniform mat4 u_normalMatrix;
  36. uniform vec3 lightDir;
  37. attribute vec3 vNormal;
  38. attribute vec4 vTexCoord;
  39. attribute vec4 vPosition;
  40. varying float v_Dot;
  41. varying vec2 v_texCoord;
  42. void main()
  43. {
  44. gl_Position = u_modelViewProjMatrix * vPosition;
  45. v_texCoord = vTexCoord.st;
  46. vec4 transNormal = u_normalMatrix * vec4(vNormal,1);
  47. v_Dot = max(dot(transNormal.xyz, lightDir), 0.0);
  48. }
  49. </script>
  50. <script id="fshader" type="x-shader/x-fragment">
  51. #ifdef GL_ES
  52. precision mediump float;
  53. #endif
  54. uniform sampler2D sampler2d;
  55. varying float v_Dot;
  56. varying vec2 v_texCoord;
  57. void main()
  58. {
  59. vec4 color = texture2D(sampler2d,v_texCoord);
  60. color += vec4(0.1,0.1,0.1,1);
  61. gl_FragColor = vec4(color.xyz * v_Dot, color.a);
  62. }
  63. </script>
  64. <script>
  65. const numRowCols = 4;
  66. const numLayers = 3;
  67. const layoutWidth = 10;
  68. const layoutHeight = 8;
  69. const globeSize = 25;
  70. const minIncAngle = 0.2;
  71. const maxIncAngle = 2;
  72. function init()
  73. {
  74. var gl = initWebGL("example", "vshader", "fshader",
  75. [ "vNormal", "vTexCoord", "vPosition"],
  76. [ 0, 0, 0, 1 ], 10000);
  77. gl.uniform3f(gl.getUniformLocation(gl.program, "lightDir"), 0, 0, 1);
  78. gl.uniform1i(gl.getUniformLocation(gl.program, "sampler2d"), 0);
  79. gl.enable(gl.TEXTURE_2D);
  80. gl.sphere = makeSphere(gl, 1, 30, 30);
  81. // get the images
  82. earthTexture = loadImageTexture(gl, "resources/earthmap1k.jpg");
  83. marsTexture = loadImageTexture(gl, "resources/mars500x250.png");
  84. return gl;
  85. }
  86. width = -1;
  87. height = -1;
  88. function reshape(ctx)
  89. {
  90. var canvas = document.getElementById('example');
  91. if (canvas.width == width && canvas.height == height)
  92. return;
  93. width = canvas.width;
  94. height = canvas.height;
  95. ctx.viewport(0, 0, width, height);
  96. ctx.perspectiveMatrix = new J3DIMatrix4();
  97. ctx.perspectiveMatrix.perspective(30, width/height, 1, 10000);
  98. ctx.perspectiveMatrix.lookat(0,0,20, 0, 0, 0, 0, 1, 0);
  99. }
  100. function drawOne(ctx, angle, x, y, z, scale, texture)
  101. {
  102. // setup VBOs
  103. ctx.enableVertexAttribArray(0);
  104. ctx.enableVertexAttribArray(1);
  105. ctx.enableVertexAttribArray(2);
  106. ctx.bindBuffer(ctx.ARRAY_BUFFER, ctx.sphere.vertexObject);
  107. ctx.vertexAttribPointer(2, 3, ctx.FLOAT, false, 0, 0);
  108. ctx.bindBuffer(ctx.ARRAY_BUFFER, ctx.sphere.normalObject);
  109. ctx.vertexAttribPointer(0, 3, ctx.FLOAT, false, 0, 0);
  110. ctx.bindBuffer(ctx.ARRAY_BUFFER, ctx.sphere.texCoordObject);
  111. ctx.vertexAttribPointer(1, 2, ctx.FLOAT, false, 0, 0);
  112. ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, ctx.sphere.indexObject);
  113. // generate the model-view matrix
  114. var mvMatrix = new J3DIMatrix4();
  115. mvMatrix.translate(x,y,z);
  116. mvMatrix.rotate(30, 1,0,0);
  117. mvMatrix.rotate(angle, 0,1,0);
  118. mvMatrix.scale(scale, scale, scale);
  119. // construct the normal matrix from the model-view matrix
  120. var normalMatrix = new J3DIMatrix4(mvMatrix);
  121. normalMatrix.invert();
  122. normalMatrix.transpose();
  123. normalMatrix.setUniform(ctx, ctx.getUniformLocation(ctx.program, "u_normalMatrix"), false);
  124. // construct the model-view * projection matrix
  125. var mvpMatrix = new J3DIMatrix4(ctx.perspectiveMatrix);
  126. mvpMatrix.multiply(mvMatrix);
  127. mvpMatrix.setUniform(ctx, ctx.getUniformLocation(ctx.program, "u_modelViewProjMatrix"), false);
  128. ctx.bindTexture(ctx.TEXTURE_2D, texture);
  129. ctx.drawElements(ctx.TRIANGLES, ctx.sphere.numIndices, ctx.UNSIGNED_SHORT, 0);
  130. }
  131. function drawPicture(ctx)
  132. {
  133. reshape(ctx);
  134. ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT);
  135. var startX = -layoutWidth/2;
  136. var startY = -layoutHeight/2;
  137. var startZ = 0;
  138. var incX = layoutWidth / (numRowCols-1);
  139. var incY = layoutHeight / (numRowCols-1);
  140. var incZ = -5;
  141. for (i = 0; i < numLayers; ++i) {
  142. for (j = 0; j < numRowCols; ++j) {
  143. for (k = 0; k < numRowCols; ++k) {
  144. var index = i * numLayers * numRowCols + j * numRowCols + k;
  145. drawOne(ctx, currentAngles[index],
  146. startX + incX * k,
  147. startY + incY * j,
  148. startZ + incZ * i,
  149. showEarth[index] ? 1 : 0.6, showEarth[index] ? earthTexture : marsTexture);
  150. currentAngles[index] += incAngles[i];
  151. if (currentAngles[index] > 360)
  152. currentAngles[index] -= 360;
  153. }
  154. }
  155. }
  156. ctx.bindTexture(ctx.TEXTURE_2D, 0);
  157. ctx.flush();
  158. framerate.snapshot();
  159. }
  160. function start()
  161. {
  162. var c = document.getElementById("example");
  163. var w = Math.floor(window.innerWidth * 0.9);
  164. var h = Math.floor(window.innerHeight * 0.9);
  165. c.width = w;
  166. c.height = h;
  167. var ctx = init();
  168. currentAngles = [ ];
  169. incAngles = [ ];
  170. showEarth = [ ];
  171. for (var i = 0; i < numRowCols * numRowCols * numLayers; ++i) {
  172. currentAngles[i] = 0;
  173. incAngles[i] = Math.random() * (maxIncAngle - minIncAngle) + minIncAngle;
  174. showEarth[i] = Math.random() > 0.5;
  175. }
  176. framerate = new Framerate("framerate");
  177. var f = function() { drawPicture(ctx) };
  178. setInterval(f, 10);
  179. }
  180. </script>
  181. <style type="text/css">
  182. canvas {
  183. border: 2px solid black;
  184. }
  185. </style>
  186. </head>
  187. <body onload="start()">
  188. <canvas id="example">
  189. There is supposed to be an example drawing here, but it's not important.
  190. </canvas>
  191. <div id="framerate"></div>
  192. </body>
  193. </html>