Well faces kinda render

This commit is contained in:
Ben
2019-10-06 20:23:41 +01:00
parent 656f97834b
commit 33735ef2e8
14 changed files with 159 additions and 122 deletions

View File

@@ -1,12 +1,11 @@
#version 330
in vec2 TexCoord;
in vec3 TexCoord;
out vec4 outColour;
uniform vec3 triangleColour;
uniform sampler2D tex;
// uniform sampler2D tex;
void main() {
outColour = texture(tex, TexCoord);
outColour = vec4(TexCoord, 1.0); // texture(tex, TexCoord);
}

View File

@@ -1,9 +1,9 @@
#version 330
in vec3 position;
in vec2 texcoord;
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 texcoord;
out vec2 TexCoord;
out vec3 TexCoord;
uniform mat4 model;
uniform mat4 view;

View File

@@ -15,6 +15,7 @@
#include <string>
#include <vector>
#include <memory>
#include <map>
#if _WIN32
#include <SDL.h>

View File

@@ -4,6 +4,7 @@
#include <logger.h>
#include "renderer/renderer.hpp"
#include "renderer/shader.hpp"
#include "renderer/camera.hpp"
#include "renderer/face.hpp"
@@ -56,8 +57,8 @@ void Game::Setup(int w, int h) {
*m_logger << LOGGER_INFO << "Creating OpenGL context" << LOGGER_ENDL;
m_glContext = SDL_GL_CreateContext(m_window);
//SDL_WarpMouseInWindow(m_window, w / 2, h / 2);
//SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_WarpMouseInWindow(m_window, w / 2, h / 2);
SDL_SetRelativeMouseMode(SDL_TRUE);
// Set VSYNC swap interval
SDL_GL_SetSwapInterval(1);
@@ -70,7 +71,7 @@ void Game::Setup(int w, int h) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#ifdef __IMGUI
IMGUI_CHECKVERSION();
@@ -109,12 +110,13 @@ void Game::Setup(int w, int h) {
void Game::Input(SDL_Event* e) {
while (SDL_PollEvent(e))
while (SDL_PollEvent(e)) {
m_activeCamera->HandleMouse(*e);
if (e->type == SDL_QUIT)
IsDisplayOpen = false;
}
m_activeCamera->MoveCamera();
m_activeCamera->HandleMouse(*e);
}
@@ -123,12 +125,20 @@ void Game::Run() {
SDL_Event e;
const float clear[] = { 0.1f, 0.45f, 0.9f, 1.0f };
glClearBufferfv(GL_COLOR, 0, clear);
m_renderer = std::make_unique<Renderer>();
m_world = std::make_unique<World>();
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Top, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Bottom, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Right, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Left, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Front, 1));
m_world->Faces.push_back(std::make_shared<Face>(FaceDirection::Back, 1));
m_world->Shaders["Basic"] = std::make_shared<Shader>();
m_world->Shaders["Basic"]->Load("E:/Games/minecraft/resources/shaders/simple");
m_world->Shaders["Basic"]->Link();
while (IsDisplayOpen) {
@@ -142,7 +152,8 @@ void Game::Run() {
if (ImGui::Button("Save")) {}
ImGui::End();
#endif
glClear(GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearBufferfv(GL_COLOR, 0, clear);
m_renderer->Render(m_world , m_activeCamera);

View File

@@ -15,8 +15,8 @@ public:
void MoveCamera();
void MouseMoved(glm::vec2 mouseDelta);
float MouseSensitivity = 0.0025f;
float CameraSpeed = 1.0f;
float MouseSensitivity = 0.0009f;
float CameraSpeed = 0.1f;
private:
float roll, pitch, yaw;

View File

@@ -1,5 +1,8 @@
#include "face.hpp"
#include "shader.hpp"
#include "camera.hpp"
Face::Face(FaceDirection direction, int textureID) {
Direction = direction;
Texture = textureID;
@@ -13,106 +16,116 @@ Face::Face(FaceDirection direction, int textureID) {
{ 0.0f, 0.0f }
};
switch (Direction) {case FaceDirection::Top:
{
if (Direction == FaceDirection::Top) {
m_verticies = {
{ -0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ -0.5f, 0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
};
}
else if (Direction == FaceDirection::Bottom) {
m_verticies = {
{ -0.5f, -0.5f, 0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, -0.5f, 0.5f },
};
}
else if (Direction == FaceDirection::Front) {
m_verticies = {
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
{ -0.5f, -0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f },
};
}
else if (Direction == FaceDirection::Back) {
m_verticies = {
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
};
}
else if (Direction == FaceDirection::Left) {
m_verticies = {
{ -0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, -0.5f, 0.5f },
{ -0.5f, -0.5f, 0.5f },
{ -0.5f, -0.5f, -0.5f },
};
} else if (Direction == FaceDirection::Right) {
m_verticies = {
{ -0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, -0.5f },
};
}
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
m_verticies = {
{ -0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ -0.5f, 0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
};
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
break;
std::vector<glm::vec3> uvs;
for (auto& uv : m_uvs) {
uvs.push_back({ uv.x, uv.y, textureID });
}
}
std::vector<glm::vec3> data;
data.insert(data.end(), m_verticies.begin(), m_verticies.end());
data.insert(data.end(), uvs.begin(), uvs.end());
case FaceDirection::Bottom:
{
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(glm::vec3), &data[0], GL_STATIC_DRAW);
m_verticies = {
{ -0.5f, -0.5f, 0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, -0.5f, 0.5f },
};
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (const void*)0);
break;
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (const void*)(m_verticies.size() * sizeof(glm::vec3)));
}
case FaceDirection::Front:
{
m_verticies = {
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
{ -0.5f, -0.5f, -0.5f },
{ -0.5f, -0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f },
};
break;
}
case FaceDirection::Back:
{
m_verticies = {
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
};
break;
}
case FaceDirection::Left:
{
m_verticies = {
{ -0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, -0.5f },
{ 0.5f, -0.5f, 0.5f },
{ 0.5f, -0.5f, 0.5f },
{ -0.5f, -0.5f, 0.5f },
{ -0.5f, -0.5f, -0.5f },
};
break;
}
case FaceDirection::Right:
{
m_verticies = {
{ -0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, -0.5f },
{ 0.5f, 0.5f, 0.5f },
{ 0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, 0.5f },
{ -0.5f, 0.5f, -0.5f },
};
break;
}
};
glBindVertexArray(1);
}
void Face::GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec2>& uvs) {
}
void Face::Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader) {
shader->Use();
glm::mat4 model = glm::mat4(1.0f);
GLint uniTrans = glGetUniformLocation(shader->Program, "model");
glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
GLint uniView = glGetUniformLocation(shader->Program, "view");
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(camera->GetViewMatrix()));
// Projection matrice
glm::mat4 proj = glm::perspective(glm::radians(45.0f), 1080.0f / 720.0f, 1.0f, 1000.0f);
// Get uniform and send it to the GPU
GLint uniProj = glGetUniformLocation(shader->Program, "proj");
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
glDrawArrays(GL_TRIANGLES, 0, m_verticies.size());
}

