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.
Certainly! Below is a complete HTML file that uses WebGL to draw a simple house with a chimney. The canvas is styled to take up the full page. To see the result, simply save the code below in a file with a `.html` extension (e.g., `house.html`) and open it in your web browser.
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 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL House with Chimney</title> <style> /* Ensure the canvas takes up the full page */ body, html { margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; background-color: #87CEEB; /* Sky blue background */ } canvas { display: block; /* Removes scrollbars */ width: 100%; height: 100%; } </style> </head> <body> <canvas id="glcanvas"></canvas> <script> // Get the canvas and initialize WebGL context const canvas = document.getElementById('glcanvas'); const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl'); if (!gl) { alert('Unable to initialize WebGL. Your browser may not support it.'); } // Adjust canvas size function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); } window.addEventListener('resize', resizeCanvas); resizeCanvas(); // Vertex shader program const vsSource = ` attribute vec2 aVertexPosition; attribute vec3 aVertexColor; varying lowp vec3 vColor; uniform float uScale; void main(void) { gl_Position = vec4(aVertexPosition * uScale, 0.0, 1.0); vColor = aVertexColor; } `; // Fragment shader program const fsSource = ` varying lowp vec3 vColor; void main(void) { gl_FragColor = vec4(vColor, 1.0); } `; // Initialize a shader program function initShaderProgram(gl, vsSource, fsSource) { const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource); const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource); const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { alert('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); gl.shaderSource(shader, source); gl.compileShader(shader); if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } const shaderProgram = initShaderProgram(gl, vsSource, fsSource); const programInfo = { program: shaderProgram, attribLocations: { vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'), vertexColor: gl.getAttribLocation(shaderProgram, 'aVertexColor'), }, uniformLocations: { scale: gl.getUniformLocation(shaderProgram, 'uScale'), }, }; // Define the geometry and color data for the house const house = { // Main body of the house (a square) vertices: [ // x, y -0.5, -0.5, 0.5, -0.5, 0.5, 0.0, -0.5, 0.0, // Roof (a triangle) -0.6, 0.0, 0.6, 0.0, 0.0, 0.5, // Chimney (a small rectangle) 0.3, 0.3, 0.5, 0.3, 0.5, 0.4, 0.3, 0.4, ], // Colors for each vertex colors: [ // Main body - brown 0.55, 0.27, 0.07, 0.55, 0.27, 0.07, 0.55, 0.27, 0.07, 0.55, 0.27, 0.07, // Roof - red 0.8, 0.0, 0.0, 0.8, 0.0, 0.0, 0.8, 0.0, 0.0, // Chimney - dark gray 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, ], // Indices for the elements to draw indices: [ // Main body 0, 1, 2, 0, 2, 3, // Roof 4, 5, 6, // Chimney 7, 8, 9, 7, 9, 10, ], }; // Create and bind buffers const positionBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(house.vertices), gl.STATIC_DRAW); const colorBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(house.colors), gl.STATIC_DRAW); const indexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(house.indices), gl.STATIC_DRAW); function drawScene() { gl.clearColor(0.529, 0.808, 0.922, 1.0); // Sky blue background gl.clear(gl.COLOR_BUFFER_BIT); gl.useProgram(programInfo.program); // Bind position buffer gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); gl.vertexAttribPointer( programInfo.attribLocations.vertexPosition, 2, // components per iteration gl.FLOAT, // type false, // normalize 0, // stride 0 // offset ); gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition); // Bind color buffer gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); gl.vertexAttribPointer( programInfo.attribLocations.vertexColor, 3, // components per iteration gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray(programInfo.attribLocations.vertexColor); // Bind index buffer gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); // Set the scale uniform based on the canvas aspect ratio const aspect = canvas.width / canvas.height; if (aspect > 1) { gl.uniform1f(programInfo.uniformLocations.scale, 1 / aspect); } else { gl.uniform1f(programInfo.uniformLocations.scale, 1.0); } // Draw the elements gl.drawElements(gl.TRIANGLES, house.indices.length, gl.UNSIGNED_SHORT, 0); } // Render loop function render() { drawScene(); requestAnimationFrame(render); } requestAnimationFrame(render); </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 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL House with Chimney</title> <style> /* Ensure the canvas takes up the full page */ body, html { margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; background-color: #87CEEB; /* Sky blue background */ } canvas { display: block; /* Removes scrollbars */ width: 100%; height: 100%; } </style> </head> <body> <canvas id="glcanvas"></canvas> <script> // Get the canvas and initialize WebGL context const canvas = document.getElementById('glcanvas'); const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl'); if (!gl) { alert('Unable to initialize WebGL. Your browser may not support it.'); } // Adjust canvas size function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); } window.addEventListener('resize', resizeCanvas); resizeCanvas(); // Vertex shader program const vsSource = ` attribute vec2 aVertexPosition; attribute vec3 aVertexColor; varying lowp vec3 vColor; uniform float uScale; void main(void) { gl_Position = vec4(aVertexPosition * uScale, 0.0, 1.0); vColor = aVertexColor; } `; // Fragment shader program const fsSource = ` varying lowp vec3 vColor; void main(void) { gl_FragColor = vec4(vColor, 1.0); } `; // Initialize a shader program function initShaderProgram(gl, vsSource, fsSource) { const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vsSource); const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fsSource); const shaderProgram = gl.createProgram(); gl.attachShader(shaderProgram, vertexShader); gl.attachShader(shaderProgram, fragmentShader); gl.linkProgram(shaderProgram); if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) { alert('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); gl.shaderSource(shader, source); gl.compileShader(shader); if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) { alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } const shaderProgram = initShaderProgram(gl, vsSource, fsSource); const programInfo = { program: shaderProgram, attribLocations: { vertexPosition: gl.getAttribLocation(shaderProgram, 'aVertexPosition'), vertexColor: gl.getAttribLocation(shaderProgram, 'aVertexColor'), }, uniformLocations: { scale: gl.getUniformLocation(shaderProgram, 'uScale'), }, }; // Define the geometry and color data for the house const house = { // Main body of the house (a square) vertices: [ // x, y -0.5, -0.5, 0.5, -0.5, 0.5, 0.0, -0.5, 0.0, // Roof (a triangle) -0.6, 0.0, 0.6, 0.0, 0.0, 0.5, // Chimney (a small rectangle) 0.3, 0.3, 0.5, 0.3, 0.5, 0.4, 0.3, 0.4, ], // Colors for each vertex colors: [ // Main body - brown 0.55, 0.27, 0.07, 0.55, 0.27, 0.07, 0.55, 0.27, 0.07, 0.55, 0.27, 0.07, // Roof - red 0.8, 0.0, 0.0, 0.8, 0.0, 0.0, 0.8, 0.0, 0.0, // Chimney - dark gray 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, ], // Indices for the elements to draw indices: [ // Main body 0, 1, 2, 0, 2, 3, // Roof 4, 5, 6, // Chimney 7, 8, 9, 7, 9, 10, ], }; // Create and bind buffers const positionBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(house.vertices), gl.STATIC_DRAW); const colorBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(house.colors), gl.STATIC_DRAW); const indexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(house.indices), gl.STATIC_DRAW); function drawScene() { gl.clearColor(0.529, 0.808, 0.922, 1.0); // Sky blue background gl.clear(gl.COLOR_BUFFER_BIT); gl.useProgram(programInfo.program); // Bind position buffer gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer); gl.vertexAttribPointer( programInfo.attribLocations.vertexPosition, 2, // components per iteration gl.FLOAT, // type false, // normalize 0, // stride 0 // offset ); gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition); // Bind color buffer gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer); gl.vertexAttribPointer( programInfo.attribLocations.vertexColor, 3, // components per iteration gl.FLOAT, false, 0, 0 ); gl.enableVertexAttribArray(programInfo.attribLocations.vertexColor); // Bind index buffer gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer); // Set the scale uniform based on the canvas aspect ratio const aspect = canvas.width / canvas.height; if (aspect > 1) { gl.uniform1f(programInfo.uniformLocations.scale, 1 / aspect); } else { gl.uniform1f(programInfo.uniformLocations.scale, 1.0); } // Draw the elements gl.drawElements(gl.TRIANGLES, house.indices.length, gl.UNSIGNED_SHORT, 0); } // Render loop function render() { drawScene(); requestAnimationFrame(render); } requestAnimationFrame(render); </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 depicts a simple, stylized drawing of a house. The house has a brown rectangular base and a red triangular roof. There is also a small black rectangle near the top right of the roof, which could represent a chimney. The background is a solid light blue color.
Does this answer say that there is a picture of a house with a chimney "The image depicts a simple, stylized drawing of a house. The house has a brown rectangular base and a red triangular roof. There is also a small black rectangle near the top right of the roof, which could represent a chimney. The background is a solid light blue color."? Answer "Yes it does" or "No it does not".
Yes it does.
Testing if the previous output contains the string es it does
: True
Testing if the previous output contains the string THREE.
: False
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.
Sure! Below is a complete HTML file that uses WebGL to draw a simple house with a chimney. The canvas fills the entire viewport, and the house is composed of basic shapes (a rectangle for the base, a triangle for the roof, and a smaller rectangle for the chimney). You can copy the following code into a file with a `.html` extension (e.g., `house.html`) and open it in your web browser to see the rendering.
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 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL House with Chimney</title> <style> /* Make the canvas take up the full page */ body, html { margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; } canvas { display: block; width: 100%; height: 100%; } </style> </head> <body> <canvas id="glcanvas"></canvas> <script type="text/javascript"> // Get the canvas element and initialize WebGL context const canvas = document.getElementById('glcanvas'); const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl'); if (!gl) { alert('WebGL not supported, please use a different browser.'); } // Resize canvas to fit the window function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); } window.addEventListener('resize', resizeCanvas); resizeCanvas(); // Vertex shader program const vsSource = ` attribute vec2 aPosition; attribute vec3 aColor; varying vec3 vColor; void main(void) { // Convert from pixels to clip space gl_Position = vec4(aPosition, 0.0, 1.0); vColor = aColor; } `; // Fragment shader program const fsSource = ` precision mediump float; varying vec3 vColor; void main(void) { gl_FragColor = vec4(vColor, 1.0); } `; // Initialize shader program 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)) { alert('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)) { alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } const shaderProgram = initShaderProgram(gl, vsSource, fsSource); // Collect attribute locations const programInfo = { program: shaderProgram, attribLocations: { vertexPosition: gl.getAttribLocation(shaderProgram, 'aPosition'), vertexColor: gl.getAttribLocation(shaderProgram, 'aColor'), }, }; // Define the house and chimney geometry function initBuffers(gl) { // Define vertices for the house base (rectangle) const baseVertices = [ // Positions // Colors (R, G, B) -0.5, -0.5, 0.8, 0.5, 0.3, // Bottom left 0.5, -0.5, 0.8, 0.5, 0.3, // Bottom right 0.5, 0.0, 0.8, 0.5, 0.3, // Top right -0.5, 0.0, 0.8, 0.5, 0.3, // Top left ]; // Define indices for the base (two triangles) const baseIndices = [ 0, 1, 2, 0, 2, 3, ]; // Define vertices for the roof (triangle) const roofVertices = [ // Positions // Colors (R, G, B) -0.6, 0.0, 0.9, 0.2, 0.2, // Left 0.6, 0.0, 0.9, 0.2, 0.2, // Right 0.0, 0.5, 0.9, 0.2, 0.2, // Top ]; // Define vertices for the chimney (rectangle) const chimneyVertices = [ // Positions // Colors (R, G, B) 0.3, 0.0, 0.6, 0.4, 0.3, // Bottom left 0.5, 0.0, 0.6, 0.4, 0.3, // Bottom right 0.5, 0.3, 0.6, 0.4, 0.3, // Top right 0.3, 0.3, 0.6, 0.4, 0.3, // Top left ]; // Define indices for the chimney (two triangles) const chimneyIndices = [ 0, 1, 2, 0, 2, 3, ]; // Create and bind buffers for the base const baseBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, baseBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(baseVertices), gl.STATIC_DRAW); const baseIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, baseIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(baseIndices), gl.STATIC_DRAW); // Create and bind buffers for the roof const roofBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, roofBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(roofVertices), gl.STATIC_DRAW); const roofIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, roofIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0, 1, 2]), gl.STATIC_DRAW); // Create and bind buffers for the chimney const chimneyBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, chimneyBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(chimneyVertices), gl.STATIC_DRAW); const chimneyIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, chimneyIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(chimneyIndices), gl.STATIC_DRAW); return { base: { buffer: baseBuffer, indices: baseIndexBuffer, vertexCount: baseIndices.length, }, roof: { buffer: roofBuffer, indices: roofIndexBuffer, vertexCount: roofIndices.length || 3, }, chimney: { buffer: chimneyBuffer, indices: chimneyIndexBuffer, vertexCount: chimneyIndices.length, }, }; } const buffers = initBuffers(gl); // Draw the scene function drawScene(gl, programInfo, buffers) { gl.clearColor(0.5, 0.8, 0.95, 1.0); // Sky blue background gl.clear(gl.COLOR_BUFFER_BIT); gl.useProgram(programInfo.program); // Function to bind buffer and draw elements function bindAndDraw(bufferInfo) { // Bind the buffer gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.buffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferInfo.indices); // Get the size of a single vertex (2 position, 3 color) const stride = (2 + 3) * Float32Array.BYTES_PER_ELEMENT; // Position attribute gl.vertexAttribPointer( programInfo.attribLocations.vertexPosition, 2, // number of components gl.FLOAT, false, stride, 0 ); gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition); // Color attribute gl.vertexAttribPointer( programInfo.attribLocations.vertexColor, 3, // number of components gl.FLOAT, false, stride, 2 * Float32Array.BYTES_PER_ELEMENT ); gl.enableVertexAttribArray(programInfo.attribLocations.vertexColor); // Draw the object gl.drawElements(gl.TRIANGLES, bufferInfo.vertexCount, gl.UNSIGNED_SHORT, 0); } // Draw the house base bindAndDraw(buffers.base); // Draw the roof bindAndDraw(buffers.roof); // Draw the chimney bindAndDraw(buffers.chimney); } drawScene(gl, programInfo, buffers); </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 | <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL House with Chimney</title> <style> /* Make the canvas take up the full page */ body, html { margin: 0; padding: 0; width: 100%; height: 100%; overflow: hidden; } canvas { display: block; width: 100%; height: 100%; } </style> </head> <body> <canvas id="glcanvas"></canvas> <script type="text/javascript"> // Get the canvas element and initialize WebGL context const canvas = document.getElementById('glcanvas'); const gl = canvas.getContext('webgl') || canvas.getContext('experimental-webgl'); if (!gl) { alert('WebGL not supported, please use a different browser.'); } // Resize canvas to fit the window function resizeCanvas() { canvas.width = window.innerWidth; canvas.height = window.innerHeight; gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight); } window.addEventListener('resize', resizeCanvas); resizeCanvas(); // Vertex shader program const vsSource = ` attribute vec2 aPosition; attribute vec3 aColor; varying vec3 vColor; void main(void) { // Convert from pixels to clip space gl_Position = vec4(aPosition, 0.0, 1.0); vColor = aColor; } `; // Fragment shader program const fsSource = ` precision mediump float; varying vec3 vColor; void main(void) { gl_FragColor = vec4(vColor, 1.0); } `; // Initialize shader program 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)) { alert('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)) { alert('An error occurred compiling the shaders: ' + gl.getShaderInfoLog(shader)); gl.deleteShader(shader); return null; } return shader; } const shaderProgram = initShaderProgram(gl, vsSource, fsSource); // Collect attribute locations const programInfo = { program: shaderProgram, attribLocations: { vertexPosition: gl.getAttribLocation(shaderProgram, 'aPosition'), vertexColor: gl.getAttribLocation(shaderProgram, 'aColor'), }, }; // Define the house and chimney geometry function initBuffers(gl) { // Define vertices for the house base (rectangle) const baseVertices = [ // Positions // Colors (R, G, B) -0.5, -0.5, 0.8, 0.5, 0.3, // Bottom left 0.5, -0.5, 0.8, 0.5, 0.3, // Bottom right 0.5, 0.0, 0.8, 0.5, 0.3, // Top right -0.5, 0.0, 0.8, 0.5, 0.3, // Top left ]; // Define indices for the base (two triangles) const baseIndices = [ 0, 1, 2, 0, 2, 3, ]; // Define vertices for the roof (triangle) const roofVertices = [ // Positions // Colors (R, G, B) -0.6, 0.0, 0.9, 0.2, 0.2, // Left 0.6, 0.0, 0.9, 0.2, 0.2, // Right 0.0, 0.5, 0.9, 0.2, 0.2, // Top ]; // Define vertices for the chimney (rectangle) const chimneyVertices = [ // Positions // Colors (R, G, B) 0.3, 0.0, 0.6, 0.4, 0.3, // Bottom left 0.5, 0.0, 0.6, 0.4, 0.3, // Bottom right 0.5, 0.3, 0.6, 0.4, 0.3, // Top right 0.3, 0.3, 0.6, 0.4, 0.3, // Top left ]; // Define indices for the chimney (two triangles) const chimneyIndices = [ 0, 1, 2, 0, 2, 3, ]; // Create and bind buffers for the base const baseBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, baseBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(baseVertices), gl.STATIC_DRAW); const baseIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, baseIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(baseIndices), gl.STATIC_DRAW); // Create and bind buffers for the roof const roofBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, roofBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(roofVertices), gl.STATIC_DRAW); const roofIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, roofIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array([0, 1, 2]), gl.STATIC_DRAW); // Create and bind buffers for the chimney const chimneyBuffer = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, chimneyBuffer); gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(chimneyVertices), gl.STATIC_DRAW); const chimneyIndexBuffer = gl.createBuffer(); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, chimneyIndexBuffer); gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(chimneyIndices), gl.STATIC_DRAW); return { base: { buffer: baseBuffer, indices: baseIndexBuffer, vertexCount: baseIndices.length, }, roof: { buffer: roofBuffer, indices: roofIndexBuffer, vertexCount: roofIndices.length || 3, }, chimney: { buffer: chimneyBuffer, indices: chimneyIndexBuffer, vertexCount: chimneyIndices.length, }, }; } const buffers = initBuffers(gl); // Draw the scene function drawScene(gl, programInfo, buffers) { gl.clearColor(0.5, 0.8, 0.95, 1.0); // Sky blue background gl.clear(gl.COLOR_BUFFER_BIT); gl.useProgram(programInfo.program); // Function to bind buffer and draw elements function bindAndDraw(bufferInfo) { // Bind the buffer gl.bindBuffer(gl.ARRAY_BUFFER, bufferInfo.buffer); gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, bufferInfo.indices); // Get the size of a single vertex (2 position, 3 color) const stride = (2 + 3) * Float32Array.BYTES_PER_ELEMENT; // Position attribute gl.vertexAttribPointer( programInfo.attribLocations.vertexPosition, 2, // number of components gl.FLOAT, false, stride, 0 ); gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition); // Color attribute gl.vertexAttribPointer( programInfo.attribLocations.vertexColor, 3, // number of components gl.FLOAT, false, stride, 2 * Float32Array.BYTES_PER_ELEMENT ); gl.enableVertexAttribArray(programInfo.attribLocations.vertexColor); // Draw the object gl.drawElements(gl.TRIANGLES, bufferInfo.vertexCount, gl.UNSIGNED_SHORT, 0); } // Draw the house base bindAndDraw(buffers.base); // Draw the roof bindAndDraw(buffers.roof); // Draw the chimney bindAndDraw(buffers.chimney); } drawScene(gl, programInfo, buffers); </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 appears to be completely blank, showing only a white background. There are no visible drawings or elements present in the screenshot. If you were expecting a specific drawing or graphic, it seems that it did not render correctly in this instance.
Does this answer say that there is a picture of a house with a chimney "The image appears to be completely blank, showing only a white background. There are no visible drawings or elements present in the screenshot. If you were expecting a specific drawing or graphic, it seems that it did not render correctly in this instance."? 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