evanw / lightgl.js

A lightweight WebGL library
MIT License
1.52k stars 146 forks source link

gl.popMatrix throws TypeError: invalid arguments in Firefox #20

Closed NaridaL closed 8 years ago

NaridaL commented 8 years ago

As I understand it, in the following example (it's the example on the index page with one line / call added.), the popMatrix call should remove the "angle" rotation, leading to a still cube. However, in Firefox I get an error "TypeError: invalid arguments" and nothing gets drawn, in Chrome nothing gets drawn and no error.

<!DOCTYPE html>
<html><body>
  <script src="lightgl.js"></script>
  <script>

var angle = 0;
var gl = GL.create();
var mesh = GL.Mesh.cube();
var shader = new GL.Shader('\
  void main() {\
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\
  }\
', '\
  void main() {\
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\
  }\
');

gl.onupdate = function(seconds) {
  angle += 45 * seconds;
};

gl.ondraw = function() {
  gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
  gl.loadIdentity();
  gl.translate(0, 0, -5);
  gl.rotate(30, 1, 0, 0);
  gl.rotate(angle, 0, 1, 0);
  gl.popMatrix(); // throws TypeError: invalid arguments

  shader.draw(mesh);
};

gl.fullscreen();
gl.animate();

  </script>
</body></html>
NaridaL commented 8 years ago

Hmm my bad, apparently I have to push a matrix before I can pop it. For some reason I thought the multMatrix function automatically pushed the current one, but this makes more sense...