Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- / ********************************************* Matrix4x4 ********************************************* /
- #ifndef _MATRIX_H_
- #define _MATRIX_H_
- #include <math.h>
- #include <string.h>
- class Matrix4x4
- {
- public:
- Matrix4x4()
- {
- identity();
- }
- void identity()
- {
- float *p = (float *)matrix;
- p[0] = p[5] = p[10] = p[15] = 1.0;
- p[1] = p[2] = p[3] = p[4] = 0.0;
- p[6] = p[7] = p[8] = p[9] = 0.0;
- p[11] = p[12] = p[13] = p[14] = 0.0;
- }
- void copy(Matrix4x4 &dst)
- {
- memcpy(dst.getMatrixPointer(), getMatrixPointer(), sizeof(float) * 16);
- }
- void copy(Matrix4x4 *dst)
- {
- memcpy(dst->getMatrixPointer(), getMatrixPointer(), sizeof(float) * 16);
- }
- float *getMatrixPointer()
- {
- return (float *)matrix;
- }
- void multiply(Matrix4x4 &mtx, Matrix4x4 &dst)
- {
- float (*dest)[4] = (float (*)[4])dst.getMatrixPointer();
- float (*m1)[4] = (float (*)[4])getMatrixPointer();
- float (*m2)[4] = (float (*)[4])mtx.getMatrixPointer();
- dest[0][0] = m1[0][0] * m2[0][0] + m1[0][1] * m2[1][0] + m1[0][2] * m2[2][0] + m1[0][3] * m2[3][0];
- dest[0][1] = m1[0][0] * m2[0][1] + m1[0][1] * m2[1][1] + m1[0][2] * m2[2][1] + m1[0][3] * m2[3][1];
- dest[0][2] = m1[0][0] * m2[0][2] + m1[0][1] * m2[1][2] + m1[0][2] * m2[2][2] + m1[0][3] * m2[3][2];
- dest[0][3] = m1[0][0] * m2[0][3] + m1[0][1] * m2[1][3] + m1[0][2] * m2[2][3] + m1[0][3] * m2[3][3];
- dest[1][0] = m1[1][0] * m2[0][0] + m1[1][1] * m2[1][0] + m1[1][2] * m2[2][0] + m1[1][3] * m2[3][0];
- dest[1][1] = m1[1][0] * m2[0][1] + m1[1][1] * m2[1][1] + m1[1][2] * m2[2][1] + m1[1][3] * m2[3][1];
- dest[1][2] = m1[1][0] * m2[0][2] + m1[1][1] * m2[1][2] + m1[1][2] * m2[2][2] + m1[1][3] * m2[3][2];
- dest[1][3] = m1[1][0] * m2[0][3] + m1[1][1] * m2[1][3] + m1[1][2] * m2[2][3] + m1[1][3] * m2[3][3];
- dest[2][0] = m1[2][0] * m2[0][0] + m1[2][1] * m2[1][0] + m1[2][2] * m2[2][0] + m1[2][3] * m2[3][0];
- dest[2][1] = m1[2][0] * m2[0][1] + m1[2][1] * m2[1][1] + m1[2][2] * m2[2][1] + m1[2][3] * m2[3][1];
- dest[2][2] = m1[2][0] * m2[0][2] + m1[2][1] * m2[1][2] + m1[2][2] * m2[2][2] + m1[2][3] * m2[3][2];
- dest[2][3] = m1[2][0] * m2[0][3] + m1[2][1] * m2[1][3] + m1[2][2] * m2[2][3] + m1[2][3] * m2[3][3];
- dest[3][0] = m1[3][0] * m2[0][0] + m1[3][1] * m2[1][0] + m1[3][2] * m2[2][0] + m1[3][3] * m2[3][0];
- dest[3][1] = m1[3][0] * m2[0][1] + m1[3][1] * m2[1][1] + m1[3][2] * m2[2][1] + m1[3][3] * m2[3][1];
- dest[3][2] = m1[3][0] * m2[0][2] + m1[3][1] * m2[1][2] + m1[3][2] * m2[2][2] + m1[3][3] * m2[3][2];
- dest[3][3] = m1[3][0] * m2[0][3] + m1[3][1] * m2[1][3] + m1[3][2] * m2[2][3] + m1[3][3] * m2[3][3];
- }
- //Set matrix rotation X
- void setRotationX(float angle)
- {
- float s = sinf(angle);
- float c = cosf(angle);
- matrix[1][1] = c;
- matrix[1][2] = s;
- matrix[2][1] = -s;
- matrix[2][2] = c;
- }
- //Set matrix rotation Y
- void setRotationY(float angle)
- {
- float s = sinf(angle);
- float c = cosf(angle);
- matrix[0][0] = c;
- matrix[0][2] = -s;
- matrix[2][0] = s;
- matrix[2][2] = c;
- }
- //Set matrix rotation Z
- void setRotationZ(float angle)
- {
- float s = sinf(angle);
- float c = cosf(angle);
- matrix[0][0] = c;
- matrix[0][1] = -s;
- matrix[1][0] = s;
- matrix[1][1] = c;
- }
- void translate(float x, float y, float z)
- {
- matrix[0][3] = matrix[0][0]*x + matrix[0][1]*y + matrix[0][2]*z;
- matrix[1][3] = matrix[1][0]*x + matrix[1][1]*y + matrix[1][2]*z;
- matrix[2][3] = matrix[2][0]*x + matrix[2][1]*y + matrix[2][2]*z;
- }
- //Rotate this matrix around X axis
- void rotateX(float angle)
- {
- Matrix4x4 rotated, tmp;
- rotated.setRotationX(angle);
- multiply(rotated, tmp); // tmp = this * rotated
- tmp.copy(this); //this = tmp
- }
- //Rotate this matrix around Y axis
- void rotateY(float angle)
- {
- Matrix4x4 rotated, tmp;
- rotated.setRotationY(angle);
- multiply(rotated, tmp); // tmp = this * rotated
- tmp.copy(this); //this = tmp
- }
- //Rotate this matrix around Z axis
- void rotateZ(float angle)
- {
- Matrix4x4 rotated, tmp;
- rotated.setRotationZ(angle);
- multiply(rotated, tmp); // tmp = this * rotated
- tmp.copy(this); //this = tmp
- }
- private:
- float matrix[4][4];
- };
- #endif
- / ********************************************* Camera.h ********************************************* /
- #ifndef _CAMERA_H_
- #define _CAMERA_H_
- #include <SFML/OpenGL.hpp>
- #include <SFML/Audio.hpp>
- #include <SFML/Graphics.hpp>
- #include <SFML/Window.hpp>
- #include <stdio.h>
- #include "Matrix.h"
- extern sf::Vector3f UP_VECTOR;
- extern sf::Vector3f DOWN_VECTOR;
- float Dot(sf::Vector3f& vec1, sf::Vector3f& vec2);
- sf::Vector3f Cross(const sf::Vector3f& vec1, const sf::Vector3f& vec2);
- void Cross(sf::Vector3f& vec1, sf::Vector3f& vec2, sf::Vector3f& result);
- sf::Vector3f Cross(sf::Vector3f& vec1, sf::Vector3f& vec2);
- class Camera
- {
- public:
- Camera(sf::Vector3f *position, float *pitch, float *yaw)
- {
- this->position = position;
- this->pitch = pitch;
- this->yaw = yaw;
- setupVectors();
- }
- sf::Vector3f getLookVector()
- {
- return *lookVector;
- }
- sf::Vector3f getUpVector()
- {
- return *upVector;
- }
- sf::Vector3f getRightVector()
- {
- return *rightVector;
- }
- sf::Vector3f getForwardVector()
- {
- return Cross(*rightVector, UP_VECTOR);
- }
- void updateMatrix()
- {
- matrix.identity();
- matrix.rotateX(*pitch);
- matrix.rotateY(*yaw);
- matrix.translate(-position->x, -position->y, -position->z);
- }
- private:
- sf::Vector3f *position;
- float *pitch, *yaw;
- Matrix4x4 matrix;
- sf::Vector3f *upVector, *lookVector, *rightVector;
- void setupVectors()
- {
- float (*p)[4] = (float (*)[4])matrix.getMatrixPointer();
- rightVector = (sf::Vector3f *)&p[0];
- upVector = (sf::Vector3f *)&p[1];
- lookVector = (sf::Vector3f *)&p[2];
- }
- };
- #endif
- / ********************************************* Camera.cpp ********************************************* /
- #include "Camera.h"
- sf::Vector3f UP_VECTOR(0.0f, 1.0f, 0.0f);
- sf::Vector3f DOWN_VECTOR(0.0f, -1.0f, 0.0f);
- float Dot(sf::Vector3f& vec1, sf::Vector3f& vec2)
- {
- return vec1.x*vec2.x + vec1.y*vec2.y + vec1.z*vec2.z;
- }
- sf::Vector3f Cross(const sf::Vector3f& vec1, const sf::Vector3f& vec2)
- {
- return sf::Vector3f(vec1.y*vec2.z - vec1.z*vec2.y, vec1.z*vec2.x - vec1.x*vec2.z, vec1.x*vec2.y - vec1.y*vec2.x);
- }
- void Cross(sf::Vector3f& vec1, sf::Vector3f& vec2, sf::Vector3f& result)
- {
- result.x = vec1.y*vec2.z - vec1.z*vec2.y;
- result.y = vec1.z*vec2.x - vec1.x*vec2.z;
- result.z = vec1.x*vec2.y - vec1.y*vec2.x;
- }
- sf::Vector3f Cross(sf::Vector3f& vec1, sf::Vector3f& vec2)
- {
- return (sf::Vector3f){vec1.y*vec2.z - vec1.z*vec2.y,
- vec1.z*vec2.x - vec1.x*vec2.z,
- vec1.x*vec2.y - vec1.y*vec2.x};
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement