- Update timers, add elapsedInMs to timer

- Added some textures
master
aurailus 2018-12-06 00:09:25 -08:00
parent b3fb9e961f
commit ee20219bf1
7 changed files with 36 additions and 26 deletions

View File

@ -16,6 +16,7 @@
#include "engine/graphics/Shader.h"
#include "mesh/MeshGenerator.h"
#include "engine/Entity.h"
#include "engine/Timer.h"
Window* window;
Shader* shader;
@ -34,7 +35,7 @@ BlockModel* createBlockModel() {
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(0.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
@ -64,12 +65,12 @@ void makeEntities(BlockModel* model) {
auto* mesh = new Mesh();
mesh->create(&vertices, &indices);
for (int i = -16; i < 16; i++) {
for (int j = -16; j < 16; j++) {
for (int i = -32; i < 32; i++) {
for (int j = -32; j < 32; j++) {
auto *chunk = new Entity();
chunk->create(mesh);
chunk->setPosition(glm::vec3(i * CHUNK_SIZE, 0, j * CHUNK_SIZE));
chunk->setScale(0.5);
chunk->setScale(1);
entities.push_back(chunk);
}
}
@ -93,9 +94,6 @@ int main() {
//Create model
BlockModel* model = createBlockModel();
auto* mesh = model->topFaces[0];
mesh->debug();
//Create entities
makeEntities(model);
@ -109,6 +107,8 @@ int main() {
//Game Loop
while (!window->getShouldClose()) {
Timer t("Game Loop");
auto now = (GLfloat)glfwGetTime();
deltaTime = now - lastTime;
lastTime = now;
@ -140,6 +140,8 @@ int main() {
//Finish Drawing
window->swapBuffers();
t.elapsedInMs();
}
return 0;

View File

@ -15,4 +15,13 @@ void Timer::elapsed() {
long elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count();
printf("%s took %ld ns.\n", this->name, elapsed);
}
}
void Timer::elapsedInMs() {
auto finish = std::chrono::high_resolution_clock::now();
double elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(finish - start).count() / (double)1000000;
printf("%s took %.2f ms.\n", this->name, elapsed);
}

View File

@ -13,6 +13,7 @@ public:
explicit Timer(const char* name);
void elapsed();
void elapsedInMs();
private:
const char* name;
std::chrono::high_resolution_clock::time_point start;

View File

@ -13,7 +13,7 @@ MeshGenerator::MeshGenerator() {
void MeshGenerator::build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockModel* model,
std::vector<float> &vertices, std::vector<unsigned int> &indices) {
auto t = Timer("MeshGen");
Timer t("Mesh Generation");
vertices.reserve(16384);
indices.reserve(4096);
@ -48,7 +48,7 @@ void MeshGenerator::build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockM
vertices.shrink_to_fit();
indices.shrink_to_fit();
t.elapsed();
t.elapsedInMs();
}
void MeshGenerator::addFaces(int x, int y, int z, vector<float> &vertices, vector<unsigned int> &indices, vector<MeshPart*> meshParts) {

View File

@ -14,11 +14,9 @@ MeshPart::MeshPart(Vertex* vertices, int vSize, unsigned int* indices, int iSize
}
//Add normals and compute tex coordinates for the given Vertex array.
void MeshPart::construct(Vertex* vertices, int vSize, unsigned int *indices, int iSize, const char* texture, MeshMod meshMod, float modValue) {
//Set the meshMod and modValue variables on the MeshPart
//Set the meshMod and modValue variables on the MeshPart.
this->meshMod = meshMod;
this->modValue = modValue;
@ -26,34 +24,36 @@ void MeshPart::construct(Vertex* vertices, int vSize, unsigned int *indices, int
//To do this, we have to assume that each group of 3 indices is a triangle (which it would be hard for it to not be)
//and that no vertexes are shared on corners or places where vectors should be interpolated.
//Iterate through the indices to find all used vertices to add normals and adjust texture coordinates.
Vertex *p1, *p2, *p3;
glm::vec3 nml;
glm::vec3 normal;
for (int i = 0; i < iSize/3; i++) {
//Get the three vertices
p1 = &vertices[indices[i*3]];
p2 = &vertices[indices[i*3 + 1]];
p3 = &vertices[indices[i*3 + 2]];
//Get the normal of the formed triangle
nml = glm::triangleNormal(*(p1->pos), *(p2->pos), *(p3->pos));
normal = glm::triangleNormal(*(p1->pos), *(p2->pos), *(p3->pos));
auto* nml = new glm::vec3(normal.x, normal.y, normal.z);
//Set the normal on the vertices
p1->nml = new glm::vec3(nml.x, nml.y, nml.z);
p2->nml = new glm::vec3(nml.x, nml.y, nml.z);
p3->nml = new glm::vec3(nml.x, nml.y, nml.z);
p1->nml = nml;
p2->nml = nml;
p3->nml = nml;
}
//TODO: use 'texture' varaible here
// glm::vec4 texBase = glm::vec4(0, 0, 1, 1);
// glm::vec2 texWidth = glm::vec2(texBase.x - texBase.z, texBase.y - texBase.w);
//Iterate through the vertices to adjust the texture coordinates to fit the atlas.
for (int i = 0; i < vSize; i++) {
Vertex* vertex = &vertices[i];
//Adjust the texture coordinates to be relative to the texture requested.
// vertex->tex->x /= 2;
// vertex->tex->y /= 2;
}
//Assign the inputted values to the struct
this->vertices = vertices;
this->vSize = vSize;
this->indices = indices;
@ -123,7 +123,6 @@ void MeshPart::debug() {
//Mesh Vertex Iterator
//Iterator to get the vertices of the MeshPart as a stream.
MeshVertexIter::MeshVertexIter(MeshPart* meshPart) {
this->meshPart = meshPart;
this->index = 0;
@ -139,7 +138,6 @@ Vertex* MeshVertexIter::next() {
//Mesh Index Iterator
//Iterator to get the indices of the MeshPart as a stream
MeshIndexIter::MeshIndexIter(MeshPart *meshPart) {
this->meshPart = meshPart;
this->index = 0;

BIN
Textures/default_dirt.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 813 B

BIN
Textures/dirt_hires.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 766 KiB