Coder Social home page Coder Social logo

Camera position extraction code about glm HOT 1 OPEN

g-truc avatar g-truc commented on August 15, 2024
Camera position extraction code

from glm.

Comments (1)

Groovounet avatar Groovounet commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.