diff --git a/src/client/main.ts b/src/client/main.ts index 526d24b..9e709a2 100644 --- a/src/client/main.ts +++ b/src/client/main.ts @@ -13,6 +13,7 @@ import {initShaderProgram} from './shaders'; import {changeFragmentShader} from './changeshader'; import {loadTexture} from './texture'; import {initMatrices} from './matrix'; +import {initVertexAttribs} from './vertexattrib'; main(); @@ -338,57 +339,7 @@ function drawScene(gl: any, gl.depthFunc(gl.LEQUAL); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); - - { - 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); - } - + initVertexAttribs(gl, programInfo, buffers); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices); gl.useProgram(programInfo.program); gl.activeTexture(gl.TEXTURE0); diff --git a/src/client/vertexattrib.ts b/src/client/vertexattrib.ts new file mode 100644 index 0000000..25403ca --- /dev/null +++ b/src/client/vertexattrib.ts @@ -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); + } +}