I'm running into a hard-to-debug issue using WebGL. I was implementing a WebGL backend for the CraftyJS game framework. Everything works absolutely fine on OSX, Linux, and Android, but when a collaborator tested on windows there was an important error -- rotated sprites rendered unrotated! I was able to replicate this across IE, Chrome, and Firefox. An example of the type of vertex program I'm using, which simply renders a colored rectangle:
attribute vec2 aPosition;
attribute vec3 aOrientation;
attribute vec4 aExtra;
attribute vec4 aColor;
varying lowp vec4 vColor;
uniform vec4 uViewport;
mat4 viewportScale = mat4(2.0 / uViewport.z, 0, 0, 0, 0, -2.0 / uViewport.w, 0,0, 0, 0,1,0, -1,+1,0,1);
vec4 viewportTranslation = vec4(uViewport.xy, 0, 0);
vec2 entityOrigin = aOrientation.xy;
mat2 entityRotationMatrix = mat2(cos(aOrientation.z), sin(aOrientation.z), -sin(aOrientation.z), cos(aOrientation.z));
void main() {
vec2 pos = aPosition;
pos = entityRotationMatrix * (pos - entityOrigin) + entityOrigin ;
gl_Position = viewportScale * (viewportTranslation + vec4(pos, 1.0/(1.0+exp(aExtra.x) ), 1) );
vColor = vec4(aColor.rgb*aColor.a*aExtra.y, aColor.a*aExtra.y);
}
The problem seems to be related to the aOrientation
attribute -- it is used to calculate the rotation matrix. Using Firefox's shader editor, if I manually specify entityOrigin
and entityRotationMatrix
then the program will render correctly. But by default, it renders as if all components of the aOrientation
attribute were just 0. Every other aspect of the shader (positioning+dimensions, color, alpha transparency, viewport scale/translation) seems to work fine; it's only behavior which depend on aOrientation
that is broken, and seemingly only on Windows.
Since I only have infrequent access to a windows machine this is annoyingly hard to debug. Are there any windows-specific (or hardware/driver specific?) issues that could somehow cause problems setting that one particular attribute?
Figured it out, once I actually had time to sit down with a windows machine and experiment -- the compiler was "optimizing" away my attribute on windows. I guess it makes sense that this might be driver/platform/hardware dependent.
The solution was to place the following lines inside of main()
instead of outside of it.
vec2 entityOrigin = aOrientation.xy;
mat2 entityRotationMatrix = mat2(cos(aOrientation.z), sin(aOrientation.z), -sin(aOrientation.z), cos(aOrientation.z));
I guess I don't really understand the difference between code that goes inside of main, and code outside of it. Something to look into! :)