简体   繁体   English

WebGL上下文无法呈现最简单的屏幕

[英]WebGL context can't render simplest screen

I'm stuck trying to render some extremely basic stuff on webgl, I've dumbed down the rendering to the most basic thing I can think of in order to find where the issue lies, but I can't even draw a simple square for some reason. 我一直在尝试在webgl上渲染一些非常基本的东西,为了找到问题所在,我将渲染工作简化到了我能想到的最基本的东西,但是我什至无法为某些原因。 The scene I really want to render is more complex, but as I said, I've dumbed it down to try to find the problem and still no luck. 我确实想渲染的场景更加复杂,但是正如我所说的,我已经将其简化以尝试发现问题,但仍然没有运气。 I'm hoping someone can take a look and find whatever I'm missing, wich I assume is a setup step at some point. 我希望有人可以看看并找到我所缺少的东西,我认为这是某个时候的设置步骤。

The gl commands I'm running (as reported by webgl inspector, without errors) are: 我正在运行的gl命令(由webgl检查器报告,没有错误)是:

  1. clearColor(0,0,0,1) clearColor(0,0,0,1)
  2. clearDepth(1) clearDepth(1)
  3. clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT) 清除(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT)
  4. useProgram([Program 2]) useProgram([程序2])
  5. bindBuffer(ARRAY_BUFFER, [Buffer 5]) bindBuffer(ARRAY_BUFFER,[缓冲区5])
  6. vertexAttribPointer(0, 2, FLOAT, false, 0, 0) vertexAttribPointer(0,2,FLOAT,false,0,0)
  7. drawArrays(TRIANGLES, 0, 6) drawArrays(TRIANGLES,0,6)

The buffer that is being used there (Buffer 5) is setup as follows: 在那里使用的缓冲区(缓冲区5)的设置如下:

bufferData(ARRAY_BUFFER, [-1,-1,1,-1,-1,1,-1,1,1,-1,1,1], STATIC_DRAW)

And the program (Program 2) data is: 程序(程序2)的数据为:

  • LINK_STATUS true LINK_STATUS是
  • VALIDATE_STATUS false VALIDATE_STATUS否
  • DELETE_STATUS false DELETE_STATUS否
  • ACTIVE_UNIFORMS 0 ACTIVE_UNIFORMS 0
  • ACTIVE_ATTRIBUTES 1 ACTIVE_ATTRIBUTES 1

Vertex shader: 顶点着色器:

#ifdef GL_ES
precision highp float;
#endif

attribute vec2 aPosition;

void main(void) {
    gl_Position = vec4(aPosition, 0, 1);
}

Fragment shader: 片段着色器:

#ifdef GL_ES
precision highp float;
#endif

void main(void) {
    gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}

Other state I think could be relevant: 我认为可能与之相关的其他状态:

  • CULL_FACE false CULL_FACE否
  • CULL_FACE_MODE BACK CULL_FACE_MODE返回
  • FRONT_FACE CCW FRONT_FACE CCW
  • BLEND false 混合错误
  • DEPTH_TEST false DEPTH_TEST否
  • VIEWPORT 0, 0 640 x 480 视口0,0640 x 480
  • SCISSOR_TEST false SCISSOR_TEST否
  • SCISSOR_BOX 0, 0 640 x 480 SCISSOR_BOX 0,0640 x 480
  • COLOR_WRITEMASK true,true,true,true COLOR_WRITEMASK true,true,true,true
  • DEPTH_WRITEMASK true DEPTH_WRITEMASK是
  • STENCIL_WRITEMASK 0xffffffff STENCIL_WRITEMASK 0xffffffff
  • FRAMEBUFFER_BINDING null FRAMEBUFFER_BINDING空

What I expected to see from that setup/commands is a red quad taking up the whole clip space, but what I see is simply the cleared screen, as the drawArrays doesn't seem to be doing anything. 我希望从设置/命令中看到的是一个红色的四边形,占据了整个剪辑空间,但是我看到的只是清除了屏幕,因为drawArrays似乎没有做任何事情。 Can anybody spot what I'm missing? 有人可以发现我所缺少的吗? Any tips on how to debug this would be very welcome too! 任何有关如何调试的技巧都将非常受欢迎!

Here: 这里:

bufferData(ARRAY_BUFFER, [-1,-1,1,-1,-1,1,-1,1,1,-1,1,1], STATIC_DRAW)

replace to: 替换为:

bufferData(ARRAY_BUFFER, new Float32Array([-1,-1,1,-1,-1,1,-1,1,1,-1,1,1]), STATIC_DRAW)

Because webgl doesn't know which type you are passing here (integer or float or byte). 因为webgl不知道您要在此处传递哪种类型(整数,浮点数或字节)。 Example: 例:

http: jsfiddle.net/9QxAz/ http:jsfiddle.net/9QxAz/

After reading @user1724911's fiddle, I found out what I missed is enabling the vertex attribute array - stupidly simple mistake. 阅读@ user1724911的小提琴之后,我发现我错过了启用顶点属性数组的方法-愚蠢的简单错误。 I'm actually surprised I didn't get any warning from webgl inspector about this, but the solution was simply to add a call to enable that attribute: 实际上,我很惊讶我没有从webgl检查器收到任何警告,但是解决方案只是添加一个调用以启用该属性:

gl.enableVertexAttribArray(program.attributes.aPosition);

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

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