Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. // zmienne globalne
  2. var gl_canvas;
  3. var gl_ctx;
  4.  
  5. var _triangleVertexBuffer;
  6. var _triangleFacesBuffer;
  7. var _position;
  8. var _uv;
  9. var _sampler;
  10. var _PosMatrix;
  11. var _MovMatrix;
  12. var _ViewMatrix;
  13. var _matrixProjection;
  14. var _matrixMovement;
  15. var _matrixView;
  16. var _cubeTexture;
  17.  
  18. var rotationSpeed = 0.001;
  19. var zoomRatio = -6;
  20.  
  21. var X, Y, Z;
  22.  
  23. // funkcja główna
  24. function runWebGL () {
  25. getRotation();
  26. gl_canvas = document.getElementById("glcanvas");
  27. gl_ctx = gl_getContext(gl_canvas);
  28. gl_initShaders();
  29. gl_initBuffers();
  30. gl_setMatrix();
  31. _cubeTexture = gl_initTexture();
  32. gl_draw(); }
  33.  
  34. // osie obrotu
  35. function getRotation() {
  36. X = document.getElementById('rotateX').checked;
  37. Y = document.getElementById('rotateY').checked;
  38. Z = document.getElementById('rotateZ').checked;
  39. }
  40.  
  41. // pobranie kontekstu WebGL
  42. function gl_getContext (canvas) {
  43. try {
  44. var ctx = canvas.getContext("webgl");
  45. ctx.viewportWidth = canvas.width;
  46. ctx.viewportHeight = canvas.height;
  47. } catch (e) {}
  48.  
  49. if (!ctx) {
  50. document.write('Unable to initialize WebGL context.')
  51. }
  52. return ctx;
  53. }
  54.  
  55. // shadery
  56. function gl_initShaders () {
  57. var vertexShader = "\n\
  58. attribute vec3 position;\n\
  59. uniform mat4 PosMatrix;\n\
  60. uniform mat4 MovMatrix;\n\
  61. uniform mat4 ViewMatrix; \n\
  62. attribute vec2 uv;\n\
  63. varying vec2 vUV;\n\
  64. void main(void) {\n\
  65. gl_Position = PosMatrix * ViewMatrix * MovMatrix * vec4(position, 1.);\n\
  66. vUV = uv;\n\
  67. }";
  68.  
  69. var fragmentShader = "\n\
  70. precision mediump float;\n\
  71. uniform sampler2D sampler;\n\
  72. varying vec2 vUV;\n\
  73. void main(void) {\n\
  74. gl_FragColor = texture2D(sampler, vUV);\n\
  75. }";
  76.  
  77. var getShader = function(source, type, typeString) {
  78. var shader = gl_ctx.createShader(type);
  79. gl_ctx.shaderSource(shader, source);
  80. gl_ctx.compileShader(shader);
  81.  
  82. if (!gl_ctx.getShaderParameter(shader, gl_ctx.COMPILE_STATUS)) {
  83. alert('error in' + typeString);
  84. return false;
  85. }
  86. return shader;
  87. };
  88.  
  89. var shader_vertex = getShader(vertexShader, gl_ctx.VERTEX_SHADER, "VERTEX");
  90. var shader_fragment = getShader(fragmentShader, gl_ctx.FRAGMENT_SHADER, "FRAGMENT");
  91.  
  92. var SHADER_PROGRAM = gl_ctx.createProgram();
  93. gl_ctx.attachShader(SHADER_PROGRAM, shader_vertex);
  94. gl_ctx.attachShader(SHADER_PROGRAM, shader_fragment);
  95. gl_ctx.linkProgram(SHADER_PROGRAM);
  96.  
  97. _PosMatrix = gl_ctx.getUniformLocation(SHADER_PROGRAM, "PosMatrix");
  98. _MovMatrix = gl_ctx.getUniformLocation(SHADER_PROGRAM, "MovMatrix");
  99. _ViewMatrix = gl_ctx.getUniformLocation(SHADER_PROGRAM, "ViewMatrix");
  100.  
  101. _sampler = gl_ctx.getUniformLocation(SHADER_PROGRAM, "sampler");
  102. _uv = gl_ctx.getAttribLocation(SHADER_PROGRAM, "uv");
  103.  
  104. _position = gl_ctx.getAttribLocation(SHADER_PROGRAM, "position");
  105. gl_ctx.enableVertexAttribArray(_uv);
  106. gl_ctx.enableVertexAttribArray(_position);
  107. gl_ctx.useProgram(SHADER_PROGRAM);
  108. gl_ctx.uniform1i(_sampler, 0);
  109. }
  110.  
  111. // bufory
  112. function gl_initBuffers () {
  113. var triangleVertices = [
  114. -1,-1,-1, 0, 0,
  115. 1,-1,-1, 1, 0,
  116. 1, 1,-1, 1, 1,
  117. -1, 1,-1, 0, 1,
  118.  
  119. -1,-1, 1, 0, 0,
  120. 1,-1, 1, 1, 0,
  121. 1, 1, 1, 1, 1,
  122. -1, 1, 1, 0, 1,
  123.  
  124. -1,-1,-1, 0, 0,
  125. -1, 1,-1, 1, 0,
  126. -1, 1, 1, 1, 1,
  127. -1,-1, 1, 0, 1,
  128.  
  129. 1,-1,-1, 0, 0,
  130. 1, 1,-1, 1, 0,
  131. 1, 1, 1, 1, 1,
  132. 1,-1, 1, 0, 1,
  133.  
  134. -1,-1,-1, 0, 0,
  135. -1,-1, 1, 1, 0,
  136. 1,-1, 1, 1, 1,
  137. 1,-1,-1, 0, 1,
  138.  
  139. -1, 1,-1, 0, 0,
  140. -1, 1, 1, 1, 0,
  141. 1, 1, 1, 1, 1,
  142. 1, 1,-1, 0, 1
  143. ];
  144.  
  145. _triangleVertexBuffer = gl_ctx.createBuffer();
  146. gl_ctx.bindBuffer(gl_ctx.ARRAY_BUFFER, _triangleVertexBuffer);
  147. gl_ctx.bufferData(gl_ctx.ARRAY_BUFFER,
  148. new Float32Array(triangleVertices),
  149. gl_ctx.STATIC_DRAW);
  150.  
  151. var triangleFaces = [
  152. 0,1,2,
  153. 0,2,3,
  154. 4,5,6,
  155. 4,6,7,
  156. 8,9,10,
  157. 8,10,11,
  158. 12,13,14,
  159. 12,14,15,
  160. 16,17,18,
  161. 16,18,19,
  162. 20,21,22,
  163. 20,22,23
  164. ];
  165.  
  166. _triangleFacesBuffer = gl_ctx.createBuffer();
  167. gl_ctx.bindBuffer(gl_ctx.ELEMENT_ARRAY_BUFFER, _triangleFacesBuffer);
  168. gl_ctx.bufferData(gl_ctx.ELEMENT_ARRAY_BUFFER,
  169. new Uint16Array(triangleFaces),
  170. gl_ctx.STATIC_DRAW);
  171. }
  172.  
  173. // macierz
  174. function gl_setMatrix () {
  175. _matrixProjection = MATRIX.getProjection(40,
  176. gl_canvas.width/gl_canvas.height, 1, 100);
  177. _matrixMovement = MATRIX.getIdentityMatrix();
  178. _matrixView = MATRIX.getIdentityMatrix();
  179. MATRIX.translateZ(_matrixView, zoomRatio);
  180. }
  181.  
  182. // tekstura
  183. function gl_initTexture() {
  184. var img = new Image();
  185. img.src = 'cubetexture2.png';
  186. img.webglTexture = false;
  187.  
  188. img.onload = function(e) {
  189. var texture = gl_ctx.createTexture();
  190.  
  191. gl_ctx.pixelStorei(gl_ctx.UNPACK_FLIP_Y_WEBGL, true);
  192. gl_ctx.bindTexture(gl_ctx.TEXTURE_2D, texture);
  193. gl_ctx.texParameteri(gl_ctx.TEXTURE_2D, gl_ctx.TEXTURE_MIN_FILTER, gl_ctx.LINEAR);
  194. gl_ctx.texParameteri(gl_ctx.TEXTURE_2D, gl_ctx.TEXTURE_MAG_FILTER, gl_ctx.LINEAR);
  195.  
  196. gl_ctx.texImage2D(gl_ctx.TEXTURE_2D, 0, gl_ctx.RGBA, gl_ctx.RGBA,
  197. gl_ctx.UNSIGNED_BYTE, img);
  198. gl_ctx.bindTexture(gl_ctx.TEXTURE_2D, null);
  199. img.webglTexture = texture;
  200. };
  201. return img;
  202. }
  203.  
  204. // rysowanie
  205. var timeOld = 0;
  206.  
  207. function gl_draw() {
  208. gl_ctx.clearColor(0.0, 0.0, 0.0, 0.0);
  209. gl_ctx.enable(gl_ctx.DEPTH_TEST);
  210. gl_ctx.depthFunc(gl_ctx.LEQUAL);
  211. gl_ctx.clearDepth(1.0);
  212.  
  213. var animate = function (time) {
  214. var dAngle = rotationSpeed * (time - timeOld);
  215. if (X) {
  216. MATRIX.rotateX(_matrixMovement, dAngle);
  217. }
  218. if (Y) {
  219. MATRIX.rotateY(_matrixMovement, dAngle);
  220. }
  221. if (Z) {
  222. MATRIX.rotateZ(_matrixMovement, dAngle);
  223. }
  224.  
  225. timeOld = time;
  226.  
  227. gl_ctx.viewport(0.0, 0.0, gl_canvas.width, gl_canvas.height);
  228. gl_ctx.clear(gl_ctx.COLOR_BUFFER_BIT | gl_ctx.DEPTH_BUFFER_BIT);
  229.  
  230. gl_ctx.uniformMatrix4fv(_PosMatrix, false, _matrixProjection);
  231. gl_ctx.uniformMatrix4fv(_MovMatrix, false, _matrixMovement);
  232. gl_ctx.uniformMatrix4fv(_ViewMatrix, false, _matrixView);
  233.  
  234. if (_cubeTexture.webglTexture) {
  235. gl_ctx.activeTexture(gl_ctx.TEXTURE0);
  236. gl_ctx.bindTexture(gl_ctx.TEXTURE_2D, _cubeTexture.webglTexture);
  237. }
  238.  
  239. gl_ctx.vertexAttribPointer(_position, 3, gl_ctx.FLOAT, false, 4*(3+2), 0);
  240. gl_ctx.vertexAttribPointer(_uv, 2, gl_ctx.FLOAT, false, 4*(3+2), 3*4);
  241.  
  242. gl_ctx.bindBuffer(gl_ctx.ARRAY_BUFFER, _triangleVertexBuffer);
  243. gl_ctx.bindBuffer(gl_ctx.ELEMENT_ARRAY_BUFFER, _triangleFacesBuffer);
  244. gl_ctx.drawElements(gl_ctx.TRIANGLES, 5*2*3, gl_ctx.UNSIGNED_SHORT, 0);
  245. gl_ctx.flush();
  246.  
  247. window.requestAnimationFrame(animate);
  248. };
  249. animate(0);
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement