# data[].attributes.body
glm::mat4 perspective(float fovy, float aspect, float near, float far) {

return glm::mat4({

1 / (aspect * (glm::tan(fovy / 2))), 0 , 0, 0

}, {

0, 1 / (glm::tan(fovy / 2)), 0, 0

}, {

0, 0, -1.0f * ((far + near) / (far - near)), -1

}, {

0, 0, -2 * ((far * near) / (far - near)), 0

});

}

int main()

{

float w = 780.0f;

float h = 750.0f;

float Fovy = 45.0f;

float NearZ = 0.1f;

float FarZ = 500.0f;

cout << to_string(perspective(glm::radians(Fovy), w / h, NearZ, FarZ)) << endl;

return 0;

}

# data[].attributes.body
Some of student's late lookAt() posting do not count.

# data[].attributes.body
glm::mat4 lookAt(glm::vec3 camPos, glm::vec3 lookat, glm::vec3 up)

{

//zc = lookat

glm::vec3 Zc = glm::normalize(camPos - lookat);

glm::vec3 Xc = glm::normalize(glm::cross(up, Zc));

glm::vec3 Yc = glm::normalize(glm::cross(Zc, Xc));

glm::mat4 res = {

{Xc.x, Yc.x, Zc.x, 0},

{Xc.y, Yc.y, Zc.y, 0},

{Xc.z, Yc.z, Zc.z, 0},

{glm::dot(-camPos, Xc), glm::dot(-camPos, Yc), glm::dot(-camPos, Zc),1}

};

return res;

}

int main()

{

glm::vec3 camPos{5, 5 , 5};

glm::vec3 lookat{0, 0, 0};

glm::vec3 up{0, 1, 0};

std::cout << glm::to_string(lookAt(camPos, lookat, up)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/mat4x4.hpp>
#include <iostream>
#include <glm/gtx/string_cast.hpp>

glm::mat4 lookAt(const glm::vec3& camPos, const glm::vec3& look, const glm::vec3& up)
{
	auto zc = glm::normalize(camPos - look);
	auto xc = glm::normalize(glm::cross(up, zc));
	auto yc = glm::normalize(glm::cross(zc, xc));
	auto negCamPos = -camPos;

	glm::mat4 rotation({
		xc.x, yc.x, zc.x, 0,
		xc.y, yc.y, zc.y, 0,
		xc.z, yc.z, zc.z, 0,
		0, 0, 0, 1
		});
	glm::mat4 translation({
		1, 0, 0, 0,
		0, 1, 0, 0,
		0, 0, 1, 0,
		negCamPos.x, negCamPos.y, negCamPos.z, 1,
		});

	return rotation * translation;
}

int main()
{
	std::cout << glm::to_string(lookAt(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0))) << std::endl;
	return 0;
}

# data[].attributes.body
glm::mat4 lookAt(const glm::vec3& campos, const glm::vec3 target, const glm::vec3& up) {
	const glm::vec3 lookDir = target - campos;

	const glm::vec3 Zc = glm::normalize(campos - target);
	const glm::vec3 Xc = glm::normalize(glm::cross(up, Zc));
	const glm::vec3 Yc = glm::normalize(glm::cross(Zc, Xc));

	const glm::mat4 tmR = {
		Xc.x, Yc.x, Zc.x, 0.0,
		Xc.y, Yc.y, Zc.y, 0.0,
		Xc.z, Yc.z, Zc.z, 0.0,
		0.0, 0.0, 0.0, 1.0
	};

	const glm::mat4 tmT = {
		1.0, 0.0, 0.0, 0.0,
		0.0, 1.0, 0.0, 0.0,
		0.0, 0.0, 1.0, 0.0,
		-campos.x, -campos.y, -campos.z, 1.0
	};


	return tmR * tmT;
}

int main()
{
	const glm::vec3 campos = { 5.0, 5.0, 5.0 };
	const glm::vec3 target = { 0.0, 0.0, 0.0 };
	const glm::vec3 up = { 0, 1.0, 0 };

	const auto res = lookAt(campos, target, up);

	std::cout << glm::to_string(res) << std::endl;

	return 0;
}

# data[].attributes.body
#include <iostream>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/mat4x4.hpp>

#include <glm/glm.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/gtx/string_cast.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far) {

glm::mat4 returnValue = {

{1 / (aspect * glm::tan(fovy / 2)), 0, 0, 0},

{0, 1 / glm::tan(fovy / 2), 0, 0},

{0, 0, -((far + near) / (far - near)), -1},

{0, 0, -(2 ((far near) / (far - near))), 0}

};

return returnValue;

}

int main() {

float w = 780.0f;

float h = 750.0f;

glm::mat4 perspectiveMatrix = perspective(glm::radians(45.0f), w / h, 0.1f, 500.0f);

std::cout << glm::to_string(perspectiveMatrix) << std::endl;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/transform.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

glm::mat4 res(0);

res[0] = glm::vec4(1.f / (aspect * tanf(glm::radians(fovy) / 2.f)), 0, 0, 0);

res[1] = glm::vec4(0, 1.f / (tanf(glm::radians(fovy) / 2)), 0, 0);

res[2] = glm::vec4(0, 0, -((far + near) / (far - near)), -1);

res[3] = glm::vec4(0, 0, -(2 (far near) / (far - near)), 0);

return res;

}

int main()

{

float H = 750;

float W = 780;

float FovY = 45.0f;

float NearZ = 0.1f;

float FarZ = 500.0f;

std::cout << glm::to_string(perspective(FovY, W / H, NearZ, FarZ)) << std::endl;

return 0;

}

# data[].attributes.body
#include <iostream>

#include <glm/glm.hpp>

#include "CMakeProject2.h"

#include <glm/ext/matrix_transform.hpp>

#include <glm/gtx/string_cast.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

 float rho = glm::tan(glm::radians(fovy / 2));

 glm::mat4 perspectiveProjectionMatrix = { // Column-wise

 {1.f / (aspect * rho), 0, 0, 0},

 {0, 1.f / rho, 0, 0},

 {0, 0, -(far + near) / (far - near), -1},

 {0, 0, -2 ((far near) / (far - near)), 0}

 };

 return perspectiveProjectionMatrix;

}

void main()

{

 float width = 780;

 float height = 750;

 float fovY = 45.f;

 float nearZ = 0.1f;

 float farZ = 500.0f;

 std::cout << glm::to_string(perspective(fovY, width / height, nearZ, farZ)) << std::endl;

 return;

}

# data[].attributes.body
#include <glm/glm.hpp>
#include <glm/matrix.hpp>
#include <glm/gtx/string_cast.hpp>
#include <iostream>

glm::mat4 perspective(float fovy, float aspect, float near, float far)
{
	glm::mat4 mat = glm::zero<glm::mat4>();

	mat[0][0] = 1 / (aspect * glm::tan(fovy / 2));
	mat[1][1] = 1 / glm::tan(fovy / 2);
	mat[2][2] = -1 * ((far + near) / (far - near));
	mat[2][3] = -1;
	mat[3][2] = -2 * ((far * near) / (far - near));
	return mat;
}

int main()
{
	int w = 780;
	int h = 750;
	float fovy = 45.0f;
	float nearZ = 0.1f;
	float farZ = 500.0f;

	glm::mat4 viewMatrix = perspective(glm::radians(fovy), w / (h * 1.0f), nearZ, farZ);

	std::cout << glm::to_string(viewMatrix) << std::endl;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/glm.hpp>

#include <iostream>

#include <glm/gtx/string_cast.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

glm::mat4 res(0.0f);

res[0][0] = 1 / (aspect * std::tan(fovy / 2.f));

res[1][1] = 1 / (std::tan(fovy / 2.f));

res[2][2] = -(far + near) / (far - near);

res[3][2] = -(2 ((far near) / (far - near)));

res[2][3] = -1;

return res;

}

int main(void)

{

float w = 780;

float h = 750;

float fovY = 45.0f;

float NearZ = 0.1f;

float FarZ = 500.0f;

float aspect = (w / h);

glm::mat4 res = perspective(glm::radians(fovY), aspect, NearZ, FarZ);

std::cout << "res " << to_string(res) << std::endl;

}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/gtc/type_ptr.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat3x3.hpp>

#include <glm/mat3x2.hpp>

const float DEGREES_TO_RADIANS = glm::pi <float>() / 180;

const float RADIANS_TO_DEGREES = 180 / glm::pi <float>();

static glm::mat4 perspective(float fovy, float aspect, float near, float far) {

float tf = 1.0f / glm::tan(fovy / 2.0f);

glm::vec4 P(tf / aspect, 0.0f, 0.0f, 0.0f);

glm::vec4 S(0.0f, tf, 0.0f, 0.0f);

glm::vec4 T(0.0f, 0.0f, -((far + near) / (far - near)), -(2.0f ((far near) / (far - near))));

glm::vec4 Q(0.0f, 0.0f, -1.0f, 0.0f);

glm::mat4 res;

res[0] = P;

res[1] = S;

res[2] = T;

res[3] = Q;

return res;

}

static int MainEx() {

float aspect = 780 / 750;

float FovY = 45.0f;

float NearZ = 0.1f;

float FarZ = 500.0f;

std::cout << glm::to_string(perspective(glm::radians(FovY), aspect, NearZ, FarZ)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/string_cast.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

glm::mat4 result(0.0f);

result[0][0] = 1.0f / (aspect * glm::tan(fovy / 2.0f));

result[1][1] = 1.0f / glm::tan(fovy / 2.0f);

result[2][2] = -(far + near) / (far - near);

result[2][3] = -1.0f;

result[3][2] = -(2.0f far near) / (far - near);

std::cout << glm::to_string(result) << std::endl;

return result;

}

int main()

{

float w = 780.0f;

float h = 750.0f;

float fovY = 45.0f;

float nearZ = 0.1f;

float farZ = 500.0f;

glm::mat4 p = perspective(glm::radians(fovY), w / h, nearZ, farZ);

return 0;

}

# data[].attributes.body
// CMakeProject2.cpp : définit le point d'entrée de l'application.

//

#define GLM_ENABLE_EXPERIMENTAL

#define GLM_FORCE_SWIZZLE

#include "glm/gtx/string_cast.hpp"

#include "CMakeProject2.h"

#include "glm/glm.hpp"

#include <glm/gtc/constants.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {

glm::vec3 Zc = glm::normalize(campos - look);

glm::vec3 Xc = glm::normalize(glm::cross(up, Zc));

glm::vec3 Yc = glm::normalize(glm::cross(Zc, Xc));

glm::mat4 res = glm::mat4(glm::vec4(Xc.x, Yc.x,Zc.x, 0), glm::vec4(Xc.y, Yc.y,Zc.y, 0), glm::vec4(Xc.z, Yc.z,Zc.z, 0), glm::vec4(-glm::dot(Xc, campos), -glm::dot(Yc, campos), -glm::dot(Zc, campos), 1));

return(res);

}

glm::mat4 perspective(float fovy, float aspect, float near, float far) {

glm::mat4 res = glm::mat4(glm::vec4(1 / glm::dot(aspect, glm::tan(fovy / 2)), 0, 0, 0), glm::vec4(0, 1 / glm::tan(fovy / 2), 0, 0), glm::vec4(0, 0, -((far + near) / (far - near)), -1), glm::vec4(0, 0, -(2 (far near) / (far - near)), 0));

return (res);

}

int main()

{

glm::vec3 cpos(5, 5, 5);

glm::vec3 lookat(0,0,0);

glm::vec3 up(0,1,0);

std::cout << glm::to_string(perspective(glm::radians(45.0f), 780.f/750.f, 0.1f, 500.0f));

return 0;

}

# data[].attributes.body
glm::mat4 viewMatrix(float fovy, float aspect, float zNear, float zFar) {
	return glm::mat4{
		1 / (aspect * tan(fovy / 2)), 0, 0, 0,
		0, 1 / tan(fovy / 2), 0, 0,
		0 , 0, -1 * ((zFar + zNear) / (zFar - zNear)), -1,
		0, 0,  -2 * ((zFar * zNear) / (zFar - zNear)), 0
	};
}

int main()
{
	//glm::vec3 camPos = { 5.0, 5.0, 5.0 };
	//glm::vec3 lookat = { 0, 0, 0 };
	//glm::vec3 up = { 0, 1.0, 0 };
	float fovY = 45.0f;
	float nearZ = 0.1f;
	float farZ = 500.0f;
	float w = 780;
	float h = 750;

	//std::cout << glm::to_string(lookAt(camPos, lookat, up)) << std::endl;
	std::cout << glm::to_string(viewMatrix(glm::radians(fovY), w / h, nearZ, farZ)) << std::endl;
	return 0;
}

# data[].attributes.body
glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

glm::mat4 result{{1/(aspect * tan(fovy/2)), 0 , 0 , 0},

{0, 1/ glm::tan(fovy/2), 0 , 0},

{0 , 0 , -((far + near)/(far - near)) , -1},

{0, 0, -(2*((far * near)/(far - near))),0}};

return result;

}

int main()

{

float w = 780;

float h = 750;

float FovY = 45.0f;

float nearZ = 0.1f;

float FarZ = 500.0f;

std::cout << glm::to_string(perspective(glm::radians(FovY), (w / h), nearZ, FarZ)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/mat4x4.hpp>
#include <iostream>
#include <glm/gtx/string_cast.hpp>

const double DEGREES_TO_RADIANS = glm::pi<float>() / 180.0;

glm::mat4 perspective(float fovy, float aspect, float near, float far)
{
	return glm::mat4({
		1 / (aspect * glm::tan(fovy / 2)), 0, 0, 0,
		0, 1 / glm::tan(fovy / 2), 0, 0,
		0, 0, - (far + near) / (far - near), -1,
		0, 0,  -2 * (far * near / (far - near)), 0
	});
}

int main()
{
	float width = 780;
	float height = 750;

	std::cout << glm::to_string(perspective(45 * DEGREES_TO_RADIANS, width / height, 0.1f, 500)) << std::endl;
	return 0;
}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>

#include <glm/glm.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/mat3x3.hpp>

#include <glm/glm.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <iostream>

#include "ComputerGraphicsLecture2.h"

glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

float a = 1 / (glm::dot(aspect, tan(fovy / 2)));

float b = 1 / tan(fovy/2);

float c = -((far + near) / (far - near));

float d = -(2 ((far near) / (far - near)));

glm::vec4 c1(a, 0 , 0, 0);

glm::vec4 c2(0, b, 0, 0);

glm::vec4 c3(0, 0, c, -1);

glm::vec4 c4(0, 0, d, 0);

glm::mat4 m;

m[0] = c1;

m[1] = c2;

m[2] = c3;

m[3] = c4;

return m;

}

int main()

{

std::cout << glm::to_string(perspective(glm::radians(45.0f), 780/750, 0.1f, 500.0f)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/vec3.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/mat4x4.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far) {

glm::mat4 persp(0.f);

persp[0] = glm::vec4(1.0f / (aspect * glm::tan(fovy / 2.0f)), 0, 0, 0);

persp[1] = glm::vec4(0, 1.0f / (glm::tan(fovy / 2.0f)), 0, 0);

persp[2] = glm::vec4(0, 0, -1 ((far + near) / (far - near)), (-1 (2 (far near) / (far - near))));

persp[3] = glm::vec4(0, 0, -1, 0);

return persp;

}

int main() {

float W = 780;

float H = 750;

float FovY = 45.0f;

float NearZ = 0.1f;

float FarZ = 500.0f;

std::cout << glm::to_string(perspective(glm::radians(FovY), W / H, NearZ, FarZ)) << std::endl;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>

#include <glm/gtx/string_cast.hpp>
#include <glm/gtc/constants.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far) {
	return {
		{ 1 / glm::dot(aspect, glm::tan(fovy / 2)), 0, 0, 0  },
		{ 0, 1 / glm::tan(fovy / 2), 0, 0 },
		{ 0, 0, -((far + near) / (far - near)), -1 },
		{ 0, 0, -(2 * ((far * near) / (far - near))), 0}
	};
}

int main() {

	float width = 780.0f;
	float height = 750.0f;
	float FovY = 45.0f;
	float NearZ = 0.1f;
	float FarZ = 500.0f;

	glm::mat4 perspectiveMatrix = perspective(glm::radians(FovY), width / height, NearZ, FarZ);
	std::cout << glm::to_string(perspectiveMatrix) << std::endl;
	return 0;
}

# data[].attributes.body
glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

glm::mat4 mtx{};

mtx[0] = {1.0F/(aspect * glm::tan(glm::radians(fovy)/2.0F)), 0, 0, 0};

mtx[1] = { 0, 1.0F / (glm::tan(glm::radians(fovy) / 2.0F)), 0, 0 };

mtx[2] = { 0, 0, -((far + near) / (far - near)), -1 };

mtx[3] = { 0, 0, -(2 ((far near) / (far - near))), 0 };

return mtx;

}

# data[].attributes.body
glm::mat4 Perspective(float fovy, float aspect, float near, float far)

{

glm::mat4 perspecticeMatrixProjection(0.f);

perspecticeMatrixProjection[0][0] = 1 / (aspect * glm::tan(fovy / 2));

perspecticeMatrixProjection[1][1] = 1 / (glm::tan(fovy / 2));

perspecticeMatrixProjection[2][2] = (far + near) / (far - near) * -1;

perspecticeMatrixProjection[3][2] = (far near) / (far - near) 2 * -1;

perspecticeMatrixProjection[2][3] = -1;

std::cout << "Perspective Matrix : " << glm::to_string(perspecticeMatrixProjection) << std::endl;

return perspecticeMatrixProjection;

}

int main()

{

float w = 780;

float h = 750;

float aspect = w / h;

float FovY = 45.0f;

float NearZ = 0.1f;

float FarZ = 500.0f;

glm::mat4 perspectiveRes = Perspective(glm::radians(FovY), aspect, NearZ, FarZ);

return 0;

}

# data[].attributes.body
glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

float f = 1.0f / tan(fovy / 2.0f);

glm::mat4 proj = glm::mat4(0.0f);

proj[0] = glm::vec4(1.0f / glm::dot(aspect, glm::tan(fovy / 2.0f)), 0, 0, 0);

proj[1] = glm::vec4(0, f, 0, 0);

proj[2] = glm::vec4(0, 0, (far + near) / (near - far), -1);

proj[3] = glm::vec4(0, 0, (2 far near) / (near - far), 0);

return proj;

}

