[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/API/WebGL_API/Tutorial/Creating_3D_objects_using_WebGL [Back]  [Original]

WebGL 3D - Web API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

WebGL 3D

5 3 . drawArray() , , gl.drawElements() .

: 4 , 6 24 , 8 . 8 . 8 24 , . , . .

In this article

initBuffers() . , 4 , 24 :

js
var vertices = [
  // (Front face)
  -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0,

  // (Back face)
  -1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, -1.0,

  // (Top face)
  -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0,

  // (Bottom face)
  -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, 1.0,

  // (Right face)
  1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0,

  // (Left face)
  -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 . , .

js
var colors = [
  [1.0, 1.0, 1.0, 1.0], //  : 
  [1.0, 0.0, 0.0, 1.0], //  : 
  [0.0, 1.0, 0.0, 1.0], //  : 
  [0.0, 0.0, 1.0, 1.0], //  : 
  [1.0, 1.0, 0.0, 1.0], //  : 
  [1.0, 0.0, 1.0, 1.0], //  : 
];

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,
);

( : element array) .

js
cubeVerticesIndexBuffer = gl.createBuffer();
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVerticesIndexBuffer);

//        .
//           .
// ,    0, 1, 2,   0, 2, 3
//   0, 1, 2   
// 0, 2, 3     
//    .

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 , , . 6 12 .

drawScene() . bindBuffer() drawElements() :

js
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVerticesIndexBuffer);
setMatrixUniforms();
gl.drawElements(gl.TRIANGLES, 36, gl.UNSIGNED_SHORT, 0);

, 6 , 36 . 24 36 . , , 36 .

. WebGL 6 .


Web Proxy Viewer  |  New URL  |  Original Page