I am using WebGL to draw a simple frustum geometry. I tried to color the front (smaller) and back (bigger) surface red, and the side surface white.
This is how it looks. Note that the smaller surface is nearer the eyes and the bigger surfaces is further from the eye. Do not be fooled by this image. It looks like the shader chose to color the sides which are furthest to the eyes and ignore faces up in the front.
How should I make this work correctly?
Followed is my shader setup: The depth buffer is cleared during initialization and never used.
function init(){
// Retrieve <canvas> element
var canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
gl = getWebGLContext(canvas);
if (!gl) {
console.log("lib1.js: init() failed to get WebGL rendering context 'gl'\n");
console.log("from the HTML-5 Canvas object named 'canvas'!\n\n");
return;
}
// Initialize shaders
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) {
console.log('lib1.js: init() failed to intialize shaders.');
return;
}
bufferSetup(gl);
// Set the background-clearing color and enable the depth test
gl.clearColor(0.0, 0.0, 0.0, 1.0); // black!
gl.enable(gl.DEPTH_TEST);
gl.enable(gl.CULL_FACE); // draw the back side of triangles
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
}
Thanks for Denis pointing this out! I also tried turning the gl.CULL_FACE
on and off, and tried gl.BACK
and gl.FRONT
. None of them works correctly.
gl.disable(gl.CULL_FACE);
// gl.cullFace(gl.BACK)