Reputation: 7360
I see that everybody normalizes his coordinates to be in range of [-1;1]. Why is this done? Is this required? Is this only for ThreeJS , or is it a common thing for every 3D framework?
Upvotes: 3
Views: 2825
Reputation: 210889
Three.js is based on WebGL and WebGL is based on OpenGL ES (Embedded System)
In WebGL / OpenGL ES there is a viewport:
OpenGL manages a rectangular viewport as part of its state which defines the placement of the rendering results in the drawing buffer. Upon creation of the WebGL context, the viewport is initialized to a rectangle with origin at (0, 0) and width and height equal to (canvas.width, canvas.height).
The viewport specifies the affine transformation of x and y from normalized device coordinates to window coordinates. The size of the drawing buffer is determined by the HTMLCanvasElement. The scissor box defines a rectangle which constrains drawing. When the scissor test is enabled only pixels that lie within the scissor box can be modified by drawing commands. When enabled drawing can only occur inside the intersection of the viewport, canvas area and the scissor box. When the scissor test is not enabled drawing can only occur inside the intersection of the viewport and canvas area.
This means gl.viewport
defines the transformation from normalized device coordinates to the viewport rectangle.
Because of that everything which is drawn and should be shown on the viewport, has to be in the normalized device space, which is in the range from (-1,-1,-1) to (1, 1, 1).
Transformation of the geometry to normalized device coordinates:
The geometry is transformed by its model matrix, to world coordinates.
To transform from wolrd coordinates to normaliced device coordinates, Three.js provides the THREE.OrthographicCamera or the THREE.PerspectiveCamera. The camera defines a view matrix and a projection matrix.
The view matrix describes the direction and position from which the scene is looked at. The view matrix transforms from the wolrd space to the view (eye) space. In general world coordinates and view coordinates are Cartesian coordinates.
The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. The projection matrix transforms from view space to the clip space. Clip space coordinates are Homogeneous coordinates. The coordinates in the clip space are transformed to the normalized device coordinates (NDC) in the range (-1, -1, -1) to (1, 1, 1) by dividing with the w
component of the clip coordinates.
Upvotes: 2