我目前正在學習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