Draw an image of a house with a chimney using WebGL in a single HTML file. Make the canvas take the full page.
Draw an image of a house with a chimney using WebGL in a single HTML file. Make the canvas take the full page.
To create a simple 3D house with a chimney using WebGL in a single HTML file, you can use the following code. This code will create a canvas that takes up the full page and renders a basic house with a chimney.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>WebGL House with Chimney</title> <style> body, html { margin: 0; padding: 0; overflow: hidden; height: 100%; } canvas { display: block; width: 100%; height: 100%; } </style> </head> <body> <canvas id="webgl-canvas"></canvas> <script> const canvas = document.getElementById('webgl-canvas'); const gl = canvas.getContext('webgl'); if (!gl) { console.error('WebGL not supported, falling back on experimental-webgl'); gl = canvas.getContext('experimental-webgl'); } if (!gl) { alert('Your browser does not support WebGL'); } // Vertex shader program const vsSource = ` attribute vec4 aVertexPosition; uniform mat4 uModelViewMatrix; uniform mat4 uProjectionMatrix; void main(void) { gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition; } `; // Fragment shader program const fsSource = ` void main(void) { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); // White color } `; // Initialize a shader program, so WebGL knows how to draw our data const shaderProgram = initShaderProgram(gl, vsSource, fsSource); // Collect all the info needed to use the shader program. const programInfo = { program: shaderProgram, attribLocations: { vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'), }, uniformLocations: { projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'), modelViewMatrix: gl.getUniformLocation(shaderProgram, 'uModelViewMatrix'), }, }; // Here's where we call the routine that builds all the objects we'll be drawing. const buffers = initBuffers(gl); // Draw the scene repeatedly function render() { drawScene(gl, programInfo, buffers); requestAnimationFrame(render); } requestAnimationFrame(render); // Initialize the buffers we'll need. For this demo, we just have one object -- a simple house. function initBuffers(gl) { // Create a buffer for the house's positions. const positionBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); // Define the positions for the house and chimney. const positions = [ // House base -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0, // Roof -1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, // Chimney 0.5, 1.0, 0.0, 0.8, 1.0, 0.0, 0.8, 1.5, 0.0, 0.5, 1.5, 0.0, ]; // Pass the positions into WebGL to build the shape. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); return { position: positionBuffer, }; } // Draw the scene. function drawScene(gl, programInfo, buffers) { // Clear the canvas before we start drawing on it. gl.clearColor(0.0, 0.0, 0.0, 1.0); // Clear to black, fully opaque gl.clearDepth(1.0); // Clear everything gl.enable(gl.DEPTH_TEST); // Enable depth testing gl.depthFunc(gl.LEQUAL); // Near things obscure far things // Clear the canvas before we start drawing on it. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // Create a perspective matrix, a special matrix that is used to simulate the distortion of perspective in a camera. const fieldOfView = 45 * Math.PI / 180; // in radians const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight; const zNear = 0.1; const zFar = 100.0; const projectionMatrix = mat4.create(); mat4.perspective(projectionMatrix, fieldOfView, aspect, zNear, zFar); // Set the drawing position to the "identity" point, which is the center of the scene. const modelViewMatrix = mat4.create(); // Now move the drawing position a bit to where we want to start drawing the house. mat4.translate(modelViewMatrix, // destination matrix modelViewMatrix, // matrix to translate [-0.0, 0.0, -6.0]); // amount to translate // Tell WebGL how to pull out the positions from the position buffer into the vertexPosition attribute. { const numComponents = 3; const type = gl.FLOAT; const normalize = false; const stride = 0; const offset = 0; gl.bindBuffer(gl.ARRAY_BUFFER, buffers.position); gl.vertexAttribPointer( programInfo.attribLocations.vertexPosition, numComponents, type, normalize, stride, offset); gl.enableVertexAttribArray( programInfo.attribLocations.vertexPosition); } // Tell WebGL to use our program when drawing gl.useProgram(programInfo.program); // Set the shader uniforms gl.uniformMatrix4fv( programInfo.uniformLocations.projectionMatrix, false, projectionMatrix); gl.uniformMatrix4fv( programInfo.uniformLocations.modelViewMatrix, false, modelViewMatrix); { const offset = 0; const vertexCount = 4; gl.drawArrays(gl.TRIANGLE_FAN, offset, vertexCount); // Draw house base } { const offset = 4; const vertexCount = 3; gl.drawArrays(gl.TRIANGLES, offset, vertexCount); // Draw roof } { const offset = 7; const vertexCount = 4; gl.drawArrays(gl.TRIANGLE_FAN, offset, vertexCount); // Draw chimney } } // Initialize a shader program, so WebGL knows how to draw our data function initShaderProgram(gl, vsSource, fsSource) { const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource); const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource); // Create the shader program const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); // If creating the shader program failed, alert if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { console.error('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram)); return null; } return shaderProgram; } // Creates a shader of the given type, uploads the source and compiles it. function loadShader(gl, type, source) { const shader = gl.createShader(type); // Send the source to the shader object gl.shaderSource(shader, source); // Compile the shader program gl.compileShader(shader); // See if it compiled successfully if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } </script> </body> </html> |
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>WebGL House with Chimney</title> <style> body, html { margin: 0; padding: 0; overflow: hidden; height: 100%; } canvas { display: block; width: 100%; height: 100%; } </style> </head> <body> <canvas id="webgl-canvas"></canvas> <script> const canvas = document.getElementById('webgl-canvas'); const gl = canvas.getContext('webgl'); if (!gl) { console.error('WebGL not supported, falling back on experimental-webgl'); gl = canvas.getContext('experimental-webgl'); } if (!gl) { alert('Your browser does not support WebGL'); } // Vertex shader program const vsSource = ` attribute vec4 aVertexPosition; uniform mat4 uModelViewMatrix; uniform mat4 uProjectionMatrix; void main(void) { gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition; } `; // Fragment shader program const fsSource = ` void main(void) { gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); // White color } `; // Initialize a shader program, so WebGL knows how to draw our data const shaderProgram = initShaderProgram(gl, vsSource, fsSource); // Collect all the info needed to use the shader program. const programInfo = { program: shaderProgram, attribLocations: { vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'), }, uniformLocations: { projectionMatrix: gl.getUniformLocation(shaderProgram, 'uProjectionMatrix'), modelViewMatrix: gl.getUniformLocation(shaderProgram, 'uModelViewMatrix'), }, }; // Here's where we call the routine that builds all the objects we'll be drawing. const buffers = initBuffers(gl); // Draw the scene repeatedly function render() { drawScene(gl, programInfo, buffers); requestAnimationFrame(render); } requestAnimationFrame(render); // Initialize the buffers we'll need. For this demo, we just have one object -- a simple house. function initBuffers(gl) { // Create a buffer for the house's positions. const positionBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); // Define the positions for the house and chimney. const positions = [ // House base -1.0, -1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0, -1.0, 1.0, 0.0, // Roof -1.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 2.0, 0.0, // Chimney 0.5, 1.0, 0.0, 0.8, 1.0, 0.0, 0.8, 1.5, 0.0, 0.5, 1.5, 0.0, ]; // Pass the positions into WebGL to build the shape. gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(positions), gl.STATIC_DRAW); return { position: positionBuffer, }; } // Draw the scene. function drawScene(gl, programInfo, buffers) { // Clear the canvas before we start drawing on it. gl.clearColor(0.0, 0.0, 0.0, 1.0); // Clear to black, fully opaque gl.clearDepth(1.0); // Clear everything gl.enable(gl.DEPTH_TEST); // Enable depth testing gl.depthFunc(gl.LEQUAL); // Near things obscure far things // Clear the canvas before we start drawing on it. gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // Create a perspective matrix, a special matrix that is used to simulate the distortion of perspective in a camera. const fieldOfView = 45 * Math.PI / 180; // in radians const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight; const zNear = 0.1; const zFar = 100.0; const projectionMatrix = mat4.create(); mat4.perspective(projectionMatrix, fieldOfView, aspect, zNear, zFar); // Set the drawing position to the "identity" point, which is the center of the scene. const modelViewMatrix = mat4.create(); // Now move the drawing position a bit to where we want to start drawing the house. mat4.translate(modelViewMatrix, // destination matrix modelViewMatrix, // matrix to translate [-0.0, 0.0, -6.0]); // amount to translate // Tell WebGL how to pull out the positions from the position buffer into the vertexPosition attribute. { const numComponents = 3; const type = gl.FLOAT; const normalize = false; const stride = 0; const offset = 0; gl.bindBuffer(gl.ARRAY_BUFFER, buffers.position); gl.vertexAttribPointer( programInfo.attribLocations.vertexPosition, numComponents, type, normalize, stride, offset); gl.enableVertexAttribArray( programInfo.attribLocations.vertexPosition); } // Tell WebGL to use our program when drawing gl.useProgram(programInfo.program); // Set the shader uniforms gl.uniformMatrix4fv( programInfo.uniformLocations.projectionMatrix, false, projectionMatrix); gl.uniformMatrix4fv( programInfo.uniformLocations.modelViewMatrix, false, modelViewMatrix); { const offset = 0; const vertexCount = 4; gl.drawArrays(gl.TRIANGLE_FAN, offset, vertexCount); // Draw house base } { const offset = 4; const vertexCount = 3; gl.drawArrays(gl.TRIANGLES, offset, vertexCount); // Draw roof } { const offset = 7; const vertexCount = 4; gl.drawArrays(gl.TRIANGLE_FAN, offset, vertexCount); // Draw chimney } } // Initialize a shader program, so WebGL knows how to draw our data function initShaderProgram(gl, vsSource, fsSource) { const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource); const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource); // Create the shader program const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); // If creating the shader program failed, alert if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { console.error('Unable to initialize the shader program: ' + gl.getProgramInfoLog(shaderProgram)); return null; } return shaderProgram; } // Creates a shader of the given type, uploads the source and compiles it. function loadShader(gl, type, source) { const shader = gl.createShader(type); // Send the source to the shader object gl.shaderSource(shader, source); // Compile the shader program gl.compileShader(shader); // See if it compiled successfully if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { console.error('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } </script> </body> </html> |
Rendering the webpage gives the following image:
This is a screenshot of a webpage that draws something with webgl. I'm trying to see if it drew what I wanted. Describe what is drawn in this image?
The image is completely black, so it seems that nothing is visibly drawn. If you expected something specific to appear, you might want to check the WebGL code or ensure that the rendering context is set up correctly.
Does this answer say that there is a picture of a house with a chimney "The image is completely black, so it seems that nothing is visibly drawn. If you expected something specific to appear, you might want to check the WebGL code or ensure that the rendering context is set up correctly."? Answer "Yes it does" or "No it does not".
No it does not.
Testing if the previous output contains the string es it does
: False
Testing if the previous output contains the string THREE.
: False