diff options
| author | Max Danielsson <max@autious.net> | 2018-03-24 23:31:06 +0100 |
|---|---|---|
| committer | Max Danielsson <max@autious.net> | 2018-03-25 18:33:19 +0200 |
| commit | 5ecee69088f1f3c90803a6924ba6df3cc43bfbd3 (patch) | |
| tree | bb2117782f8a93f0d547820afaeee43f685caeeb /src/core.c | |
| parent | 22c8ea76aaf63d29d21dd416d8d624dbfd322ba4 (diff) | |
| download | raylib-5ecee69088f1f3c90803a6924ba6df3cc43bfbd3.tar.gz raylib-5ecee69088f1f3c90803a6924ba6df3cc43bfbd3.zip | |
Add 3d orthographic projection mode
Diffstat (limited to 'src/core.c')
| -rw-r--r-- | src/core.c | 73 |
1 files changed, 63 insertions, 10 deletions
@@ -919,13 +919,26 @@ void Begin3dMode(Camera camera) rlPushMatrix(); // Save previous matrix, which contains the settings for the 2d ortho projection rlLoadIdentity(); // Reset current matrix (PROJECTION) - // Setup perspective projection - float aspect = (float)screenWidth/(float)screenHeight; - double top = 0.01*tan(camera.fovy*0.5*DEG2RAD); - double right = top*aspect; + if(camera.type == CAMERA_PERSPECTIVE) + { + // Setup perspective projection + float aspect = (float)screenWidth/(float)screenHeight; + double top = 0.01*tan(camera.fovy*0.5*DEG2RAD); + double right = top*aspect; + + rlFrustum(-right, right, -top, top, 0.01, 1000.0); + } + else if(camera.type == CAMERA_ORTHOGRAPHIC) + { + // Setup orthographic projection + float aspect = (float)screenWidth/(float)screenHeight; + double top = camera.fovy/2.0; + double right = top*aspect; + + rlOrtho(-right,right,-top,top, 0.01, 1000.0); + } // NOTE: zNear and zFar values are important when computing depth buffer values - rlFrustum(-right, right, -top, top, 0.01, 1000.0); rlMatrixMode(RL_MODELVIEW); // Switch back to modelview matrix rlLoadIdentity(); // Reset current matrix (MODELVIEW) @@ -1013,22 +1026,48 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera) TraceLog(LOG_DEBUG, "Device coordinates: (%f, %f, %f)", deviceCoords.x, deviceCoords.y, deviceCoords.z); - // Calculate projection matrix from perspective - Matrix matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)GetScreenWidth()/(double)GetScreenHeight()), 0.01, 1000.0); - // Calculate view matrix from camera look at Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up); + Matrix matProj; + + if(camera.type == CAMERA_PERSPECTIVE) + { + // Calculate projection matrix from perspective + matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)GetScreenWidth()/(double)GetScreenHeight()), 0.01, 1000.0); + } + else if(camera.type == CAMERA_ORTHOGRAPHIC) + { + float aspect = (float)screenWidth/(float)screenHeight; + double top = camera.fovy/2.0; + double right = top*aspect; + // Calculate projection matrix from orthographic + matProj = MatrixOrtho(-right, right, -top, top, 0.01, 1000.0); + } + // Unproject far/near points Vector3 nearPoint = rlUnproject((Vector3){ deviceCoords.x, deviceCoords.y, 0.0f }, matProj, matView); Vector3 farPoint = rlUnproject((Vector3){ deviceCoords.x, deviceCoords.y, 1.0f }, matProj, matView); + // Unproject the mouse cursor in the near plane. + // We need this as the source position because orthographic projects, compared to perspect doesn't have a + // convergence point, meaning that the "eye" of the camera is more like a plane than a point. + Vector3 cameraPlanePointerPos = rlUnproject((Vector3){ deviceCoords.x, deviceCoords.y, -1.0f }, matProj, matView); + // Calculate normalized direction vector Vector3 direction = Vector3Subtract(farPoint, nearPoint); direction = Vector3Normalize(direction); + if(camera.type == CAMERA_PERSPECTIVE) + { + ray.position = camera.position; + } + else if(camera.type == CAMERA_ORTHOGRAPHIC) + { + ray.position = cameraPlanePointerPos; + } + // Apply calculated vectors to ray - ray.position = camera.position; ray.direction = direction; return ray; @@ -1038,7 +1077,21 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera) Vector2 GetWorldToScreen(Vector3 position, Camera camera) { // Calculate projection matrix (from perspective instead of frustum - Matrix matProj = MatrixPerspective(camera.fovy*DEG2RAD, (double)GetScreenWidth()/(double)GetScreenHeight(), 0.01, 1000.0); + Matrix matProj; + + if(camera.type == CAMERA_PERSPECTIVE) + { + // Calculate projection matrix from perspective + matProj = MatrixPerspective(camera.fovy*DEG2RAD, ((double)GetScreenWidth()/(double)GetScreenHeight()), 0.01, 1000.0); + } + else if(camera.type == CAMERA_ORTHOGRAPHIC) + { + float aspect = (float)screenWidth/(float)screenHeight; + double top = camera.fovy/2.0; + double right = top*aspect; + // Calculate projection matrix from orthographic + matProj = MatrixOrtho(-right, right, -top, top, 0.01, 1000.0); + } // Calculate view matrix from camera look at (and transpose it) Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up); |
