Hi guys I been leanring WebGL and trying to make a Tetris game out of it.
I have a couple of questions I'd like to ask:
- For this game I wanted to first draw the grid as the background. However I noticed that after I drew the line, if I use
gl.clear(gl.COLOR_BUFFER_BIT );
after, it will clear all the lines I drew before. I understand thatgl.clear(gl.COLOR_BUFFER_BIT );
is about clearing the color buffer (and you probably will ask why I would want to do that. Just bear with me. ). Then I tried usegl.uniform4f( uColor, 0, 0, 0, 1);
to send the color again to the fragment shader but it doesn't help.
The snippet is like this
window.onload = function(){
getGLContext();
initShaders();
drawLines( 0, 0, 400,400 );
gl.clear(gl.COLOR_BUFFER_BIT );
gl.uniform4f( uColor, 0, 0, 0, 1);
}
For the game I need the grid as background and I need
requestAnimationFrame
for the game loop and will render Tetrominos inside the loop. Therefore after drawing the line I used thisdraw()
to draw other Tetrominos. However it removes the line I drew before. And when I comment outgl.clear(gl.COLOR_BUFFER_BIT );
insidedraw()
, it will remove the line along with background color.function draw() { gl.clear(gl.COLOR_BUFFER_BIT ); gl.drawArrays(gl.TRIANGLES, 0, index*6); requestAnimationFrame(draw); }
Here is the demo: https://codepen.io/zhenghaohe/pen/LqxpjB
Hope you could answer these two questions. Thanks!