Added Camera.

This commit is contained in:
2014-03-09 20:32:57 +01:00
parent b5349ba93c
commit fcbf802cc2
6 changed files with 140 additions and 47 deletions
+74
View File
@@ -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);
}
+53
View File
@@ -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__
+2
View File
@@ -97,6 +97,7 @@
</Lib>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Camera.cpp" />
<ClCompile Include="Engine.h" />
<ClCompile Include="Frame.cpp" />
<ClCompile Include="GameFrame.cpp" />
@@ -117,6 +118,7 @@
<ClCompile Include="Systems\SoundsSystem.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Camera.h" />
<ClInclude Include="Color.h" />
<ClInclude Include="Component.h" />
<ClInclude Include="Components\Camera.h" />
@@ -43,6 +43,7 @@
<ClCompile Include="GameFrame.cpp">
<Filter>GUI</Filter>
</ClCompile>
<ClCompile Include="Camera.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Component.h" />
@@ -140,6 +141,7 @@
<Filter>GUI</Filter>
</ClInclude>
<ClInclude Include="GameWorld.h" />
<ClInclude Include="Camera.h" />
</ItemGroup>
<ItemGroup>
<None Include="Shaders\Fragment.glsl">
+5 -46
View File
@@ -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()
{
// Initialize GLFW
@@ -82,6 +41,9 @@ void Renderer::Initialize()
}
glEnable(GL_DEPTH_TEST);
// Create Camera
m_Camera = std::make_shared<Camera>(90.f, WIDTH / HEIGHT, 0.01f, 1000.f);
LoadContent();
}
@@ -92,18 +54,15 @@ void Renderer::Draw(double _dt)
m_ShaderProgram.Bind();
viewMatrix = glm::mat4();
viewMatrix = MyCamera.getViewMatrix(); //TEMP
glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix();
glm::mat4 MVP;
for(int i = 0; i < ModelsToRender.size(); i++)
{
glActiveTexture(GL_TEXTURE0);
//FIXA MED FLERA TEXTURER
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));
glBindVertexArray(ModelsToRender[i]->VAO);
glDrawArrays(GL_TRIANGLES, 0, ModelsToRender[i]->Vertices.size());
+4 -1
View File
@@ -19,6 +19,7 @@
#include <glm/gtc/type_ptr.hpp>
#include "glerror.h"
#include "Camera.h"
#include "ShaderProgram.h"
#include "Model.h"
@@ -33,7 +34,6 @@ public:
std::vector<Model*> ModelsToRender;
Renderer();
void Initialize();
@@ -46,11 +46,14 @@ public:
void LoadContent();
GLFWwindow* GetWindow() const { return m_Window; }
std::shared_ptr<Camera> GetCamera() const { return m_Camera; }
private:
GLFWwindow* m_Window;
GLint m_glVersion[2];
GLchar* m_glVendor;
std::shared_ptr<Camera> m_Camera;
};
#endif // Renderer_h__