BlockDef Class and BlockModel::Square convenience function

- Made BlockDef class
- Made BlockAtlas reference BlockDefs
- Made static BlockModel::Square method for creating cube BlockMeshes.
- Played with some lua interpretation, commented out.
master
aurailus 2018-12-17 13:04:25 -08:00
parent c96e612a9f
commit 5221797d0a
12 changed files with 224 additions and 97 deletions

View File

@ -8,14 +8,20 @@ include_directories(Libraries/glfw_linux/include)
include_directories(Libraries/glm)
include_directories(Libraries/stb_image)
include_directories(Libraries/cute)
include_directories(Libraries/boost_1_68_0)
#include_directories(Libraries/boost_1_68_0)
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
find_package(Boost COMPONENTS system thread)
#find_package(Boost COMPONENTS system thread)
link_directories(Libraries/glew/lib)
add_executable(GlProject GlProject/Main.cpp GlProject/engine/graphics/Mesh.cpp GlProject/engine/graphics/Mesh.h GlProject/engine/PerlinNoise.cpp GlProject/engine/PerlinNoise.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/engine/Timer.cpp GlProject/engine/Timer.h GlProject/blocks/BlockAtlas.cpp GlProject/blocks/BlockAtlas.h GlProject/blocks/BlockDef.cpp GlProject/blocks/BlockDef.h GlProject/mesh/MeshPart.cpp GlProject/mesh/MeshPart.h GlProject/mesh/MeshMod.h GlProject/mesh/Vertex.cpp GlProject/mesh/Vertex.h GlProject/mesh/BlockModel.cpp GlProject/mesh/BlockModel.h GlProject/engine/TextureAtlas.cpp GlProject/engine/TextureAtlas.h GlProject/UDP.cpp GlProject/UDP.h GlProject/world/World.cpp GlProject/world/World.h GlProject/world/BlockChunk.cpp GlProject/world/BlockChunk.h GlProject/engine/helpers/ArrayTrans3D.h GlProject/world/MeshChunk.cpp GlProject/world/MeshChunk.h)
add_executable(GlProject GlProject/Main.cpp GlProject/engine/graphics/Mesh.cpp GlProject/engine/graphics/Mesh.h GlProject/engine/PerlinNoise.cpp GlProject/engine/PerlinNoise.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/engine/Timer.cpp GlProject/engine/Timer.h GlProject/blocks/BlockAtlas.cpp GlProject/blocks/BlockAtlas.h GlProject/blocks/BlockDef.cpp GlProject/blocks/BlockDef.h GlProject/mesh/MeshPart.cpp GlProject/mesh/MeshPart.h GlProject/mesh/MeshMod.h GlProject/mesh/Vertex.cpp GlProject/mesh/Vertex.h GlProject/mesh/BlockModel.cpp GlProject/mesh/BlockModel.h GlProject/engine/TextureAtlas.cpp GlProject/engine/TextureAtlas.h GlProject/world/World.cpp GlProject/world/World.h GlProject/world/BlockChunk.cpp GlProject/world/BlockChunk.h GlProject/engine/helpers/ArrayTrans3D.h GlProject/world/MeshChunk.cpp GlProject/world/MeshChunk.h)
target_link_libraries(GlProject ${OPENGL_gl_LIBRARY} glfw libGLEW.so pthread)
target_link_libraries(GlProject
${OPENGL_gl_LIBRARY}
glfw
libGLEW.so
pthread
lua
dl)

View File

