Added Fly Camera and Vertex Colors

This commit is contained in:
viktorljung
2015-12-01 15:56:26 +01:00
parent 04d10bf7af
commit 07d037ae57
7 changed files with 95 additions and 14 deletions
+2
View File
@@ -3,6 +3,7 @@
#include "../Common.h" #include "../Common.h"
#include "../OpenGL.h" #include "../OpenGL.h"
#include "../GLM.h"
#include "../Core/Util/Rectangle.h" #include "../Core/Util/Rectangle.h"
#include "Camera.h" #include "Camera.h"
#include "RenderQueue.h" #include "RenderQueue.h"
@@ -29,6 +30,7 @@ public:
} }
virtual void Initialize() = 0; virtual void Initialize() = 0;
virtual void Update(double dt) = 0;
virtual void Draw(RenderQueueCollection& rq) = 0; virtual void Draw(RenderQueueCollection& rq) = 0;
protected: protected:
+4 -2
View File
@@ -12,20 +12,22 @@ class Renderer : public IRenderer
{ {
public: public:
virtual void Initialize() override; virtual void Initialize() override;
virtual void Update(double dt) override;
virtual void Draw(RenderQueueCollection& rq) override; virtual void Draw(RenderQueueCollection& rq) override;
private: private:
//----------------------Variables----------------------// //----------------------Variables----------------------//
RenderQueueCollection m_TempRQ; RenderQueueCollection m_TempRQ;
Texture* m_ErrorTexture; Texture* m_ErrorTexture;
Texture* m_WhiteTexture;
float m_CameraMoveSpeed = 3.f;
//----------------------Functions----------------------// //----------------------Functions----------------------//
void InitializeWindow(); void InitializeWindow();
void InitializeShaders(); void InitializeShaders();
//TODO: Render: Remove ModelsToDraw from Renderer. //TODO: Render: Remove ModelsToDraw from Renderer.
void ModelsToDraw(); void ModelsToDraw();
void EnqueueModel(Model* model); void EnqueueModel(Model* model);
void InputUpdate(double dt);
//--------------------ShaderPrograms-------------------// //--------------------ShaderPrograms-------------------//
ShaderProgram m_BasicForwardProgram; ShaderProgram m_BasicForwardProgram;
}; };
+5 -3
View File
@@ -8,10 +8,12 @@ uniform sampler2D texture0;
in VertexData{ in VertexData{
vec3 VertexPosition; vec3 Position;
vec3 Normal; vec3 Normal;
vec2 TextureCoordinate; vec2 TextureCoordinate;
} Input; vec4 DiffuseColor;
}Input;
out vec4 fragmentColor; out vec4 fragmentColor;
@@ -19,7 +21,7 @@ out vec4 fragmentColor;
void main() void main()
{ {
vec4 texel = texture2D(texture0, Input.TextureCoordinate); vec4 texel = texture2D(texture0, Input.TextureCoordinate);
fragmentColor = texel; fragmentColor = texel * Input.DiffuseColor;
} }
+16 -6
View File
@@ -5,20 +5,30 @@ uniform mat4 V;
uniform mat4 P; uniform mat4 P;
layout(location = 0) in vec3 Position; layout(location = 0) in vec3 Position;
layout(location = 1) in vec2 TextureCoord; layout(location = 1) in vec3 Normal;
layout(location = 2) in vec3 Normal; layout(location = 2) in vec3 Tangent;
layout(location = 3) in vec3 BiTangent;
layout(location = 4) in vec2 TextureCoords;
layout(location = 5) in vec4 DiffuseVertexColor;
layout(location = 6) in vec4 SpecularVertexColor;
layout(location = 7) in vec4 BoneIndices1;
layout(location = 8) in vec4 BoneIndices2;
layout(location = 9) in vec4 BoneWeights1;
layout(location = 10) in vec4 BoneWeights2;
out VertexData{ out VertexData{
vec3 VertexPosition; vec3 Position;
vec3 Normal; vec3 Normal;
vec2 TextureCoordinate; vec2 TextureCoordinate;
} Output; vec4 DiffuseColor;
}Output;
void main() void main()
{ {
gl_Position = P*V*M * vec4(Position, 1.0); gl_Position = P*V*M * vec4(Position, 1.0);
Output.VertexPosition = Position; Output.Position = Position;
Output.TextureCoordinate = TextureCoord; Output.TextureCoordinate = TextureCoords;
Output.Normal = Normal; Output.Normal = Normal;
Output.DiffuseColor = DiffuseVertexColor;
} }
+6
View File
@@ -17,6 +17,12 @@ glm::vec3 Camera::Forward()
{ {
return m_Orientation * glm::vec3(0, 0, -1); return m_Orientation * glm::vec3(0, 0, -1);
} }
glm::vec3 Camera::Right()
{
return m_Orientation * glm::vec3(1, 0, 0);
}
// //
//glm::vec3 Camera::Right() //glm::vec3 Camera::Right()
//{ //{
+61 -3
View File
@@ -13,6 +13,7 @@ void Renderer::Initialize()
glfwSwapInterval(m_VSYNC); glfwSwapInterval(m_VSYNC);
m_ErrorTexture = ResourceManager::Load<Texture>("Textures/Core/ErrorTexture.png"); m_ErrorTexture = ResourceManager::Load<Texture>("Textures/Core/ErrorTexture.png");
m_WhiteTexture = ResourceManager::Load<Texture>("Textures/Core/Blank.png");
InitializeShaders(); InitializeShaders();
ModelsToDraw(); ModelsToDraw();
} }
@@ -65,8 +66,6 @@ void Renderer::InitializeShaders()
m_BasicForwardProgram.Link(); m_BasicForwardProgram.Link();
} }
void Renderer::ModelsToDraw() void Renderer::ModelsToDraw()
{ {
Model *m = ResourceManager::Load<Model>("Models/translation_widget.obj"); Model *m = ResourceManager::Load<Model>("Models/translation_widget.obj");
@@ -102,6 +101,65 @@ void Renderer::EnqueueModel(Model* model)
} }
} }
void Renderer::InputUpdate(double dt)
{
glm::vec3 m_Position = m_Camera->Position();
if (glfwGetKey(m_Window, GLFW_KEY_O) == GLFW_PRESS)
{
m_Position = glm::vec3(0.f, 0.f, 5.f);
}
if (glfwGetKey(m_Window, GLFW_KEY_W) == GLFW_PRESS)
{
m_Position += m_Camera->Forward() * m_CameraMoveSpeed * (float)dt;
}
if (glfwGetKey(m_Window, GLFW_KEY_S) == GLFW_PRESS)
{
m_Position -= m_Camera->Forward() * m_CameraMoveSpeed * (float)dt;
}
if (glfwGetKey(m_Window, GLFW_KEY_D) == GLFW_PRESS)
{
m_Position += m_Camera->Right() * m_CameraMoveSpeed * (float)dt;
}
if (glfwGetKey(m_Window, GLFW_KEY_A) == GLFW_PRESS)
{
m_Position -= m_Camera->Right() * m_CameraMoveSpeed * (float)dt;
}
if (glfwGetKey(m_Window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
{
m_CameraMoveSpeed = 5.f;
}
else {
m_CameraMoveSpeed = 0.5f;
}
if (glfwGetKey(m_Window, GLFW_KEY_SPACE) == GLFW_PRESS) {
static double mousePosX, mousePosY;
glfwGetCursorPos(m_Window, &mousePosX, &mousePosY);
double deltaX, deltaY;
deltaX = mousePosX - (float)Resolution().Width / 2;
deltaY = mousePosY - (float)Resolution().Height / 2;
float rotationY = -deltaY / 300.f;
float rotationX = -deltaX / 300.f;
glm::quat orientation = m_Camera->Orientation();
orientation = orientation * glm::angleAxis<float>(rotationY, glm::vec3(1, 0, 0));
orientation = glm::angleAxis<float>(rotationX, glm::vec3(0, 1, 0)) * orientation;
m_Camera->SetOrientation(orientation);
glfwSetCursorPos(m_Window, Resolution().Width / 2, Resolution().Height / 2);
}
m_Camera->SetPosition(m_Position);
}
void Renderer::Update(double dt)
{
InputUpdate(dt);
}
void Renderer::Draw(RenderQueueCollection& rq) void Renderer::Draw(RenderQueueCollection& rq)
{ {
//TODO: Render: Clean up draw code //TODO: Render: Clean up draw code
@@ -130,7 +188,7 @@ void Renderer::Draw(RenderQueueCollection& rq)
glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture); glBindTexture(GL_TEXTURE_2D, modelJob->DiffuseTexture->m_Texture);
} else { } else {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_ErrorTexture->m_Texture); glBindTexture(GL_TEXTURE_2D, m_WhiteTexture->m_Texture);
} }
glBindVertexArray(modelJob->Model->VAO); glBindVertexArray(modelJob->Model->VAO);
+1
View File
@@ -49,6 +49,7 @@ void Game::Tick()
m_EventBroker->Swap(); m_EventBroker->Swap();
m_InputManager->Update(dt); m_InputManager->Update(dt);
m_Renderer->Update(dt);
m_EventBroker->Swap(); m_EventBroker->Swap();
//TODO: Render: This is not used, but will be used later when we dont add models to RQ in renderer. //TODO: Render: This is not used, but will be used later when we dont add models to RQ in renderer.