Continued to simplify main function and build up game engine api
This commit is contained in:
parent
922a02aa1c
commit
18c39130db
BIN
PlanetMiner
BIN
PlanetMiner
Binary file not shown.
BIN
bin/main.o
BIN
bin/main.o
Binary file not shown.
BIN
bin/worldgen.o
BIN
bin/worldgen.o
Binary file not shown.
37
src/main.c
37
src/main.c
@ -22,7 +22,9 @@ GLFWwindow** window;
|
|||||||
|
|
||||||
static GLfloat vertexBufferData[1000];
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
@ -50,8 +52,6 @@ int main()
|
|||||||
|
|
||||||
vec3s direction = {0.0f, 0.0f, -5.0f};
|
vec3s direction = {0.0f, 0.0f, -5.0f};
|
||||||
|
|
||||||
printf("Entering main loop\n");
|
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
|
||||||
@ -69,8 +69,17 @@ int main()
|
|||||||
|
|
||||||
shaderSetMat4(programID, "view", &view);
|
shaderSetMat4(programID, "view", &view);
|
||||||
shaderSetMat4(programID, "model", &model);
|
shaderSetMat4(programID, "model", &model);
|
||||||
|
|
||||||
|
updateWindow(window, vertexBuffer);
|
||||||
|
}
|
||||||
|
while(!shouldTerminate(window));
|
||||||
|
|
||||||
|
terminateProgram(&vertexBuffer, &vertexArrayID, programID);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void updateWindow(GLFWwindow** window, GLuint vertexBuffer)
|
||||||
|
{
|
||||||
glEnableVertexAttribArray(0);
|
glEnableVertexAttribArray(0);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
|
||||||
|
|
||||||
@ -85,20 +94,24 @@ int main()
|
|||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 20*3);
|
glDrawArrays(GL_TRIANGLES, 0, 20*3);
|
||||||
glDisableVertexAttribArray(0);
|
glDisableVertexAttribArray(0);
|
||||||
|
|
||||||
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
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);
|
glDeleteProgram(programID);
|
||||||
glDeleteVertexArrays(1, &vertexArrayID);
|
glDeleteVertexArrays(1, vertexArrayIDPtr);
|
||||||
|
|
||||||
glfwTerminate();
|
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
11
src/meshing.h
Normal 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;
|
@ -12,18 +12,18 @@
|
|||||||
#define TWICE_RSQRT5 0.8944271909999159f
|
#define TWICE_RSQRT5 0.8944271909999159f
|
||||||
#define GOLDEN 1.618033988749895f
|
#define GOLDEN 1.618033988749895f
|
||||||
|
|
||||||
worldMesh* generateWorld(vec3s position)
|
mesh* generateWorld(vec3s position)
|
||||||
{
|
{
|
||||||
// placeholder struct for world mesh
|
// placeholder struct for world mesh
|
||||||
worldMesh* mesh = (worldMesh*) malloc(sizeof(worldMesh));
|
mesh* worldMesh = (mesh*) malloc(sizeof(mesh));
|
||||||
populateIcosphere(mesh, ORDER, 1.0f);
|
populateIcosphere(worldMesh, ORDER, 1.0f);
|
||||||
return mesh;
|
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);
|
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
|
// Initial vertex array. Not used other than to initialize heap array in mesh
|
||||||
GLfloat vertices[] = {
|
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,
|
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
|
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;
|
vec3s vertex;
|
||||||
for( int i = 0; i < vertexArrayCount; i += 3)
|
for( int i = 0; i < numVertices; i += 3)
|
||||||
{
|
{
|
||||||
vertex.x = vertices[i];
|
vertex.x = vertices[i];
|
||||||
vertex.y = vertices[i + 1];
|
vertex.y = vertices[i + 1];
|
||||||
|
@ -1,14 +1,7 @@
|
|||||||
#include <cglm/struct.h>
|
#include <cglm/struct.h>
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
|
|
||||||
typedef struct worldMesh
|
#include "meshing.h"
|
||||||
{
|
|
||||||
unsigned int vertexArrayCount;
|
|
||||||
unsigned int triangleArrayCount;
|
|
||||||
GLfloat* vertices;
|
|
||||||
unsigned int* triangles;
|
|
||||||
} worldMesh;
|
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* @brief top-level world generation function
|
* @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
|
* @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);
|
mesh* generateWorld(vec3s pos);
|
||||||
void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius);
|
void populateIcosphere(mesh* mesh, unsigned char order, GLfloat radius);
|
||||||
void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius);
|
void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius);
|
||||||
|
|
||||||
unsigned int reducedSymmetricPair(unsigned int, unsigned int);
|
unsigned int reducedSymmetricPair(unsigned int, unsigned int);
|
||||||
|
Loading…
Reference in New Issue
Block a user