Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.44 KB | None | 0 0
  1. import 'dart:html';
  2. import 'dart:web_gl';
  3. import 'dart:typed_data';
  4.  
  5. void main() {
  6.   CanvasElement canvas = querySelector('#game_canvas');
  7.   RenderingContext gl = canvas.getContext('experimental-webgl');
  8.  
  9.   /*======== Defining and storing the geometry ===========*/
  10.  
  11.   Float32List vertices = Float32List.fromList([
  12.     -0.5,
  13.     0.5,
  14.     0.0,
  15.     -0.5,
  16.     -0.5,
  17.     0.0,
  18.     0.5,
  19.     -0.5,
  20.     0.0,
  21.   ]);
  22.  
  23.   Uint32List indices = Uint32List.fromList([0, 1, 2]);
  24.  
  25.   // Create an empty buffer object to store vertex buffer
  26.   var vertex_buffer = gl.createBuffer();
  27.  
  28.   // Bind appropriate array buffer to it
  29.   gl.bindBuffer(WebGL.ARRAY_BUFFER, vertex_buffer);
  30.  
  31.   // Pass the vertex data to the buffer
  32.   gl.bufferData(WebGL.ARRAY_BUFFER, vertices, WebGL.STATIC_DRAW);
  33.  
  34.   // Unbind the buffer
  35.   gl.bindBuffer(WebGL.ARRAY_BUFFER, null);
  36.  
  37.   // Create an empty buffer object to store Index buffer
  38.   var Index_Buffer = gl.createBuffer();
  39.  
  40.   // Bind appropriate array buffer to it
  41.   gl.bindBuffer(WebGL.ELEMENT_ARRAY_BUFFER, Index_Buffer);
  42.  
  43.   // Pass the vertex data to the buffer
  44.   gl.bufferData(
  45.       WebGL.ELEMENT_ARRAY_BUFFER, indices, WebGL.STATIC_DRAW);
  46.  
  47.   // Unbind the buffer
  48.   gl.bindBuffer(WebGL.ELEMENT_ARRAY_BUFFER, null);
  49.  
  50.   /*================ Shaders ====================*/
  51.  
  52.   // Vertex shader source code
  53.   var vertCode = 'attribute vec3 coordinates;' +
  54.       'void main(void) {' +
  55.       ' gl_Position = vec4(coordinates, 1.0);' +
  56.       '}';
  57.  
  58.   // Create a vertex shader object
  59.   var vertShader = gl.createShader(WebGL.VERTEX_SHADER);
  60.  
  61.   // Attach vertex shader source code
  62.   gl.shaderSource(vertShader, vertCode);
  63.  
  64.   // Compile the vertex shader
  65.   gl.compileShader(vertShader);
  66.  
  67.   //fragment shader source code
  68.   var fragCode =
  69.       'void main(void) {' + ' gl_FragColor = vec4(0.0, 0.0, 0.0, 0.1);' + '}';
  70.  
  71.   // Create fragment shader object
  72.   var fragShader = gl.createShader(WebGL.FRAGMENT_SHADER);
  73.  
  74.   // Attach fragment shader source code
  75.   gl.shaderSource(fragShader, fragCode);
  76.  
  77.   // Compile the fragmentt shader
  78.   gl.compileShader(fragShader);
  79.  
  80.   // Create a shader program object to store
  81.   // the combined shader program
  82.   var shaderProgram = gl.createProgram();
  83.  
  84.   // Attach a vertex shader
  85.   gl.attachShader(shaderProgram, vertShader);
  86.  
  87.   // Attach a fragment shader
  88.   gl.attachShader(shaderProgram, fragShader);
  89.  
  90.   // Link both the programs
  91.   gl.linkProgram(shaderProgram);
  92.  
  93.   // Use the combined shader program object
  94.   gl.useProgram(shaderProgram);
  95.  
  96.   /*======= Associating shaders to buffer objects =======*/
  97.  
  98.   // Bind vertex buffer object
  99.   gl.bindBuffer(WebGL.ARRAY_BUFFER, vertex_buffer);
  100.  
  101.   // Bind index buffer object
  102.   gl.bindBuffer(WebGL.ELEMENT_ARRAY_BUFFER, Index_Buffer);
  103.  
  104.   // Get the attribute location
  105.   var coord = gl.getAttribLocation(shaderProgram, "coordinates");
  106.  
  107.   // Point an attribute to the currently bound VBO
  108.   gl.vertexAttribPointer(coord, 3, WebGL.FLOAT, false, 0, 0);
  109.  
  110.   // Enable the attribute
  111.   gl.enableVertexAttribArray(coord);
  112.  
  113.   /*=========Drawing the triangle===========*/
  114.  
  115.   // Clear the canvas
  116.   gl.clearColor(0.5, 0.5, 0.5, 0.9);
  117.  
  118.   // Enable the depth test
  119.   gl.enable(WebGL.DEPTH_TEST);
  120.  
  121.   // Clear the color buffer bit
  122.   gl.clear(WebGL.COLOR_BUFFER_BIT);
  123.  
  124.   // Set the view port
  125.   gl.viewport(0, 0, canvas.width, canvas.height);
  126.  
  127.   // Draw the triangle
  128.   gl.drawElements(WebGL.TRIANGLES, indices.length, WebGL.UNSIGNED_SHORT, 0);
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement