Comments (1)
By Damian Trebilco:
`#include "../glm/glm.hpp"
#include "../glm/gtc/matrix_transform.hpp"
using namespace glm;
vec3 ExtractCameraPos(const mat4 & a_modelView)
{
// Get the 3 basis vector planes at the camera origin and transform them into model space.
//
// NOTE: Planes have to be transformed by the inverse transpose of a matrix
// Nice reference here: http://www.opengl.org/discussion_boards/showthread.php/159564-Clever-way-to-transform-plane-by-matrix
//
// So for a transform to model space we need to do:
// inverse(transpose(inverse(MV)))
// This equals : transpose(MV) - see Lemma 5 in http://mathrefresher.blogspot.com.au/2007/06/transpose-of-matrix.html
//
// As each plane is simply (1,0,0,0), (0,1,0,0), (0,0,1,0) we can pull the data directly from the transpose matrix.
//
mat4 modelViewT = transpose(a_modelView);
// Get plane normals
vec3 n1(modelViewT[0]);
vec3 n2(modelViewT[1]);
vec3 n3(modelViewT[2]);
// Get plane distances
float d1(modelViewT[0].w);
float d2(modelViewT[1].w);
float d3(modelViewT[2].w);
// Get the intersection of these 3 planes
// http://paulbourke.net/geometry/3planes/
vec3 n2n3 = cross(n2, n3);
vec3 n3n1 = cross(n3, n1);
vec3 n1n2 = cross(n1, n2);
vec3 top = (n2n3 * d1) + (n3n1 * d2) + (n1n2 * d3);
float denom = dot(n1, n2n3);
return top / -denom;
}
vec3 ExtractCameraPos2(const mat4 & a_modelView)
{
// Get the 3 basis vector planes at the camera origin and transform them into model space.
//
// NOTE: Planes have to be transformed by the inverse transpose of a matrix
// Nice reference here: http://www.opengl.org/discussion_boards/showthread.php/159564-Clever-way-to-transform-plane-by-matrix
//
// So for a transform to model space we need to do:
// inverse(transpose(inverse(MV)))
// This equals : transpose(MV) - see Lemma 5 in http://mathrefresher.blogspot.com.au/2007/06/transpose-of-matrix.html
//
// As each plane is simply (1,0,0,0), (0,1,0,0), (0,0,1,0) we can pull the data directly from the transpose matrix.
//
mat4 modelViewT = transpose(a_modelView);
// Get plane normals
vec3 n1(modelViewT[0]);
vec3 n2(modelViewT[1]);
vec3 n3(modelViewT[2]);
// Get plane distances
float d1(modelViewT[0].w);
float d2(modelViewT[1].w);
float d3(modelViewT[2].w);
// Get the intersection of these 3 planes
// (uisng math from RealTime Collision Detection by Christer Ericson)
vec3 n2n3 = cross(n2, n3);
float denom = dot(n1, n2n3);
vec3 top = (n2n3 * d1) + cross(n1, (d3_n2) - (d2_n3));
return top / -denom;
}
vec3 ExtractCameraPos3(const mat4 & a_modelView)
{
// Get the 3 basis vector planes at the camera origin and transform them into model space.
//
// NOTE: Planes have to be transformed by the inverse transpose of a matrix
// Nice reference here: http://www.opengl.org/discussion_boards/showthread.php/159564-Clever-way-to-transform-plane-by-matrix
//
// So for a transform to model space we need to do:
// inverse(transpose(inverse(MV)))
// This equals : transpose(MV) - see Lemma 5 in http://mathrefresher.blogspot.com.au/2007/06/transpose-of-matrix.html
//
// As each plane is simply (1,0,0,0), (0,1,0,0), (0,0,1,0) we can pull the data directly from the transpose matrix.
//
mat4 modelViewT = transpose(a_modelView);
// Get plane normals
vec3 n1(modelViewT[0]);
vec3 n2(modelViewT[1]);
vec3 n3(modelViewT[2]);
// Get plane distances
float d1(modelViewT[0].w);
float d2(modelViewT[1].w);
float d3(modelViewT[2].w);
// Get the intersection of these 3 planes
// (uisng math from RealTime Collision Detection by Christer Ericson)
vec3 n2n3 = cross(n2, n3);
float denom = dot(n1, n2n3);
vec3 d(d1,d2,d3);
vec3 v = cross(n1, d);
vec3 top;
top.x = dot(d, n2n3);
top.y = dot(n3, v);
top.z = -dot(n2, v);
return top / -denom;
}
vec3 ExtractCameraPos_NoScale(const mat4 & a_modelView)
{
mat3 rotMat(a_modelView);
vec3 d(a_modelView[3]);
vec3 retVec = -d * rotMat;
return retVec;
}
void PrintVec3(const vec3 & a_vec)
{
printf("%f %f %f", a_vec.x, a_vec.y, a_vec.z);
}
int main(int argc, char * argv[])
{
vec3 cameraPos(1.234f, 2.657f, 7.865f);
// Create a model view matrix with translations and scales
mat4 modelView(1.0f);
modelView = rotate(modelView, 0.5f, vec3(1.0f, 0.0f, 0.0f));
modelView = rotate(modelView, 0.2f, vec3(0.0f, 1.0f, 0.0f));
modelView = rotate(modelView, 0.3f, vec3(0.0f, 0.0f, 1.0f));
modelView = scale(modelView, vec3(2.0f, 3.0f, 4.0f));
modelView = translate(modelView, -cameraPos);
vec3 cameraPos2 = ExtractCameraPos(modelView);
vec3 cameraPos2a = ExtractCameraPos2(modelView);
// ExtractCameraPos3 does not seem as accurate as the previous 2 methods
vec3 cameraPos2b = ExtractCameraPos3(modelView);
mat4 viewModel = inverse(modelView);
vec3 cameraPos3(viewModel[3]);
// Do not add scale if using this
//vec3 cameraPos4 = ExtractCameraPos_NoScale(modelView);
PrintVec3(cameraPos); printf(" - Base Camera\n");
PrintVec3(cameraPos2); printf(" - Plane intersection extraction\n");
PrintVec3(cameraPos2a); printf(" - Plane intersection extraction - optimize\n");
PrintVec3(cameraPos2b); printf(" - Plane intersection extraction alternate\n");
PrintVec3(cameraPos3); printf(" - Inverse Matrix\n");
return 0;
}`
from glm.
Related Issues (20)
- Trouble finding glm with cmake's find_package
- Vector GLM Logo HOT 1
- Quaternion constructor order doesn't depend on GLM_FORCE_QUAT_DATA_WXYZ HOT 1
- Defaulted functions causes initialization to 0 in constructor HOT 2
- No cmake install target HOT 1
- Swizzle support for CUDA HOT 4
- Disabled gtx/hash.hpp?
- Make HIP compatible HOT 3
- Quaternion order DOES matter, can we please have a macro for the order? HOT 3
- Update license date HOT 1
- glm::polar documentation question
- C++20 support for constexpr and SIMD simultaneously
- quaternion_exponential.inl appears to incorrectly calculate exp(q)
- License statement grammer issue. HOT 1
- orientate4 & orientate3 call yawPitchRoll with wrong parameter order
- CUDA compiler error in type_aligned.hpp HOT 1
- Unnecessary warnings from matrix_projection.inl HOT 1
- Why unit test "ext_matrix_clip_space.cpp" is not used?
- glm::intersectRayTriangle might be broken
- Undefined behaviour warning
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from glm.