Earth.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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>Earth and Mars</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. function init()
  66. {
  67. var gl = initWebGL("example", "vshader", "fshader",
  68. [ "vNormal", "vTexCoord", "vPosition"],
  69. [ 0, 0, 0, 1 ], 10000);
  70. gl.uniform3f(gl.getUniformLocation(gl.program, "lightDir"), 0, 0, 1);
  71. gl.uniform1i(gl.getUniformLocation(gl.program, "sampler2d"), 0);
  72. gl.enable(gl.TEXTURE_2D);
  73. gl.sphere = makeSphere(gl, 1, 30, 30);
  74. // get the images
  75. earthTexture = loadImageTexture(gl, "resources/earthmap1k.jpg");
  76. marsTexture = loadImageTexture(gl, "resources/mars500x250.png");
  77. return gl;
  78. }
  79. width = -1;
  80. height = -1;
  81. function reshape(ctx)
  82. {
  83. var canvas = document.getElementById('example');
  84. if (canvas.width == width && canvas.width == height)
  85. return;
  86. width = canvas.width;
  87. height = canvas.height;
  88. ctx.viewport(0, 0, width, height);
  89. ctx.perspectiveMatrix = new J3DIMatrix4();
  90. ctx.perspectiveMatrix.perspective(30, width/height, 1, 10000);
  91. ctx.perspectiveMatrix.lookat(0,0,6, 0, 0, 0, 0, 1, 0);
  92. }
  93. function drawOne(ctx, angle, x, y, z, scale, texture)
  94. {
  95. // setup VBOs
  96. ctx.enableVertexAttribArray(0);
  97. ctx.enableVertexAttribArray(1);
  98. ctx.enableVertexAttribArray(2);
  99. ctx.bindBuffer(ctx.ARRAY_BUFFER, ctx.sphere.vertexObject);
  100. ctx.vertexAttribPointer(2, 3, ctx.FLOAT, false, 0, 0);
  101. ctx.bindBuffer(ctx.ARRAY_BUFFER, ctx.sphere.normalObject);
  102. ctx.vertexAttribPointer(0, 3, ctx.FLOAT, false, 0, 0);
  103. ctx.bindBuffer(ctx.ARRAY_BUFFER, ctx.sphere.texCoordObject);
  104. ctx.vertexAttribPointer(1, 2, ctx.FLOAT, false, 0, 0);
  105. ctx.bindBuffer(ctx.ELEMENT_ARRAY_BUFFER, ctx.sphere.indexObject);
  106. // generate the model-view matrix
  107. var mvMatrix = new J3DIMatrix4();
  108. mvMatrix.translate(x,y,z);
  109. mvMatrix.rotate(30, 1,0,0);
  110. mvMatrix.rotate(angle, 0,1,0);
  111. mvMatrix.scale(scale, scale, scale);
  112. // construct the normal matrix from the model-view matrix
  113. var normalMatrix = new J3DIMatrix4(mvMatrix);
  114. normalMatrix.invert();
  115. normalMatrix.transpose();
  116. normalMatrix.setUniform(ctx, ctx.getUniformLocation(ctx.program, "u_normalMatrix"), false);
  117. // construct the model-view * projection matrix
  118. var mvpMatrix = new J3DIMatrix4(ctx.perspectiveMatrix);
  119. mvpMatrix.multiply(mvMatrix);
  120. mvpMatrix.setUniform(ctx, ctx.getUniformLocation(ctx.program, "u_modelViewProjMatrix"), false);
  121. ctx.bindTexture(ctx.TEXTURE_2D, texture);
  122. ctx.drawElements(ctx.TRIANGLES, ctx.sphere.numIndices, ctx.UNSIGNED_SHORT, 0);
  123. }
  124. function drawPicture(ctx)
  125. {
  126. reshape(ctx);
  127. ctx.clear(ctx.COLOR_BUFFER_BIT | ctx.DEPTH_BUFFER_BIT);
  128. drawOne(ctx, currentAngle, -1, 0, 0, 1, earthTexture);
  129. drawOne(ctx, -currentAngle, 1, 0, 0, 0.6, marsTexture);
  130. ctx.flush();
  131. framerate.snapshot();
  132. currentAngle += incAngle;
  133. if (currentAngle > 360)
  134. currentAngle -= 360;
  135. }
  136. function start()
  137. {
  138. var c = document.getElementById("example");
  139. var w = Math.floor(window.innerWidth * 0.9);
  140. var h = Math.floor(window.innerHeight * 0.9);
  141. c.width = w;
  142. c.height = h;
  143. var ctx = init();
  144. currentAngle = 0;
  145. incAngle = 0.2;
  146. var f = function() { drawPicture(ctx) };
  147. setInterval(f, 10);
  148. framerate = new Framerate("framerate");
  149. }
  150. </script>
  151. <style type="text/css">
  152. canvas {
  153. border: 2px solid black;
  154. }
  155. </style>
  156. </head>
  157. <body onload="start()">
  158. <canvas id="example">
  159. There is supposed to be an example drawing here, but it's not important.
  160. </canvas>
  161. <div id="framerate"></div>
  162. <div id="console"></div>
  163. </body>
  164. </html>