【发布时间】:2015-03-28 15:21:16
【问题描述】:
我正在使用 DuoCode 尝试在 webGL 中绘制 2D 多边形。为此,我编写了特定的着色器,它们采用 2D 顶点和 z 位置以及其他一些数据:
<!-- Fragment shader program -->
<script id="shader-fs" type="x-shader/x-fragment">
varying highp vec2 vTextureCoord;
uniform highp vec3 uColor;
uniform sampler2D uSampler;
uniform int uSamplerCount;
void main(void) {
highp vec4 texColor =vec4(uColor, 1.0);
if(uSamplerCount > 0)
texColor = texture2D(uSampler, vTextureCoord);
gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}
</script>
<!-- Vertex shader program -->
<script id="shader-vs" type="x-shader/x-vertex">
attribute highp vec2 aVertexPosition;
attribute highp vec2 aTextureCoord;
uniform highp vec2 uPosition;
uniform highp float uZLayer;
varying highp vec2 vTextureCoord;
void main(void) {
gl_Position = vec4(uPosition + aVertexPosition, uZLayer, 1.0);
vTextureCoord = aTextureCoord;
}
</script>
在这种情况下,我禁用了纹理并尝试绘制一个白色多边形。着色器编译没有错误。
我这样创建顶点缓冲区:
public void UpdateBuffers()
{
System.Console.WriteLine("Updating buffers");
System.Console.Write("Vertices: ");
for (int i = 0; i < rotatedPoints.length; i++)
System.Console.Write(rotatedPoints[i] + ", ");
System.Console.WriteLine(" ");
System.Console.Write("texCoords: ");
for (int i = 0; i < texCoords.length; i++)
System.Console.Write(texCoords[i] + ", ");
System.Console.WriteLine(" ");
System.Console.Write("Triangles: ");
for (int i = 0; i < triangles.length; i++)
System.Console.Write(triangles[i] + ", ");
System.Console.WriteLine(" ");
gl.bindBuffer(GL.ARRAY_BUFFER, bVertexPositions);
gl.bufferData(GL.ARRAY_BUFFER, rotatedPoints.As<ArrayBufferView>(), GL.STATIC_DRAW);
gl.bindBuffer(GL.ARRAY_BUFFER, bTextureCoords);
gl.bufferData(GL.ARRAY_BUFFER, texCoords.As<ArrayBufferView>(), GL.STATIC_DRAW);
gl.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, bIndices);
gl.bufferData(GL.ELEMENT_ARRAY_BUFFER, triangles.As<ArrayBufferView>(), GL.STATIC_DRAW);
}
它再次没有错误并给我以下输出:
更新缓冲区 顶点:0, 0, 10, 0, 10, 10, 0, 10,
texCoords: 0, 1, 1, 1, 1, 0, 0, 0,
三角形:3、0、1、3、1、2、
(顶点是 2-component)
我正在尝试像这样绘制一个多边形:
gl.viewport(0, 0, (int)canvas.width, (int)canvas.height);
gl.clear(GL.COLOR_BUFFER_BIT | GL.DEPTH_BUFFER_BIT);
testSprite.Draw(aVertexPosition, aTextureCoord, uColor, uPosition, uZLayer, uSampler, uSamplerCount);
a 表示属性,u 表示这里的制服位置。
绘图本身如下所示:
public void Draw(uint aVertexPosition, uint aTextureCoord,
WebGLUniformLocation uColor, WebGLUniformLocation uPosition, WebGLUniformLocation uZLayer, WebGLUniformLocation uSampler, WebGLUniformLocation uSamplerCount)
{
//position
gl.uniform2f(uPosition, this.position.x, this.position.y);
gl.uniform1f(uZLayer, this.position.z);
gl.uniform3f(uColor, 1f, 0.5f, 0.5f);
gl.uniform1i(uSamplerCount, 0);
//vertex data
gl.bindBuffer(GL.ARRAY_BUFFER, bVertexPositions);
gl.vertexAttribPointer(aVertexPosition, 2, GL.FLOAT, false, 0, 0);
gl.bindBuffer(GL.ARRAY_BUFFER, bTextureCoords);
gl.vertexAttribPointer(aTextureCoord, 2, GL.FLOAT, false, 0, 0);
gl.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, bIndices);
//texture
// gl.activeTexture(GL.TEXTURE0);
// gl.bindTexture(GL.TEXTURE_2D, texture);
// gl.uniform1i(uSampler, 0);
//draw
gl.drawElements(GL.TRIANGLES, triangles.length, GL.UNSIGNED_SHORT, 0);
}
JS 控制台再次没有错误。 不幸的是,屏幕只是保持黑色。我认为有错误可能与顶点数组有 2 个组件的事实有关,但我无法弄清楚。我以为我已经了解了 openGL 的基础知识,但显然我错了,因为这应该不是那么难的问题。然而我什么都画不出来。如果你需要它,我可以发布剩余的代码,比如创建缓冲区,但我认为我的错误一定在这段代码的某个地方。我希望这不是一篇“在我的代码中某处找到错误”的帖子,但我真的不知道从哪里开始,我就是想不通我的错误。
编辑:发现我的错误,我将 int 传递给元素索引列表,但我将它们标记为 ushort
【问题讨论】:
-
我想看看你的整个代码。你可以为此创建一个 plunker 或类似的东西吗?乍一看,我的直觉是您的顶点超出了范围(-1 到 1,除非在某处使用同质坐标进行了一些缩放),或者实际上没有任何东西在编译您的脚本。
-
精灵代码:pastebin.com/LZVLGS59 程序代码:pastebin.com/ZvYPMJNU