我目前正在学习WebGL和Javascript的过程。一个任务需要我使用WebGL创建多个形状,并且它们都是不同的颜色,但是,我无法弄清楚如何设置它以便每个形状都具有它自己的颜色。WebGL绘制不同颜色的多种形状
// HelloTriangle.js (c) 2012 matsuda
// Vertex shader program
var VSHADER_SOURCE =
'attribute vec4 a_Position;\n' +
'void main() {\n' +
' gl_Position = a_Position;\n' +
'}\n';
// Fragment shader program
var FSHADER_SOURCE =
'precision mediump float;\n' +
'uniform vec4 u_FragColor;\n' +
'void main() {\n' +
' gl_FragColor = u_FragColor;\n' +
'}\n';
function main() {
// Retrieve <canvas> element
var canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
var gl = getWebGLContext(canvas);
if (!gl) {
console.log('Failed to get the rendering context for WebGL');
return;
}
// Initialize shaders
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('Failed to intialize shaders.');
return;
}
// Write the positions of vertices to a vertex shader
var n = initVertexBuffers(gl);
if (n < 0) {
console.log('Failed to set the positions of the vertices');
return;
}
// Specify the color for clearing <canvas>
gl.clearColor(0, 0, 0, 1);
// Clear <canvas>
gl.clear(gl.COLOR_BUFFER_BIT);
// Draw the rectangle
gl.drawArrays(gl.TRIANGLES, 0, 3); // Triangle One
gl.drawArrays(gl.TRIANGLES, 3, 3); // Triangle Two
gl.drawArrays(gl.TRIANGLE_FAN, 6, 6); // Triangle Fan
}
function randColor() // Assign Random color values
{
setR = Math.random();
setG = Math.random();
setB = Math.random();
}
function initVertexBuffers(gl) {
var vertices = new Float32Array([
0.1, 0.1, -0.1, -0.1, 0.1, -0.1, // First Triangle
0.15, 0.25, 0.1, 0.2, -0.1, 0.2, // Second Triangle
0.75, 0.65, 0.35, 0.25, 0.65, 0.55,
0.65, 0.25, 0.35, 0.45, 0.25, 0.15 // Triangle Fan
]);
var n = 6; // The number of vertices
// Create a buffer object
var vertexBuffer = gl.createBuffer();
if (!vertexBuffer) {
console.log('Failed to create the buffer object');
return -1;
}
// Bind the buffer object to target
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
// Write date into the buffer object
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
var a_Position = gl.getAttribLocation(gl.program, 'a_Position');
if (a_Position < 0) {
console.log('Failed to get the storage location of a_Position');
return -1;
}
// Get the storage location of u_FragColor
var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor');
if (!u_FragColor) {
console.log('Failed to get the storage location of u_FragColor');
return;
}
//Pass color of point to u_FragColor
randColor();
gl.uniform4f(u_FragColor, setR, setG, setB, 1.0);
// Assign the buffer object to a_Position variable
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0);
// Enable the assignment to a_Position variable
gl.enableVertexAttribArray(a_Position);
return n;
}
<canvas id="webgl" width="400" height="400">
Please use a browser that supports "canvas"
</canvas>
<script src="../lib/webgl-utils.js"></script>
<script src="../lib/webgl-debug.js"></script>
<script src="../lib/cuon-utils.js"></script>
我敢肯定,我应该修改代码如下:
//Pass color of point to u_FragColor
randColor();
gl.uniform4f(u_FragColor, setR, setG, setB, 1.0);
然而,我无法弄清楚如何让这个IT卖场我试图绘制每个形状的值。我认为通过在每次绘图之前随机更改颜色,这可以解决问题,但事实并非如此。任何洞察到这一点将不胜感激。
谢谢。
[当前页上的最后一个示例绘制矩形在随机颜色(http://webglfundamentals.org/ webgl/lessons/webgl-fundamentals.html) – gman
谢谢你,但是,我很难确定哪个部分使得每个绘制的对象是不同的颜色,这是我的问题。 – Zach
没关系,我想通了! 谢谢,这确实有些帮助。 – Zach