繁体   English   中英

WebGL渲染2d精灵的纹理。 没有出现

[英]WebGL rendering a texture for a 2d sprite. Not showing up

以前有一个用于渲染此小正方形的颜色顶点,现在我想使用图像。 我使用的是我躺在附近的图像精灵,宽度为64x128。

我没有收到任何错误,但是也没有任何显示。 首先着色器:

<script id="shader-fs" type="x-shader/x-fragment">
  precision mediump float;

  varying vec2 vTextureCoord;

  uniform sampler2D uSampler;

  void main(void) {
    gl_FragColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
  }
</script>

<script id="shader-vs" type="x-shader/x-vertex">
  attribute vec3 aVertexPosition;
  attribute vec2 aTextureCoord;

  uniform mat4 uMVMatrix;
  uniform mat4 uPMatrix;

  varying vec2 vTextureCoord;

  void main(void) {
    gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
    vTextureCoord = aTextureCoord;
  }
</script>

对于缓冲区:

initBuffers: function () {
  this.planePositionBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, this.planePositionBuffer);

  var vertices = [
    1.0, 1.0, 0.0,
    -1.0, 1.0, 0.0,
    1.0, -1.0, 0.0,
    -1.0, -1.0, 0.0
  ];

  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
  this.planePositionBuffer.itemSize = 3;
  this.planePositionBuffer.numItems = 4;

  this.textureBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
  var textureCoords = [
    0.0, 1.0,
    0.0, 0.0,
    1.0, 1.0,
    1.0, 0.0
  ];

  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
  this.textureBuffer.itemSize = 2;
  this.textureBuffer.numItems = 4;
}

初始化着色器:

initShaders: function () {
  this.shader = new Shader("shader");
  var shaderProgram = this.shader.shaderProgram;
  shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
  gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

  shaderProgram.textureCoordAttribute = gl.getAttribLocation(shaderProgram, "aTextureCoord");
  gl.enableVertexAttribArray(shaderProgram.textureCoordAttribute);

  shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
  shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
  shaderProgram.samplerUniform = gl.getUniformLocation(shaderProgram, "uSampler");
}

和抽奖电话。

draw: function () {
  this.resize();
  var delta = this.getDeltaTime();

  gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

  mat4.identity(this.mvMatrix);
  mat4.identity(this.pMatrix);

  mat4.perspective(this.pMatrix, 45 * Math.PI / 180, gl.canvas.clientWidth / gl.canvas.clientHeight, 0.1, 100.0);
  mat4.translate(this.pMatrix, this.pMatrix, [0.0, 0.0, -50.0]);

  var shaderProgram = this.shader.shaderProgram;

  for (var i = 0; i < this.objects.length; i++) {
    this.objects[i].update(delta, this.mvMatrix);
  }

  gl.bindBuffer(gl.ARRAY_BUFFER, this.planePositionBuffer);
  gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, this.planePositionBuffer.itemSize, gl.FLOAT, false, 0, 0);

  gl.bindBuffer(gl.ARRAY_BUFFER, this.textureBuffer);
  gl.vertexAttribPointer(shaderProgram.textureCoordAttribute, this.textureBuffer.itemSize, gl.FLOAT, false, 0, 0);

  gl.activeTexture(gl.TEXTURE0);
  gl.bindTexture(gl.TEXTURE_2D, me.loader.getImage('test'));
  gl.uniform1i(shaderProgram.samplerUniform, 0);

  this.setMatrixUniforms();
  gl.drawArrays(gl.TRIANGLE_STRIP, 0, this.planePositionBuffer.numItems);
  requestAnimationFrame(this.draw.bind(this));
},

我的纹理坐标基于我在android上的opengles绘图中找到的一篇文章。 想法是坐标应该按左下->左上->右下->右上的顺序排列。

除了上面的代码片段之外,还可以在此处找到其损坏状态的完整源代码: https : //github.com/agmcleod/webgl-2dexperiment/tree/13f31f70037fdd4515c1336423337a1e82ab4e89

看起来WebGL代码都是正确的。 但是,我从未调用过bindAllTextures函数来实际使用webgl设置纹理进行渲染。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM