Generation functions use struct pointers now :)

This commit is contained in:
Adog64 2023-12-13 13:24:57 -05:00
parent 5c579bed90
commit 8c507756c3
5 changed files with 16 additions and 15 deletions

View File

@ -67,7 +67,7 @@ int main()
glBindVertexArray(vertexArrayID); glBindVertexArray(vertexArrayID);
GLuint programID = loadShaders( "src/SimpleVertexShader.vertexshader", "src/SimpleFragmentShader.fragmentshader" ); GLuint programID = loadShaders( "src/shader.vert", "src/shader.frag" );
GLuint matrixID = glGetUniformLocation(programID, "MVP"); GLuint matrixID = glGetUniformLocation(programID, "MVP");
@ -78,7 +78,7 @@ int main()
vec3s up = {0.0f, 1.0f, 0.0f}; vec3s up = {0.0f, 1.0f, 0.0f};
vec3s planetPos = {0.0f, 0.0f, 0.0f}; vec3s planetPos = {0.0f, 0.0f, 0.0f};
worldMesh mesh = generateWorld(planetPos); worldMesh* mesh = generateWorld(planetPos);
setVertexBufferData(mesh.vertices, mesh.triangles, mesh.triangleArrayCount); setVertexBufferData(mesh.vertices, mesh.triangles, mesh.triangleArrayCount);
GLuint vertexbuffer; GLuint vertexbuffer;

View File

@ -9,17 +9,18 @@
#define TWICE_RSQRT5 0.8944271909999159f #define TWICE_RSQRT5 0.8944271909999159f
#define GOLDEN 1.618033988749895f #define GOLDEN 1.618033988749895f
worldMesh generateWorld(vec3s position) worldMesh* generateWorld(vec3s position)
{ {
// placeholder struct for world mesh // placeholder struct for world mesh
world worldMesh* mesh = (worldMesh*) malloc(sizeof(worldMesh));
return mesh; return mesh;
} }
void populateIcosphere(unsigned char order, GLfloat radius) void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius)
{ {
int T = powf(4, order); int T = powf(4, order);
mesh.vertexArrayCount = (10*T + 2) * 3; // Final heap array size for vertices mesh->vertexArrayCount = (10*T + 2) * 3; // Final heap array size for vertices
// Initial vertex array. Not used other than to initialize heap array in mesh // Initial vertex array. Not used other than to initialize heap array in mesh
GLfloat vertices[] = { GLfloat vertices[] = {
@ -36,15 +37,15 @@ void populateIcosphere(unsigned char order, GLfloat radius)
9, 8, 1, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7 9, 8, 1, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7
}; };
mesh.triangleArrayCount = 20*T*3; mesh->triangleArrayCount = 20*T*3;
normalizeVertices(vertices, mesh.vertexArrayCount, radius); normalizeVertices(vertices, mesh->vertexArrayCount, radius);
// Move vetex and triangle data into mesh struct // Move vetex and triangle data into mesh struct
mesh.vertices = (GLfloat*) malloc(sizeof(GLfloat) * mesh.vertexArrayCount); mesh->vertices = (GLfloat*) malloc(sizeof(GLfloat) * mesh->vertexArrayCount);
memcpy(mesh.vertices, vertices, sizeof(GLfloat) * mesh.vertexArrayCount); memcpy(mesh->vertices, vertices, sizeof(GLfloat) * mesh->vertexArrayCount);
mesh.triangles = (unsigned int*) malloc(sizeof(unsigned int) * mesh.triangleArrayCount); mesh->triangles = (unsigned int*) malloc(sizeof(unsigned int) * mesh->triangleArrayCount);
memcpy(mesh.triangles, triangles, sizeof(unsigned int) * 60); memcpy(mesh->triangles, triangles, sizeof(unsigned int) * 60);
} }
void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius) void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius)

View File

@ -16,6 +16,6 @@ typedef struct worldMesh
* @param[in] pos the position of the center of the roughly spherical world * @param[in] pos the position of the center of the roughly spherical world
* @return the world mesh as a heap-allocated list of vertices. * @return the world mesh as a heap-allocated list of vertices.
*/ */
worldMesh generateWorld(vec3s pos); worldMesh* generateWorld(vec3s pos);
worldMesh populateIcosphere(unsigned char order, GLfoat radius); void populateIcosphere(worldMesh* mesh, unsigned char order, GLfoat radius);
void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius);