refacto : split vertex attrib configuration
This commit is contained in:
parent
94251a4806
commit
3ab1c7b1ca
|
@ -13,6 +13,7 @@ import {initShaderProgram} from './shaders';
|
||||||
import {changeFragmentShader} from './changeshader';
|
import {changeFragmentShader} from './changeshader';
|
||||||
import {loadTexture} from './texture';
|
import {loadTexture} from './texture';
|
||||||
import {initMatrices} from './matrix';
|
import {initMatrices} from './matrix';
|
||||||
|
import {initVertexAttribs} from './vertexattrib';
|
||||||
|
|
||||||
main();
|
main();
|
||||||
|
|
||||||
|
@ -338,57 +339,7 @@ function drawScene(gl: any,
|
||||||
gl.depthFunc(gl.LEQUAL);
|
gl.depthFunc(gl.LEQUAL);
|
||||||
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
initVertexAttribs(gl, programInfo, buffers);
|
||||||
{
|
|
||||||
const numComponents = 3;
|
|
||||||
const type = gl.FLOAT;
|
|
||||||
const normalize = false;
|
|
||||||
const stride = 0;
|
|
||||||
const offset = 0;
|
|
||||||
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.positions);
|
|
||||||
gl.vertexAttribPointer(
|
|
||||||
programInfo.attribLocations.vertexPosition,
|
|
||||||
numComponents,
|
|
||||||
type,
|
|
||||||
normalize,
|
|
||||||
stride,
|
|
||||||
offset);
|
|
||||||
gl.enableVertexAttribArray(
|
|
||||||
programInfo.attribLocations.vertexPosition);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
const numComponents = 3;
|
|
||||||
const type = gl.FLOAT;
|
|
||||||
const normalize = false;
|
|
||||||
const stride = 0;
|
|
||||||
const offset = 0;
|
|
||||||
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.normals);
|
|
||||||
gl.vertexAttribPointer(
|
|
||||||
programInfo.attribLocations.vertexNormal,
|
|
||||||
numComponents,
|
|
||||||
type,
|
|
||||||
normalize,
|
|
||||||
stride,
|
|
||||||
offset);
|
|
||||||
gl.enableVertexAttribArray(
|
|
||||||
programInfo.attribLocations.vertexNormal);
|
|
||||||
}
|
|
||||||
{
|
|
||||||
const numComponents = 2;
|
|
||||||
const type = gl.FLOAT;
|
|
||||||
const normalize = false;
|
|
||||||
const stride = 0;
|
|
||||||
const offset = 0;
|
|
||||||
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.uvs);
|
|
||||||
gl.vertexAttribPointer(programInfo.attribLocations.textureCoord,
|
|
||||||
numComponents,
|
|
||||||
type,
|
|
||||||
normalize,
|
|
||||||
stride,
|
|
||||||
offset);
|
|
||||||
gl.enableVertexAttribArray(programInfo.attribLocations.textureCoord);
|
|
||||||
}
|
|
||||||
|
|
||||||
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
|
||||||
gl.useProgram(programInfo.program);
|
gl.useProgram(programInfo.program);
|
||||||
gl.activeTexture(gl.TEXTURE0);
|
gl.activeTexture(gl.TEXTURE0);
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
/**
|
||||||
|
* Initialize Vertex Attributes
|
||||||
|
* @param {any} gl the WebGL content
|
||||||
|
* @param {any} programInfo WebGL program information
|
||||||
|
* @param {any} buffers the buffers to init
|
||||||
|
*/
|
||||||
|
export function initVertexAttribs(gl: any, programInfo: any, buffers: any) {
|
||||||
|
{
|
||||||
|
const numComponents = 3;
|
||||||
|
const type = gl.FLOAT;
|
||||||
|
const normalize = false;
|
||||||
|
const stride = 0;
|
||||||
|
const offset = 0;
|
||||||
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.positions);
|
||||||
|
gl.vertexAttribPointer(
|
||||||
|
programInfo.attribLocations.vertexPosition,
|
||||||
|
numComponents,
|
||||||
|
type,
|
||||||
|
normalize,
|
||||||
|
stride,
|
||||||
|
offset);
|
||||||
|
gl.enableVertexAttribArray(
|
||||||
|
programInfo.attribLocations.vertexPosition);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const numComponents = 3;
|
||||||
|
const type = gl.FLOAT;
|
||||||
|
const normalize = false;
|
||||||
|
const stride = 0;
|
||||||
|
const offset = 0;
|
||||||
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.normals);
|
||||||
|
gl.vertexAttribPointer(
|
||||||
|
programInfo.attribLocations.vertexNormal,
|
||||||
|
numComponents,
|
||||||
|
type,
|
||||||
|
normalize,
|
||||||
|
stride,
|
||||||
|
offset);
|
||||||
|
gl.enableVertexAttribArray(
|
||||||
|
programInfo.attribLocations.vertexNormal);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
const numComponents = 2;
|
||||||
|
const type = gl.FLOAT;
|
||||||
|
const normalize = false;
|
||||||
|
const stride = 0;
|
||||||
|
const offset = 0;
|
||||||
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.uvs);
|
||||||
|
gl.vertexAttribPointer(programInfo.attribLocations.textureCoord,
|
||||||
|
numComponents,
|
||||||
|
type,
|
||||||
|
normalize,
|
||||||
|
stride,
|
||||||
|
offset);
|
||||||
|
gl.enableVertexAttribArray(programInfo.attribLocations.textureCoord);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue