sean_s

Untitled

Aug 24th, 2023 (edited)
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Vector from './vector';
  2. import Ray from './ray';
  3. import Intersection from './intersection';
  4. import Shader from './shader';
  5. import Material from './material';
  6.  
  7. /**
  8.  * Class representing an axis aligned box
  9.  */
  10. export default class Box {
  11.   /**
  12.    * The box's vertices
  13.    */
  14.   vertices: Array<Vector>;
  15.   /**
  16.    * The indices of the vertices that
  17.    * together form the faces of the box
  18.    */
  19.   indices: Array<number>;
  20.  
  21.   /**
  22.      * The buffer containing the box's vertices
  23.      */
  24.   vertexBuffer: WebGLBuffer;
  25.   /**
  26.    * The indices describing which vertices form a triangle
  27.    */
  28.   indexBuffer: WebGLBuffer;
  29.   // TODO add variable for color buffer
  30.   ambientBuffer: WebGLBuffer;
  31.   diffuseBuffer: WebGLBuffer;
  32.   specularBuffer: WebGLBuffer;
  33.  
  34.   //colorBuffer: WebGLBuffer;
  35.   /**
  36.    * The amount of indices
  37.    */
  38.   elements: number;
  39.  
  40.  
  41.   /**
  42.    * Creates an axis aligned box
  43.    * @param minPoint The minimum Point
  44.    * @param maxPoint The maximum Point
  45.    */
  46.   constructor(private gl: WebGL2RenderingContext,
  47.     minPoint: Vector,
  48.     maxPoint: Vector) {
  49.     /*
  50.       7----6
  51.      /|   /|   2 = maxPoint
  52.     3----2 |   4 = minPoint
  53.     | 4--|-5   Looking into negative z direction
  54.     |/   |/
  55.     0----1
  56.      */
  57.  
  58.     const mi = minPoint;
  59.     const ma = maxPoint;
  60.     this.vertices = [
  61.       new Vector(mi.x*4-1, mi.y*4-2, ma.z, 1),
  62.       new Vector(ma.x*4-1, mi.y*4-2, ma.z, 1),
  63.       new Vector(ma.x*4-1, ma.y*4-2, ma.z, 1),
  64.       new Vector(mi.x*4-1, ma.y*4-2, ma.z, 1),
  65.       new Vector(ma.x*4-1, mi.y*4-2, mi.z, 1),
  66.       new Vector(mi.x*4-1, mi.y*4-2, mi.z, 1),
  67.       new Vector(mi.x*4-1, ma.y*4-2, mi.z, 1),
  68.       new Vector(ma.x*4-1, ma.y*4-2, mi.z, 1)
  69.     ];
  70.     this.indices = [
  71.       0, 1, 2, 2, 3, 0,
  72.       4, 5, 6, 6, 7, 4,
  73.       1, 4, 7, 7, 2, 1,
  74.       3, 2, 7, 7, 6, 3,
  75.       5, 0, 3, 3, 6, 5,
  76.       5, 4, 1, 1, 0, 5
  77.     ];
  78.  
  79.     if(!gl){
  80.       return;
  81.     }
  82.  
  83.     const vertexBuffer = gl.createBuffer();
  84.         gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
  85.         gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertices.flatMap(v => v.data.slice(0,3))), gl.STATIC_DRAW);
  86.         this.vertexBuffer = vertexBuffer;
  87.         const indexBuffer = gl.createBuffer();
  88.         gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  89.         gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.indices), gl.STATIC_DRAW);
  90.         this.indexBuffer = indexBuffer;
  91.         this.elements = this.indices.length;
  92.  
  93.         // TODO create  and fill color buffer
  94.  
  95.  
  96.  
  97.         var color = [1.0,1.0,0, 1.0,1.0,1.0, 0,1,0, 0,0,0, 1,1,1, 0,1,0, 0,0,0, 1,1,0,  ];
  98.  
  99.  
  100.     const ambientBuffer = this.gl.createBuffer();
  101.     const diffuseBuffer = this.gl.createBuffer();
  102.     const specularBuffer= this.gl.createBuffer();
  103.  
  104.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, ambientBuffer);
  105.     this.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
  106.     this.ambientBuffer = ambientBuffer;
  107.  
  108.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, diffuseBuffer);
  109.     this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
  110.     this.diffuseBuffer = diffuseBuffer;
  111.  
  112.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, specularBuffer);
  113.     this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
  114.     this.specularBuffer = specularBuffer;
  115.  
  116.     console.log("INITIALIZER completed");
  117.  
  118.   }
  119.  
  120.   /**
  121.    * Calculates the intersection of the AAbox with the given ray
  122.    * @param ray The ray to intersect with
  123.    * @return The intersection if there is one, null if there is none
  124.    */
  125.   intersect(ray: Ray): Intersection | null {
  126.     // TODO implement intersection
  127.     return null;
  128.   }
  129.  
  130.  
  131.   /**
  132.      * Renders the box
  133.      * @param shader The shader used to render
  134.      */
  135.   render(shader: Shader) {
  136.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
  137.     const positionLocation = shader.getAttributeLocation("a_position");
  138.     this.gl.enableVertexAttribArray(positionLocation);
  139.     this.gl.vertexAttribPointer(positionLocation,
  140.         3, this.gl.FLOAT, false, 0, 0);
  141.  
  142.    //  // TODO bind color buffer
  143.    //  this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.colorBuffer);
  144.    //
  145.    //  // ADDED stuff
  146.    // const colorLocation = shader.getAttributeLocation( "a_color");
  147.    //  this.gl.enableVertexAttribArray(colorLocation);
  148.    //
  149.    //  this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.colorBuffer);
  150.    //
  151.    //
  152.    //  this.gl.vertexAttribPointer(colorLocation,
  153.    //      3,                                      // triangle
  154.    //      this.gl.FLOAT,                          // FLOAT
  155.    //      false,                                  // do not normalize
  156.    //      0,                                      // stride
  157.    //      0);                                     // OFFSET
  158.  
  159.  
  160.     // this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.ambientBuffer);
  161.     // console.log(this.ambientBuffer);
  162.     //
  163.     // const colorLocation = shader.getAttributeLocation( "a_color");
  164.     //
  165.     //
  166.     // this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  167.     // this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
  168.     //
  169.     // this.gl.disableVertexAttribArray(positionLocation);
  170.     // // TODO disable color vertex attrib array
  171.     // this.gl.disableVertexAttribArray(colorLocation);
  172.  
  173.  
  174.  
  175.  
  176.     const colorLocation = shader.getAttributeLocation( "a_vertexColor");
  177.  
  178.     this.gl.vertexAttribPointer(colorLocation, 3, this.gl.FLOAT, false, 4, 0);
  179.     this.gl.enableVertexAttribArray(colorLocation);
  180.     console.log("colorLocation is : "); console.log(colorLocation);
  181.  
  182.  
  183.  
  184.  
  185.     //shader.getUniformVec3("vs_color").set(new Vector(0.2,0.4,0.0));
  186.  
  187.  
  188.     this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  189.     this.gl.drawArrays(this.gl.TRIANGLE_FAN, 0, this.elements);
  190.  
  191.  
  192.     // this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  193.     // this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
  194.     //
  195.     // this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  196.     // this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
  197.  
  198.     this.gl.disableVertexAttribArray(positionLocation);
  199.  
  200. }
  201.  
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment