WebGL and OpenGL Differences
WebGL is based on the OpenGL ES 2.0 specification, and retains the semantics of OpenGL ES in order to maximize portability to mobile devices. There are some significant differences in behavior of similar APIs between OpenGL ES 2.0 and the OpenGL API on desktop systems. Many OpenGL programmers are familiar with the semantics on the desktop, and may not know about these differences in behavior. We highlight them here for clarity.
Non-Power of Two Texture Support
While OpenGL 2.0 and later for the desktop offer full support for non-power-of-two (NPOT) textures, OpenGL ES 2.0 and WebGL have only limited NPOT support. The restrictions are defined in Sections 3.8.2, "Shader Execution", and 3.7.11, "Mipmap Generation", of the [[1][OpenGL ES 2.0 specification]], and are summarized here:
- generateMipmap(target) generates an INVALID_OPERATION OpenGL error if the level 0 image of the texture currently bound to target has an NPOT width or height.
- Sampling an NPOT texture in a shader will produce the RGBA color (0, 0, 0, 1) if:
- The minification filter is set to anything but NEAREST or LINEAR; in other words, if it uses one of the mipmapped filters.
- The repeat mode is set to anything but CLAMP_TO_EDGE. Repeating NPOT textures are not supported.
If your application doesn't require the REPEAT wrap mode, and can tolerate the lack of mipmaps, then you can simply configure the WebGLTexture object appropriately at creation time:
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);