| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ru/docs/Web/API/WebGL_API/Tutorial/Creating_3D_objects_using_WebGL | [Back] [Original] |
Get to know MDN better
This page was translated from English by the community. Learn more and join the MDN Web Docs community.
, 5 , . , gl.drawArrays() , , , gl.drawElements().
: , . , 24- , , . , 24 , 8, , , - 3 , .
, , initBuffers(). , , 24 (4 ):
var vertices = [
//
-1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0,
//
-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0,
//
-1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0,
//
-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0,
//
1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0,
//
-1.0, -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0,
];
24- . , .
var colors = [
[1.0, 1.0, 1.0, 1.0], // Front face: white
[1.0, 0.0, 0.0, 1.0], // Back face: red
[0.0, 1.0, 0.0, 1.0], // Top face: green
[0.0, 0.0, 1.0, 1.0], // Bottom face: blue
[1.0, 1.0, 0.0, 1.0], // Right face: yellow
[1.0, 0.0, 1.0, 1.0], // Left face: purple
];
var generatedColors = [];
for (j = 0; j < 6; j++) {
var c = colors[j];
for (var i = 0; i < 4; i++) {
generatedColors = generatedColors.concat(c);
}
}
cubeVerticesColorBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, cubeVerticesColorBuffer);
gl.bufferData(
gl.ARRAY_BUFFER,
new Float32Array(generatedColors),
gl.STATIC_DRAW,
);
, .
cubeVerticesIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVerticesIndexBuffer);
// ,
// ,
// .
var cubeVertexIndices = [
0,
1,
2,
0,
2,
3, // front
4,
5,
6,
4,
6,
7, // back
8,
9,
10,
8,
10,
11, // top
12,
13,
14,
12,
14,
15, // bottom
16,
17,
18,
16,
18,
19, // right
20,
21,
22,
20,
22,
23, // left
];
// GL
gl.bufferData(
gl.ELEMENT_ARRAY_BUFFER,
new Uint16Array(cubeVertexIndices),
gl.STATIC_DRAW,
);
cubeVertexIndices , . 12 .
drawScene() , , , gl.bindBuffer() gl.drawElements():
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVerticesIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);
, 6 , 36 , . , , , .
6 , .
View the complete code | Open this demo on a new page
This page was last modified on 3 . 2023 . by MDN contributors.
WebGLRenderingContextWebGLBufferWebGLFramebufferWebGLRenderbufferWebGLObjectWebGLProgramWebGLShaderWebGLTextureWebGLUniformLocationWebGLActiveInfoWebGLShaderPrecisionFormatWebGLContextEventWebGLQueryWebGLSamplerWebGLSyncWebGLTransformFeedbackWebGLVertexArrayObjectWebGL2RenderingContextWEBGL_compressed_texture_s3tcWEBGL_compressed_texture_s3tc_srgbWEBGL_compressed_texture_etc1WEBGL_compressed_texture_pvrtcWEBGL_debug_renderer_infoWEBGL_debug_shadersWEBGL_depth_textureOES_element_index_uintEXT_frag_depthWEBGL_lose_contextEXT_texture_filter_anisotropicEXT_sRGBOES_standard_derivativesOES_texture_floatWEBGL_draw_buffersOES_texture_float_linearEXT_shader_texture_lodOES_texture_half_floatOES_texture_half_float_linearWEBGL_color_buffer_floatEXT_color_buffer_half_floatOES_vertex_array_objectANGLE_instanced_arraysEXT_blend_minmaxEXT_disjoint_timer_queryOES_draw_buffers_indexedYour blueprint for a better internet.
Portions of this content are 19982026 by individual mozilla.org contributors. Content available under a Creative Commons license.
| Web Proxy Viewer | New URL | Original Page |