- Prevent invalid memory addressing in the MeshPart construct method.

master
aurailus 2018-12-05 20:59:12 -08:00
parent aa837305ce
commit 74b98e4b39
7 changed files with 47 additions and 41 deletions

View File

@ -1,6 +1,7 @@
<component name="ProjectDictionaryState">
<dictionary name="aurailus">
<words>
<w>cout</w>
<w>glfw</w>
<w>shaders</w>
<w>verts</w>

View File

@ -31,6 +31,7 @@ GLfloat deltaTime = 0.0f;
GLfloat lastTime = 0.0f;
BlockModel createBlockModel() {
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)),
@ -48,31 +49,31 @@ BlockModel createBlockModel() {
void makeEntities(BlockModel model) {
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, &model);
delete meshGen;
Mesh* mesh = new Mesh();
mesh->create(&m->vertices[0], &m->indices[0], (int)m->vertices.size(), (int)m->indices.size());
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 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, &model);
// delete meshGen;
//
// Mesh* mesh = new Mesh();
// mesh->create(&m->vertices[0], &m->indices[0], (int)m->vertices.size(), (int)m->indices.size());
//
// 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() {

View File

@ -25,6 +25,7 @@ private:
std::vector<unsigned int>* indices;
unsigned int indOffset;
//TODO:: Replace the pointers to ints to non-pointer shorts(?)
void addBlockModel(int* x, int* y, int* z, BlockModel* model);
void add_vertices(std::vector<float> *verts, std::vector<unsigned int> *inds, int *x, int *y, int *z);
@ -41,6 +42,8 @@ private:
void cleanup();
};
//Temporary class for storing Vertices and Indices.
//TODO: Replace this class with pointers to new Vertices and Indices in the build method.
struct MeshData {
std::vector<float> vertices;
std::vector<unsigned int> indices;

View File

@ -26,30 +26,28 @@ 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.
Vertex *p1, *p2, *p3;
glm::vec3 nml;
//Iterate through the indices to find all used vertices to add normals and adjust texture coordinates.
Vertex *p1, *p2, *p3;
glm::vec3 nml;
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];
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); //TODO: do this right
auto* normal = new glm::vec3(nml.x, nml.y, nml.z);
nml = glm::triangleNormal(*(p1->pos), *(p2->pos), *(p3->pos)); //TODO: do this right
//Set the normal on the vertices
p1->nml = normal;
p2->nml = normal;
p3->nml = normal;
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);
//TODO: get the tex dimensions from the texture atlas here
glm::vec4 texBase = glm::vec4(0, 0, 1, 1);
glm::vec2 texWidth = glm::vec2(texBase.x - texBase.z, texBase.y - texBase.w);
// //TODO: get the tex dimensions from the texture atlas here
// glm::vec4 texBase = glm::vec4(0, 0, 1, 1);
// glm::vec2 texWidth = glm::vec2(texBase.x - texBase.z, texBase.y - texBase.w);
//Adjust the texture coordinates to be relative to the texture requested.
//TODO: implement this aaaa
@ -87,6 +85,7 @@ MeshIndexIter* MeshPart::getIndexIterator() {
return new MeshIndexIter(this);
}
//Print information about this MeshPart into std::cout.
void MeshPart::debug() {
std::cout << "Debugging MeshPart" << std::endl << "Vertices:" << std::endl;
@ -94,7 +93,7 @@ void MeshPart::debug() {
while (vI->hasNext()) {
Vertex* v = vI->next();
std::cout << v->pos->x << ", " << v->pos->y << ", " << v->pos->z << std::endl;
std::cout << v->pos->x << ", " << v->pos->y << ", " << v->pos->z << " | " << v->nml->x << ", " << v->nml->y << ", " << v->nml->z << std::endl;
}
auto* iI = getIndexIterator();

View File

@ -33,12 +33,12 @@ public:
private:
void construct(Vertex* vertices, int vSize, unsigned int* indices, int iSize, const char* texture, MeshMod meshMod, float modValue);
//TODO: Free these values from memory
float modValue;
//TODO: Free these values from memory
Vertex* vertices;
int vSize;
unsigned int* indices;
int iSize;
const char* texture;

View File

@ -4,6 +4,7 @@
#include "Vertex.h"
Vertex::Vertex() = default;
Vertex::Vertex(glm::vec3* pos, glm::vec3* nml, glm::vec2* tex) {
this->pos = pos;

View File

@ -10,6 +10,7 @@
struct Vertex {
public:
Vertex();
Vertex(glm::vec3* pos, glm::vec3* nml, glm::vec2* tex);
glm::vec3* pos;