Organization
parent
537a242a5c
commit
629c44c775
|
@ -3,18 +3,40 @@ project(GlProject)
|
|||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
|
||||
include_directories(GlProject/Libraries/glew/include/GL)
|
||||
include_directories(GlProject/Libraries/glfw_linux/include)
|
||||
include_directories(GlProject/Libraries/glm)
|
||||
include_directories(GlProject/Libraries/stb_image)
|
||||
include_directories(Libraries/glew/include/GL)
|
||||
include_directories(Libraries/glfw_linux/include)
|
||||
include_directories(Libraries/glm)
|
||||
include_directories(Libraries/stb_image)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(glfw3 REQUIRED)
|
||||
|
||||
link_directories(GlProject/Libraries/glew/lib)
|
||||
link_directories(Libraries/glew/lib)
|
||||
|
||||
add_executable(GlProject
|
||||
GlProject/Main.cpp GlProject/Mesh.cpp GlProject/Mesh.h GlProject/Entity.cpp GlProject/Entity.h GlProject/Shader.cpp GlProject/Shader.h GlProject/Window.cpp GlProject/Window.h GlProject/Camera.cpp GlProject/Camera.h GlProject/Texture.cpp GlProject/Texture.h GlProject/MeshGenerator.cpp GlProject/MeshGenerator.h GlProject/MeshData.cpp GlProject/MeshData.h)
|
||||
GlProject/Main.cpp
|
||||
GlProject/engine/graphics/Mesh.cpp
|
||||
GlProject/engine/graphics/Mesh.h
|
||||
GlProject/engine/Entity.cpp
|
||||
GlProject/engine/Entity.h
|
||||
GlProject/engine/graphics/Shader.cpp
|
||||
GlProject/engine/graphics/Shader.h
|
||||
GlProject/engine/Window.cpp
|
||||
GlProject/engine/Window.h
|
||||
GlProject/engine/Camera.cpp
|
||||
GlProject/engine/Camera.h
|
||||
GlProject/engine/graphics/Texture.cpp
|
||||
GlProject/engine/graphics/Texture.h
|
||||
GlProject/mesh/MeshGenerator.cpp
|
||||
GlProject/mesh/MeshGenerator.h
|
||||
GlProject/mesh/MeshData.cpp
|
||||
GlProject/mesh/MeshData.h
|
||||
GlProject/engine/Timer.cpp
|
||||
GlProject/engine/Timer.h
|
||||
GlProject/blocks/BlockAtlas.cpp
|
||||
GlProject/blocks/BlockAtlas.h
|
||||
GlProject/blocks/BlockDef.cpp
|
||||
GlProject/blocks/BlockDef.h)
|
||||
|
||||
target_link_libraries(GlProject
|
||||
${OPENGL_gl_LIBRARY}
|
||||
|
|
|
@ -11,14 +11,15 @@
|
|||
#include <gtc/matrix_transform.hpp>
|
||||
#include <gtc/type_ptr.hpp>
|
||||
|
||||
#include "Window.h"
|
||||
#include "Mesh.h"
|
||||
#include "Entity.h"
|
||||
#include "Shader.h"
|
||||
#include "Camera.h"
|
||||
#include "Texture.h"
|
||||
#include "MeshData.h"
|
||||
#include "MeshGenerator.h"
|
||||
#include "engine/Window.h"
|
||||
#include "engine/graphics/Mesh.h"
|
||||
#include "engine/Entity.h"
|
||||
#include "engine/graphics/Shader.h"
|
||||
#include "engine/Camera.h"
|
||||
#include "engine/graphics/Texture.h"
|
||||
#include "mesh/MeshData.h"
|
||||
#include "mesh/MeshGenerator.h"
|
||||
#include "engine/Timer.h"
|
||||
|
||||
Window* window;
|
||||
Shader* shader;
|
||||
|
@ -33,22 +34,30 @@ GLfloat lastTime = 0.0f;
|
|||
|
||||
void makeEntities() {
|
||||
|
||||
auto meshGen = new MeshGenerator();
|
||||
int array[4][4][4] {
|
||||
{ {0, 1, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 1, 1, 0} },
|
||||
{ {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 1, 1, 0}, {1, 0, 0, 1} },
|
||||
{ {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 1, 1, 0}, {1, 0, 0, 1} },
|
||||
{ {0, 1, 1, 0}, {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 1, 1, 0} }};
|
||||
MeshData* m = meshGen->generate(array);
|
||||
int array[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
|
||||
for (int i = 0; i < CHUNK_SIZE; i++) {
|
||||
for (int j = 0; j < CHUNK_SIZE; j++) {
|
||||
for (int k = 0; k < CHUNK_SIZE; k++) {
|
||||
array[i][j][k] = (j < 8) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto meshGen = new MeshGenerator();
|
||||
MeshData *m = meshGen->build(array);
|
||||
delete meshGen;
|
||||
|
||||
Mesh* mesh = new Mesh();
|
||||
mesh->create(&m->vertices[0], &m->indices[0], (int)m->vertices.size(), (int)m->indices.size());
|
||||
|
||||
auto* chunk = new Entity();
|
||||
chunk->create(mesh);
|
||||
chunk->setPosition(glm::vec3(0, 0, -5));
|
||||
// chunk->setScale(0.5);
|
||||
entities.push_back(chunk);
|
||||
for (int i = -16; i < 16; i++) {
|
||||
for (int j = -16; j < 16; j++) {
|
||||
auto *chunk = new Entity();
|
||||
chunk->create(mesh);
|
||||
chunk->setPosition(glm::vec3(i * CHUNK_SIZE, 0, j * CHUNK_SIZE));
|
||||
chunk->setScale(0.5);
|
||||
entities.push_back(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
@ -60,10 +69,10 @@ int main() {
|
|||
camera = new Camera(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0, 1, 0), -90.0f, 0.0f, 10.0f, 0.1f);
|
||||
|
||||
//Load Textures
|
||||
brickTexture = new Texture((char*)"../GlProject/Textures/brick.png");
|
||||
brickTexture = new Texture((char*)"../Textures/brick.png");
|
||||
brickTexture->load();
|
||||
|
||||
dirtTexture = new Texture((char*)"../GlProject/Textures/dirt.png");
|
||||
dirtTexture = new Texture((char*)"../Textures/default_dirt.png");
|
||||
dirtTexture->load();
|
||||
|
||||
//Create entities
|
||||
|
@ -71,7 +80,7 @@ int main() {
|
|||
|
||||
//Create shader
|
||||
shader = new Shader();
|
||||
shader->createFromFile("../GlProject/Shaders/world.vs", "../GlProject/Shaders/world.fs");
|
||||
shader->createFromFile("../GlProject/shader/world.vs", "../GlProject/shader/world.fs");
|
||||
|
||||
glm::mat4 projectionMatrix = glm::perspective(45.0f, window->getBufferWidth() / window->getBufferHeight(), 0.1f, 100.0f);
|
||||
|
||||
|
|
|
@ -1,176 +0,0 @@
|
|||
//
|
||||
// Created by aurailus on 01/12/18.
|
||||
//
|
||||
|
||||
#include <cstdio>
|
||||
#include "MeshGenerator.h"
|
||||
|
||||
MeshGenerator::MeshGenerator() = default;
|
||||
|
||||
MeshData* MeshGenerator::generate(int blocks[4][4][4]) {
|
||||
std::vector<float> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
|
||||
int lastIndex = 0;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
for (int k = 0; k < 4; k++) {
|
||||
if (blocks[i][j][k] == 1) {
|
||||
add_top_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
add_bottom_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
|
||||
add_front_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
add_back_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
|
||||
add_left_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
add_right_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto* m = new MeshData();
|
||||
m->vertices = vertices;
|
||||
m->indices = indices;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_face(std::vector<float> *meshVerts, std::vector<unsigned int> *meshInds, int *indOffset,
|
||||
std::vector<float> *faceVerts, std::vector<unsigned int> *faceInds, int *x, int *y, int *z) {
|
||||
|
||||
int numOfVertices = (int)(faceVerts->size() / 5); //Replace 5 with however many values there are per vert
|
||||
|
||||
glm::vec3 normal;
|
||||
|
||||
for (int i = 0; i < numOfVertices; i++) {
|
||||
//Vertices
|
||||
meshVerts->push_back((*faceVerts)[i*5 + 0] + *x);
|
||||
meshVerts->push_back((*faceVerts)[i*5 + 1] + *y);
|
||||
meshVerts->push_back((*faceVerts)[i*5 + 2] + *z);
|
||||
|
||||
//TexCoords
|
||||
meshVerts->push_back((*faceVerts)[i*5 + 3]);
|
||||
meshVerts->push_back((*faceVerts)[i*5 + 4]);
|
||||
|
||||
//Normals
|
||||
if (i % 4 == 0) {
|
||||
normal = glm::triangleNormal(
|
||||
glm::vec3((*faceVerts)[(i )*5 + 0], (*faceVerts)[(i )*5 + 1], (*faceVerts)[(i )*5 + 2]),
|
||||
glm::vec3((*faceVerts)[(i+1)*5 + 0], (*faceVerts)[(i+1)*5 + 1], (*faceVerts)[(i+1)*5 + 2]),
|
||||
glm::vec3((*faceVerts)[(i+2)*5 + 0], (*faceVerts)[(i+2)*5 + 1], (*faceVerts)[(i+2)*5 + 2]));
|
||||
}
|
||||
meshVerts->push_back(normal.x);
|
||||
meshVerts->push_back(normal.y);
|
||||
meshVerts->push_back(normal.z);
|
||||
}
|
||||
|
||||
for (auto i : *faceInds) {
|
||||
meshInds->push_back(*indOffset + i);
|
||||
}
|
||||
|
||||
*indOffset += numOfVertices;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_top_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z) {
|
||||
float face_vertices[] {
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_bottom_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z) {
|
||||
float face_vertices[] {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_front_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z){
|
||||
float face_vertices[] {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_back_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z){
|
||||
float face_vertices[] {
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_left_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z){
|
||||
float face_vertices[] {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_right_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z){
|
||||
float face_vertices[] {
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z);
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
//
|
||||
// Created by aurailus on 01/12/18.
|
||||
//
|
||||
|
||||
#ifndef GLPROJECT_MESHGENERATOR_H
|
||||
#define GLPROJECT_MESHGENERATOR_H
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
|
||||
#include "MeshData.h"
|
||||
#include <vector>
|
||||
#include <gtx/normal.hpp>
|
||||
|
||||
class MeshGenerator {
|
||||
public:
|
||||
MeshGenerator();
|
||||
|
||||
MeshData* generate(int blocks[4][4][4]);
|
||||
private:
|
||||
void add_face(std::vector<float> *meshVerts, std::vector<unsigned int> *meshInds, int *indOffset,
|
||||
std::vector<float> *faceVerts, std::vector<unsigned int> *faceInds, int *x, int *y, int *z);
|
||||
|
||||
void add_top_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
void add_bottom_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
|
||||
void add_front_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
void add_back_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
|
||||
void add_left_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
void add_right_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
};
|
||||
|
||||
|
||||
#endif //GLPROJECT_MESHGENERATOR_H
|
Binary file not shown.
Before Width: | Height: | Size: 766 KiB |
|
@ -0,0 +1,5 @@
|
|||
//
|
||||
// Created by aurailus on 02/12/18.
|
||||
//
|
||||
|
||||
#include "BlockAtlas.h"
|
|
@ -0,0 +1,14 @@
|
|||
//
|
||||
// Created by aurailus on 02/12/18.
|
||||
//
|
||||
|
||||
#ifndef GLPROJECT_BLOCKATLAS_H
|
||||
#define GLPROJECT_BLOCKATLAS_H
|
||||
|
||||
|
||||
class BlockAtlas {
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //GLPROJECT_BLOCKATLAS_H
|
|
@ -0,0 +1,5 @@
|
|||
//
|
||||
// Created by aurailus on 02/12/18.
|
||||
//
|
||||
|
||||
#include "BlockDef.h"
|
|
@ -0,0 +1,13 @@
|
|||
//
|
||||
// Created by aurailus on 02/12/18.
|
||||
//
|
||||
|
||||
#ifndef GLPROJECT_BLOCKDEF_H
|
||||
#define GLPROJECT_BLOCKDEF_H
|
||||
|
||||
class BlockDef {
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif //GLPROJECT_BLOCKDEF_H
|
|
@ -8,7 +8,7 @@
|
|||
#include <glm.hpp>
|
||||
#include <gtc/matrix_transform.hpp>
|
||||
|
||||
#include "Mesh.h"
|
||||
#include "graphics/Mesh.h"
|
||||
|
||||
class Entity {
|
||||
public:
|
|
@ -0,0 +1,18 @@
|
|||
//
|
||||
// Created by aurailus on 02/12/18.
|
||||
//
|
||||
|
||||
#include "Timer.h"
|
||||
|
||||
Timer::Timer(const char* name) {
|
||||
this->name = name;
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
}
|
||||
|
||||
void Timer::elapsed() {
|
||||
auto finish = std::chrono::high_resolution_clock::now();
|
||||
|
||||
long elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count();
|
||||
|
||||
printf("%s took %ld ns.\n", this->name, elapsed);
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
//
|
||||
// Created by aurailus on 02/12/18.
|
||||
//
|
||||
|
||||
#ifndef GLPROJECT_TIMER_H
|
||||
#define GLPROJECT_TIMER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
explicit Timer(const char* name);
|
||||
|
||||
void elapsed();
|
||||
private:
|
||||
const char* name;
|
||||
std::chrono::high_resolution_clock::time_point start;
|
||||
};
|
||||
|
||||
|
||||
#endif //GLPROJECT_TIMER_H
|
|
@ -100,6 +100,8 @@ double Window::getDeltaY() {
|
|||
return deltaY;
|
||||
}
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-parameter"
|
||||
void Window::handleKeys(GLFWwindow* glfwWindow, int key, int code, int action, int mode) {
|
||||
auto window = static_cast<Window*>(glfwGetWindowUserPointer(glfwWindow));
|
||||
|
||||
|
@ -116,6 +118,7 @@ void Window::handleKeys(GLFWwindow* glfwWindow, int key, int code, int action, i
|
|||
}
|
||||
}
|
||||
}
|
||||
#pragma clang diagnostic pop
|
||||
|
||||
Window::~Window() {
|
||||
glfwDestroyWindow(mainWindow);
|
|
@ -27,12 +27,12 @@ void Texture::load() {
|
|||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||
glTexParameteri(GL_TEXTURE, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);
|
||||
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
// glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
|
|
@ -0,0 +1,196 @@
|
|||
//
|
||||
// Created by aurailus on 01/12/18.
|
||||
//
|
||||
|
||||
#include <cstdio>
|
||||
#include "MeshGenerator.h"
|
||||
#include "../engine/Timer.h"
|
||||
|
||||
MeshGenerator::MeshGenerator() {
|
||||
vertices = new std::vector<float>;
|
||||
indices = new std::vector<unsigned int>;
|
||||
indOffset = 0;
|
||||
}
|
||||
|
||||
MeshData* MeshGenerator::build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE]) {
|
||||
auto t = Timer("MeshGen");
|
||||
|
||||
vertices->reserve(32768);
|
||||
indices->reserve(8192);
|
||||
|
||||
for (int i = 0; i < CHUNK_SIZE; i++) {
|
||||
for (int j = 0; j < CHUNK_SIZE; j++) {
|
||||
for (int k = 0; k < CHUNK_SIZE; k++) {
|
||||
if (blocks[i][j][k] == 1) {
|
||||
add_top_face(&k, &j, &i);
|
||||
add_bottom_face(&k, &j, &i);
|
||||
|
||||
add_front_face(&k, &j, &i);
|
||||
add_back_face(&k, &j, &i);
|
||||
|
||||
add_left_face(&k, &j, &i);
|
||||
add_right_face(&k, &j, &i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vertices->shrink_to_fit();
|
||||
indices->shrink_to_fit();
|
||||
|
||||
auto* m = new MeshData();
|
||||
m->vertices = *vertices;
|
||||
m->indices = *indices;
|
||||
|
||||
t.elapsed();
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_vertices(std::vector<float> *verts, std::vector<unsigned int> *inds, int *x, int *y, int *z) {
|
||||
//How many values there are per vertex
|
||||
const unsigned int VERT_SIZE = 5;
|
||||
|
||||
//Total vertices in the array
|
||||
int numOfVertices = (int)(verts->size() / VERT_SIZE);
|
||||
|
||||
glm::vec3 normal;
|
||||
|
||||
for (int i = 0; i < numOfVertices; i++) {
|
||||
//Vertices
|
||||
vertices->push_back((*verts)[i*VERT_SIZE + 0] + *x);
|
||||
vertices->push_back((*verts)[i*VERT_SIZE + 1] + *y);
|
||||
vertices->push_back((*verts)[i*VERT_SIZE + 2] + *z);
|
||||
|
||||
//TexCoords
|
||||
vertices->push_back((*verts)[i*VERT_SIZE + 3]);
|
||||
vertices->push_back((*verts)[i*VERT_SIZE + 4]);
|
||||
|
||||
//Normals
|
||||
if (i % 4 == 0) {
|
||||
normal = glm::triangleNormal(
|
||||
glm::vec3((*verts)[(i )*VERT_SIZE + 0], (*verts)[(i )*VERT_SIZE + 1], (*verts)[(i )*VERT_SIZE + 2]),
|
||||
glm::vec3((*verts)[(i+1)*VERT_SIZE + 0], (*verts)[(i+1)*VERT_SIZE + 1], (*verts)[(i+1)*VERT_SIZE + 2]),
|
||||
glm::vec3((*verts)[(i+2)*VERT_SIZE + 0], (*verts)[(i+2)*VERT_SIZE + 1], (*verts)[(i+2)*VERT_SIZE + 2]));
|
||||
}
|
||||
vertices->push_back(normal.x);
|
||||
vertices->push_back(normal.y);
|
||||
vertices->push_back(normal.z);
|
||||
}
|
||||
|
||||
for (auto i : *inds) {
|
||||
indices->push_back(indOffset + i);
|
||||
}
|
||||
|
||||
indOffset += numOfVertices;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_top_face(int *x, int *y, int *z) {
|
||||
float face_vertices[] {
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_vertices(&vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_bottom_face(int *x, int *y, int *z) {
|
||||
float face_vertices[] {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_vertices(&vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_front_face(int *x, int *y, int *z){
|
||||
float face_vertices[] {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_vertices(&vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_back_face(int *x, int *y, int *z){
|
||||
float face_vertices[] {
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_vertices(&vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_left_face(int *x, int *y, int *z){
|
||||
float face_vertices[] {
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_vertices(&vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::add_right_face(int *x, int *y, int *z){
|
||||
float face_vertices[] {
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
};
|
||||
unsigned int face_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
|
||||
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
|
||||
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
|
||||
|
||||
add_vertices(&vec, &vec2, x, y, z);
|
||||
}
|
||||
|
||||
void MeshGenerator::cleanup() {
|
||||
|
||||
}
|
||||
|
||||
MeshGenerator::~MeshGenerator() {
|
||||
cleanup();
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
//
|
||||
// Created by aurailus on 01/12/18.
|
||||
//
|
||||
|
||||
#ifndef GLPROJECT_MESHGENERATOR_H
|
||||
#define GLPROJECT_MESHGENERATOR_H
|
||||
|
||||
#define GLM_ENABLE_EXPERIMENTAL
|
||||
|
||||
#include "MeshData.h"
|
||||
#include <vector>
|
||||
#include <gtx/normal.hpp>
|
||||
|
||||
const int CHUNK_SIZE = 16;
|
||||
|
||||
class MeshGenerator {
|
||||
public:
|
||||
MeshGenerator();
|
||||
MeshData* build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE]);
|
||||
~MeshGenerator();
|
||||
private:
|
||||
std::vector<float>* vertices;
|
||||
std::vector<unsigned int>* indices;
|
||||
unsigned int indOffset;
|
||||
|
||||
void add_vertices(std::vector<float> *verts, std::vector<unsigned int> *inds, int *x, int *y, int *z);
|
||||
|
||||
void add_top_face(int *x, int *y, int *z);
|
||||
void add_bottom_face(int *x, int *y, int *z);
|
||||
|
||||
void add_front_face(int *x, int *y, int *z);
|
||||
void add_back_face(int *x, int *y, int *z);
|
||||
|
||||
void add_left_face(int *x, int *y, int *z);
|
||||
void add_right_face(int *x, int *y, int *z);
|
||||
|
||||
void cleanup();
|
||||
};
|
||||
|
||||
|
||||
#endif //GLPROJECT_MESHGENERATOR_H
|
|
@ -8,6 +8,5 @@ out vec4 fragColor;
|
|||
uniform sampler2D tex;
|
||||
|
||||
void main() {
|
||||
// fragColor = color;
|
||||
fragColor = texture(tex, fragTex) * color;
|
||||
}
|
Before Width: | Height: | Size: 1.9 MiB After Width: | Height: | Size: 1.9 MiB |
Loading…
Reference in New Issue