int ex2()

{

glm::mat4 proj = perspective(glm::radians(45.0f), 780.0f / 750.0f, 0.1f, 500.0f);

std::cout << to_string(proj) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/string_cast.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

glm::mat4 result(0.0f);

result[0][0] = 1.0f / (aspect * glm::tan(fovy / 2.0f));

result[1][1] = 1.0f / glm::tan(fovy / 2.0f);

result[2][2] = -(far + near) / (far - near);

result[2][3] = -1.0f;

result[3][2] = -(2.0f far near) / (far - near);

std::cout << glm::to_string(result) << std::endl;

return result;

}

int main()

{

float w = 780.0f;

float h = 750.0f;

float fovY = 45.0f;

float nearZ = 0.1f;

float farZ = 500.0f;

glm::mat4 p = perspective(glm::radians(fovY), w / h, nearZ, farZ);

return 0;

}

# data[].attributes.body
#include <iostream>

#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far) {
    glm::mat4 m(0.0f);
    float ratio = tan((fovy * (M_PI / 180.f)) / 2.0f);

    m[0][0] = 1 / (aspect * ratio);
    m[1][1] = 1 / ratio;
    m[2][2] = -(far + near)/(far - near);
    m[2][3] = -1.f;
    m[3][2] = -(2 * far * near)/(far - near);

    return m;
}