@ -27,81 +27,10 @@ World* world;
GLfloat deltaTime = 0.0f;
GLfloat lastTime = 0.0f;
BlockAtlas* createAtlas() {
Vertex* leftVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 1.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
};
auto* leftInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* leftPart = new MeshPart(leftVerts, 4, leftInds, 6, "default_grass_side", textureAtlas);
Vertex* rightVerts = new Vertex[4] {
Vertex(new glm::vec3(1.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 1.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
};
auto* rightInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* rightPart = new MeshPart(rightVerts, 4, rightInds, 6, "default_grass_side", textureAtlas);
Vertex* topVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 1.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 0.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
};
auto* topInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* topPart = new MeshPart(topVerts, 4, topInds, 6, "default_grass_top", textureAtlas);
Vertex* bottomVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 0.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 0.0f, 1.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
};
auto* bottomInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* bottomPart = new MeshPart(bottomVerts, 4, bottomInds, 6, "default_dirt", textureAtlas);
Vertex* frontVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 1.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 1.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 1.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
};
auto* frontInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* frontPart = new MeshPart(frontVerts, 4, frontInds, 6, "default_grass_side", textureAtlas);
Vertex* backVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 0.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 0.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
};
auto* backInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* backPart = new MeshPart(backVerts, 4, backInds, 6, "default_grass_side", textureAtlas);
auto* bm = new BlockModel(leftPart, rightPart, topPart, bottomPart, frontPart, backPart, nullptr, true, true);
return new BlockAtlas(bm);
void registerBlocks() {
auto* bm = BlockModel::Square("default_grass_top", "default_dirt", "default_grass_side", "default_grass_side", "default_grass_side", "default_grass_side", textureAtlas);
auto* def = new BlockDef("grass", bm);
blockAtlas->registerBlock(def);
}
void genChunks(World* world) {
@ -135,7 +64,58 @@ void genChunks(World* world) {
}
}
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
lua_State* L;
static int average(lua_State *L) {
int n = lua_gettop(L);
double sum = 0;
for (int i = 1; i <= n; i++) {
sum += lua_tonumber(L, i);
}
lua_pushnumber(L, sum / n);
lua_pushnumber(L, sum);
return 2; //Number of results
}
int main(int argc, char* argv[]) {
// int x = 4, y = 3, sum;
//
// L = luaL_newstate();
//
// //Open base libraries
// luaL_openlibs(L);
// lua_register(L, "average", average);
//
// luaL_dofile(L, "../file.lua");
//
// //Call the "add" function
// lua_getglobal(L, "add");
// lua_pushnumber(L, x);
// lua_pushnumber(L, y);
//
// lua_call(L, 2, 1);
//
// sum = (int)lua_tointeger(L, -1);
// lua_pop(L, 1);
//
// lua_close(L);
//
// printf("Hold up %i", sum);
// getchar();
//
// return 0;
Timer boot("Initialization");
window = new Window(1366, 768);
@ -144,7 +124,8 @@ int main(int argc, char* argv[]) {
camera = new Camera(glm::vec3(0.0f, 16.0f, 0.0f), glm::vec3(0, 1, 0), -90.0f, -45.0f, 10.0f, 0.1f);
textureAtlas = new TextureAtlas("../Textures");
blockAtlas = createAtlas();
blockAtlas = new BlockAtlas(textureAtlas);
registerBlocks();
world = new World(blockAtlas);
genChunks(world);
@ -182,20 +163,18 @@ int main(int argc, char* argv[]) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader->useShader();
glUniformMatrix4fv(shader->getProjectionLocation(), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(shader->getViewLocation(), 1, GL_FALSE, glm::value_ptr(camera->calculateViewMatrix()));
textureAtlas->getTexture()->use();
glUniformMatrix4fv(shader->getProjectionLocation(), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(shader->getViewLocation(), 1, GL_FALSE, glm::value_ptr(camera->calculateViewMatrix()));
//Render chunks
world->draw(shader->getModelLocation());
Shader::clearShader();
//Finish Drawing
window->swapBuffers();
// t.elapsedMs();
// t.elapsedMs(); //Print frame time
}
return 0;

View File

@ -4,10 +4,25 @@
#include "BlockAtlas.h"
BlockAtlas::BlockAtlas(BlockModel *model) {
blockModel = model;
BlockAtlas::BlockAtlas(TextureAtlas *textureAtlas) {
this->textureAtlas = textureAtlas;
}
BlockModel* BlockAtlas::getBlockModel() {
return blockModel;
void BlockAtlas::registerBlock(BlockDef* def) {
definitions.push_back(def);
}
BlockDef* BlockAtlas::getBlock(int id) {
if (id >= 0 && id < definitions.size()) {
return definitions.at((unsigned long)id);
}
std::cout << "Block ID " << id << " is not defined! Returning null pointer." << std::endl;
return nullptr;
}
BlockAtlas::~BlockAtlas() {
for (auto b : definitions) {
delete b;
}
definitions.clear();
}

View File

@ -7,15 +7,20 @@
#include "../mesh/BlockModel.h"
#include "BlockDef.h"
class BlockAtlas {
public:
BlockAtlas() = default;
explicit BlockAtlas(BlockModel* model);
explicit BlockAtlas(TextureAtlas* textureAtlas);
BlockModel* getBlockModel();
void registerBlock(BlockDef* def);
BlockDef* getBlock(int id);
~BlockAtlas();
private:
BlockModel* blockModel;
std::vector<BlockDef*> definitions;
TextureAtlas* textureAtlas;
};

View File

@ -3,3 +3,16 @@
//
#include "BlockDef.h"
BlockModel *BlockDef::getModel() {
return model;
}
BlockDef::BlockDef(std::string identifier, BlockModel *model) {
this->identifier = identifier;
this->model = model;
}
BlockDef::~BlockDef() {
delete this->model;
}

View File

@ -5,8 +5,19 @@
#ifndef GLPROJECT_BLOCKDEF_H
#define GLPROJECT_BLOCKDEF_H
#include "../mesh/BlockModel.h"
class BlockDef {
public:
BlockDef(std::string identifier, BlockModel* model);
BlockModel* getModel();
~BlockDef();
private:
BlockModel* model;
std::string identifier;
};

View File

@ -83,4 +83,89 @@ BlockModel::~BlockModel() {
for (MeshPart* m : noCulledFaces) { delete m; }
noCulledFaces.clear();
}
//Static methods to generate a basic cube BlockModel.
//These are just convenience functions.
BlockModel* BlockModel::Square(const char *tex, TextureAtlas* atlas) {
return BlockModel::Square(tex, tex, tex, tex, tex, tex, atlas);
}
BlockModel* BlockModel::Square(const char* top, const char* bottom, const char* front, const char* back, const char* left, const char* right, TextureAtlas* atlas) {
Vertex* leftVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 1.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
};
auto* leftInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* leftPart = new MeshPart(leftVerts, 4, leftInds, 6, left, atlas);
Vertex* rightVerts = new Vertex[4] {
Vertex(new glm::vec3(1.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 1.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
};
auto* rightInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* rightPart = new MeshPart(rightVerts, 4, rightInds, 6, right, atlas);
Vertex* topVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 1.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 0.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
};
auto* topInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* topPart = new MeshPart(topVerts, 4, topInds, 6, top, atlas);
Vertex* bottomVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 0.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 0.0f, 1.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
};
auto* bottomInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* bottomPart = new MeshPart(bottomVerts, 4, bottomInds, 6, bottom, atlas);
Vertex* frontVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 1.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 1.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 1.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
};
auto* frontInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* frontPart = new MeshPart(frontVerts, 4, frontInds, 6, front, atlas);
Vertex* backVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 1.0f, 0.0f), nullptr, new glm::vec2(1.0f, 0.0f)),
Vertex(new glm::vec3(1.0f, 0.0f, 0.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
};
auto* backInds = new unsigned int[6] {
0, 1, 2, 2, 3, 0
};
auto* backPart = new MeshPart(backVerts, 4, backInds, 6, back, atlas);
auto* model = new BlockModel(leftPart, rightPart, topPart, bottomPart, frontPart, backPart, nullptr, true, true);
return model;
}

View File

@ -27,6 +27,9 @@ public:
MeshPart* backFace, MeshPart* noCulledFace, bool culls, bool visible);
~BlockModel();
static BlockModel* Square(const char* top, const char* bottom, const char* front, const char* back, const char* left, const char* right, TextureAtlas* atlas);
static BlockModel* Square(const char* tex, TextureAtlas* atlas);
};

View File

@ -25,7 +25,8 @@ void MeshGenerator::build(BlockChunk* chunk, BlockAtlas* atlas,
glm::vec3 off;
glm::vec3 check;
BlockModel* model = atlas->getBlockModel();
//TODO: Move this to the loop and make it use 'i' instead of 0
BlockModel* model = atlas->getBlock(0)->getModel();
for (int i = 0; i < 4096; i++) {
if (chunk->getBlock(i) == 1) {

View File

@ -51,10 +51,9 @@ private:
std::map<glm::vec3*, BlockChunk*> blockChunks;
std::map<glm::vec3*, MeshChunk*> meshChunks;
std::unordered_set<glm::vec3*> meshGenQueue;
const int MAX_CONCURRENT_THREADS = 32;
std::unordered_set<glm::vec3*> meshGenQueue;
std::vector<ThreadData*> genThreads;
BlockAtlas* blockAtlas;

10
Lua/file.lua Normal file
View File

@ -0,0 +1,10 @@
print "Hello, world"
-- Add numbers together
function add(x, y)
return x + y
end
avg, sum = average(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
print("Avg " .. avg)
print("Sum " .. sum)

BIN
atlas.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 KiB