First commit
This commit is contained in:
97
engine/quad.js
Executable file
97
engine/quad.js
Executable file
@@ -0,0 +1,97 @@
|
||||
import primitives from './primitives.js';
|
||||
import {math, vector3, matrix4} from './math.js';
|
||||
/**
|
||||
* quad this.engine.quad.draw( shader );
|
||||
**/
|
||||
|
||||
class quad{
|
||||
|
||||
constructor( ) {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* set viewport
|
||||
* @param {(viewport)} viewport
|
||||
**/
|
||||
setViewport( viewport ){
|
||||
|
||||
this.viewport = viewport;
|
||||
this.gl = viewport.gl;
|
||||
|
||||
|
||||
var plane = this.viewport.primitives.createPlane(2, 2, 1, 1);
|
||||
|
||||
console.log(plane);
|
||||
|
||||
this.quadView = matrix4.lookAt([0, 0, 0], [0, -1, 0], [0, 0, -1]);
|
||||
this.quadProjection = matrix4.orthographic(-1, 1, -1, 1, -1, 1);
|
||||
|
||||
this.viewProjection = matrix4.mul(this.quadView, this.quadProjection);
|
||||
|
||||
this.quadVertices = this.gl.createBuffer();
|
||||
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.quadVertices);
|
||||
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(plane.vertices), this.gl.STATIC_DRAW);
|
||||
this.quadVertices.name = 'position';
|
||||
this.quadVertices.itemSize = 3;
|
||||
this.quadVertices.numItems = plane.vertices.length / 3;
|
||||
|
||||
this.quadUv = this.gl.createBuffer();
|
||||
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.quadUv);
|
||||
this.gl.bufferData(this.gl.ARRAY_BUFFER, new Float32Array(plane.textureCoordinates), this.gl.STATIC_DRAW);
|
||||
this.quadUv.name = 'uv';
|
||||
this.quadUv.itemSize = 2;
|
||||
this.quadUv.numItems = plane.textureCoordinates.length / 2;
|
||||
|
||||
|
||||
this.quadIndices = this.gl.createBuffer();
|
||||
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.quadIndices);
|
||||
this.gl.bufferData(this.gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(plane.indices), this.gl.STATIC_DRAW);
|
||||
this.quadIndices.name = 'index';
|
||||
this.quadIndices.itemSize = 1;
|
||||
this.quadIndices.numItems = plane.indices.length;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
draw( shader ) {
|
||||
|
||||
|
||||
|
||||
if( !shader.compiled ) {
|
||||
|
||||
shader.setViewport( this.viewport );
|
||||
shader.compile();
|
||||
|
||||
}
|
||||
|
||||
shader.update();
|
||||
|
||||
var quadVertices = this.quadVertices;
|
||||
var quadIndices = this.quadIndices;
|
||||
var quadUv = this.quadUv;
|
||||
|
||||
//console.log(quadUv);
|
||||
|
||||
this.gl.useProgram( shader.program );
|
||||
|
||||
var attribute = shader.getAttributeByName('position');
|
||||
|
||||
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, quadVertices);
|
||||
this.gl.vertexAttribPointer(attribute, quadVertices.itemSize, this.gl.FLOAT, false, 0, 0);
|
||||
|
||||
var attribute = shader.getAttributeByName('uv');
|
||||
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, quadUv);
|
||||
this.gl.vertexAttribPointer(attribute, quadUv.itemSize, this.gl.FLOAT, false, 0, 0);
|
||||
|
||||
this.gl.bindBuffer( this.gl.ELEMENT_ARRAY_BUFFER, quadIndices );
|
||||
this.gl.drawElements( this.gl.TRIANGLES, quadIndices.numItems, this.gl.UNSIGNED_SHORT, 0 );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export {quad as default};
|
||||
Reference in New Issue
Block a user