Generic add_face function in MeshGenerator

master
aurailus 2018-12-01 23:07:36 -08:00
parent 10351c5269
commit 537a242a5c
7 changed files with 96 additions and 223 deletions

View File

@ -3,6 +3,7 @@
<words>
<w>glfw</w>
<w>shaders</w>
<w>verts</w>
</words>
</dictionary>
</component>

View File

@ -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();
}

View File

@ -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);

View File

@ -37,164 +37,140 @@ MeshData* MeshGenerator::generate(int blocks[4][4][4]) {
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 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<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> 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<float> *vertices, std::vector<unsigned int> *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<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> 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<float> *vertices, std::vector<unsigned int> *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<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> 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<float> *vertices, std::vector<unsigned int> *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<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> 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<float> *vertices, std::vector<unsigned int> *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<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> 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<float> *vertices, std::vector<unsigned int> *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<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> 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);
}

View File

@ -5,8 +5,11 @@
#ifndef GLPROJECT_MESHGENERATOR_H
#define GLPROJECT_MESHGENERATOR_H
#define GLM_ENABLE_EXPERIMENTAL
#include "MeshData.h"
#include <vector>
#include <gtx/normal.hpp>
class MeshGenerator {
public:
@ -14,6 +17,9 @@ public:
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);

View File

@ -9,5 +9,5 @@ uniform sampler2D tex;
void main() {
// fragColor = color;
fragColor = texture(tex, fragTex);
fragColor = texture(tex, fragTex) * color;
}

View File

@ -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;
}