到目前为止,我已成功显示具有修复颜色的立方体。但是,我希望立方体的颜色可以改变。所以我试着替换这段代码:
var cBuffer = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, cBuffer );
gl.bufferData( gl.ARRAY_BUFFER, flatten(vertexColors1), gl.STATIC_DRAW );
var vColor = gl.getAttribLocation( program, "vColor" );
gl.vertexAttribPointer( vColor, 4, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vColor );
使用:
vColor = gl.getUniformLocation(program, "vColor");
gl.uniformMatrix4fv(vColor,false,flatten(vertexColors1));
我还将顶点着色器中的vColor类型从属性更改为uniform。在片段着色器中,我使用变化。但是,这些方法不起作用。立方体的颜色变为白色,而不是我在vertexColors1中指定的颜色。有什么建议吗?
着色器代码:
<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec4 vPosition;
uniform vec4 vColor;
//attribute vec4 vColor;
varying vec4 fColor;
uniform mat4 modelViewMatrix;
void main()
{
fColor = vColor;
gl_Position = modelViewMatrix * vPosition;
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
precision mediump float;
varying vec4 fColor;
void
main()
{
gl_FragColor = fColor;
}
</script>