int main(void) {
    glm::mat4 m = perspective(45.0f, 780.f / 750.f, 0.1f, 500.f);

    std::cout << glm::to_string(m) << std::endl;

    return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <glm/gtx/string_cast.hpp>


glm::mat4 perspective(float fovy, float aspect, float zNear, float zFar) {
    glm::mat4 perspectiveMatrix(0.0f);
    perspectiveMatrix[0][0] = 1.0f / (aspect * tan(glm::radians(fovy) / 2.0f));
    perspectiveMatrix[1][1] = 1.0f / tan(glm::radians(fovy) / 2.0f);
    perspectiveMatrix[2][2] = -(zFar + zNear) / (zFar - zNear);
    perspectiveMatrix[2][3] = -1.0f;
    perspectiveMatrix[3][2] = -(2.0f * zFar * zNear) / (zFar - zNear);

    return perspectiveMatrix;
}


int main() {
    std::cout << to_string(perspective(45.0f, (780.0f / 750.0f), 0.1f, 500.0f)) << std::endl;
    return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/vec3.hpp>
#include <glm/gtx/string_cast.hpp>
#include <iostream>

#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>

static glm::mat4 perspective(float fovy, float aspect, float near, float far)
{
	glm::mat4 per = {
		{ 1 / glm::dot(aspect, glm::tan(fovy / 2)), 0, 0, 0 },
		{ 0, 1 / glm::tan(fovy / 2), 0, 0 },
		{ 0, 0, -((far + near) / (far - near)), -1 },
		{ 0, 0, -(2 * ((far * near) / (far - near))), 0}
	};

	return per;
}

int main()
{
	float width = 780;
	float height = 750;
	float fovy = 45.0f;
	float near = 0.1f;
	float far = 500.0f;

	std::cout << glm::to_string(perspective(glm::radians(fovy), width / height, near, far)) << std::endl;

	return 0;
}

# data[].attributes.body
glm::mat4 perspective(float fovy, float aspect, float near, float far) {
	const float tanFovY_2 = std::tan(fovy / 2.0f);

	const glm::mat4 matrix{
		1.0f / (aspect * tanFovY_2), 0.f, 0.f, 0.f,
		0.f, 1.0f / tanFovY_2, 0.f, 0.f,
		0.f, 0.f, (near + far) / (near - far), -1.0f,
		0.f, 0.f, 2 * (near * far) / (near - far), 0.f
	};

	return matrix;
}

int main()
{
	const auto W = 780.f;
	const auto H = 750.f;
	const auto FovY = 45.0f * 3.14159f / 180.0f;
	const auto NearZ = 0.1f;
	const auto FarZ = 500.0f;

	const auto matrix = perspective(FovY, W / H, NearZ, FarZ);

	std::cout << glm::to_string(matrix) << std::endl;

	return 0;
}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/mat4x4.hpp>

const double DEG_TO_RAD = glm::pi<double>() / 180.0;

const double RAD_TO_DEG = 180.0 / glm::pi<double>();

glm::mat4 perpective(float fovy, float aspect, float near, float far)

{

fovy = fovy * DEG_TO_RAD;

float f = 1.0f / glm::tan(fovy * 0.5f);

float nf = 1.0f / (near - far);

return glm::mat4(f / aspect, 0.0f, 0.0f, 0.0f,

0.0f, f, 0.0f, 0.0f,

0.0f, 0.0f, (far + near) nf, (2.0f far * near) / (near - far),

0.0f, 0.0f, -1.0f, 0.0f);

}

int main()

{

// W = 780 H = 750 FovY = 45.0f NearZ = 0.1f; FarZ = 500.0f;

float W = 780.0f;

float H = 750.0f;

float FovY = 45.0f;

float NearZ = 0.1f;

float FarZ = 500.0f;

glm::mat4 proj = perpective(FovY, W / H, NearZ, FarZ);

std::cout << glm::to_string(proj) << std::endl;

return 0;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <glm/gtx/string_cast.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far)
{
  float f = 1.0f / tan(fovy / 2.0f);
  float nf = 1.0f / (near - far);

  glm::mat4 proj = glm::mat4(0.0f);
  proj[0][0] = f / aspect;
  proj[1][1] = f;
  proj[2][2] = (far + near) * nf;
  proj[2][3] = -1.0f;
  proj[3][2] = 2.0f * far * near * nf;

  return proj;
}

int main(void)
{
  float fovy = 45.0f;
  float near = 0.1f;
  float far = 500.0f;
  float aspect = (float)780 / (float)750;

  fovy = glm::radians(fovy);
  glm::mat4 proj = perspective(fovy, aspect, near, far);

  std::cout << glm::to_string(proj) << std::endl;

  return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <iostream>

#define PI 3.14159265358979323846

#define DEG2RAD(x) ((x) * (PI / 180.f))

template<typename T>

void display(T x, const std::string& prefix = "") {

std::cout << prefix << glm::to_string(x) << std::endl;

}

glm::mat4 perspective(float fovy, float aspect, float near, float far) {

float fovyRad = DEG2RAD(fovy);

glm::mat4 perspectiveMatrix(

1.f / (aspect * tanf(fovyRad / 2.f)), 0, 0, 0,

0, 1.f / tanf(fovyRad / 2.f), 0, 0,

0, 0, -(far + near)/(far - near), -1,

0, 0, -(2.f ((far near)/(far - near))), 0

);

return perspectiveMatrix;

}

int main(void) {

const float W = 780.f;

const float H = 750.f;

float fovY = 45.f;

float nearZ = .1f;

float farZ = 500.f;

glm::mat4 perspectiveMatrix = perspective(fovY, W / H, nearZ, farZ);

display(perspectiveMatrix, "PerspectiveMatrix= ");

return 0;

}

# data[].attributes.body
glm::mat4 perspective(float fovy, float aspect, float near, float far) {

glm::mat4 res = {

{1 / (aspect * tan(fovy / 2)), 0, 0, 0},

{0, 1 / tan(fovy / 2), 0, 0},

{0, 0, -((far + near) / (far - near)), -1 },

{0, 0, -(2 ((far near) / (far - near))), 0}

};

return res;

}

int main() {

glm::to_string(perspective(glm::radians(45.0f), 780.0f/750.0f, 0.1f, 500.0f)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/gtc/constants.hpp>

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far) {

glm::mat4 pers {

{ 1 / (aspect * std::tan(fovy / 2)), 0, 0, 0},

{ 0, 1 / std::tan(fovy / 2), 0, 0},

{ 0, 0, -(far + near) / (far - near), -1},

{ 0, 0, -2.0f (far near/(far - near)), 0},

};

return pers;

}

int main() {

std::cout << glm::to_string(perspective(glm::radians(45.0f), 780.0f/750.0f, 0.1f, 500.0f)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include "CMakeProject1.h"

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/mat4x4.hpp>

#include <iostream>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

using namespace std;

glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

float f = 1.0f / tan(fovy / 2.0f);

glm::mat4 m(0.0f);

m[0][0] = f / aspect;

m[1][1] = f;

m[2][2] = (far + near) / (near - far);

m[2][3] = -1.0f;

m[3][2] = (2.0f far near) / (near - far);

return m;

}

int main()

{

const float DEGREES_TO_RADIANS = glm::pi<float>() / 180.0;

const float RADIANS_TO_DEGREES = 180.0 / glm::pi<float>();

float W = 780.0f;

float H = 750.0f;

float FovY = 45.0f;

float NearZ = 0.1f;

float FarZ = 500.0f;

float Aspect = W / H;

glm::mat4 projection = perspective(FovY * DEGREES_TO_RADIANS, Aspect, NearZ, FarZ);

std::cout << "perspective: " << glm::to_string(projection) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>

#include <glm/glm.hpp>

#include <iostream>

glm::mat4 Perspective(float fovy, float aspect, float zNear, float zFar)

{

const float tanHalfFovy = tan(fovy / 2.f);

glm::mat4 result(0.f);

result[0][0] = 1.f / (aspect * tanHalfFovy);

result[1][1] = 1.f / (tanHalfFovy);

result[2][2] = -((zFar + zNear) / (zFar - zNear));

result[2][3] = -1.f;

result[3][2] = -((2.0f ((zFar zNear) / (zFar - zNear))));

return result;

}

int main()

{

float width = 780.f;

float height = 750.f;

float fovY = 45.f;

float nearZ = 0.1f;

float farZ = 500.f;

glm::mat4 proj = Perspective(glm::radians(fovY), width / height, nearZ, farZ);

std::cout << glm::to_string(proj) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/string_cast.hpp>
#include <iostream>
#include <glm/glm.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far)
{
	float f = 1.0f / glm::tan(fovy / 2.0f);
	glm::mat4 proj = glm::mat4(1.0f);
	proj[0][0] = f / aspect;
	proj[1][1] = f;
	proj[2][2] = -(far + near) / (near - far);
	proj[2][3] = -1.0f;
	proj[3][2] = -(2.0f * far * near) / (near - far);
	proj[3][3] = 0.0f;
	return proj;

}

int main()
{
	float width = 780;
	float height = 750;
	float fovy = 45.0f;
	float aspect = width / height;
	float near = 0.1f;
	float far = 500.0f;
	glm::mat4 proj = perspective(glm::radians(fovy), aspect, near, far);
	std::cout << glm::to_string(proj) << std::endl;
	return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/mat4x4.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far){

float f = 1.0f / glm::tan(fovy / 2.0f);

glm::mat4 proj(

glm::vec4(f / aspect, 0, 0, 0),

glm::vec4(0, f, 0, 0),

glm::vec4(0, 0, - (far + near) / (far - near), -1),

glm::vec4(0, 0, -((2 far near) / (far - near)), 0)

);

return proj;

}

int main() {

float fovy = glm::radians(45.0f);

float aspect = 4.0f / 3.0f;

float near = 0.1f;

float far = 100.0f;

glm::mat4 proj = perspective(fovy, aspect, near, far);

std::cout << glm::to_string(proj) << std::endl;

return 0;

}

# data[].attributes.body
glm::mat4 perspective(float fovy, float aspect, float near, float far) {
  glm::mat4 proj(1.0f);
  proj[1][1] = 1.0f / std::tan(fovy / 2.0f);
  proj[0][0] = proj[1][1] / aspect;
  proj[2][2] = (far + near) / (near - far);
  proj[2][3] = 2.0f * far * near / (near - far);
  proj[3][2] = -1.0f;
  proj[3][3] = 0.0f;
  return proj;
}

int main() {
  float W = 780;
  float H = 750;
  float FovY = 45.0f;
  float NearZ = 0.1f;
  float FarZ = 500.0f;

  auto aspect = W / H;
  auto proj = perspective(glm::radians(FovY), aspect, NearZ, FarZ);
  std::cout << "perspective matrix:\n" << glm::to_string(proj) << std::endl;
  return 0;
}

# data[].attributes.body
static glm::mat4 Perspective(float fovy, float aspect, float near, float far)

{

float f = 1.0f / tan(glm::radians(fovy) / 2.0f);

glm::mat4 perspective = {

f / aspect, 0, 0, 0,

0, f, 0, 0,

0, 0, (far + near) / (near - far), -1,

0, 0, (2 far near) / (near - far), 0

};

return perspective;

}

int main()

{

float nearz = 0.1f;

float farz = 500.0f;

float fovy = 45.0f;

float aspect = 780.0f / 750.0f;

Perspective(fovy, aspect, nearz, farz);

std::cout << "Perspective" << std::endl;

std::cout << to_string(Perspective(fovy, aspect, nearz, farz)) << std::endl << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/glm.hpp>

#include <iostream>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/string_cast.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

float f = 1.0f / glm::tan(fovy / 2.0f);

glm::mat4 projectionMatrix = glm::mat4(1.0f);

projectionMatrix[0][0] = f / aspect;

projectionMatrix[1][1] = f;

projectionMatrix[2][2] = (far + near) / (near - far);

projectionMatrix[2][3] = -1.0f;

projectionMatrix[3][2] = (2.0f far near) / (near - far);

return projectionMatrix;

}

int main()

{

int width = 780;

int height = 750;

glm::mat4 perspectiveMatrix = perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 500.0f);

std::cout << glm::to_string(perspectiveMatrix) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <iostream>

glm::mat4 perspective(float fovy, float aspect, float near, float far) {
    float f = 1.0f / glm::tan(fovy / 2.0f);

    return glm::mat4(
        glm::vec4(f / aspect, 0, 0, 0),
        glm::vec4(0, f, 0, 0),
        glm::vec4(0, 0, (far + near) / (near - far), -1),
        glm::vec4(0, 0, (2 * far * near) / (near - far), 0)
    );
}

int main() {
    float W = 780;
    float H = 750;
    float FovY = 45.0f;
    float NearZ = 0.1f;
    float FarZ = 500.0f;

    glm::mat4 proj = perspective(glm::radians(FovY), W / H, NearZ, FarZ);

    std::cout << glm::to_string(proj) << std::endl;

    return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <glm/gtx/string_cast.hpp>

glm::mat4 perspective(float fovy, float aspect, float near, float far) {
	glm::mat4 perspectiveMatrix;
	perspectiveMatrix[0] = { 1.0f / (aspect * tan(glm::radians(fovy) / 2.0f)), 0.0f, 0.0f, 0.0f };
	perspectiveMatrix[1] = { 0.0f, 1.0f / tan(glm::radians(fovy) / 2.0f), 0.0f, 0.0f };
	perspectiveMatrix[2] = { 0.0f, 0.0f, -(far + near) / (far - near), -1.0f };
	perspectiveMatrix[3] = { 0.0f, 0.0f, -(2.0f * far * near / (far - near)), 0.0f };
	return perspectiveMatrix;
}

int main()
{
	float W = 780;
	float H = 750;
	float FovY = 45.0f;
	float NearZ = 0.1f;
	float FarZ = 500.0f;


	std::cout << glm::to_string(perspective(FovY, W / H, NearZ, FarZ)) << std::endl;
	return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <glm/gtx/string_cast.hpp>


glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {
    glm::vec3 zaxis = glm::normalize(campos - look);
    glm::vec3 xaxis = glm::normalize(glm::cross(glm::normalize(up), zaxis));
    glm::vec3 yaxis = glm::cross(zaxis, xaxis);

    glm::mat4 viewMatrix(1.0f);

    viewMatrix[0][0] = xaxis.x;
    viewMatrix[1][0] = xaxis.y;
    viewMatrix[2][0] = xaxis.z;

    viewMatrix[0][1] = yaxis.x;
    viewMatrix[1][1] = yaxis.y;
    viewMatrix[2][1] = yaxis.z;

    viewMatrix[0][2] = zaxis.x;
    viewMatrix[1][2] = zaxis.y;
    viewMatrix[2][2] = zaxis.z;

    viewMatrix[3][0] = -glm::dot(xaxis, campos);
    viewMatrix[3][1] = -glm::dot(yaxis, campos);
    viewMatrix[3][2] = -glm::dot(zaxis, campos);

    return viewMatrix;
}

int main() {
    glm::vec3 camPos = glm::vec3(5, 5, 5);
    glm::vec3 lookat = glm::vec3(0, 0, 0);
    glm::vec3 up = glm::vec3(0, 1, 0);

    std::cout << to_string(lookAt(camPos, lookat, up)) << std::endl;

    return 0;
}

# data[].attributes.body
glm::mat4 perspective(float fovy, float aspect, float near, float far)

{

return glm::mat4{

{1.0f / (aspect * tan(fovy / 2.0f)), 0.0f, 0.0f, 0.0f},

{0.0f, 1.0f / tan(fovy / 2.0f), 0.0f, 0.0f},

{0.0f, 0.0f, (far + near) / (near - far), -1.0f},

{0.0f, 0.0f, (2.0f far near) / (near - far), 0.0f}

};

}

int main()

{

glm::mat4 m = perspective(glm::radians(45.0f), 780.0f / 750.0f, 0.1f, 500.0f);

std::cout << glm::to_string(m) << std::endl;

return 0;

}

# data[].attributes.body
glm::mat4 lookAt(const glm::vec3 &campos, const glm::vec3 &look, const glm::vec3 &up) {
    glm::vec3 Zc = glm::normalize(campos - look);
    glm::vec3 Xc = glm::normalize(glm::cross(up, Zc));
    glm::vec3 Yc = glm::normalize(glm::cross(Zc, Xc));

    return glm::mat4(
        glm::vec4(Xc.x, Yc.x, Zc.x, 0),
        glm::vec4(Xc.y, Yc.y, Zc.y, 0),
        glm::vec4(Xc.z, Yc.z, Zc.z, 0),
        glm::vec4(-glm::dot(Xc, campos), -glm::dot(Yc, campos), -glm::dot(Zc, campos), 1)
    );
}


int main() {
    glm::vec3 cpos(5, 5, 5);
    glm::vec3 lookat(0, 0, 0);
    glm::vec3 up(0, 1, 0);

    std::cout << glm::to_string(lookAt(cpos, lookat, up)) << std::endl;

    return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <glm/gtx/string_cast.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)
{
  glm::mat4 result(1.0f);
  glm::vec3 z = glm::normalize(campos - look);
  glm::vec3 x = glm::normalize(glm::cross(up, z));
  glm::vec3 y = glm::cross(z, x);

  glm::mat4 translation = glm::translate(glm::mat4(1.0f), -campos);
  glm::mat4 rotation = glm::mat4(1.0f);
  rotation[0] = glm::vec4(x, 0);
  rotation[1] = glm::vec4(y, 0);
  rotation[2] = glm::vec4(z, 0);
  rotation = glm::transpose(rotation);


  result = rotation * translation;
  return result;
}
int main(void)
{

  glm::vec3 camPos(5, 5, 5);
  glm::vec3 lookat(0, 0, 0);
  glm::vec3 up(0, 1, 0);

  std::cout << glm::to_string(lookAt(camPos, lookat, up)) << std::endl;
  return 0;
}

# data[].attributes.body
// CMakeProject2.cpp : définit le point d'entrée de l'application.

//

#define GLM_ENABLE_EXPERIMENTAL

#define GLM_FORCE_SWIZZLE

#include "glm/gtx/string_cast.hpp"

#include "CMakeProject2.h"

#include "glm/glm.hpp"

#include <glm/gtc/constants.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {

glm::vec3 Zc = glm::normalize(campos - look);

glm::vec3 Xc = glm::normalize(glm::cross(up, Zc));

glm::vec3 Yc = glm::normalize(glm::cross(Zc, Xc));

glm::mat4 res = glm::mat4(glm::vec4(Xc.x, Yc.x,Zc.x, 0), glm::vec4(Xc.y, Yc.y,Zc.y, 0), glm::vec4(Xc.z, Yc.z,Zc.z, 0), glm::vec4(-glm::dot(Xc, campos), -glm::dot(Yc, campos), -glm::dot(Zc, campos), 1));

return(res);

}

int main()

{

glm::vec3 cpos(5, 5, 5);

glm::vec3 lookat(0,0,0);

glm::vec3 up(0,1,0);

std::cout << glm::to_string(lookAt(cpos, lookat, up));

return 0;

}

# data[].attributes.body
#include <iostream>

#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>
#include <glm/mat4x4.hpp>
#include <glm/vec3.hpp>

glm::mat4 lookAt(const glm::vec3 &cameraPos, const glm::vec3 &target, const glm::vec3 &up) {
    glm::vec3 Z = glm::normalize(cameraPos - target);
    glm::vec3 X = glm::normalize(glm::cross(up, Z));
    glm::vec3 Y = glm::normalize(glm::cross(Z, X));

    return glm::translate(glm::mat4(glm::transpose(glm::mat3(X, Y, Z))), -cameraPos);
}

int main(void) {
    glm::mat4 m = lookAt(glm::vec3(5.f, 5.f, 5.f), glm::vec3(), glm::vec3(0.f, 1.f, 0.f));

    std::cout << glm::to_string(m) << std::endl;

    return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/string_cast.hpp>

glm::mat4 lookAt(glm::vec3 camPos, glm::vec3 look, glm::vec3 up)

{

const glm::vec3 w{ glm::normalize(camPos - look) };

const glm::vec3 u{ glm::normalize(glm::cross(up, w)) };

const glm::vec3 v{ glm::cross(w, u) };

return glm::mat4{

{u.x, v.x, w.x, 0.f},

{u.y, v.y, w.y, 0.f},

{u.z, v.z, w.z, 0.f},

{glm::dot(u, camPos), glm::dot(v, camPos), glm::dot(w, camPos), 1.f}

};

}

int main()

{

glm::mat4 m = lookAt(glm::vec3(5.0f, 5.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

std::cout << glm::to_string(m) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL
#include<iostream>
#include<glm/glm.hpp>
#include<glm/gtx/transform.hpp>
#include<glm/gtx/matrix_transform_2d.hpp>
#include <glm/gtx/string_cast.hpp>

glm::mat4 lookAt(glm::vec3 camPos, glm::vec3 lookat, glm::vec3 up) {
	glm::vec3 zc = glm::normalize(camPos - lookat);
	glm::vec3 xc = glm::normalize(glm::cross(up, zc));
	glm::vec3 yc = glm::normalize(glm::cross(zc, xc));

	glm::mat4 rotation(1);
	rotation[0] = { xc[0], yc[0], zc[0], 0 };
	rotation[1] = { xc[1], yc[1], zc[1], 0 };
	rotation[0] = { xc[2], yc[2], zc[2], 0 };

	glm::mat4 translation(1);
	translation = glm::translate(translation, -camPos);
	return rotation * translation;
}

int main()
{
	glm::vec3 camPos = { 5.0, 5.0, 5.0 };
	glm::vec3 lookat = { 0, 0, 0 };
	glm::vec3 up = { 0, 1.0, 0 };

	std::cout << glm::to_string(lookAt(camPos, lookat, up)) << std::endl;
	return 0;
}

# data[].attributes.body
glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {
    glm::mat4 result;
    glm::vec3 z = glm::normalize(campos - look);
    glm::vec3 x = glm::normalize(glm::cross(up, z));
    glm::vec3 y = glm::normalize(glm::cross(z, x));

    result[0] = glm::vec4(x.x, y.x, z.x, 0.0f);
    result[1] = glm::vec4(x.y, y.y, z.y, 0.0f);
    result[2] = glm::vec4(x.z, y.z, z.z, 0.0f);
    result[3] = glm::vec4(-glm::dot(x, campos), -glm::dot(x, campos), -glm::dot(z, campos), 1.0f);
    return result;
}

# data[].attributes.body
#include <iostream>

#include <glm/glm.hpp>

# define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/mat4x4.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/transform.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {

glm::mat4 rotation;

glm::mat4 translation(1.0f);

glm::vec3 Z = glm::normalize(campos - look);

glm::vec3 X = glm::normalize(glm::cross(up, Z));

glm::vec3 Y = glm::normalize(glm::cross(Z, X));

rotation[0] = { X.x, Y.x, Z.x, 0 };

rotation[1] = { X.y, Y.y, Z.y, 0 };

rotation[2] = { X.z, Y.z, Z.z, 0 };

rotation[3] = { 0, 0, 0, 1 };

translation[3] = { -campos.x, -campos.y, -campos.z, 1 };

glm::mat4 res = rotation * translation;

return res;

}

int main()

{

const double DEGREES_TO_RADIANS = glm::pi<float>() / 180.0;

const double RADIANS_TO_DEGREES = 180.0 / glm::pi<float>();

glm::vec3 camPos(5, 5, 5);

glm::vec3 lookat(0, 0, 0);

glm::vec3 up(0, 1, 0);

glm::mat4 res = lookAt(camPos, lookat, up);

std::cout << to_string(res) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include "TestProject.h"

#include <glm/gtx/string_cast.hpp>

#include <glm/glm.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

using namespace std;

glm::mat4 lookAt(glm::vec3 camPos, glm::vec3 target, glm::vec3 up)

{

glm::vec3 z = glm::normalize(camPos - target);

glm::vec3 x = glm::normalize(glm::cross(up, z));

glm::vec3 y = glm::normalize(glm::cross(z, x));

glm::mat4 viewMatrix = {

{x.x, y.x, z.x, 0},

{x.y, y.y, z.y, 0},

{x.z, y.z, z.z, 0},

{glm::dot(-camPos, x), glm::dot(-camPos, y), glm::dot(-camPos, z), 1}

};

return viewMatrix;

}

int main()

{

glm::vec3 camPos(5, 5, 5);

glm::vec3 target(0, 0, 0);

glm::vec3 up(0, 1, 0);

glm::mat4 viewMatrix = lookAt(camPos, target, up);

std::cout << glm::to_string(viewMatrix) << std::endl;

return 0;

}

# data[].attributes.body
glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {

glm::vec3 z = glm::normalize(campos - look);

glm::vec3 x = glm::normalize(glm::cross(up, z));

glm::vec3 y = glm::normalize(glm::cross(z, x));

glm::mat4 res = { {x.x, y.x, z.x, 0}, {x.y, y.y, z.y, 0}, {x.z, y.z, z.z, 0}, {glm::dot(-campos, x), glm::dot(-campos, y), glm::dot(-campos, z), 1}};

return res;

}

int main() {

std::cout << glm::to_string(lookAt(glm::vec3{ 5,5,5 }, glm::vec3{ 0,0,0 }, glm::vec3{ 0,1,0 })) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include "CMakeProject1.h"

#include <glm/vec3.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat3x2.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 lookat, glm::vec3 up)

{

glm::vec3 z{glm::normalize(campos - lookat)};

glm::vec3 x{glm::normalize(glm::cross(up, z)) };

glm::vec3 y{glm::normalize(glm::cross(z, x)) };

glm::mat4 rotation{};

rotation[0] = { x.x, y.x, z.x, 0 };

rotation[1] = { x.y, y.y, z.y, 0 };

rotation[2] = { x.z, y.z, z.z, 0 };

rotation[3] = { 0, 0, 0, 1 };

glm::mat4 translation{};

translation[0] = { 1, 0, 0, 0 };

translation[1] = { 0, 1, 0, 0 };

translation[2] = { 0, 0, 1, 0 };

translation[3] = { -campos.x, -campos.y, -campos.z, 1 };

return rotation * translation;

}

int main()

{

std::cout << glm::to_string(lookAt({ 5.0F,5.0F,5.0F }, { 0.0F,0.0F,0.0F }, { 0.0F,1.0F,0.0F })) << '\n';

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/transform.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 Zc = glm::normalize(campos - look);

glm::vec3 Xc = glm::normalize(glm::cross(up, Zc));

glm::vec3 Yc = glm::normalize(glm::cross(Zc, Xc));

glm::vec4 r1(Xc.x, Yc.x, Zc.x, 0);

glm::vec4 r2(Xc.y, Yc.y, Zc.y, 0);

glm::vec4 r3(Xc.z, Yc.z, Zc.z, 0);

glm::vec4 r4(0, 0, 0, 1);

glm::mat4 res(r1, r2, r3, r4);

//glm::mat4 res = ((Xc.x, Yc.x, Zc.x, 0), (Xc.y, Yc.y, Zc.y, 0), (Xc.z, Yc.z, Zc.z, 0), (0, 0, 0, 1));

return res;

}

int main()

{

glm::vec3 camPos(5, 5, 5);

glm::vec3 lookat(0, 0, 0);

glm::vec3 up(0, 1, 0);

std::cout << glm::to_string(lookAt(camPos, lookat, up)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/vec3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>

#include <glm/gtx/string_cast.hpp>
#include <glm/gtc/constants.hpp>

glm::mat4 lookAt(const glm::vec3 &camPos, const glm::vec3 &lookAt, const glm::vec3 &up) {
	glm::vec3 z = glm::normalize(camPos - lookAt);
	glm::vec3 x = glm::normalize(glm::cross(up, z));
	glm::vec3 y = glm::normalize(glm::cross(z, x));
	return { {x.x, y.x, z.x, 0}, {x.y, y.y, z.y, 0}, {x.z, y.z, z.z, 0}, {glm::dot(-camPos, x) , glm::dot(-camPos, y), glm::dot(-camPos, z), 1}};
}

int main() {

	glm::vec3 camPos(5, 5, 5);
	glm::vec3 lookat(0, 0, 0);
	glm::vec3 up(0, 1, 0);
	std::cout << glm::to_string(lookAt(camPos, lookat, up));
	return 0;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/vec3.hpp>
#include <glm/gtx/string_cast.hpp>
#include <iostream>

#include <glm/mat4x4.hpp>
#include <glm/mat2x2.hpp>
#include <glm/mat3x2.hpp>

static glm::mat4 lookAt(const glm::vec3 &camPos, const glm::vec3 &look, const glm::vec3 &up)
{
	glm::vec3 z = glm::normalize(camPos - look);
	glm::vec3 x = glm::normalize(glm::cross(up, z));
	glm::vec3 y = glm::normalize(glm::cross(z, x));

	glm::mat4 res = {
		{x.x, y.x, z.x, 0},
		{x.y, y.y, z.y, 0},
		{ x.z, y.z, z.z, 0 },
		{glm::dot(-camPos, x), glm::dot(-camPos, y), glm::dot(-camPos, z), 1}
	};
	return res;
}

int main()
{
	glm::vec3 camPos(5, 5, 5);
	glm::vec3 look(0, 0, 0);
	glm::vec3 up(0, 1, 0);

	std::cout << glm::to_string(lookAt(camPos, look, up)) << std::endl;
	return 0;
}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>

#include <glm/glm.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/mat3x3.hpp>

#include <glm/glm.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <iostream>

#include "ComputerGraphicsLecture2.h"

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 zc = glm::normalize(campos - look);

glm::vec3 xc = glm::normalize(glm::cross(up, zc));

glm::vec3 yc = glm::normalize(glm::cross(zc, xc));

glm::vec4 c1(xc.x, yc.x, zc.x, 0);

glm::vec4 c2(xc.y, yc.y, zc.y, 0);

glm::vec4 c3(xc.z, yc.z, zc.z, 0);

glm::vec4 c4(-glm::dot(xc, campos), -glm::dot(yc, campos), -glm::dot(zc, campos), 1);

glm::mat4 m;

m[0] = c1;

m[1] = c2;

m[2] = c3;

m[3] = c4;

return m;

}

int main()

{

std::cout << glm::to_string(lookAt(glm::vec3(5, 5, 5), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0))) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtc/constants.hpp>

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/mat4x4.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {

glm::vec3 z = glm::normalize(campos - look);

glm::vec3 x = glm::normalize(glm::cross(up, z));

glm::vec3 y = glm::normalize(glm::cross(z, x));

glm::mat4 rot(1.0);

rot[0] = glm::vec4(x[0], y[0], z[0], 0);

rot[1] = glm::vec4(x[1], y[1], z[1], 0);

rot[2] = glm::vec4(x[2], y[2], z[2], 0);

glm::mat4 t(1.0);

glm::mat4 translation = glm::translate(t, -campos);

glm::mat4 viewingMatrix = rot * translation;

return viewingMatrix;

}

int main() {

glm::vec3 campos(5, 5, 5);

glm::vec3 look(0, 0, 0);

glm::vec3 up(0, 1, 0);

std::cout << glm::to_string(lookAt(campos, look, up)) << std::endl;

}

# data[].attributes.body
glm::mat4 lookAt(glm::vec3 camPos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 z = glm::normalize(camPos - look);

glm::vec3 x = glm::normalize(glm::cross(up, z));

glm::vec3 y = glm::normalize(glm::cross(z, x));

glm::vec4 v1 = glm::vec4(x[0], y[0], z[0], 0.f);

glm::vec4 v2 = glm::vec4(x[1], y[1], z[1], 0.f);

glm::vec4 v3 = glm::vec4(x[2], y[2], z[2], 0.f);

glm::mat4 m(1.0);

m[0] = v1;

m[1] = v2;

m[2] = v3;

glm::mat4 t(1.0);

glm::mat4 translation = glm::translate(t, -camPos);

glm::mat4 viewMatrix = m * translation;

std::cout << "viewMatrix -> " << glm::to_string(viewMatrix) << std::endl;

return viewMatrix;

}

int main()

{

glm::vec3 camPos(5.f, 5.f, 5.f);

glm::vec3 look(0.f, 0.f, 0.f);

glm::vec3 up(0.f, 1.f, 0.f);

glm::mat4 result = lookAt(camPos, look, up);

return 0;

}

# data[].attributes.body
#include <iostream>
#include <glm/glm.hpp>
#include "CMakeProject2.h"
#include <glm/ext/matrix_transform.hpp>
#include <glm/gtx/string_cast.hpp>
glm::mat4 lookAt(glm::vec3 camPos, glm::vec3 lookAt, glm::vec3 up)
{
 camPos *= -1;
 glm::mat4 identityMatrix{ 1.0F };
 glm::vec3 newZAxis = glm::normalize(lookAt - camPos);
 glm::vec3 newXAxis = glm::normalize(glm::cross(up, newZAxis));
 glm::vec3 newYAxis = glm::normalize(glm::cross(newZAxis, newXAxis));
 glm::mat4 translateMatrix = glm::translate(identityMatrix, camPos);
 glm::mat4 rotateMatrix{ // Column wise matrix
 {newXAxis.x, newYAxis.x, newZAxis.x, 0},
 {newXAxis.y, newYAxis.y, newZAxis.y, 0},
 {newXAxis.z, newYAxis.z, newZAxis.z, 0},
 {0, 0, 0, 1}
 };
 glm::mat4 viewMatrix = rotateMatrix * translateMatrix;
 return viewMatrix;
}

void main()
{
 glm::vec3 camPos{ 5, 5, 5 };
 glm::vec3 lookAtPoint{ 0, 0, 0 };
 glm::vec3 up{ 0, 1, 0 };
 std::cout << glm::to_string(lookAt(camPos, lookAtPoint, up)) << std::endl;
 return;
}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/string_cast.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 f = glm::normalize(look - campos);

glm::vec3 s = glm::normalize(glm::cross(f, up));

glm::vec3 u = glm::cross(s, f);

glm::vec3 Zc = glm::normalize(campos - look);

glm::vec3 Xc = glm::normalize(glm::cross(up, Zc));

glm::vec3 Yc = glm::cross(Zc, Xc);

glm::mat4 result(1.0f);

result[0][0] = Xc.x;

result[1][0] = Xc.y;

result[2][0] = Xc.z;

result[0][1] = Yc.x;

result[1][1] = Yc.y;

result[2][1] = Yc.z;

result[0][2] = Zc.x;

result[1][2] = Zc.y;

result[2][2] = Zc.z;

result[3][0] = glm::dot(Xc, campos);

result[3][1] = glm::dot(Yc, campos);

result[3][2] = -glm::dot(Zc, campos);

std::cout << glm::to_string(result) << std::endl;

return result;

}

int main()

{

glm::vec3 campos(5, 5, 5);

glm::vec3 look(0, 0, 0);

glm::vec3 up(0, 1, 0);

lookAt(campos, look, up);

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/gtc/constants.hpp>

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

//const float DEGREES_TO_RADIANS = 180.0 / glm::pi<float>();

glm::mat4 lookat(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {

glm::vec3 zc = glm::normalize(campos - look);

glm::vec3 xc = glm::normalize(glm::cross(up, zc));

glm::vec3 yc = glm::normalize(glm::cross(zc, xc));

glm::vec4 c1 = glm::vec4(xc.x, yc.x, zc.x, 0);

glm::vec4 c2 = glm::vec4(xc.y, yc.y, zc.y, 0);

glm::vec4 c3 = glm::vec4(xc.z, yc.z, zc.z, 0);

glm::vec4 c4 = glm::vec4(0, 0, 0, 1);

glm::mat4 rot;

rot[0] = c1;

rot[1] = c2;

rot[2] = c3;

rot[3] = c4;

return rot;

}

int main() {

glm::vec3 cam_pos (5, 5, 5);

glm::vec3 look_at(0, 0, 0);

glm::vec3 up(0, 1, 0);

std::cout << glm::to_string(lookat(cam_pos, look_at, up)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <iostream>

template<typename T>

void display(T x, const std::string& prefix = "") {

std::cout << prefix << glm::to_string(x) << std::endl;

}

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {

glm::mat4x4 translationMatrix(1, 0, 0, -look.x, 0, 1, 0, -look.y, 0, 0, 1, -look.z, 0, 0, 0, 1.f);

glm::vec3 zAxis = glm::normalize(campos - look);

glm::vec3 xAxis = glm::normalize(glm::cross(up, zAxis));

glm::vec3 yAxis = glm::normalize(glm::cross(zAxis, xAxis));

glm::mat4x4 rotationMatrix(xAxis.x, yAxis.x, zAxis.x, 0,

xAxis.y, yAxis.y, zAxis.y, 0,

xAxis.z, yAxis.z, zAxis.z, 0,

0, 0, 0, 1);

glm::mat4x4 viewMatrix = rotationMatrix * translationMatrix;

return viewMatrix;

}

int main(void) {

glm::vec3 camPos(5, 5, 5);

glm::vec3 lookat(0, 0, 0);

glm::vec3 up(0, 1, 0);

glm::mat4 viewMatrix = lookAt(camPos, lookat, up);

display(viewMatrix, "viewMatrix = ");

return 0;

}

# data[].attributes.body
#include "CMakeProject1.h"

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat3x2.hpp>

#include <glm/mat4x4.hpp>

#include <iostream>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

using namespace std;

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 zaxis = glm::normalize(campos - look);

glm::vec3 xaxis = glm::normalize(glm::cross(up, zaxis));

glm::vec3 yaxis = glm::cross(zaxis, xaxis);

glm::mat4 view = glm::mat4(1.0f);

view[0][0] = xaxis.x;

view[1][0] = xaxis.y;

view[2][0] = xaxis.z;

view[0][1] = yaxis.x;

view[1][1] = yaxis.y;

view[2][1] = yaxis.z;

view[0][2] = zaxis.x;

view[1][2] = zaxis.y;

view[2][2] = zaxis.z;

view[3][0] = -glm::dot(xaxis, campos);

view[3][1] = -glm::dot(yaxis, campos);

view[3][2] = -glm::dot(zaxis, campos);

return view;

}

int main()

{

const float DEGREES_TO_RADIANS = glm::pi<float>() / 180.0;

const float RADIANS_TO_DEGREES = 180.0 / glm::pi<float>();

glm::vec3 camPos(5, 5, 5);

glm::vec3 lookat(0, 0, 0);

glm::vec3 up(0, 1, 0);

glm::mat4 view = lookAt(camPos, lookat, up);

std::cout << glm::to_string(view) << std::endl;

return 0;

}

# data[].attributes.body
glm::mat4x4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)
{
	glm::vec3 zc = glm::normalize(campos - look);
	glm::vec3 xc = glm::normalize(glm::cross(up, zc));
	glm::vec3 yc = glm::normalize(glm::cross(zc, xc));

	glm::mat4x4 viewMatRot = {
		{xc.x, yc.x, zc.x, 0},
		{xc.y, yc.y, zc.y, 0},
		{xc.z, yc.z, zc.z, 0},
		{0, 0, 0, 1}
	};

	glm::mat4x4 viewMatTrans(1);
	viewMatTrans[3][0] = -campos.x;
	viewMatTrans[3][1] = -campos.y;
	viewMatTrans[3][2] = -campos.z;
	return viewMatRot * viewMatTrans;
}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/glm.hpp>

#include <iostream>

#include <glm/gtx/transform.hpp>

#include <glm/gtx/matrix_transform_2d.hpp>

#include <glm/gtx/string_cast.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 zaxis = glm::normalize(campos - look);

glm::vec3 xaxis = glm::normalize(glm::cross(up, zaxis));

glm::vec3 yaxis = glm::cross(zaxis, xaxis);

glm::mat4 viewMatrix = glm::mat4(1.0f);

viewMatrix[0] = glm::vec4(xaxis, 0.0f);

viewMatrix[1] = glm::vec4(yaxis, 0.0f);

viewMatrix[2] = glm::vec4(zaxis, 0.0f);

viewMatrix = glm::transpose(viewMatrix);

viewMatrix = glm::translate(viewMatrix, -campos);

return viewMatrix;

}

int main()

{

glm::vec3 cameraPos = glm::vec3(5.0f, 5.0f, 5.0f);

glm::vec3 lookat = glm::vec3(0.0f, 0.0f, 0.0f);

glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);

glm::mat4 viewMatrix = lookAt(cameraPos, lookat, up);

std::cout << glm::to_string(viewMatrix) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <glm/gtx/string_cast.hpp>
#include <glm/gtx/transform.hpp>
#include <iostream>
#include <stdexcept>

glm::mat4 lookAt(glm::vec3 camPos, glm::vec3 look, glm::vec3 up) {
  auto zc = glm::normalize(camPos - look);
  auto xc = glm::normalize(glm::cross(up, zc));
  auto yc = glm::normalize(glm::cross(zc, xc));

  glm::mat4 view(1.0f);
  view[0] = glm::vec4(xc, 0.0f);
  view[1] = glm::vec4(yc, 0.0f);
  view[2] = glm::vec4(zc, 0.0f);

  view = glm::transpose(view);
  view = glm::translate(view, -camPos);
  return view;
}

int main() {
  glm::vec3 camPos(5, 5, 5);
  glm::vec3 look(0, 0, 0);
  glm::vec3 up(0, 1, 0);

  std::cout << "view matrix: " << glm::to_string(lookAt(camPos, look, up))
            << std::endl;

  return 0;
}

# data[].attributes.body
static glm::mat4 LookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 zaxis = glm::normalize(campos - look);

glm::vec3 xaxis = glm::normalize(glm::cross(up, zaxis));

glm::vec3 yaxis = glm::cross(zaxis, xaxis);

glm::mat4 view = {

xaxis.x, yaxis.x, zaxis.x, 0,

xaxis.y, yaxis.y, zaxis.y, 0,

xaxis.z, yaxis.z, zaxis.z, 0,

-glm::dot(xaxis, campos), -glm::dot(yaxis, campos), -glm::dot(zaxis, campos), 1

};

return view;

}

int main()

{

glm::vec3 campos(5, 5, 5);

glm::vec3 look(0, 0, 0);

glm::vec3 up(0, 1, 0);

LookAt(campos, look, up);

std::cout << to_string(LookAt(campos, look, up)) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/mat4x4.hpp>

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up){

glm::vec3 zaxis = glm::normalize(campos - look);

glm::vec3 xaxis = glm::normalize(glm::cross(up, zaxis));

glm::vec3 yaxis = glm::cross(zaxis, xaxis);

glm::mat4 view(

glm::vec4(xaxis.x, yaxis.x, zaxis.x, 0),

glm::vec4(xaxis.y, yaxis.y, zaxis.y, 0),

glm::vec4(xaxis.z, yaxis.z, zaxis.z, 0),

glm::vec4(-glm::dot(xaxis, campos), -glm::dot(yaxis, campos), -glm::dot(zaxis, campos), 1)

);

return view;

}

int main() {

glm::vec3 campos = glm::vec3(5,5,5);

glm::vec3 look = glm::vec3(0,0,0);

glm::vec3 up = glm::vec3(0,1,0);

glm::mat4 view = lookAt(campos, look, up);

std::cout << glm::to_string(view) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/gtc/type_ptr.hpp>

#include <glm/mat2x2.hpp>

#include <glm/mat4x4.hpp>

#include <glm/mat3x3.hpp>

#include <glm/mat3x2.hpp>

const float DEGREES_TO_RADIANS = glm::pi <float>() / 180;

const float RADIANS_TO_DEGREES = 180 / glm::pi <float>();

static glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up) {

glm::vec3 Z = glm::normalize(campos - look);

glm::vec3 X = glm::normalize(glm::cross(up, Z));

glm::vec3 Y = glm::normalize(glm::cross(Z, X));

glm::vec4 P(X.x, Y.x, Z.x, 0);

glm::vec4 S(X.y, Y.y, Z.y, 0);

glm::vec4 T(X.z, Y.z, Z.z, 0);

glm::vec4 Q(0, 0, 0, 1);

glm::mat4 res;

res[0] = P;

res[1] = S;

res[2] = T;

res[3] = Q;

return res;

}

static int main() {

glm::vec3 camPos(5, 5, 5);

glm::vec3 lookat(0, 0, 0);

glm::vec3 up(0, 1, 0);

std::cout << to_string(lookAt(camPos, lookat, up)) << std::endl;

return 0;

}

# data[].attributes.body
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/matrix_transform_2d.hpp>
#include <glm/gtx/string_cast.hpp>

glm::mat4 lookAt(glm::vec3 camPos, glm::vec3 look, glm::vec3 up)
{
	glm::vec3 zaxis = glm::normalize(camPos - look);
	glm::vec3 xaxis = glm::normalize(glm::cross(up, zaxis));
	glm::vec3 yaxis = glm::cross(zaxis, xaxis);

	glm::mat4 view = glm::mat4(1.0f);

	view[0][0] = xaxis.x;
	view[1][0] = xaxis.y;
	view[2][0] = xaxis.z;

	view[0][1] = yaxis.x;
	view[1][1] = yaxis.y;
	view[2][1] = yaxis.z;

	view[0][2] = zaxis.x;
	view[1][2] = zaxis.y;
	view[2][2] = zaxis.z;

	view[3][0] = -glm::dot(xaxis, camPos);
	view[3][1] = -glm::dot(yaxis, camPos);
	view[3][2] = -glm::dot(zaxis, camPos);

	return view;
}

void main()
{
	glm::vec3 camPos(5, 5, 5);
	glm::vec3 look(0, 0, 0);
	glm::vec3 up(0, 1, 0);
	glm::mat4 view = lookAt(camPos, look, up);
	std::cout << glm::to_string(view) << std::endl;
}

# data[].attributes.body
glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 zaxis = glm::normalize(campos - look);

glm::vec3 xaxis = glm::normalize(glm::cross(up, zaxis));

glm::vec3 yaxis = glm::cross(zaxis, xaxis);

glm::mat4 view = glm::mat4(1.0f);

view[0] = glm::vec4(xaxis.x, yaxis.x, zaxis.x, 0);

view[1] = glm::vec4(xaxis.y, yaxis.y, zaxis.y, 0);

view[2] = glm::vec4(xaxis.z, yaxis.z, zaxis.z, 0);

view[3] = glm::vec4(-glm::dot(xaxis, campos), -glm::dot(yaxis, campos), -glm::dot(zaxis, campos), 1);

return view;

}

int ex1()

{

glm::vec3 camPos = glm::vec3(5, 5, 5);

glm::vec3 look = glm::vec3(0, 0, 0);

glm::vec3 up = glm::vec3(0, 1, 0);

glm::mat4 view = lookAt(camPos, look, up);

std::cout << to_string(view) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_ENABLE_EXPERIMENTAL

#include <glm/gtx/string_cast.hpp>

#include <glm/glm.hpp>

#include <iostream>

glm::mat4 LookAt(const glm::vec3& cameraPosition, const glm::vec3& look, const glm::vec3& up)

{

glm::vec3 zaxis = glm::normalize(cameraPosition - look);

glm::vec3 xaxis = glm::normalize(glm::cross(up, zaxis));

glm::vec3 yaxis = glm::cross(zaxis, xaxis);

glm::mat4 translation = glm::mat4(1.0f);

translation[3] = glm::vec4(-cameraPosition, 1.0f);

glm::mat4 rotation = glm::mat4(1.0f);

rotation[0][0] = xaxis.x;

rotation[1][0] = xaxis.y;

rotation[2][0] = xaxis.z;

rotation[0][1] = yaxis.x;

rotation[1][1] = yaxis.y;

rotation[2][1] = yaxis.z;

rotation[0][2] = zaxis.x;

rotation[1][2] = zaxis.y;

rotation[2][2] = zaxis.z;

return rotation * translation;

}

int main()

{

glm::vec3 camPos = glm::vec3(5.f);

glm::vec3 look = glm::vec3(0.f);

glm::vec3 up = glm::vec3(0.f, 1.f, 0.f);

glm::mat4 view = LookAt(camPos, look, up);

std::cout << glm::to_string(view) << std::endl;

return 0;

}

# data[].attributes.body
#define GLM_FORCE_SWIZZLE

#define GLM_ENABLE_EXPERIMENTAL

#include <iostream>

#include <glm/glm.hpp>

#include <glm/gtx/string_cast.hpp>

#include <glm/gtc/constants.hpp>

#include <glm/mat4x4.hpp>

const double DEG_TO_RAD = glm::pi<double>() / 180.0;

const double RAD_TO_DEG = 180.0 / glm::pi<double>();

glm::mat4 lookAt(glm::vec3 campos, glm::vec3 look, glm::vec3 up)

{

glm::vec3 f = glm::normalize(campos - look);

glm::vec3 s = glm::normalize(glm::cross(up, f));

glm::vec3 u = glm::cross(f, s);

glm::mat4 rot(1.0f);

rot[0] = glm::vec4(s, 0.0f);

rot[1] = glm::vec4(u, 0.0f);

rot[2] = glm::vec4(f, 0.0f);

rot = glm::transpose(rot);

return rot;

}

int main()

{

glm::vec3 campos(5.0f, 5.0f, 5.0f);

glm::vec3 look(0.0f, 0.0f, 0.0f);

glm::vec3 up(0.0f, 1.0f, 0.0f);

glm::mat4 view = lookAt(campos, look, up);

std::cout << glm::to_string(view) << std::endl;

return 0;

}