Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.45 KB | None | 0 0
  1. <html><head>
  2. <title>Learning WebGL โ€” lesson 6</title>
  3. <meta http-equiv="content-type" content="text/html; charset=windows-1252">
  4.  
  5. <script type="text/javascript" src="index_pliki/glMatrix-0.js"></script>
  6. <script type="text/javascript" src="index_pliki/webgl-utils.js"></script>
  7.  
  8. <script id="shader-fs" type="x-shader/x-fragment">
  9. precision mediump float;
  10.  
  11. varying vec2 vTextureCoord;
  12.  
  13. uniform sampler2D uSampler;
  14.  
  15. void main(void) {
  16. gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
  17. }
  18. </script>
  19.  
  20. <script id="shader-vs" type="x-shader/x-vertex">
  21. attribute vec3 aVertexPosition;
  22. attribute vec2 aTextureCoord;
  23.  
  24. uniform mat4 uMVMatrix;
  25. uniform mat4 uPMatrix;
  26.  
  27. varying vec2 vTextureCoord;
  28.  
  29.  
  30. void main(void) {
  31. gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
  32. vTextureCoord = aTextureCoord;
  33. }
  34. </script>
  35.  
  36.  
  37. <script type="text/javascript">
  38.  
  39. var gl;
  40.  
  41. function initGL(canvas) {
  42. try {
  43. gl = canvas.getContext("experimental-webgl");
  44. gl.viewportWidth = canvas.width;
  45. gl.viewportHeight = canvas.height;
  46. } catch (e) {
  47. }
  48. if (!gl) {
  49. alert("Could not initialise WebGL, sorry :-(");
  50. }
  51. }
  52.  
  53.  
  54. function getShader(gl, id) {
  55. var shaderScript = document.getElementById(id);
  56. if (!shaderScript) {
  57. return null;
  58. }
  59.  
  60. var str = "";
  61. var k = shaderScript.firstChild;
  62. while (k) {
  63. if (k.nodeType == 3) {
  64. str += k.textContent;
  65. }
  66. k = k.nextSibling;
  67. }
  68.  
  69. var shader;
  70. if (shaderScript.type == "x-shader/x-fragment") {
  71. shader = gl.createShader(gl.FRAGMENT_SHADER);
  72. } else if (shaderScript.type == "x-shader/x-vertex") {
  73. shader = gl.createShader(gl.VERTEX_SHADER);
  74. } else {
  75. return null;
  76. }
  77.  
  78. gl.shaderSource(shader, str);
  79. gl.compileShader(shader);
  80.  
  81. if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  82. alert(gl.getShaderInfoLog(shader));
  83. return null;
  84. }
  85.  
  86. return shader;
  87. }
  88.  
  89.  
  90. var shaderProgram;
  91.  
  92. function initShaders() {
  93. var fragmentShader = getShader(gl, "shader-fs");
  94. var vertexShader = getShader(gl, "shader-vs");
  95.  
  96. shaderProgram = gl.createProgram();
  97. gl.attachShader(shaderProgram, vertexShader);
  98. gl.attachShader(shaderProgram, fragmentShader);
  99. gl.linkProgram(shaderProgram);
  100.  
  101. if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
  102. alert("Could not initialise shaders");
  103. }
  104.  
  105. gl.useProgram(shaderProgram);
  106.  
  107. shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
  108. gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);
  109.  
  110. shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
  111. gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);
  112.  
  113. shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
  114. shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
  115. shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
  116. }
  117.  
  118.  
  119. function handleLoadedTexture(textures) {
  120. gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true);
  121.  
  122. gl.bindTexture(gl.TEXTURE_2D, textures[0]);
  123. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textures[0].image);
  124. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
  125. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
  126.  
  127. gl.bindTexture(gl.TEXTURE_2D, textures[1]);
  128. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textures[1].image);
  129. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  130. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
  131.  
  132. gl.bindTexture(gl.TEXTURE_2D, textures[2]);
  133. gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textures[2].image);
  134. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
  135. gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST);
  136. gl.generateMipmap(gl.TEXTURE_2D);
  137.  
  138. gl.bindTexture(gl.TEXTURE_2D, null);
  139. }
  140.  
  141.  
  142. var crateTextures = Array();
  143.  
  144. function initTexture() {
  145. var crateImage = new Image();
  146.  
  147. for (var i=0; i < 3; i++) {
  148. var texture = gl.createTexture();
  149. texture.image = crateImage;
  150. crateTextures.push(texture);
  151. }
  152.  
  153. crateImage.onload = function () {
  154. handleLoadedTexture(crateTextures)
  155. }
  156. crateImage.src = "crate.gif";
  157. }
  158.  
  159.  
  160. var mvMatrix = mat4.create();
  161. var mvMatrixStack = [];
  162. var pMatrix = mat4.create();
  163.  
  164. function mvPushMatrix() {
  165. var copy = mat4.create();
  166. mat4.set(mvMatrix, copy);
  167. mvMatrixStack.push(copy);
  168. }
  169.  
  170. function mvPopMatrix() {
  171. if (mvMatrixStack.length == 0) {
  172. throw "Invalid popMatrix!";
  173. }
  174. mvMatrix = mvMatrixStack.pop();
  175. }
  176.  
  177.  
  178. function setMatrixUniforms() {
  179. gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
  180. gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
  181. }
  182.  
  183.  
  184. function degToRad(degrees) {
  185. return degrees * Math.PI / 180;
  186. }
  187.  
  188.  
  189. var xRot = 0;
  190. var xSpeed = 0;
  191.  
  192. var yRot = 0;
  193. var ySpeed = 0;
  194.  
  195. var z = -5.0;
  196.  
  197. var filter = 0;
  198.  
  199.  
  200. var currentlyPressedKeys = {};
  201.  
  202. function handleKeyDown(event) {
  203. currentlyPressedKeys[event.keyCode] = true;
  204.  
  205. if (String.fromCharCode(event.keyCode) == "F") {
  206. filter += 1;
  207. if (filter == 3) {
  208. filter = 0;
  209. }
  210. }
  211. }
  212.  
  213.  
  214. function handleKeyUp(event) {
  215. currentlyPressedKeys[event.keyCode] = false;
  216. }
  217.  
  218.  
  219. function handleKeys() {
  220. if (currentlyPressedKeys[33]) {
  221. // Page Up
  222. z -= 0.05;
  223. }
  224. if (currentlyPressedKeys[34]) {
  225. // Page Down
  226. z += 0.05;
  227. }
  228. if (currentlyPressedKeys[37]) {
  229. // Left cursor key
  230. ySpeed -= 1;
  231. }
  232. if (currentlyPressedKeys[39]) {
  233. // Right cursor key
  234. ySpeed += 1;
  235. }
  236. if (currentlyPressedKeys[38]) {
  237. // Up cursor key
  238. xSpeed -= 1;
  239. }
  240. if (currentlyPressedKeys[40]) {
  241. // Down cursor key
  242. xSpeed += 1;
  243. }
  244. }
  245.  
  246.  
  247. var cubeVertexPositionBuffer;
  248. var cubeVertexTextureCoordBuffer;
  249. var cubeVertexIndexBuffer;
  250. function initBuffers() {
  251. cubeVertexPositionBuffer = gl.createBuffer();
  252. gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
  253. vertices = [
  254. -1.0, -1.0, 1.0,
  255. 1.0, -1.0, 1.0,
  256. 0.0, 1.0, 0.0,
  257. -1.0, -1.0, 1.0,
  258.  
  259. -1.0, -1.0, -1.0,
  260. -1.0, -1.0, -1.0,
  261. 0.0, 1.0, 0.0,
  262. 1.0, -1.0, -1.0,
  263.  
  264. 1.0, 1.0, -1.0,
  265. 1.0, 1.0, -1.0,
  266. 1.0, 1.0, -1.0,
  267. 1.0, 1.0, -1.0,
  268.  
  269. -1.0, -1.0, -1.0,
  270. 1.0, -1.0, -1.0,
  271. 1.0, -1.0, 1.0,
  272. -1.0, -1.0, 1.0,
  273.  
  274. 1.0, -1.0, -1.0,
  275. 0.0, 1.0, 0.0,
  276. 0.0, 1.0, 0.0,
  277. 1.0, -1.0, 1.0,
  278.  
  279. -1.0, -1.0, -1.0,
  280. -1.0, -1.0, 1.0,
  281. 0.0, 1.0, 0.0,
  282. 0.0, 1.0, 0.0,
  283. ];
  284. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  285. cubeVertexPositionBuffer.itemSize = 3;
  286. cubeVertexPositionBuffer.numItems = 24;
  287.  
  288. cubeVertexTextureCoordBuffer = gl.createBuffer();
  289. gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);
  290. var textureCoords = [
  291. // Front face
  292. 0.0, 0.0,
  293. 0.25, 0.0,
  294. 0.5, 1.0,
  295. 0.5, 1.0,
  296.  
  297. // Back face
  298. 0.75, 0.0,
  299. 0.5, 1.0,
  300. 0.5, 1.0,
  301. 0.5, 0.0,
  302.  
  303. // Top face
  304. 0.0, 1.0,
  305. 0.0, 0.0,
  306. 1.0, 0.0,
  307. 1.0, 1.0,
  308.  
  309. // Bottom face
  310. 1.0, 1.0,
  311. 0.0, 1.0,
  312. 0.0, 0.0,
  313. 1.0, 0.0,
  314.  
  315. // Right face
  316. 0.5, 0.0,
  317. 0.5, 1.0,
  318. 0.5, 1.0,
  319. 0.25, 0.0,
  320.  
  321. // Left face
  322. 0.75, 0.0,
  323. 0.5, 0.0,
  324. 0.5, 1.0,
  325. 1.0, 1.0,
  326. ];
  327. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
  328. cubeVertexTextureCoordBuffer.itemSize = 2;
  329. cubeVertexTextureCoordBuffer.numItems = 24;
  330.  
  331. cubeVertexIndexBuffer = gl.createBuffer();
  332. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
  333. var cubeVertexIndices = [
  334. 0, 1, 2, 0, 2, 3, // Front face
  335. 4, 5, 6, 4, 6, 7, // Back face
  336. 8, 9, 10, 8, 10, 11, // Top face
  337. 12, 13, 14, 12, 14, 15, // Bottom face
  338. 16, 17, 18, 16, 18, 19, // Right face
  339. 20, 21, 22, 20, 22, 23 // Left face
  340. ]
  341. gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
  342. cubeVertexIndexBuffer.itemSize = 1;
  343. cubeVertexIndexBuffer.numItems = 36;
  344. }
  345.  
  346.  
  347. function drawScene() {
  348. gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
  349. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  350.  
  351. mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
  352.  
  353. mat4.identity(mvMatrix);
  354.  
  355. mat4.translate(mvMatrix, [0.0, 0.0, z]);
  356.  
  357. mat4.rotate(mvMatrix, degToRad(xRot), [1, 0, 0]);
  358. mat4.rotate(mvMatrix, degToRad(yRot), [0, 1, 0]);
  359.  
  360. gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
  361. gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
  362.  
  363. gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);
  364. gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0);
  365.  
  366.  
  367. gl.activeTexture(gl.TEXTURE0);
  368. gl.bindTexture(gl.TEXTURE_2D, crateTextures[filter]);
  369. gl.uniform1i(shaderProgram.samplerUniform, 0);
  370.  
  371. gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
  372. setMatrixUniforms();
  373. gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0);
  374. }
  375.  
  376.  
  377. var lastTime = 0;
  378.  
  379. function animate() {
  380. var timeNow = new Date().getTime();
  381. if (lastTime != 0) {
  382. var elapsed = timeNow - lastTime;
  383.  
  384. xRot += (xSpeed * elapsed) / 1000.0;
  385. yRot += (ySpeed * elapsed) / 1000.0;
  386. }
  387. lastTime = timeNow;
  388. }
  389.  
  390.  
  391. function tick() {
  392. requestAnimFrame(tick);
  393. handleKeys();
  394. drawScene();
  395. animate();
  396. }
  397.  
  398.  
  399.  
  400. function webGLStart() {
  401. var canvas = document.getElementById("lesson06-canvas");
  402. initGL(canvas);
  403. initShaders();
  404. initBuffers();
  405. initTexture();
  406.  
  407. gl.clearColor(0.0, 0.0, 0.0, 1.0);
  408. gl.enable(gl.DEPTH_TEST);
  409.  
  410. document.onkeydown = handleKeyDown;
  411. document.onkeyup = handleKeyUp;
  412.  
  413. tick();
  414. }
  415.  
  416. </script>
  417.  
  418.  
  419. </head>
  420.  
  421.  
  422. <body onload="webGLStart();">
  423. <a href="http://learningwebgl.com/blog/?p=571">&lt;&lt; Back to Lesson 6</a><br>
  424.  
  425. <canvas id="lesson06-canvas" style="border: none;" width="500" height="500"></canvas>
  426.  
  427. <h2>Controls:</h2>
  428.  
  429. <ul>
  430. <li><code>Page Up</code>/<code>Page Down</code> to zoom out/in
  431. </li><li>Cursor keys: make the cube rotate (the longer you hold down a cursor key, the more it accelerates)
  432. </li><li><code>F</code> to toggle through three different kinds of texture filters
  433. </li></ul>
  434.  
  435. <br>
  436. <a href="http://learningwebgl.com/blog/?p=571">&lt;&lt; Back to Lesson 6</a><br>
  437.  
  438.  
  439.  
  440. </body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement