refacto : separated draw function
This commit is contained in:
		
							parent
							
								
									c4f3c75154
								
							
						
					
					
						commit
						828872215b
					
				| 
						 | 
					@ -0,0 +1,59 @@
 | 
				
			||||||
 | 
					import {initMatrices} from './matrix';
 | 
				
			||||||
 | 
					import {initVertexAttribs} from './vertexattrib';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Draws a webgl scene
 | 
				
			||||||
 | 
					 * @param {any} gl the WebGL context
 | 
				
			||||||
 | 
					 * @param {any} programInfo WebGL program information
 | 
				
			||||||
 | 
					 * @param {any} buffers the buffers to draw
 | 
				
			||||||
 | 
					 * @param {number} params various parameterss
 | 
				
			||||||
 | 
					 * @param {any} texture the texture to load
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					export function drawScene(gl: any,
 | 
				
			||||||
 | 
						programInfo: any,
 | 
				
			||||||
 | 
						buffers: any,
 | 
				
			||||||
 | 
						params: any,
 | 
				
			||||||
 | 
						texture: any) {
 | 
				
			||||||
 | 
						gl.clearColor(0.0, 0.0, 0.0, 1.0);
 | 
				
			||||||
 | 
						gl.clearDepth(1.0);
 | 
				
			||||||
 | 
						gl.enable(gl.DEPTH_TEST);
 | 
				
			||||||
 | 
						gl.depthFunc(gl.LEQUAL);
 | 
				
			||||||
 | 
						gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						initVertexAttribs(gl, programInfo, buffers);
 | 
				
			||||||
 | 
						gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
 | 
				
			||||||
 | 
						gl.useProgram(programInfo.program);
 | 
				
			||||||
 | 
						gl.activeTexture(gl.TEXTURE0);
 | 
				
			||||||
 | 
						gl.bindTexture(gl.TEXTURE_2D, texture);
 | 
				
			||||||
 | 
						gl.uniform1i(programInfo.uniformLocations.uSampler, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						const [
 | 
				
			||||||
 | 
							projectionMatrix,
 | 
				
			||||||
 | 
							viewMatrix,
 | 
				
			||||||
 | 
							modelMatrix,
 | 
				
			||||||
 | 
							normalModelMatrix,
 | 
				
			||||||
 | 
						] = initMatrices(gl, params);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (let i = 0; i < params.instanceNumber; i++) {
 | 
				
			||||||
 | 
							gl.uniformMatrix4fv(
 | 
				
			||||||
 | 
								programInfo.uniformLocations.projectionMatrix,
 | 
				
			||||||
 | 
								false,
 | 
				
			||||||
 | 
								projectionMatrix);
 | 
				
			||||||
 | 
							gl.uniformMatrix4fv(
 | 
				
			||||||
 | 
								programInfo.uniformLocations.viewMatrix,
 | 
				
			||||||
 | 
								false,
 | 
				
			||||||
 | 
								viewMatrix);
 | 
				
			||||||
 | 
							gl.uniformMatrix4fv(
 | 
				
			||||||
 | 
								programInfo.uniformLocations.modelMatrix,
 | 
				
			||||||
 | 
								false,
 | 
				
			||||||
 | 
								modelMatrix[i]);
 | 
				
			||||||
 | 
							gl.uniformMatrix4fv(
 | 
				
			||||||
 | 
								programInfo.uniformLocations.normalModelMatrix,
 | 
				
			||||||
 | 
								false,
 | 
				
			||||||
 | 
								normalModelMatrix[i]);
 | 
				
			||||||
 | 
							const vertexCount = params.length;
 | 
				
			||||||
 | 
							const type = gl.UNSIGNED_SHORT;
 | 
				
			||||||
 | 
							const offset = 0;
 | 
				
			||||||
 | 
							gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -6,8 +6,7 @@ import fsSource4 from './shaders/texture.frag';
 | 
				
			||||||
import {fetchObj, updateObj} from './objutils';
 | 
					import {fetchObj, updateObj} from './objutils';
 | 
				
			||||||
import {initShaderProgram} from './shaders';
 | 
					import {initShaderProgram} from './shaders';
 | 
				
			||||||
import {loadTexture} from './texture';
 | 
					import {loadTexture} from './texture';
 | 
				
			||||||
import {initMatrices} from './matrix';
 | 
					import {drawScene} from './draw';
 | 
				
			||||||
import {initVertexAttribs} from './vertexattrib';
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import {uiUpdateParams,
 | 
					import {uiUpdateParams,
 | 
				
			||||||
	uiUpdateTexture,
 | 
						uiUpdateTexture,
 | 
				
			||||||
| 
						 | 
					@ -121,60 +120,3 @@ async function main() {
 | 
				
			||||||
	uiUpdateShader(context);
 | 
						uiUpdateShader(context);
 | 
				
			||||||
	requestAnimationFrame(render);
 | 
						requestAnimationFrame(render);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Draw a webgl scene
 | 
					 | 
				
			||||||
 * @param {any} gl the WebGL context
 | 
					 | 
				
			||||||
 * @param {any} programInfo WebGL program information
 | 
					 | 
				
			||||||
 * @param {any} buffers the buffers to draw
 | 
					 | 
				
			||||||
 * @param {number} params various parameterss
 | 
					 | 
				
			||||||
 * @param {any} texture the texture to load
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
function drawScene(gl: any,
 | 
					 | 
				
			||||||
	programInfo: any,
 | 
					 | 
				
			||||||
	buffers: any,
 | 
					 | 
				
			||||||
	params: any,
 | 
					 | 
				
			||||||
	texture: any) {
 | 
					 | 
				
			||||||
	gl.clearColor(0.0, 0.0, 0.0, 1.0);
 | 
					 | 
				
			||||||
	gl.clearDepth(1.0);
 | 
					 | 
				
			||||||
	gl.enable(gl.DEPTH_TEST);
 | 
					 | 
				
			||||||
	gl.depthFunc(gl.LEQUAL);
 | 
					 | 
				
			||||||
	gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	initVertexAttribs(gl, programInfo, buffers);
 | 
					 | 
				
			||||||
	gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
 | 
					 | 
				
			||||||
	gl.useProgram(programInfo.program);
 | 
					 | 
				
			||||||
	gl.activeTexture(gl.TEXTURE0);
 | 
					 | 
				
			||||||
	gl.bindTexture(gl.TEXTURE_2D, texture);
 | 
					 | 
				
			||||||
	gl.uniform1i(programInfo.uniformLocations.uSampler, 0);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	const [
 | 
					 | 
				
			||||||
		projectionMatrix,
 | 
					 | 
				
			||||||
		viewMatrix,
 | 
					 | 
				
			||||||
		modelMatrix,
 | 
					 | 
				
			||||||
		normalModelMatrix,
 | 
					 | 
				
			||||||
	] = initMatrices(gl, params);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	for (let i = 0; i < params.instanceNumber; i++) {
 | 
					 | 
				
			||||||
		gl.uniformMatrix4fv(
 | 
					 | 
				
			||||||
			programInfo.uniformLocations.projectionMatrix,
 | 
					 | 
				
			||||||
			false,
 | 
					 | 
				
			||||||
			projectionMatrix);
 | 
					 | 
				
			||||||
		gl.uniformMatrix4fv(
 | 
					 | 
				
			||||||
			programInfo.uniformLocations.viewMatrix,
 | 
					 | 
				
			||||||
			false,
 | 
					 | 
				
			||||||
			viewMatrix);
 | 
					 | 
				
			||||||
		gl.uniformMatrix4fv(
 | 
					 | 
				
			||||||
			programInfo.uniformLocations.modelMatrix,
 | 
					 | 
				
			||||||
			false,
 | 
					 | 
				
			||||||
			modelMatrix[i]);
 | 
					 | 
				
			||||||
		gl.uniformMatrix4fv(
 | 
					 | 
				
			||||||
			programInfo.uniformLocations.normalModelMatrix,
 | 
					 | 
				
			||||||
			false,
 | 
					 | 
				
			||||||
			normalModelMatrix[i]);
 | 
					 | 
				
			||||||
		const vertexCount = params.length;
 | 
					 | 
				
			||||||
		const type = gl.UNSIGNED_SHORT;
 | 
					 | 
				
			||||||
		const offset = 0;
 | 
					 | 
				
			||||||
		gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue