Skip to content

Commit

Permalink
Episode 27 - Chunk Renderer and Shader
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopson97 committed Jul 25, 2017
1 parent dac88a8 commit cbf8da8
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 6 deletions.
14 changes: 14 additions & 0 deletions Data/Shaders/Chunk_Fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#version 330

out vec4 colour;

in vec2 passTextureCoords;

uniform sampler2D ourTexture;

uniform float time;

void main()
{
colour = texture(ourTexture, passTextureCoords);
}
18 changes: 18 additions & 0 deletions Data/Shaders/Chunk_Vertex.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#version 330

layout (location = 0) in vec3 inVertexPosition;
layout (location = 1) in vec2 inTextureCoords;

out vec2 passTextureCoords;

uniform mat4 viewMatrix;
uniform mat4 projMatrix;

void main()
{
gl_Position = projMatrix * viewMatrix *
vec4 (inVertexPosition.xyz,
1.0);

passTextureCoords = inTextureCoords;
}
5 changes: 5 additions & 0 deletions Minecraft Tutorial.cbp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
<Add directory="D:/_Programming Libraries/C++/SFML/MinGW32/lib" />
<Add directory="D:/_Programming Libraries/C++/glew-2.0.0/lib/Release/Win32" />
</Linker>
<Unit filename="Data/Shaders/Simple_Fragment.glsl" />
<Unit filename="Data/Shaders/Simple_Vertex.glsl" />
<Unit filename="Source/Application.cpp" />
<Unit filename="Source/Application.h" />
<Unit filename="Source/Camera.cpp" />
Expand All @@ -67,10 +69,13 @@
<Unit filename="Source/Maths/Matrix_Maths.h" />
<Unit filename="Source/Model.cpp" />
<Unit filename="Source/Model.h" />
<Unit filename="Source/Renderer/RChunk.h" />
<Unit filename="Source/Renderer/RMaster.cpp" />
<Unit filename="Source/Renderer/RMaster.h" />
<Unit filename="Source/Renderer/RSimple.cpp" />
<Unit filename="Source/Renderer/RSimple.h" />
<Unit filename="Source/Shaders/Chunk_Shader.cpp" />
<Unit filename="Source/Shaders/Chunk_Shader.h" />
<Unit filename="Source/Shaders/Shader_Loader.cpp" />
<Unit filename="Source/Shaders/Shader_Loader.h" />
<Unit filename="Source/Shaders/Shader_Program.cpp" />
Expand Down
28 changes: 28 additions & 0 deletions Source/Renderer/RChunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "RChunk.h"

#include "../Camera.h"
#include "../World/Chunk/Chunk_Section.h"

namespace Renderer
{
void Chunk_Renderer::draw(const Chunk::Chunk_Section& chunk)
{
m_chunks.push_back(&chunk);
}

void Chunk_Renderer::update(const Camera& camera)
{
m_shader.bind();

m_shader.setViewMatrix(camera.getViewMatrix());
m_shader.setProjMatrix(camera.getProjectionMatrix());

for (auto& chunk : m_chunks)
{

}

m_chunks.clear();
}

}
32 changes: 32 additions & 0 deletions Source/Renderer/RChunk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef RCHUNK_H_INCLUDED
#define RCHUNK_H_INCLUDED

#include <vector>
#include <SFML/System/Clock.hpp>

#include "../Shaders/Chunk_Shader.h"

namespace Chunk
{
class Chunk_Section;
}

struct Camera;

namespace Renderer
{
class Chunk_Renderer
{
public:
void draw (const Chunk::Chunk_Section& chunk);

void update(const Camera& camera);

private:
std::vector<const Chunk::Chunk_Section*> m_chunks;

Shader::Chunk_Shader m_shader;
};
}

#endif // RCHUNK_H_INCLUDED
7 changes: 7 additions & 0 deletions Source/Renderer/RMaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Renderer
void Master::update(const Camera& camera)
{
m_simpleRenderer.update(camera);
m_chunkRenderer.update(camera);

Display::get().update();
}
Expand All @@ -21,4 +22,10 @@ namespace Renderer
m_simpleRenderer.draw(model);
}

void Master::draw(const Chunk::Chunk_Section& chunk)
{
m_chunkRenderer.draw(chunk);
}


}
3 changes: 3 additions & 0 deletions Source/Renderer/RMaster.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MASTER_H_INCLUDED

#include "RSimple.h"
#include "RChunk.h"

class Quad;
struct Camera;
Expand All @@ -16,9 +17,11 @@ namespace Renderer
void update(const Camera& camera);

void draw(const Quad& model);
void draw(const Chunk::Chunk_Section& model);

private:
Simple m_simpleRenderer;
Chunk_Renderer m_chunkRenderer;
};
}

Expand Down
13 changes: 13 additions & 0 deletions Source/Shaders/Chunk_Shader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "Chunk_Shader.h"

namespace Shader
{
Chunk_Shader::Chunk_Shader()
: Simple_Shader ("Chunk_Vertex", "Chunk_Fragment")
{
getUniformLocations();
}

void Chunk_Shader::getUniformLocations()
{ }
}
18 changes: 18 additions & 0 deletions Source/Shaders/Chunk_Shader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CHUNK_SHADER_H_INCLUDED
#define CHUNK_SHADER_H_INCLUDED

#include "Simple_Shader.h"

namespace Shader
{
class Chunk_Shader : public Simple_Shader
{
public:
Chunk_Shader();

private:
void getUniformLocations() override;
};
}

#endif // CHUNK_SHADER_H_INCLUDED
6 changes: 3 additions & 3 deletions Source/Shaders/Simple_Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

namespace Shader
{
Simple_Shader::Simple_Shader()
: Shader_Program ("Simple_Vertex", "Simple_Fragment")
Simple_Shader::Simple_Shader(const std::string& vertexFile,
const std::string& fragmentFile)
: Shader_Program (vertexFile, fragmentFile)
{
getUniformLocations();
}
Expand Down Expand Up @@ -38,7 +39,6 @@ namespace Shader
m_locationViewMatrix = glGetUniformLocation(getID(), "viewMatrix");
m_locationModelMatrix = glGetUniformLocation(getID(), "modelMatrix");
m_locationProjMatrix = glGetUniformLocation(getID(), "projMatrix");

}

}
9 changes: 6 additions & 3 deletions Source/Shaders/Simple_Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ namespace Shader
class Simple_Shader : public Shader_Program
{
public:
Simple_Shader();
Simple_Shader(const std::string& vertexFile = "Simple_Vertex",
const std::string& fragmentFile = "Simple_Fragment");

void setTime(float time);

void setViewMatrix (const Matrix4& matrix);
void setModelMatrix (const Matrix4& matrix);
void setProjMatrix (const Matrix4& matrix);

private:
void getUniformLocations() override;
protected:
virtual void getUniformLocations() override;


private:
GLuint m_locationTime = 0;
GLuint m_locationViewMatrix = 0;
GLuint m_locationModelMatrix = 0;
Expand Down
1 change: 1 addition & 0 deletions Source/States/Game_State.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace State
{
public:
Game_State(Application& application);
virtual ~Game_State() = default;

virtual void input (Camera& camera) = 0;
virtual void update (Camera& camera, float dt) = 0;
Expand Down

0 comments on commit cbf8da8

Please sign in to comment.