If your WebGL vertex coordinates after projection (after the vertex shader runs) are 1000x larger than they should be, but:
- The projection matrix is correct (you've double-checked it),
- The vertex data is correct (you've displayed it and verified),
- The data is loaded into the shader properly,
then the most likely causes are:
1. Homogeneous Coordinates Not Divided (No Perspective Divide)
After the vertex shader, the output is in clip space (homogeneous coordinates, gl_Position). The GPU automatically divides by w (the fourth component) to get normalized device coordinates (NDC), but if you are reading/interpreting the value of gl_Position directly in JavaScript or in a debug output, you are seeing the pre-divided value.
- Clip space:
gl_Position = (x, y, z, w)
- NDC:
(x/w, y/w, z/w)
If your w is 0.001, then x/w is 1000x x.
Check:
- Are you reading
gl_Position directly in a transform feedback or debug output, and not dividing by w?
- Are you using
gl_Position.xyz somewhere in your code, instead of dividing by gl_Position.w?
2. Projection Matrix Scale
Even if you believe the projection matrix is correct, double-check the scale of the matrix. If the matrix is off by a factor of 1000 (for example, if your near/far plane values are off by 1000x), it will scale your coordinates accordingly.
- Check the values in your projection matrix. Are they what you expect?
- Check the units of your near/far planes. Are you using meters in one place and millimeters in another?
3. Vertex Data Units Mismatch
If your vertex data is in millimeters but your projection matrix expects meters (or vice versa), you will get a 1000x scale error.
- Check the units of your vertex data and the expected units in your projection matrix.
4. Matrix Order (Row-major vs Column-major)
WebGL expects matrices in column-major order. If you upload a matrix in row-major order, it will be transposed, which can cause scaling and other issues.
- Check how you upload your matrix to the shader. Are you using
gl.uniformMatrix4fv(..., false, ...)? The second parameter (false) means "do not transpose".
5. Shader Code Error
Check your vertex shader code. Are you multiplying the vertex by the projection matrix correctly? The usual order is:
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
If you have the order wrong, or multiply by the wrong matrix, you can get scaling errors.
Summary Checklist
Most likely:
If you are seeing a 1000x scale, and everything else is correct, you are probably not dividing by w after the vertex shader. Remember, the GPU does this automatically for rasterization, but if you are reading/interpreting the value yourself, you must do it manually.
If you can share your vertex shader code and how you are reading/interpreting the projected coordinates, I can give more specific advice!