Continued to simplify main function and build up game engine api

This commit is contained in:
Adog64 2024-01-20 09:48:15 -05:00
parent 922a02aa1c
commit 18c39130db
7 changed files with 48 additions and 41 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -22,7 +22,9 @@ GLFWwindow** window;
static GLfloat vertexBufferData[1000];
void setVertexBufferData(GLfloat*, unsigned int*, unsigned int);
void updateWindow(GLFWwindow** window, GLuint vertexBuffer);
void terminateProgram(GLuint* vertexBufferPtr, GLuint* vertexArrayIDPtr, GLuint programID);
bool shouldTerminate(GLFWwindow** window);
int main()
{
@ -50,8 +52,6 @@ int main()
vec3s direction = {0.0f, 0.0f, -5.0f};
printf("Entering main loop\n");
do
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
@ -70,7 +70,16 @@ int main()
shaderSetMat4(programID, "view", &view);
shaderSetMat4(programID, "model", &model);
updateWindow(window, vertexBuffer);
}
while(!shouldTerminate(window));
terminateProgram(&vertexBuffer, &vertexArrayID, programID);
return 0;
}
void updateWindow(GLFWwindow** window, GLuint vertexBuffer)
{
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
@ -86,19 +95,23 @@ int main()
glDrawArrays(GL_TRIANGLES, 0, 20*3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
while( (glfwGetKey(window, GLFW_KEY_Q) != GLFW_PRESS
&& glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS)
&& !glfwWindowShouldClose(window));
glDeleteBuffers(1, &vertexBuffer);
void terminateProgram(GLuint* vertexBufferPtr, GLuint* vertexArrayIDPtr, GLuint programID)
{
glDeleteBuffers(1, vertexBufferPtr);
glDeleteProgram(programID);
glDeleteVertexArrays(1, &vertexArrayID);
glDeleteVertexArrays(1, vertexArrayIDPtr);
glfwTerminate();
return 0;
}
bool shouldTerminate(GLFWwindow** window)
{
// quit on <ESC>, <Q>, or when otherwise told to
return glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS
|| glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS
|| glfwWindowShouldClose(window);
}

11
src/meshing.h Normal file
View File

@ -0,0 +1,11 @@
#include <cglm/struct.h>
typedef struct
{
unsigned int numVertices;
unsigned int numTexCoords;
unsigned int numNormals;
vec3s* vertices;
vec2s* texCoords;
vec3s* normals;
} mesh;

View File

@ -12,18 +12,18 @@
#define TWICE_RSQRT5 0.8944271909999159f
#define GOLDEN 1.618033988749895f
worldMesh* generateWorld(vec3s position)
mesh* generateWorld(vec3s position)
{
// placeholder struct for world mesh
worldMesh* mesh = (worldMesh*) malloc(sizeof(worldMesh));
populateIcosphere(mesh, ORDER, 1.0f);
return mesh;
mesh* worldMesh = (mesh*) malloc(sizeof(mesh));
populateIcosphere(worldMesh, ORDER, 1.0f);
return worldMesh;
}
void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius)
void populateIcosphere(mesh* worldMesh, unsigned char order, GLfloat radius)
{
int T = powf(4, order);
mesh->vertexArrayCount = (10*T + 2) * 3; // Final heap array size for vertices
worldMesh->numVertices = (10*T + 2) * 3; // Final heap array size for vertices
// Initial vertex array. Not used other than to initialize heap array in mesh
GLfloat vertices[] = {
@ -39,23 +39,13 @@ void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius)
3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
9, 8, 1, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7
};
mesh->triangleArrayCount = 20*T*3;
normalizeVertices(vertices, mesh->vertexArrayCount, radius);
// Move vetex and triangle data into mesh struct
mesh->vertices = (GLfloat*) malloc(sizeof(GLfloat) * mesh->vertexArrayCount);
memcpy(mesh->vertices, vertices, sizeof(GLfloat) * mesh->vertexArrayCount);
mesh->triangles = (unsigned int*) malloc(sizeof(unsigned int) * mesh->triangleArrayCount);
memcpy(mesh->triangles, triangles, sizeof(unsigned int) * 60);
}
void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius)
void normalizeVertices(GLfloat* vertices, unsigned int numVertices, GLfloat radius)
{
printf("Normalizing %d vertices\n", vertexArrayCount);
printf("Normalizing %d vertices\n", numVertices);
vec3s vertex;
for( int i = 0; i < vertexArrayCount; i += 3)
for( int i = 0; i < numVertices; i += 3)
{
vertex.x = vertices[i];
vertex.y = vertices[i + 1];

View File

@ -1,14 +1,7 @@
#include <cglm/struct.h>
#include <GL/glew.h>
typedef struct worldMesh
{
unsigned int vertexArrayCount;
unsigned int triangleArrayCount;
GLfloat* vertices;
unsigned int* triangles;
} worldMesh;
#include "meshing.h"
/*!
* @brief top-level world generation function
@ -16,8 +9,8 @@ typedef struct worldMesh
* @param[in] pos the position of the center of the roughly spherical world
* @return the world mesh as a heap-allocated list of vertices.
*/
worldMesh* generateWorld(vec3s pos);
void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius);
mesh* generateWorld(vec3s pos);
void populateIcosphere(mesh* mesh, unsigned char order, GLfloat radius);
void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius);
unsigned int reducedSymmetricPair(unsigned int, unsigned int);