DanAlucard

3.- triángulo relleno con WebGL

Dec 27th, 2011
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 11.65 KB | None | 0 0
  1. <!--
  2. Define a simple GLSL (OpenGL Shading Language) fragment shader.
  3. -->
  4. <script id="shader-fs" type="x-shader/x-fragment">
  5.     void main(void) {
  6.         gl_FragColor = vec4(0.6, 0.0, 0.0, 1.0);
  7.     }
  8. </script>
  9.  
  10.  
  11. <!--
  12. Define a simple GLSL (OpenGL Shading Language) vertex shader.
  13. -->
  14. <script id="shader-vs" type="x-shader/x-vertex">
  15.     attribute vec3 vertexPosition;
  16.  
  17.     uniform mat4 modelViewMatrix;
  18.     uniform mat4 perspectiveMatrix;
  19.  
  20.     void main(void) {
  21.         gl_Position = perspectiveMatrix * modelViewMatrix * vec4(vertexPosition, 1.0);
  22.     }
  23. </script>
  24.  
  25.  
  26.  
  27. <script type="text/javascript">
  28.     window.onload = loadScene;
  29.  
  30.     /*
  31.      * Initialises WebGL and creates the 3D scene.
  32.      */
  33.     function loadScene(){
  34.        
  35.         //    Get the canvas element
  36.         var canvas = document.getElementById("webGLCanvas");
  37.         //    Get the WebGL context
  38.         var gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
  39.         //    Check whether the WebGL context is available or not
  40.         //    if it's not available exit
  41.         if(!gl){
  42.             alert("Unfortunately, your browser/machine doesn't support WebGL :(");
  43.             return;
  44.         }
  45.         //    Set the viewport to the canvas width and height
  46.         gl.viewport(0, 0, canvas.width, canvas.height);
  47.  
  48.        
  49.        
  50.         //    Load the vertex shader that's defined in a separate script
  51.         //    block at the top of this script block.
  52.  
  53.         //    Grab the script element
  54.         var vertexShaderScript = document.getElementById("shader-vs");
  55.         //    Create a vertex shader object
  56.         var vertexShader = gl.createShader(gl.VERTEX_SHADER);
  57.         //    Load the shader with the source strings from the script element
  58.         gl.shaderSource(vertexShader, vertexShaderScript.text);
  59.         //    Compile the shader source code string
  60.         gl.compileShader(vertexShader);
  61.         //    Check if the shader has compiled without errors
  62.         if(!gl.getShaderParameter(vertexShader, gl.COMPILE_STATUS)) {
  63.             alert("Couldn't compile the vertex shader");
  64.             //    Clean up
  65.             gl.deleteShader(vertexShader);
  66.             return;
  67.         }
  68.  
  69.        
  70.        
  71.         //    Load the fragment shader that's defined in a separate script
  72.         //    block at the top of this script block.
  73.        
  74.         //    Grab the element
  75.         var fragmentShaderScript = document.getElementById("shader-fs");
  76.         //    Create a fragment shader object
  77.         var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
  78.         //    Load the shader with the source strings from the script element
  79.         gl.shaderSource(fragmentShader, fragmentShaderScript.text);
  80.         //    Compile the shader source code string
  81.         gl.compileShader(fragmentShader);
  82.         //    Check if the shader has compiled without errors
  83.         if(!gl.getShaderParameter(fragmentShader, gl.COMPILE_STATUS)) {
  84.             alert("Couldn't compile the fragment shader");
  85.             //    Clean up
  86.             gl.deleteShader(fragmentShader);
  87.             return;
  88.         }
  89.  
  90.        
  91.        
  92.         //    Create a shader program. From the OpenGL documentation:
  93.         //    A program object is an object to which shader objects can be attached.
  94.         //    This provides a mechanism to specify the shader objects that will be linked to
  95.         //    create a program. It also provides a means for checking the compatibility of the
  96.         //    shaders that will be used to create a program (for instance, checking the
  97.         //    compatibility between a vertex shader and a fragment shader).
  98.         gl.program = gl.createProgram();
  99.         //    Attach the vertex shader to the program
  100.         gl.attachShader(gl.program, vertexShader);
  101.         //    Attach the fragment shader to the program
  102.         gl.attachShader(gl.program, fragmentShader);
  103.         //    Before we can use the shaders for rendering, we have to link the program
  104.         //    object.
  105.         gl.linkProgram(gl.program);
  106.         //    Check the status of the link operation to see if it was linked without
  107.         //    errors.
  108.         if (!gl.getProgramParameter(gl.program, gl.LINK_STATUS)) {
  109.             alert("Unable to initialise shaders");
  110.             //    Clean up
  111.             gl.deleteProgram(gl.program);
  112.             gl.deleteProgram(vertexShader);
  113.             gl.deleteProgram(fragmentShader);
  114.             return;
  115.         }
  116.         //    Install the program as part of the current rendering state
  117.         gl.useProgram(gl.program);
  118.         //    Get the vertexPosition attribute from the linked shader program
  119.         var vertexPosition = gl.getAttribLocation(gl.program, "vertexPosition");
  120.         //    Enable the vertexPosition vertex attribute array. If enabled, the array
  121.         //    will be accessed an used for rendering when calls are made to commands like
  122.         //    gl.drawArrays, gl.drawElements, etc.
  123.         gl.enableVertexAttribArray(vertexPosition);
  124.  
  125.         //    Clear the color buffer (r, g, b, a) with the specified color
  126.         gl.clearColor(0.0, 0.0, 0.0, 1.0);
  127.         //    Clear the depth buffer. The value specified is clamped to the range [0,1].
  128.         //    More info about depth buffers: http://en.wikipedia.org/wiki/Depth_buffer
  129.         gl.clearDepth(1.0);
  130.         //    Enable depth testing. This is a technique used for hidden surface removal.
  131.         //    It assigns a value (z) to each pixel that represents the distance from this
  132.         //    pixel to the viewer. When another pixel is drawn at the same location the z
  133.         //    values are compared in order to determine which pixel should be drawn.
  134.         gl.enable(gl.DEPTH_TEST);
  135.         //    Specify which function to use for depth buffer comparisons. It compares the
  136.         //    value of the incoming pixel against the one stored in the depth buffer.
  137.         //    Possible values are (from the OpenGL documentation):
  138.         //    GL_NEVER - Never passes.
  139.         //    GL_LESS - Passes if the incoming depth value is less than the stored depth value.
  140.         //    GL_EQUAL - Passes if the incoming depth value is equal to the stored depth value.
  141.         //    GL_LEQUAL - Passes if the incoming depth value is less than or equal to the stored depth value.
  142.         //    GL_GREATER - Passes if the incoming depth value is greater than the stored depth value.
  143.         //    GL_NOTEQUAL - Passes if the incoming depth value is not equal to the stored depth value.
  144.         //    GL_GEQUAL - Passes if the incoming depth value is greater than or equal to the stored depth value.
  145.         //    GL_ALWAYS - Always passes.                        
  146.         gl.depthFunc(gl.LEQUAL);
  147.  
  148.        
  149.        
  150.         //    Now create a shape. For instance, a filled triangle.
  151.        
  152.        
  153.        
  154.         //    First create a vertex buffer in which we can store our data.
  155.         var vertexBuffer = gl.createBuffer();
  156.         //    Bind the buffer object to the ARRAY_BUFFER target.
  157.         gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  158.         //    Specify the vertex positions (x, y, z)
  159.         var vertices = new Float32Array([
  160.             0.0,  1.0,  4.0,
  161.             -1.0, -1.0,  4.0,
  162.             1.0, -1.0,  4.0
  163.         ]);
  164.  
  165.         //    Creates a new data store for the vertices array which is bound to the ARRAY_BUFFER.
  166.         //    The third paramater indicates the usage pattern of the data store. Possible values are
  167.         //    (from the OpenGL documentation):
  168.         //    The frequency of access may be one of these:      
  169.         //    STREAM - The data store contents will be modified once and used at most a few times.
  170.         //    STATIC - The data store contents will be modified once and used many times.
  171.         //    DYNAMIC - The data store contents will be modified repeatedly and used many times.
  172.         //    The nature of access may be one of these:
  173.         //    DRAW - The data store contents are modified by the application, and used as the source for
  174.         //           GL drawing and image specification commands.
  175.         //    READ - The data store contents are modified by reading data from the GL, and used to return
  176.         //           that data when queried by the application.
  177.         //    COPY - The data store contents are modified by reading data from the GL, and used as the source
  178.         //           for GL drawing and image specification commands.                        
  179.         gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
  180.  
  181.         //    Clear the color buffer and the depth buffer
  182.         gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  183.  
  184.         //    Define the viewing frustum parameters
  185.         //    More info: http://en.wikipedia.org/wiki/Viewing_frustum
  186.         //    More info: http://knol.google.com/k/view-frustum
  187.         var fieldOfView = 30.0;
  188.         var aspectRatio = canvas.width / canvas.height;
  189.         var nearPlane = 1.0;
  190.         var farPlane = 10000.0;
  191.         var top = nearPlane * Math.tan(fieldOfView * Math.PI / 360.0);
  192.         var bottom = -top;
  193.         var right = top * aspectRatio;
  194.         var left = -right;
  195.  
  196.         //     Create the perspective matrix. The OpenGL function that's normally used for this,
  197.         //     glFrustum() is not included in the WebGL API. That's why we have to do it manually here.
  198.         //     More info: http://www.cs.utk.edu/~vose/c-stuff/opengl/glFrustum.html
  199.         var a = (right + left) / (right - left);
  200.         var b = (top + bottom) / (top - bottom);
  201.         var c = (farPlane + nearPlane) / (farPlane - nearPlane);
  202.         var d = (2 * farPlane * nearPlane) / (farPlane - nearPlane);
  203.         var x = (2 * nearPlane) / (right - left);
  204.         var y = (2 * nearPlane) / (top - bottom);
  205.         var perspectiveMatrix = [
  206.             x, 0, a, 0,
  207.             0, y, b, 0,
  208.             0, 0, c, d,
  209.             0, 0, -1, 0
  210.         ];
  211.  
  212.         //     Create the modelview matrix
  213.         //     More info about the modelview matrix: http://3dengine.org/Modelview_matrix
  214.         //     More info about the identity matrix: http://en.wikipedia.org/wiki/Identity_matrix
  215.         var modelViewMatrix = [
  216.             1, 0, 0, 0,
  217.             0, 1, 0, 0,
  218.             0, 0, 1, 0,
  219.             0, 0, 0, 1
  220.         ];
  221.         //     Get the vertex position attribute location from the shader program
  222.         var vertexPosAttribLocation = gl.getAttribLocation(gl.program, "vertexPosition");
  223.         //     Specify the location and format of the vertex position attribute
  224.         gl.vertexAttribPointer(vertexPosAttribLocation, 3.0, gl.FLOAT, false, 0, 0);
  225.         //     Get the location of the "modelViewMatrix" uniform variable from the
  226.         //     shader program
  227.         var uModelViewMatrix = gl.getUniformLocation(gl.program, "modelViewMatrix");
  228.         //     Get the location of the "perspectiveMatrix" uniform variable from the
  229.         //     shader program
  230.         var uPerspectiveMatrix = gl.getUniformLocation(gl.program, "perspectiveMatrix");
  231.         //     Set the values
  232.         gl.uniformMatrix4fv(uModelViewMatrix, false, new Float32Array(perspectiveMatrix));
  233.         gl.uniformMatrix4fv(uPerspectiveMatrix, false, new Float32Array(modelViewMatrix));
  234.         //     Draw the triangles in the vertex buffer. The first parameter specifies what
  235.         //     drawing mode to use. This can be GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP,
  236.         //     GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_TRIANGLES, GL_QUAD_STRIP,
  237.         //     GL_QUADS, and GL_POLYGON
  238.         gl.drawArrays(gl.TRIANGLES, 0, vertices.length / 3.0);
  239.         gl.flush();
  240.     }
  241. </script>
  242.  
  243. <canvas id="webGLCanvas" width="500" height="500"></canvas>
Add Comment
Please, Sign In to add comment