|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +import Image from 'image-raub'; |
| 4 | +import glfw from 'glfw-raub'; |
| 5 | +const { Document } = glfw; |
| 6 | + |
| 7 | +import gl from '../index.js'; |
| 8 | +import { mat4 } from './utils/glMatrix-0.9.5.min.js'; |
| 9 | +import { alignedShaders } from './utils/presets.mjs'; |
| 10 | +import { buildShader } from './utils/build-shader.mjs'; |
| 11 | + |
| 12 | + |
| 13 | +const vertices = [ |
| 14 | + // Front face |
| 15 | + -1.0, -1.0, 0.5, |
| 16 | + 1.0, -1.0, 0.5, |
| 17 | + 1.0, 1.0, 0.5, |
| 18 | + -1.0, 1.0, 0.5, |
| 19 | +]; |
| 20 | + |
| 21 | +const textureCoords = [ |
| 22 | + // Front face |
| 23 | + 0.0, 0.0, |
| 24 | + 1.0, 0.0, |
| 25 | + 1.0, 1.0, |
| 26 | + 0.0, 1.0, |
| 27 | +]; |
| 28 | + |
| 29 | +const cubeVertexIndices = [ |
| 30 | + 0, 1, 2, 0, 2, 3, // Front face |
| 31 | +]; |
| 32 | + |
| 33 | +Document.setWebgl(gl); |
| 34 | +const document = new Document({ |
| 35 | + vsync: true, autoEsc: true, autoFullscreen: true, title: 'Aligned', |
| 36 | +}); |
| 37 | + |
| 38 | +const shaderProgram = buildShader(alignedShaders); |
| 39 | +gl.useProgram(shaderProgram); |
| 40 | + |
| 41 | +shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, 'aVertexPosition'); |
| 42 | +gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute); |
| 43 | + |
| 44 | +shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, 'aTextureCoord'); |
| 45 | +gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute); |
| 46 | + |
| 47 | +shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, 'uPMatrix'); |
| 48 | +shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, 'uMVMatrix'); |
| 49 | +shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, 'uSampler'); |
| 50 | + |
| 51 | +const texLena = gl.createTexture(); |
| 52 | +texLena.image = new Image(); |
| 53 | +texLena.image.onload = () => { |
| 54 | + console.log( |
| 55 | + `Loaded image: ${texLena.image.src}`, |
| 56 | + `${texLena.image.width}x${texLena.image.height}`, |
| 57 | + ); |
| 58 | + gl.bindTexture(gl.TEXTURE_2D, texLena); |
| 59 | + gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true); |
| 60 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, texLena.image); |
| 61 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 62 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 63 | + gl.bindTexture(gl.TEXTURE_2D, null); |
| 64 | +}; |
| 65 | +texLena.image.src = 'img/glass.gif'; |
| 66 | + |
| 67 | + |
| 68 | +const mvMatrix = mat4.create(); |
| 69 | +const pMatrix = mat4.create(); |
| 70 | + |
| 71 | + |
| 72 | +const setMatrixUniforms = () => { |
| 73 | + gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix); |
| 74 | + gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix); |
| 75 | +}; |
| 76 | + |
| 77 | +const cubeVertexPositionBuffer = gl.createBuffer(); |
| 78 | +gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer); |
| 79 | +gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW); |
| 80 | +cubeVertexPositionBuffer.itemSize = 3; |
| 81 | + |
| 82 | +const cubeVertexTextureCoordBuffer = gl.createBuffer(); |
| 83 | +gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer); |
| 84 | +gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW); |
| 85 | +cubeVertexTextureCoordBuffer.itemSize = 2; |
| 86 | + |
| 87 | +const cubeVertexIndexBuffer = gl.createBuffer(); |
| 88 | +gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer); |
| 89 | +gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW); |
| 90 | +cubeVertexIndexBuffer.numItems = 6; |
| 91 | + |
| 92 | + |
| 93 | +const drawScene = () => { |
| 94 | + gl.clearColor(0.05, 0.1, 0.05, 1.0); |
| 95 | + gl.enable(gl.DEPTH_TEST); |
| 96 | + |
| 97 | + gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); |
| 98 | + gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 99 | + |
| 100 | + gl.useProgram(shaderProgram); |
| 101 | + gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute); |
| 102 | + gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute); |
| 103 | + |
| 104 | + mat4.ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0, pMatrix); |
| 105 | + |
| 106 | + mat4.identity(mvMatrix); |
| 107 | + |
| 108 | + gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer); |
| 109 | + gl.vertexAttribPointer( |
| 110 | + shaderProgram.vertexPositionAttribute, cubeVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0, |
| 111 | + ); |
| 112 | + |
| 113 | + gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer); |
| 114 | + gl.vertexAttribPointer( |
| 115 | + shaderProgram.textureCoordAttribute, cubeVertexTextureCoordBuffer.itemSize, gl.FLOAT, false, 0, 0, |
| 116 | + ); |
| 117 | + |
| 118 | + gl.activeTexture(gl.TEXTURE0); |
| 119 | + gl.bindTexture(gl.TEXTURE_2D, texLena); |
| 120 | + gl.uniform1i(shaderProgram.samplerUniform, 0); |
| 121 | + |
| 122 | + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer); |
| 123 | + setMatrixUniforms(); |
| 124 | + gl.drawElements(gl.TRIANGLES, cubeVertexIndexBuffer.numItems, gl.UNSIGNED_SHORT, 0); |
| 125 | + |
| 126 | + // Cleanup GL state |
| 127 | + gl.bindTexture(gl.TEXTURE_2D, null); |
| 128 | + gl.bindBuffer(gl.ARRAY_BUFFER, null); |
| 129 | + gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null); |
| 130 | + gl.useProgram(null); |
| 131 | +}; |
| 132 | + |
| 133 | + |
| 134 | +const tick = () => { |
| 135 | + drawScene(); |
| 136 | + |
| 137 | + document.requestAnimationFrame(tick); |
| 138 | +}; |
| 139 | + |
| 140 | +document.requestAnimationFrame(tick); |
0 commit comments