用三角形在WebGL中制作网格平面

时间:2016-02-21 04:06:04

标签: javascript opengl-es webgl

我试图在WebGL中创建一个三角形平面,我有以下代码。但是,当我在浏览器中渲染它时,它只给我一条与y轴平行的垂直线。

var quads = 200;
for (var y = 0; y <= quads; ++y) {
  var v = y / quads * 40;
  for (var x = 0; x <= quads; ++x) {
    var u = x / quads * 40;
    recipient.vertices.push( vec3(u, v, 1))
    recipient.normals.push( vec3(0, 0, 1))
  }
}

var rowSize = (quads + 1);
for (var y = 0; y < quads; ++y) {
  var rowOffset0 = (y + 0) * rowSize;
  var rowOffset1 = (y + 1) * rowSize;
  for (var x = 0; x < quads; ++x) {
    recipient.indices.push(rowOffset0, rowOffset0 + 1, rowOffset1);
    recipient.indices.push(rowOffset1, rowOffset0 + 1, rowOffset1 + 1);
  }
}

另外作为一个后续问题,我希望得到一些关于如何用这个平面制作曲面的技巧,类似于山丘。

1 个答案:

答案 0 :(得分:2)

original answer中有一个错误。应该是这个

var quads = 200;
for (var y = 0; y <= quads; ++y) {
  var v = y / quads;
  for (var x = 0; x <= quads; ++x) {
    var u = x / quads;
    recipient.vertices.push( vec3(u, v, 1))
    recipient.normals.push( vec3(0, 0, 1))
  }
}

var rowSize = (quads + 1);
for (var y = 0; y < quads; ++y) {
  var rowOffset0 = (y + 0) * rowSize;
  var rowOffset1 = (y + 1) * rowSize;
  for (var x = 0; x < quads; ++x) {
    var offset0 = rowOffset0 + x;
    var offset1 = rowOffset1 + x;
    recipient.indices.push(offset0, offset0 + 1, offset1);
    recipient.indices.push(offset1, offset0 + 1, offset1 + 1);
  }
}

也固定在original answer

function Vec3Array() {
  this.array = [];
  this.push = function(v3) {
    this.array.push.apply(this.array, v3);
  }
}

function vec3(x, y, z) {
  return [x, y, z];
}
  
var recipient = {
  vertices: new Vec3Array(),
  normals: new Vec3Array(),
  indices: [],
};
  
var quads = 200;
for (var y = 0; y <= quads; ++y) {
  var v = y / quads;
  for (var x = 0; x <= quads; ++x) {
    var u = x / quads;
    recipient.vertices.push( vec3(u, v, 1))
    recipient.normals.push( vec3(0, 0, 1))
  }
}

var rowSize = (quads + 1);
for (var y = 0; y < quads; ++y) {
  var rowOffset0 = (y + 0) * rowSize;
  var rowOffset1 = (y + 1) * rowSize;
  for (var x = 0; x < quads; ++x) {
    var offset0 = rowOffset0 + x;
    var offset1 = rowOffset1 + x;
    recipient.indices.push(offset0, offset0 + 1, offset1);
    recipient.indices.push(offset1, offset0 + 1, offset1 + 1);
  }
}


var gl = twgl.getWebGLContext(document.getElementById("c"));
var programInfo = twgl.createProgramInfo(gl, ["vs", "fs"]);

var arrays = {
  position: recipient.vertices.array,
  indices: recipient.indices,
};
var bufferInfo = twgl.createBufferInfoFromArrays(gl, arrays);

function render(time) {
  time *= 0.001;
  twgl.resizeCanvasToDisplaySize(gl.canvas);
  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);

  var scale = 2 + (Math.sin(time) * 0.5 + 0.5) * 16;
  var uniforms = {
    matrix: [
      scale, 0, 0, 0,
      0, scale, 0, 0,
      0, 0, 1, 0,
      -1, -1, 0, 1,
    ],
  };

  gl.useProgram(programInfo.program);
  twgl.setBuffersAndAttributes(gl, programInfo, bufferInfo);
  twgl.setUniforms(programInfo, uniforms);
  twgl.drawBufferInfo(gl, gl.LINE_STRIP, bufferInfo);

  requestAnimationFrame(render);
}
requestAnimationFrame(render);
html, body {
  width: 100%;
  height: 100%;
  margin: 0;
}
canvas { 
  width: 100%;
  height: 100%;
}
<script src="https://twgljs.org/dist/twgl.min.js"></script>
<script id="vs" type="notjs">
attribute vec4 position;

uniform mat4 matrix;

void main() {
  gl_Position = matrix * position;
}
</script>
<script id="fs" type="notjs">
precision mediump float;

void main() {
  gl_FragColor = vec4(1,0,0,1);
}
  </script>
<canvas id="c"></canvas>