View File

@@ -3,6 +3,9 @@
#include "../common.hpp"
class Camera;
class Shader;
enum FaceDirection {
Top,
Bottom,
@@ -17,16 +20,20 @@ public:
Face(FaceDirection direction, int textureID);
void GetMesh(std::vector<glm::vec3>& verts, std::vector<glm::vec2>& uvs);
void Render(std::shared_ptr<Camera> camera, std::shared_ptr<Shader> shader);
int Texture = 0;
FaceDirection Direction = FaceDirection::Up;
FaceDirection Direction = FaceDirection::Top;
private:
std::vector<glm::vec3> m_verticies;
std::vector<glm::vec2> m_uvs;
GLuint m_vao;
GLuint m_vbo;
};
#endif

View File

@@ -1,5 +1,9 @@
#include "renderer.hpp"
#include "../world/world.hpp"
#include "shader.hpp"
#include "face.hpp"
Renderer::Renderer() {
}
@@ -7,6 +11,8 @@ Renderer::Renderer() {
// Perform the render passes
void Renderer::Render(std::shared_ptr<World> world, std::shared_ptr<Camera> camera) {
for (int i = 0; i < world->Faces.size(); i++) {
world->Faces[i]->Render(camera, world->Shaders["Basic"]);
}
}

View File

@@ -3,8 +3,8 @@
#include "../common.hpp"
#include "../world/world.hpp"
#include "camera.hpp"
class Camera;
class World;
// Does GL render passes then returns to the game loop
class Renderer {

View File

@@ -3,7 +3,7 @@
Shader::Shader()
: m_fileReader() {
m_program = 0;
Program = 0;
m_frag = 0;
m_vert = 0;
@@ -23,8 +23,6 @@ void Shader::Load(std::string path) {
Load(fragmentLocation, GL_FRAGMENT_SHADER);
*m_logger << LOGGER_INFO << "Fragment shader at '" << fragmentLocation << "' loaded..." << LOGGER_ENDL;
Link();
}
@@ -63,23 +61,23 @@ void Shader::Link() {
*m_logger << LOGGER_INFO << "Fragment shader '" << m_frag << "' compiled..." << LOGGER_ENDL;
}
m_program = glCreateProgram();
Program = glCreateProgram();
glAttachShader(m_program, m_vert);
glAttachShader(m_program, m_frag);
glAttachShader(Program, m_vert);
glAttachShader(Program, m_frag);
glLinkProgram(m_program);
glLinkProgram(Program);
glDeleteShader(m_vert);
glDeleteShader(m_frag);
*m_logger << LOGGER_INFO << "Program '" << m_program << "' loaded..." << LOGGER_ENDL;
*m_logger << LOGGER_INFO << "Program '" << Program << "' loaded..." << LOGGER_ENDL;
}
void Shader::Use() {
glUseProgram(m_program);
glUseProgram(Program);
}
@@ -104,7 +102,7 @@ bool Shader::m_CheckShader(GLuint uid) {
Shader::~Shader() {
glDeleteProgram(m_program);
glDeleteProgram(Program);
glDeleteShader(m_vert);
glDeleteShader(m_frag);

View File

@@ -13,6 +13,7 @@ public:
void Load(std::string path);
void Load(std::string path, GLenum type);
GLuint Program;
void Link();
void Use();
@@ -25,7 +26,6 @@ private:
FileReader m_fileReader;
GLuint m_program;
GLuint m_vert;
GLuint m_frag;
};

0
src/renderer/voxel.cpp Normal file
View File

0
src/renderer/voxel.hpp Normal file
View File

View File

@@ -3,11 +3,13 @@
#include "../common.hpp"
class Shader;
class Face;
class World {
public:
std::map<std::string, std::shared_ptr<Shader>> Shaders;
std::vector<std::shared_ptr<Face>> Faces;
};