sean_s

Untitled

Aug 24th, 2023 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Vector from './vector';
  2. import Intersection from './intersection';
  3. import Ray from './ray';
  4. import Material from './material';
  5. import Shader from './shader';
  6.  
  7. /**
  8.  * A class representing a sphere
  9.  */
  10. export default class Sphere {
  11.  
  12.   vertexBuffer: WebGLBuffer;
  13.   /**
  14.    * The indices describing which vertices form a triangle
  15.    */
  16.   indexBuffer: WebGLBuffer;
  17.   /**
  18.    * The normals on the surface at each vertex location
  19.    */
  20.   normalBuffer: WebGLBuffer;
  21.   /**
  22.    * The color of each vertex
  23.    */
  24.   ambientBuffer: WebGLBuffer;
  25.   diffuseBuffer: WebGLBuffer;
  26.   specularBuffer: WebGLBuffer;
  27.   /**
  28.    * The amount of indices
  29.    *
  30.    */
  31.  
  32.   // colorBuffer: WebGLBuffer;
  33.   elements: number;
  34.  
  35.   /**
  36.    * Creates a new Sphere with center and radius
  37.    * @param center The center of the Sphere
  38.    * @param radius The radius of the Sphere
  39.    * @param color The color of the Sphere
  40.    */
  41.   constructor(
  42.     private gl: WebGL2RenderingContext,
  43.     public center: Vector = new Vector(0, 0, 0, 1),
  44.     public radius: number = .5,
  45.     public material: Material = Material.simply["red"]
  46.   ) {
  47.     let vertices = [];
  48.     let indices = [];
  49.     let normals = [];
  50.  
  51.     if(!gl){
  52.       return;
  53.     }
  54.  
  55.     let ringsize = 30;
  56.     for (let ring = 0; ring < ringsize; ring++) {
  57.         for (let ring2 = 0; ring2 < ringsize; ring2++) {
  58.             let theta = ring * Math.PI * 2 / ringsize ;
  59.             let phi = ring2 * Math.PI * 2 / ringsize;
  60.             let x = (radius *
  61.                 Math.sin(theta) *
  62.                 Math.cos(phi) +
  63.                 center.x
  64.             );
  65.             let y = (radius *
  66.                 Math.sin(theta) *
  67.                 Math.sin(phi) +
  68.                 center.y
  69.             );
  70.             let z = (radius *
  71.                 Math.cos(theta) +
  72.                 center.z-2
  73.             );
  74.             vertices.push(x);
  75.             vertices.push(y);
  76.             vertices.push(z);
  77.  
  78.             let normal = (new Vector(x, y, z, 1)).sub(center).normalize();
  79.             normals.push(normal.x);
  80.             normals.push(normal.y);
  81.             normals.push(normal.z);
  82.         }
  83.     }
  84.  
  85.     for (let ring = 0; ring < ringsize / 2; ring++) {
  86.         for (let ring2 = 0; ring2 < ringsize; ring2++) {
  87.             indices.push(ring * ringsize + ring2);
  88.             indices.push((ring + 1) * ringsize + ring2);
  89.             indices.push(ring * ringsize + ((ring2 + 1) % ringsize));
  90.  
  91.             indices.push(ring * ringsize + ((ring2 + 1) % ringsize));
  92.             indices.push((ring + 1) * ringsize + ring2);
  93.             indices.push((ring + 1) * ringsize + ((ring2 + 1) % ringsize));
  94.         }
  95.     }
  96.  
  97.     const vertexBuffer = this.gl.createBuffer();
  98.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, vertexBuffer);
  99.     this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(vertices), this.gl.STATIC_DRAW);
  100.     this.vertexBuffer = vertexBuffer;
  101.     const indexBuffer = gl.createBuffer();
  102.     this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
  103.     this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), this.gl.STATIC_DRAW);
  104.     this.indexBuffer = indexBuffer;
  105.     const normalBuffer = this.gl.createBuffer();
  106.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, normalBuffer);
  107.     this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(normals), this.gl.STATIC_DRAW);
  108.     this.normalBuffer = normalBuffer;
  109.     this.elements = indices.length;
  110.  
  111.     // TODO  create colorBuffer
  112.  
  113.  
  114.     var color = [1,0,1, 1,0,1, 1,0,1, ];
  115.  
  116.  
  117.     const ambientBuffer = this.gl.createBuffer();
  118.     const diffuseBuffer = this.gl.createBuffer();
  119.     const specularBuffer= this.gl.createBuffer();
  120.  
  121.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, ambientBuffer);
  122.     this.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
  123.     this.ambientBuffer = ambientBuffer;
  124.  
  125.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, diffuseBuffer);
  126.     this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
  127.     this.diffuseBuffer = diffuseBuffer;
  128.  
  129.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, specularBuffer);
  130.     this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
  131.     this.specularBuffer = specularBuffer;
  132.  
  133.     console.log("INITIALIZER completed");
  134.  
  135.  
  136.   }
  137.  
  138.  
  139.   /**
  140.    * Calculates the intersection of the sphere with the given ray
  141.    * @param ray The ray to intersect with
  142.    * @return The intersection if there is one, null if there is none
  143.    */
  144.   intersect(ray: Ray): Intersection | null {
  145.  
  146.     const rayDirection = ray.direction.normalize();
  147.     const rayOrigin = ray.origin.sub(this.center);
  148.  
  149.  
  150.     const a = (rayOrigin.dot(rayDirection)) ** 2;
  151.     const b = (rayOrigin.dot(rayOrigin));
  152.     const c = (this.radius ** 2);
  153.  
  154.     const discriminant = a - b + c;
  155.  
  156.  
  157.  
  158.     if (discriminant < 0){
  159.       return null;
  160.     } else {
  161.       const t1 = (-rayOrigin.dot(rayDirection)) + (Math.sqrt(discriminant));
  162.       const t2 = (-rayOrigin.dot(rayDirection)) - (Math.sqrt(discriminant));
  163.  
  164.       const t = Math.min(t1, t2);
  165.  
  166.       const interactionPoint = rayDirection.mul(t).add(ray.origin);
  167.       const surfaceNormal = (interactionPoint.sub(this.center)).normalize();
  168.  
  169.       return new Intersection(t, interactionPoint, surfaceNormal, this.material);
  170.     }
  171.   }
  172.  
  173.  
  174.   /**
  175.      * Renders the sphere
  176.      * @param {Shader} shader - The shader used to render
  177.      */
  178.   render(shader: Shader) {                                                  // overload target
  179.     this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
  180.     const positionLocation = shader.getAttributeLocation("a_position");
  181.     this.gl.enableVertexAttribArray(positionLocation);
  182.     this.gl.vertexAttribPointer(positionLocation, 3, this.gl.FLOAT, false, 0, 0);
  183.  
  184.     // TODO: bind color buffer
  185.  
  186.  
  187.  
  188.  
  189.  
  190.     const colorLocation = shader.getAttributeLocation( "a_vertexColor");
  191.  
  192.     this.gl.vertexAttribPointer(colorLocation, //buffer
  193.                                 3, // count
  194.                                 this.gl.FLOAT, //type
  195.                                 false, // normalize
  196.                                 4, // wie viel überspringen pro schritt (stride)
  197.                                 0 // wie viele am anfang vernachlässigen (offset)
  198.                                 );
  199.     this.gl.enableVertexAttribArray(colorLocation);
  200.     console.log("colorLocation is : "); console.log(colorLocation);
  201.  
  202.  
  203.  
  204.  
  205.     //shader.getUniformVec3("v_color").set(new Vector(0.2,0.4,0.0));
  206.  
  207.  
  208.     this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  209.     this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
  210.  
  211.  
  212.     // this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  213.     // this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
  214.     //
  215.     // this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
  216.     // this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
  217.  
  218.     //this.gl.disableVertexAttribArray(positionLocation);
  219.     //this.gl.disableVertexAttribArray(colorLocation);
  220.  
  221.  
  222.  
  223.  
  224.  
  225.   }
  226.  
  227. }
  228.  
Advertisement
Add Comment
Please, Sign In to add comment