Added Camera.
This commit is contained in:
@@ -0,0 +1,74 @@
|
|||||||
|
#include "Camera.h"
|
||||||
|
|
||||||
|
Camera::Camera(float yFOV, float aspectRatio, float nearClip, float farClip)
|
||||||
|
{
|
||||||
|
m_FOV = yFOV;
|
||||||
|
m_AspectRatio = aspectRatio;
|
||||||
|
m_NearClip = nearClip;
|
||||||
|
m_FarClip = farClip;
|
||||||
|
|
||||||
|
m_Position = glm::vec3(0.0);
|
||||||
|
m_Pitch = 0.f;
|
||||||
|
m_Yaw = 0.f;
|
||||||
|
|
||||||
|
UpdateProjectionMatrix();
|
||||||
|
UpdateViewMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 Camera::Forward()
|
||||||
|
{
|
||||||
|
return glm::rotate(glm::vec3(0.f, 0.f, -1.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::vec3 Camera::Right()
|
||||||
|
{
|
||||||
|
return glm::rotate(glm::vec3(1.f, 0.f, 0.f), -m_Yaw, glm::vec3(0.f, 1.f, 0.f));
|
||||||
|
}
|
||||||
|
|
||||||
|
glm::mat4 Camera::Orientation()
|
||||||
|
{
|
||||||
|
glm::mat4 orientation(1.f);
|
||||||
|
orientation = glm::rotate(orientation, m_Pitch, glm::vec3(1.f, 0.f, 0.f));
|
||||||
|
orientation = glm::rotate(orientation, m_Yaw, glm::vec3(0.f, 1.f, 0.f));
|
||||||
|
return orientation;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::AspectRatio(float val)
|
||||||
|
{
|
||||||
|
m_AspectRatio = val;
|
||||||
|
UpdateProjectionMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::Position(glm::vec3 val)
|
||||||
|
{
|
||||||
|
m_Position = val;
|
||||||
|
UpdateViewMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Camera::Pitch(float val)
|
||||||
|
{
|
||||||
|
m_Pitch = val;
|
||||||
|
UpdateViewMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::Yaw(float val)
|
||||||
|
{
|
||||||
|
m_Yaw = val;
|
||||||
|
UpdateViewMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::UpdateProjectionMatrix()
|
||||||
|
{
|
||||||
|
m_ProjectionMatrix = glm::perspective(
|
||||||
|
m_FOV,
|
||||||
|
m_AspectRatio,
|
||||||
|
m_NearClip,
|
||||||
|
m_FarClip
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Camera::UpdateViewMatrix()
|
||||||
|
{
|
||||||
|
m_ViewMatrix = glm::translate(Orientation(), -m_Position);
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
#ifndef Camera_h__
|
||||||
|
#define Camera_h__
|
||||||
|
|
||||||
|
#define GLM_FORCE_RADIANS
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/constants.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
#include <glm/gtx/rotate_vector.hpp>
|
||||||
|
|
||||||
|
class Camera
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Camera(float yFOV, float aspectRatio, float nearClip, float farClip);
|
||||||
|
|
||||||
|
glm::vec3 Forward();
|
||||||
|
glm::vec3 Right();
|
||||||
|
glm::mat4 Orientation();
|
||||||
|
|
||||||
|
float AspectRatio() const { return m_AspectRatio; }
|
||||||
|
void AspectRatio(float val);
|
||||||
|
|
||||||
|
glm::vec3 Position() const { return m_Position; }
|
||||||
|
void Position(glm::vec3 val);
|
||||||
|
|
||||||
|
float Pitch() const { return m_Pitch; }
|
||||||
|
void Pitch(float val);
|
||||||
|
float Yaw() const { return m_Yaw; }
|
||||||
|
void Yaw(float val);
|
||||||
|
|
||||||
|
glm::mat4 ProjectionMatrix() const { return m_ProjectionMatrix; }
|
||||||
|
void ProjectionMatrix(glm::mat4 val) { m_ProjectionMatrix = val; }
|
||||||
|
|
||||||
|
glm::mat4 ViewMatrix() const { return m_ViewMatrix; }
|
||||||
|
void ViewMatrix(glm::mat4 val) { m_ViewMatrix = val; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void UpdateProjectionMatrix();
|
||||||
|
void UpdateViewMatrix();
|
||||||
|
|
||||||
|
float m_FOV;
|
||||||
|
float m_AspectRatio;
|
||||||
|
float m_NearClip;
|
||||||
|
float m_FarClip;
|
||||||
|
|
||||||
|
glm::vec3 m_Position;
|
||||||
|
float m_Pitch;
|
||||||
|
float m_Yaw;
|
||||||
|
|
||||||
|
glm::mat4 m_ProjectionMatrix;
|
||||||
|
glm::mat4 m_ViewMatrix;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // Camera_h__
|
||||||
@@ -97,6 +97,7 @@
|
|||||||
</Lib>
|
</Lib>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="Camera.cpp" />
|
||||||
<ClCompile Include="Engine.h" />
|
<ClCompile Include="Engine.h" />
|
||||||
<ClCompile Include="Frame.cpp" />
|
<ClCompile Include="Frame.cpp" />
|
||||||
<ClCompile Include="GameFrame.cpp" />
|
<ClCompile Include="GameFrame.cpp" />
|
||||||
@@ -117,6 +118,7 @@
|
|||||||
<ClCompile Include="Systems\SoundsSystem.cpp" />
|
<ClCompile Include="Systems\SoundsSystem.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Camera.h" />
|
||||||
<ClInclude Include="Color.h" />
|
<ClInclude Include="Color.h" />
|
||||||
<ClInclude Include="Component.h" />
|
<ClInclude Include="Component.h" />
|
||||||
<ClInclude Include="Components\Camera.h" />
|
<ClInclude Include="Components\Camera.h" />
|
||||||
|
|||||||
@@ -43,6 +43,7 @@
|
|||||||
<ClCompile Include="GameFrame.cpp">
|
<ClCompile Include="GameFrame.cpp">
|
||||||
<Filter>GUI</Filter>
|
<Filter>GUI</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="Camera.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="Component.h" />
|
<ClInclude Include="Component.h" />
|
||||||
@@ -140,6 +141,7 @@
|
|||||||
<Filter>GUI</Filter>
|
<Filter>GUI</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="GameWorld.h" />
|
<ClInclude Include="GameWorld.h" />
|
||||||
|
<ClInclude Include="Camera.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="Shaders\Fragment.glsl">
|
<None Include="Shaders\Fragment.glsl">
|
||||||
|
|||||||
@@ -4,47 +4,6 @@ Renderer::Renderer()
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------ TEMPCAMERA --------------------
|
|
||||||
struct Camera
|
|
||||||
{
|
|
||||||
glm::vec3 Position;
|
|
||||||
float Pitch;
|
|
||||||
float Yaw;
|
|
||||||
float Roll;
|
|
||||||
|
|
||||||
glm::mat4 getViewMatrix()
|
|
||||||
{
|
|
||||||
glm::mat4 view;
|
|
||||||
view = glm::rotate(view, Pitch, glm::vec3(1,0,0));
|
|
||||||
view = glm::rotate(view, Yaw, glm::vec3(0,1,0));
|
|
||||||
view = glm::rotate(view, Roll, glm::vec3(0,0,1));
|
|
||||||
view = glm::translate(view, -Position);
|
|
||||||
|
|
||||||
return view;
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::vec3 Forward()
|
|
||||||
{
|
|
||||||
glm::mat4 Orientation;
|
|
||||||
Orientation = glm::rotate(Orientation, Pitch, glm::vec3(1,0,0));
|
|
||||||
Orientation = glm::rotate(Orientation, Yaw, glm::vec3(0,1,0));
|
|
||||||
|
|
||||||
return glm::vec3(glm::vec4(0,0,-1,0) * Orientation);
|
|
||||||
}
|
|
||||||
|
|
||||||
glm::vec3 Right()
|
|
||||||
{
|
|
||||||
glm::mat4 Orientation;
|
|
||||||
Orientation = glm::rotate(Orientation, Pitch, glm::vec3(1,0,0));
|
|
||||||
Orientation = glm::rotate(Orientation, Yaw, glm::vec3(0,1,0));
|
|
||||||
|
|
||||||
return glm::vec3(glm::vec4(1,0,0,0) * Orientation);
|
|
||||||
}
|
|
||||||
|
|
||||||
Camera() : Position(0.0f, 2.0f, 0.0f), Pitch(1.0f), Yaw(4.5f), Roll(0.f) { }
|
|
||||||
} MyCamera;
|
|
||||||
//-----------------TempCamera-.---------------------
|
|
||||||
|
|
||||||
void Renderer::Initialize()
|
void Renderer::Initialize()
|
||||||
{
|
{
|
||||||
// Initialize GLFW
|
// Initialize GLFW
|
||||||
@@ -82,6 +41,9 @@ void Renderer::Initialize()
|
|||||||
}
|
}
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
|
||||||
|
// Create Camera
|
||||||
|
m_Camera = std::make_shared<Camera>(90.f, WIDTH / HEIGHT, 0.01f, 1000.f);
|
||||||
|
|
||||||
LoadContent();
|
LoadContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,18 +54,15 @@ void Renderer::Draw(double _dt)
|
|||||||
|
|
||||||
m_ShaderProgram.Bind();
|
m_ShaderProgram.Bind();
|
||||||
|
|
||||||
viewMatrix = glm::mat4();
|
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix();
|
||||||
|
|
||||||
viewMatrix = MyCamera.getViewMatrix(); //TEMP
|
|
||||||
|
|
||||||
glm::mat4 MVP;
|
glm::mat4 MVP;
|
||||||
|
|
||||||
for(int i = 0; i < ModelsToRender.size(); i++)
|
for(int i = 0; i < ModelsToRender.size(); i++)
|
||||||
{
|
{
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
//FIXA MED FLERA TEXTURER
|
//FIXA MED FLERA TEXTURER
|
||||||
glBindTexture(GL_TEXTURE_2D, ModelsToRender[i]->texture[0]->texture);
|
glBindTexture(GL_TEXTURE_2D, ModelsToRender[i]->texture[0]->texture);
|
||||||
//MVP = projectionMatrix * viewMatrix * /* ModelMatrix */;
|
MVP = cameraMatrix; // * ModelMatrix;
|
||||||
glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
|
glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
|
||||||
glBindVertexArray(ModelsToRender[i]->VAO);
|
glBindVertexArray(ModelsToRender[i]->VAO);
|
||||||
glDrawArrays(GL_TRIANGLES, 0, ModelsToRender[i]->Vertices.size());
|
glDrawArrays(GL_TRIANGLES, 0, ModelsToRender[i]->Vertices.size());
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
#include <glm/gtc/type_ptr.hpp>
|
#include <glm/gtc/type_ptr.hpp>
|
||||||
|
|
||||||
#include "glerror.h"
|
#include "glerror.h"
|
||||||
|
#include "Camera.h"
|
||||||
#include "ShaderProgram.h"
|
#include "ShaderProgram.h"
|
||||||
#include "Model.h"
|
#include "Model.h"
|
||||||
|
|
||||||
@@ -33,7 +34,6 @@ public:
|
|||||||
|
|
||||||
std::vector<Model*> ModelsToRender;
|
std::vector<Model*> ModelsToRender;
|
||||||
|
|
||||||
|
|
||||||
Renderer();
|
Renderer();
|
||||||
|
|
||||||
void Initialize();
|
void Initialize();
|
||||||
@@ -46,11 +46,14 @@ public:
|
|||||||
void LoadContent();
|
void LoadContent();
|
||||||
|
|
||||||
GLFWwindow* GetWindow() const { return m_Window; }
|
GLFWwindow* GetWindow() const { return m_Window; }
|
||||||
|
std::shared_ptr<Camera> GetCamera() const { return m_Camera; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLFWwindow* m_Window;
|
GLFWwindow* m_Window;
|
||||||
GLint m_glVersion[2];
|
GLint m_glVersion[2];
|
||||||
GLchar* m_glVendor;
|
GLchar* m_glVendor;
|
||||||
|
|
||||||
|
std::shared_ptr<Camera> m_Camera;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // Renderer_h__
|
#endif // Renderer_h__
|
||||||
Reference in New Issue
Block a user