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.
Below is an example of how you can draw a simple house with a chimney using WebGL in a single HTML file. This example uses the WebGL API to create a canvas that takes up the full page and renders a simple 3D scene.
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>House with Chimney</title> <style> body { margin: 0; background-color: #f0f0f0; } #canvas { width: 100%; height: 100vh; display: block; } </style> </head> <body> <canvas id="canvas"></canvas> <script> // Get the canvas element var canvas = document.getElementById('canvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; // Get the WebGL context var gl = canvas.getContext('webgl'); // Define the vertices for the house var houseVertices = new Float32Array([ // Front face -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, // Back face -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, // Left face -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, // Right face 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, // Top face -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, // Bottom face -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, ]); // Define the indices for the house var houseIndices = new Uint16Array([ // Front face 0, 1, 2, 0, 2, 3, // Back face 4, 5, 6, 4, 6, 7, // Left face 8, 9, 10, 8, 10, 11, // Right face 12, 13, 14, 12, 14, 15, // Top face 16, 17, 18, 16, 18, 19, // Bottom face 20, 21, 22, 20, 22, 23, ]); // Define the vertices for the chimney var chimneyVertices = new Float32Array([ // Front face -0.1, 0.5, 0.1, 0.1, 0.5, 0.1, 0.1, 1.0, 0.1, -0.1, 1.0, 0.1, // Back face -0.1, 0.5, -0.1, 0.1, 0.5, -0.1, 0.1, 1.0, -0.1, -0.1, 1.0, -0.1, // Left face -0.1, 0.5, -0.1, -0.1, 0.5, 0.1, -0.1, 1.0, 0.1, -0.1, 1.0, -0.1, // Right face 0.1, 0.5, -0.1, 0.1, 0.5, 0.1, 0.1, 1.0, 0.1, 0.1, 1.0, -0.1, // Top face -0.1, 1.0, -0.1, -0.1, 1.0, 0.1, 0.1, 1.0, 0.1, 0.1, 1.0, -0.1, // Bottom face -0.1, 0.5, -0.1, -0.1, 0.5, 0.1, 0.1, 0.5, 0.1, 0.1, 0.5, -0.1, ]); // Define the indices for the chimney var chimneyIndices = new Uint16Array([ // Front face 0, 1, 2, 0, 2, 3, // Back face 4, 5, 6, 4, 6, 7, // Left face 8, 9, 10, 8, 10, 11, // Right face 12, 13, 14, 12, 14, 15, // Top face 16, 17, 18, 16, 18, 19, // Bottom face 20, 21, 22, 20, 22, 23, ]); // Create a vertex buffer for the house var houseVertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, houseVertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, houseVertices, gl.STATIC_DRAW); // Create an index buffer for the house var houseIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, houseIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, houseIndices, gl.STATIC_DRAW); // Create a vertex buffer for the chimney var chimneyVertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, chimneyVertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, chimneyVertices, gl.STATIC_DRAW); // Create an index buffer for the chimney var chimneyIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, chimneyIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, chimneyIndices, gl.STATIC_DRAW); // Create a vertex shader var vertexShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vertexShader, ` attribute vec3 position; uniform mat4 transformation; void main() { gl_Position = transformation * vec4(position, 1.0); } `); gl.compileShader(vertexShader); // Create a fragment shader var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fragmentShader, ` void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } `); gl.compileShader(fragmentShader); // Create a program var program = gl.createProgram(); gl.attachShader(program, vertexShader); gl.attachShader(program, fragmentShader); gl.linkProgram(program); gl.useProgram(program); // Get the location of the position attribute var positionLocation = gl.getAttribLocation(program, 'position'); // Get the location of the transformation uniform var transformationLocation = gl.getUniformLocation(program, 'transformation'); // Set up the perspective var perspective = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, -0.2, 0.0, 0.0, -1.0, 0.0 ]; // Set up the view var view = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ]; // Set up the model var model = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -5.0, 1.0 ]; // Set up the transformation var transformation = [ perspective[0], perspective[1], perspective[2], perspective[3], perspective[4], perspective[5], perspective[6], perspective[7], perspective[8], perspective[9], perspective[10], perspective[11], perspective[12], perspective[13], perspective[14], perspective[15] ]; // Set the transformation uniform gl.uniformMatrix4fv(transformationLocation, false, transformation); // Set the position attribute gl.enableVertexAttribArray(positionLocation); // Clear the canvas gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // Draw the house gl.bindBuffer(gl.ARRAY_BUFFER, houseVertexBuffer); gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, houseIndexBuffer); gl.drawElements(gl.TRIANGLES, houseIndices.length, gl.UNSIGNED_SHORT, 0); // Draw the chimney gl.bindBuffer(gl.ARRAY_BUFFER, chimneyVertexBuffer); gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, chimneyIndexBuffer); gl.drawElements(gl.TRIANGLES, chimneyIndices.length, gl.UNSIGNED_SHORT, 0); </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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>House with Chimney</title> <style> body { margin: 0; background-color: #f0f0f0; } #canvas { width: 100%; height: 100vh; display: block; } </style> </head> <body> <canvas id="canvas"></canvas> <script> // Get the canvas element var canvas = document.getElementById('canvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; // Get the WebGL context var gl = canvas.getContext('webgl'); // Define the vertices for the house var houseVertices = new Float32Array([ // Front face -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, 0.5, // Back face -0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, // Left face -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5, -0.5, // Right face 0.5, -0.5, -0.5, 0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, // Top face -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, // Bottom face -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, ]); // Define the indices for the house var houseIndices = new Uint16Array([ // Front face 0, 1, 2, 0, 2, 3, // Back face 4, 5, 6, 4, 6, 7, // Left face 8, 9, 10, 8, 10, 11, // Right face 12, 13, 14, 12, 14, 15, // Top face 16, 17, 18, 16, 18, 19, // Bottom face 20, 21, 22, 20, 22, 23, ]); // Define the vertices for the chimney var chimneyVertices = new Float32Array([ // Front face -0.1, 0.5, 0.1, 0.1, 0.5, 0.1, 0.1, 1.0, 0.1, -0.1, 1.0, 0.1, // Back face -0.1, 0.5, -0.1, 0.1, 0.5, -0.1, 0.1, 1.0, -0.1, -0.1, 1.0, -0.1, // Left face -0.1, 0.5, -0.1, -0.1, 0.5, 0.1, -0.1, 1.0, 0.1, -0.1, 1.0, -0.1, // Right face 0.1, 0.5, -0.1, 0.1, 0.5, 0.1, 0.1, 1.0, 0.1, 0.1, 1.0, -0.1, // Top face -0.1, 1.0, -0.1, -0.1, 1.0, 0.1, 0.1, 1.0, 0.1, 0.1, 1.0, -0.1, // Bottom face -0.1, 0.5, -0.1, -0.1, 0.5, 0.1, 0.1, 0.5, 0.1, 0.1, 0.5, -0.1, ]); // Define the indices for the chimney var chimneyIndices = new Uint16Array([ // Front face 0, 1, 2, 0, 2, 3, // Back face 4, 5, 6, 4, 6, 7, // Left face 8, 9, 10, 8, 10, 11, // Right face 12, 13, 14, 12, 14, 15, // Top face 16, 17, 18, 16, 18, 19, // Bottom face 20, 21, 22, 20, 22, 23, ]); // Create a vertex buffer for the house var houseVertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, houseVertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, houseVertices, gl.STATIC_DRAW); // Create an index buffer for the house var houseIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, houseIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, houseIndices, gl.STATIC_DRAW); // Create a vertex buffer for the chimney var chimneyVertexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, chimneyVertexBuffer); gl.bufferData(gl.ARRAY_BUFFER, chimneyVertices, gl.STATIC_DRAW); // Create an index buffer for the chimney var chimneyIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, chimneyIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, chimneyIndices, gl.STATIC_DRAW); // Create a vertex shader var vertexShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vertexShader, ` attribute vec3 position; uniform mat4 transformation; void main() { gl_Position = transformation * vec4(position, 1.0); } `); gl.compileShader(vertexShader); // Create a fragment shader var fragmentShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fragmentShader, ` void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } `); gl.compileShader(fragmentShader); // Create a program var program = gl.createProgram(); gl.attachShader(program, vertexShader); gl.attachShader(program, fragmentShader); gl.linkProgram(program); gl.useProgram(program); // Get the location of the position attribute var positionLocation = gl.getAttribLocation(program, 'position'); // Get the location of the transformation uniform var transformationLocation = gl.getUniformLocation(program, 'transformation'); // Set up the perspective var perspective = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, -0.2, 0.0, 0.0, -1.0, 0.0 ]; // Set up the view var view = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 ]; // Set up the model var model = [ 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -5.0, 1.0 ]; // Set up the transformation var transformation = [ perspective[0], perspective[1], perspective[2], perspective[3], perspective[4], perspective[5], perspective[6], perspective[7], perspective[8], perspective[9], perspective[10], perspective[11], perspective[12], perspective[13], perspective[14], perspective[15] ]; // Set the transformation uniform gl.uniformMatrix4fv(transformationLocation, false, transformation); // Set the position attribute gl.enableVertexAttribArray(positionLocation); // Clear the canvas gl.clearColor(0.0, 0.0, 0.0, 1.0); gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); // Draw the house gl.bindBuffer(gl.ARRAY_BUFFER, houseVertexBuffer); gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, houseIndexBuffer); gl.drawElements(gl.TRIANGLES, houseIndices.length, gl.UNSIGNED_SHORT, 0); // Draw the chimney gl.bindBuffer(gl.ARRAY_BUFFER, chimneyVertexBuffer); gl.vertexAttribPointer(positionLocation, 3, gl.FLOAT, false, 0, 0); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, chimneyIndexBuffer); gl.drawElements(gl.TRIANGLES, chimneyIndices.length, gl.UNSIGNED_SHORT, 0); </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 to be rendered, there might be an issue with the WebGL code or the rendering context.
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 to be rendered, there might be an issue with the WebGL code or the rendering context."? 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