Skip to content

Commit

Permalink
no glm
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Romain committed Aug 25, 2024
1 parent af4466b commit e9f3757
Showing 1 changed file with 144 additions and 10 deletions.
154 changes: 144 additions & 10 deletions src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,139 @@
#include "Frustum.h"

#include <Rcpp.h>
#include <cmath>

#include <GL/glu.h>

#include <cmath>
static bool InvertMatrix(const GLfloat m[16], GLfloat invOut[16])
{
GLfloat inv[16], det;
int i;

inv[0] = m[5] * m[10] * m[15] -
m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] +
m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] -
m[13] * m[7] * m[10];

inv[4] = -m[4] * m[10] * m[15] +
m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] -
m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] +
m[12] * m[7] * m[10];

inv[8] = m[4] * m[9] * m[15] -
m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] +
m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] -
m[12] * m[7] * m[9];

inv[12] = -m[4] * m[9] * m[14] +
m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] -
m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] +
m[12] * m[6] * m[9];

inv[1] = -m[1] * m[10] * m[15] +
m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] -
m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] +
m[13] * m[3] * m[10];

inv[5] = m[0] * m[10] * m[15] -
m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] +
m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] -
m[12] * m[3] * m[10];

inv[9] = -m[0] * m[9] * m[15] +
m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] -
m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] +
m[12] * m[3] * m[9];

inv[13] = m[0] * m[9] * m[14] -
m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] +
m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] -
m[12] * m[2] * m[9];

inv[2] = m[1] * m[6] * m[15] -
m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] +
m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] -
m[13] * m[3] * m[6];

inv[6] = -m[0] * m[6] * m[15] +
m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] -
m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] +
m[12] * m[3] * m[6];

#include <glm/glm.hpp>
#include <glm/gtc/matrix_inverse.hpp>
#include <glm/gtc/type_ptr.hpp>
inv[10] = m[0] * m[5] * m[15] -
m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] +
m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] -
m[12] * m[3] * m[5];

inv[14] = -m[0] * m[5] * m[14] +
m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] -
m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] +
m[12] * m[2] * m[5];

inv[3] = -m[1] * m[6] * m[11] +
m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] -
m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] +
m[9] * m[3] * m[6];

inv[7] = m[0] * m[6] * m[11] -
m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] +
m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] -
m[8] * m[3] * m[6];

inv[11] = -m[0] * m[5] * m[11] +
m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] -
m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] +
m[8] * m[3] * m[5];

inv[15] = m[0] * m[5] * m[10] -
m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] +
m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] -
m[8] * m[2] * m[5];

det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];

if (det == 0)
return false;

det = 1.0 / det;

for (i = 0; i < 16; i++)
invOut[i] = inv[i] * det;

return true;
}

Camera::Camera()
{
Expand Down Expand Up @@ -255,13 +380,22 @@ void Camera::look()
GLfloat viewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, viewMatrix);

glm::mat4 ConversionMatrix = glm::make_mat4(viewMatrix);
glm::vec4 cameraPos4 = glm::inverse(ConversionMatrix) * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
glm::vec3 CameraPos = glm::vec3(cameraPos4);
/*glm::mat4 ConversionMatrix = glm::make_mat4(viewMatrix);
glm::vec4 cameraPos4 = glm::inverse(ConversionMatrix) * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
glm::vec3 CameraPos = glm::vec3(cameraPos4);*/

/*GLfloat CameraPos[4];
CameraPos[0] = invMatrix[0] * 0.0f + invMatrix[4] * 0.0f + invMatrix[8] * 0.0f + invMatrix[12] * 1.0f;
CameraPos[1] = invMatrix[1] * 0.0f + invMatrix[5] * 0.0f + invMatrix[9] * 0.0f + invMatrix[13] * 1.0f;
CameraPos[2] = invMatrix[2] * 0.0f + invMatrix[6] * 0.0f + invMatrix[10] * 0.0f + invMatrix[14] * 1.0f;
CameraPos[3] = invMatrix[3] * 0.0f + invMatrix[7] * 0.0f + invMatrix[11] * 0.0f + invMatrix[15] * 1.0f;*/

GLfloat invMatrix[16];
InvertMatrix(viewMatrix, invMatrix);

x = CameraPos[0];
y = CameraPos[1];
z = CameraPos[2];
x = invMatrix[12];
y = invMatrix[13];
z = invMatrix[14];
}

bool Camera::see(float px, float py, float pz, float hsize)
Expand Down

0 comments on commit e9f3757

Please sign in to comment.