Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Vector from './vector';
- import Ray from './ray';
- import Intersection from './intersection';
- import Shader from './shader';
- import Material from './material';
- /**
- * Class representing an axis aligned box
- */
- export default class Box {
- /**
- * The box's vertices
- */
- vertices: Array<Vector>;
- /**
- * The indices of the vertices that
- * together form the faces of the box
- */
- indices: Array<number>;
- /**
- * The buffer containing the box's vertices
- */
- vertexBuffer: WebGLBuffer;
- /**
- * The indices describing which vertices form a triangle
- */
- indexBuffer: WebGLBuffer;
- // TODO add variable for color buffer
- ambientBuffer: WebGLBuffer;
- diffuseBuffer: WebGLBuffer;
- specularBuffer: WebGLBuffer;
- //colorBuffer: WebGLBuffer;
- /**
- * The amount of indices
- */
- elements: number;
- /**
- * Creates an axis aligned box
- * @param minPoint The minimum Point
- * @param maxPoint The maximum Point
- */
- constructor(private gl: WebGL2RenderingContext,
- minPoint: Vector,
- maxPoint: Vector) {
- /*
- 7----6
- /| /| 2 = maxPoint
- 3----2 | 4 = minPoint
- | 4--|-5 Looking into negative z direction
- |/ |/
- 0----1
- */
- const mi = minPoint;
- const ma = maxPoint;
- this.vertices = [
- new Vector(mi.x*4-1, mi.y*4-2, ma.z, 1),
- new Vector(ma.x*4-1, mi.y*4-2, ma.z, 1),
- new Vector(ma.x*4-1, ma.y*4-2, ma.z, 1),
- new Vector(mi.x*4-1, ma.y*4-2, ma.z, 1),
- new Vector(ma.x*4-1, mi.y*4-2, mi.z, 1),
- new Vector(mi.x*4-1, mi.y*4-2, mi.z, 1),
- new Vector(mi.x*4-1, ma.y*4-2, mi.z, 1),
- new Vector(ma.x*4-1, ma.y*4-2, mi.z, 1)
- ];
- this.indices = [
- 0, 1, 2, 2, 3, 0,
- 4, 5, 6, 6, 7, 4,
- 1, 4, 7, 7, 2, 1,
- 3, 2, 7, 7, 6, 3,
- 5, 0, 3, 3, 6, 5,
- 5, 4, 1, 1, 0, 5
- ];
- if(!gl){
- return;
- }
- const vertexBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
- gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertices.flatMap(v => v.data.slice(0,3))), gl.STATIC_DRAW);
- this.vertexBuffer = vertexBuffer;
- const indexBuffer = gl.createBuffer();
- gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
- gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.indices), gl.STATIC_DRAW);
- this.indexBuffer = indexBuffer;
- this.elements = this.indices.length;
- // TODO create and fill color buffer
- 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, ];
- const ambientBuffer = this.gl.createBuffer();
- const diffuseBuffer = this.gl.createBuffer();
- const specularBuffer= this.gl.createBuffer();
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, ambientBuffer);
- this.gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
- this.ambientBuffer = ambientBuffer;
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, diffuseBuffer);
- this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
- this.diffuseBuffer = diffuseBuffer;
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, specularBuffer);
- this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(color), this.gl.STATIC_DRAW);
- this.specularBuffer = specularBuffer;
- console.log("INITIALIZER completed");
- }
- /**
- * Calculates the intersection of the AAbox with the given ray
- * @param ray The ray to intersect with
- * @return The intersection if there is one, null if there is none
- */
- intersect(ray: Ray): Intersection | null {
- // TODO implement intersection
- return null;
- }
- /**
- * Renders the box
- * @param shader The shader used to render
- */
- render(shader: Shader) {
- this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.vertexBuffer);
- const positionLocation = shader.getAttributeLocation("a_position");
- this.gl.enableVertexAttribArray(positionLocation);
- this.gl.vertexAttribPointer(positionLocation,
- 3, this.gl.FLOAT, false, 0, 0);
- // // TODO bind color buffer
- // this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.colorBuffer);
- //
- // // ADDED stuff
- // const colorLocation = shader.getAttributeLocation( "a_color");
- // this.gl.enableVertexAttribArray(colorLocation);
- //
- // this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.colorBuffer);
- //
- //
- // this.gl.vertexAttribPointer(colorLocation,
- // 3, // triangle
- // this.gl.FLOAT, // FLOAT
- // false, // do not normalize
- // 0, // stride
- // 0); // OFFSET
- // this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.ambientBuffer);
- // console.log(this.ambientBuffer);
- //
- // const colorLocation = shader.getAttributeLocation( "a_color");
- //
- //
- // this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
- // this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
- //
- // this.gl.disableVertexAttribArray(positionLocation);
- // // TODO disable color vertex attrib array
- // this.gl.disableVertexAttribArray(colorLocation);
- const colorLocation = shader.getAttributeLocation( "a_vertexColor");
- this.gl.vertexAttribPointer(colorLocation, 3, this.gl.FLOAT, false, 4, 0);
- this.gl.enableVertexAttribArray(colorLocation);
- console.log("colorLocation is : "); console.log(colorLocation);
- //shader.getUniformVec3("vs_color").set(new Vector(0.2,0.4,0.0));
- this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
- this.gl.drawArrays(this.gl.TRIANGLE_FAN, 0, this.elements);
- // this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
- // this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
- //
- // this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer);
- // this.gl.drawElements(this.gl.TRIANGLES, this.elements, this.gl.UNSIGNED_SHORT, 0);
- this.gl.disableVertexAttribArray(positionLocation);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment