diff --git a/.idea/dictionaries/aurailus.xml b/.idea/dictionaries/aurailus.xml index f5cf3e55..ec95bb6d 100644 --- a/.idea/dictionaries/aurailus.xml +++ b/.idea/dictionaries/aurailus.xml @@ -3,6 +3,7 @@ glfw shaders + verts \ No newline at end of file diff --git a/GlProject/Main.cpp b/GlProject/Main.cpp index 25bacf7c..1adb27bf 100644 --- a/GlProject/Main.cpp +++ b/GlProject/Main.cpp @@ -32,141 +32,23 @@ GLfloat deltaTime = 0.0f; GLfloat lastTime = 0.0f; void makeEntities() { -// unsigned int indices[] { -// 0, 3, 1, -// 1, 3, 2, -// 2, 3, 0, -// 0, 1, 2 -// }; -// -// GLfloat vertices[] = { -// // [x] [y] [z] [u] [v] -// -1.0f, -1.0f, -0.5f, 0.0f, 0.0f, -// 0.0f, -1.0f, 1.0f, 0.5f, 0.0f, -// 1.0f, -1.0f, -0.5f, 1.0f, 0.0f, -// 0.0f, 1.0f, 0.0f, 0.5f, 1.0f, -// }; - -// unsigned int indices[] { -// //Bottom -// 0, 2, 3, -// 3, 1, 0, -// //Top -// 4, 6, 7, -// 7, 5, 4, -// //Left -// 0, 1, 5, -// 5, 4, 0, -// //Right -// 7, 3, 2, -// 2, 6, 7, -// //Back -// 0, 2, 6, -// 6, 4, 0, -// //Front -// 1, 3, 7, -// 7, 5, 1 -// }; -// -// GLfloat vertices[] = { -// -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -// -0.5f, -0.5f, 0.5f, 1.0f, 0.0, -// 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, -// 0.5f, -0.5f, 0.5f, 0.0f, 1.0f, -// -// -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 indices[] = { - //Top Face - 0, 1, 2, 2, 3, 0, - //Bottom Face - 0+4, 1+4, 2+4, 2+4, 3+4, 0+4, - //Front Face - 0+8, 1+8, 2+8, 2+8, 3+8, 0+8, - //Back Face - 0+12, 1+12, 2+12, 2+12, 3+12, 0+12, - //Left Face - 0+16, 1+16, 2+16, 2+16, 3+16, 0+16, - //Right Face - 0+20, 1+20, 2+20, 2+20, 3+20, 0+20, - }; - - GLfloat vertices[] = { - //Top Face - -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, - //Bottom Face - -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, - //Front Face - -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, - //Back Face - 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, - //Left Face - -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, - //Right Face - -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, - }; - -// Mesh* mesh = new Mesh(); -// mesh->create(vertices, indices, (sizeof(vertices)/sizeof(*vertices)), (sizeof(indices)/sizeof(*indices))); auto meshGen = new MeshGenerator(); - int array[4][4][4] { - { {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, - { {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, - { {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, - { {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }}; + { {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); Mesh* mesh = new Mesh(); + mesh->create(&m->vertices[0], &m->indices[0], (int)m->vertices.size(), (int)m->indices.size()); - float* verts = &m->vertices[0]; - unsigned int* indxs = &m->indices[0]; - - mesh->create(verts, indxs, (int)m->vertices.size(), (int)m->indices.size()); - - auto *tri = new Entity(); - tri->create(mesh); - tri->setPosition(glm::vec3(0, 0, -5)); - tri->setScale(0.5); - entities.push_back(tri); - -// for (auto y = -1; y < 1; y++) { -// for (auto i = -1; i < 2; i++) { -// for (auto j = -4; j < -2; j++) { -// -// auto *tri = new Entity(); -// tri->create(mesh); -// tri->setPosition(glm::vec3(i * 1.45, (y + 0.5) * 1.6, j)); -// tri->setScale(0.7); -// -// entities.push_back(tri); -// } -// } -// } + auto* chunk = new Entity(); + chunk->create(mesh); + chunk->setPosition(glm::vec3(0, 0, -5)); +// chunk->setScale(0.5); + entities.push_back(chunk); } int main() { @@ -221,7 +103,6 @@ int main() { for (auto &entity : entities) { glUniformMatrix4fv(shader->getModelLocation(), 1, GL_FALSE, glm::value_ptr(entity->getModelMatrix())); - entity->setAngle(*entity->getAngle() + 0.5f); entity->draw(); } diff --git a/GlProject/Mesh.cpp b/GlProject/Mesh.cpp index a8333748..1c2d288a 100644 --- a/GlProject/Mesh.cpp +++ b/GlProject/Mesh.cpp @@ -31,12 +31,15 @@ void Mesh::create(GLfloat *vertices, unsigned int *indices, unsigned int vertCou glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertCount, vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) * 5, nullptr); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) * 8, nullptr); glEnableVertexAttribArray(0); //If dynamic amounts of attrib arrays are needed, then do these before drawing - glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) * 5, (void*)(sizeof(vertices[0]) * 3)); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) * 8, (void*)(sizeof(vertices[0]) * 3)); glEnableVertexAttribArray(1); + glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0]) * 8, (void*)(sizeof(vertices[0]) * 5)); + glEnableVertexAttribArray(2); + glBindBuffer(GL_ARRAY_BUFFER, 0); diff --git a/GlProject/MeshGenerator.cpp b/GlProject/MeshGenerator.cpp index d754848b..0a3d1091 100644 --- a/GlProject/MeshGenerator.cpp +++ b/GlProject/MeshGenerator.cpp @@ -37,164 +37,140 @@ MeshData* MeshGenerator::generate(int blocks[4][4][4]) { return m; } +void MeshGenerator::add_face(std::vector *meshVerts, std::vector *meshInds, int *indOffset, + std::vector *faceVerts, std::vector *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 *vertices, std::vector *indices, int *lastIndex, int *x, int *y, int *z) { - float top_vertices[] { + 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 top_indices[] { + unsigned int face_indices[] { 0, 1, 2, 2, 3, 0 }; - int indexCount = 4; - for (int i = 0; i < (sizeof(top_vertices) / sizeof(top_vertices[0]) / 5); i++) { - vertices->push_back(top_vertices[i*5 + 0] + *x); - vertices->push_back(top_vertices[i*5 + 1] + *y); - vertices->push_back(top_vertices[i*5 + 2] + *z); - vertices->push_back(top_vertices[i*5 + 3]); - vertices->push_back(top_vertices[i*5 + 4]); - } + std::vector vec(std::begin(face_vertices), std::end(face_vertices)); + std::vector vec2(std::begin(face_indices), std::end(face_indices)); - for (auto i : top_indices) { - indices->push_back(*lastIndex + i); - } - - *lastIndex += indexCount; + add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z); } void MeshGenerator::add_bottom_face(std::vector *vertices, std::vector *indices, int *lastIndex, int *x, int *y, int *z) { - float top_vertices[] { + 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 top_indices[] { + unsigned int face_indices[] { 0, 1, 2, 2, 3, 0 }; - int indexCount = 4; - for (int i = 0; i < (sizeof(top_vertices) / sizeof(top_vertices[0]) / 5); i++) { - vertices->push_back(top_vertices[i*5 + 0] + *x); - vertices->push_back(top_vertices[i*5 + 1] + *y); - vertices->push_back(top_vertices[i*5 + 2] + *z); - vertices->push_back(top_vertices[i*5 + 3]); - vertices->push_back(top_vertices[i*5 + 4]); - } + std::vector vec(std::begin(face_vertices), std::end(face_vertices)); + std::vector vec2(std::begin(face_indices), std::end(face_indices)); - for (auto i : top_indices) { - indices->push_back(*lastIndex + i); - } - - *lastIndex += indexCount; + add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z); } void MeshGenerator::add_front_face(std::vector *vertices, std::vector *indices, int *lastIndex, int *x, int *y, int *z){ - float front_vertices[] { + 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 front_indices[] { + unsigned int face_indices[] { 0, 1, 2, 2, 3, 0 }; - int indexCount = 4; - for (int i = 0; i < (sizeof(front_vertices) / sizeof(front_vertices[0]) / 5); i++) { - vertices->push_back(front_vertices[i*5 + 0] + *x); - vertices->push_back(front_vertices[i*5 + 1] + *y); - vertices->push_back(front_vertices[i*5 + 2] + *z); - vertices->push_back(front_vertices[i*5 + 3]); - vertices->push_back(front_vertices[i*5 + 4]); - } + std::vector vec(std::begin(face_vertices), std::end(face_vertices)); + std::vector vec2(std::begin(face_indices), std::end(face_indices)); - for (auto i : front_indices) { - indices->push_back(*lastIndex + i); - } - - *lastIndex += indexCount; + add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z); } void MeshGenerator::add_back_face(std::vector *vertices, std::vector *indices, int *lastIndex, int *x, int *y, int *z){ - float front_vertices[] { + 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 front_indices[] { + unsigned int face_indices[] { 0, 1, 2, 2, 3, 0 }; - int indexCount = 4; - for (int i = 0; i < (sizeof(front_vertices) / sizeof(front_vertices[0]) / 5); i++) { - vertices->push_back(front_vertices[i*5 + 0] + *x); - vertices->push_back(front_vertices[i*5 + 1] + *y); - vertices->push_back(front_vertices[i*5 + 2] + *z); - vertices->push_back(front_vertices[i*5 + 3]); - vertices->push_back(front_vertices[i*5 + 4]); - } + std::vector vec(std::begin(face_vertices), std::end(face_vertices)); + std::vector vec2(std::begin(face_indices), std::end(face_indices)); - for (auto i : front_indices) { - indices->push_back(*lastIndex + i); - } - - *lastIndex += indexCount; + add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z); } void MeshGenerator::add_left_face(std::vector *vertices, std::vector *indices, int *lastIndex, int *x, int *y, int *z){ - float front_vertices[] { + 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 front_indices[] { + unsigned int face_indices[] { 0, 1, 2, 2, 3, 0 }; - int indexCount = 4; - for (int i = 0; i < (sizeof(front_vertices) / sizeof(front_vertices[0]) / 5); i++) { - vertices->push_back(front_vertices[i*5 + 0] + *x); - vertices->push_back(front_vertices[i*5 + 1] + *y); - vertices->push_back(front_vertices[i*5 + 2] + *z); - vertices->push_back(front_vertices[i*5 + 3]); - vertices->push_back(front_vertices[i*5 + 4]); - } + std::vector vec(std::begin(face_vertices), std::end(face_vertices)); + std::vector vec2(std::begin(face_indices), std::end(face_indices)); - for (auto i : front_indices) { - indices->push_back(*lastIndex + i); - } - - *lastIndex += indexCount; + add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z); } void MeshGenerator::add_right_face(std::vector *vertices, std::vector *indices, int *lastIndex, int *x, int *y, int *z){ - float front_vertices[] { + 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 front_indices[] { + unsigned int face_indices[] { 0, 1, 2, 2, 3, 0 }; - int indexCount = 4; - for (int i = 0; i < (sizeof(front_vertices) / sizeof(front_vertices[0]) / 5); i++) { - vertices->push_back(front_vertices[i*5 + 0] + *x); - vertices->push_back(front_vertices[i*5 + 1] + *y); - vertices->push_back(front_vertices[i*5 + 2] + *z); - vertices->push_back(front_vertices[i*5 + 3]); - vertices->push_back(front_vertices[i*5 + 4]); - } + std::vector vec(std::begin(face_vertices), std::end(face_vertices)); + std::vector vec2(std::begin(face_indices), std::end(face_indices)); - for (auto i : front_indices) { - indices->push_back(*lastIndex + i); - } - - *lastIndex += indexCount; + add_face(vertices, indices, lastIndex, &vec, &vec2, x, y, z); } \ No newline at end of file diff --git a/GlProject/MeshGenerator.h b/GlProject/MeshGenerator.h index d6b591ab..b37b1b76 100644 --- a/GlProject/MeshGenerator.h +++ b/GlProject/MeshGenerator.h @@ -5,8 +5,11 @@ #ifndef GLPROJECT_MESHGENERATOR_H #define GLPROJECT_MESHGENERATOR_H +#define GLM_ENABLE_EXPERIMENTAL + #include "MeshData.h" #include +#include class MeshGenerator { public: @@ -14,6 +17,9 @@ public: MeshData* generate(int blocks[4][4][4]); private: + void add_face(std::vector *meshVerts, std::vector *meshInds, int *indOffset, + std::vector *faceVerts, std::vector *faceInds, int *x, int *y, int *z); + void add_top_face(std::vector *vertices, std::vector *indices, int *lastIndex, int *x, int *y, int *z); void add_bottom_face(std::vector *vertices, std::vector *indices, int *lastIndex, int *x, int *y, int *z); diff --git a/GlProject/Shaders/world.fs b/GlProject/Shaders/world.fs index 03aa573f..d98fb8ee 100644 --- a/GlProject/Shaders/world.fs +++ b/GlProject/Shaders/world.fs @@ -9,5 +9,5 @@ uniform sampler2D tex; void main() { // fragColor = color; - fragColor = texture(tex, fragTex); + fragColor = texture(tex, fragTex) * color; } \ No newline at end of file diff --git a/GlProject/Shaders/world.vs b/GlProject/Shaders/world.vs index 029e313f..b4c69c8c 100644 --- a/GlProject/Shaders/world.vs +++ b/GlProject/Shaders/world.vs @@ -2,6 +2,7 @@ layout (location = 0) in vec3 pos; layout (location = 1) in vec2 tex; +layout (location = 2) in vec3 nor; uniform mat4 model; uniform mat4 projection; @@ -12,6 +13,11 @@ out vec2 fragTex; void main() { gl_Position = projection * view * model * vec4(pos, 1.0); - color = vec4(clamp(pos, 0.0f, 1.0f), 1.0f); +// color = vec4(clamp(pos, 0.0f, 1.0f), 1.0f); + + vec4 myColor = vec4(1, 1, 1, 0) * (0.8 + abs(nor.x) * 0.15); + myColor += nor.y * 0.15; + + color = myColor; fragTex = tex; } \ No newline at end of file