Started simplifying main.c by modularizing different subsystems

This commit is contained in:
Adog64 2024-01-13 22:52:39 -05:00
parent 46bde19035
commit 922a02aa1c
17 changed files with 209 additions and 107 deletions

View File

@ -25,7 +25,7 @@ LDLIBS =
# add include paths (start with -I)
INC =
# finds all your objects that corrispond to your .cpp files, system agnostic version
# finds all your objects that corrispond to your .c files, system agnostic version
OBJECTS := $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(wildcard $(SRC_DIR)/*.c))
.PHONY: all

Binary file not shown.

BIN
bin/initialization.o Normal file

Binary file not shown.

Binary file not shown.

BIN
bin/renderer.o Normal file

Binary file not shown.

BIN
bin/shaders.o Normal file

Binary file not shown.

Binary file not shown.

48
src/initialization.c Normal file
View File

@ -0,0 +1,48 @@
#include "initialization.h"
void initializeWindowSystem(GLFWwindow** windowPtr)
{
glewExperimental = true;
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW :(\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
*windowPtr = glfwCreateWindow(WINW, WINH, "Planet Miner", NULL, NULL);
if( *windowPtr == NULL )
{
fprintf( stderr, "Failed to create window. If you are using an Intel GPU, please note that they are not compatible with OpenGL 3.3\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(*windowPtr);
glewExperimental = true;
if( glewInit() != GLEW_OK )
{
fprintf( stderr, "Failed to initialize GLEW\n" );
getchar();
glfwTerminate();
return -1;
}
glfwSetInputMode(*windowPtr, GLFW_STICKY_KEYS, GL_TRUE);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
printf("Window: %d\n", (int)windowPtr);
}
void initializeVertexBuffer(GLuint* vertexBufferPtr, GLfloat* vertexBufferData)
{
glGenBuffers(1, vertexBufferPtr);
glBindBuffer(GL_ARRAY_BUFFER, *vertexBufferPtr);
glBufferData(GL_ARRAY_BUFFER, sizeof(*vertexBufferData), vertexBufferData, GL_STATIC_DRAW);
}

11
src/initialization.h Normal file
View File

@ -0,0 +1,11 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdbool.h>
#include <stdio.h>
#define WINW 800
#define WINH 450
void initializeWindowSystem(GLFWwindow** windowPtr);
void initializeVertexBuffer(GLuint* vertexBufferPtr, GLfloat* vertexBufferDataPtr);
void initializeRenderer();

View File

@ -5,86 +5,48 @@
#include <math.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <cglm/struct.h>
#include "worldgen.h"
#define WINW 800
#define WINH 450
#include "shaders.h"
#include "initialization.h"
#include "renderer.h"
#define FOV 1.4f
#define SHADER_BUF_SIZE 1000
GLuint loadShaders(const char*, const char*);
GLFWwindow** window;
static GLfloat vertexBufferData[1000];
void setVertexBufferData(GLfloat*, unsigned int*, unsigned int);
int main()
{
glewExperimental = true;
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW :(\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(WINW, WINH, "Planet Miner", NULL, NULL);
if( window == NULL )
{
fprintf( stderr, "Failed to create window. If you are using an Intel GPU, please note that they are not compatible with OpenGL 3.3\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if( glewInit() != GLEW_OK )
{
fprintf( stderr, "Failed to initialize GLEW\n" );
getchar();
glfwTerminate();
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// start initialization
initializeWindowSystem(&window);
GLuint vertexArrayID;
glGenVertexArrays(1, &vertexArrayID);
glBindVertexArray(vertexArrayID);
//glEnable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
GLuint programID = loadShaders( "src/shader.vert", "src/shader.frag" );
GLuint matrixID = glGetUniformLocation(programID, "MVP");
mat4s projection = glms_perspective(FOV, 16.0f / 9.0f, 0.1f, 100.0f);
shaderSetMat4(programID, "projection", &projection);
vec3s eye = {0.0f, 0.0f, 5.0f};
vec3s center = {0.0f, 0.0f, 0.0f};
vec3s up = {0.0f, 1.0f, 0.0f};
vec3s planetPos = {0.0f, 0.0f, 0.0f};
worldMesh* mesh = generateWorld(planetPos);
setVertexBufferData(mesh->vertices, mesh->triangles, mesh->triangleArrayCount);
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW);
GLuint vertexBuffer;
initializeVertexBuffer(&vertexBuffer, &vertexBufferData);
vec3s direction = {0.0f, 0.0f, -5.0f};
@ -96,14 +58,21 @@ int main()
mat4s view = glms_lookat(eye, center, up);
mat4s model = glms_mat4_identity();
mat4s mvp = glms_mat4_mul(glms_mat4_mul(projection, view), model);
// start shaders
glUseProgram(programID);
shaderSet3f(programID, "objectColor", 1.0f, 0.5f, 0.31f);
shaderSet3f(programID, "lightColor", 1.0f, 1.0f, 1.0f);
shaderSet3f(programID, "lightPos", 1.0f, 1.0f, 1.0f);
shaderSetVec3(programID, "viewPos", &eye);
shaderSetMat4(programID, "view", &view);
shaderSetMat4(programID, "model", &model);
glUniformMatrix4fv(matrixID, 1, GL_FALSE, &mvp);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(
0,
@ -116,6 +85,7 @@ int main()
glDrawArrays(GL_TRIANGLES, 0, 20*3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
@ -124,7 +94,7 @@ int main()
&& glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS)
&& !glfwWindowShouldClose(window));
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &vertexBuffer);
glDeleteProgram(programID);
glDeleteVertexArrays(1, &vertexArrayID);
@ -132,38 +102,3 @@ int main()
return 0;
}
void setVertexBufferData(GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount)
{
vec3s triangleVerts[3];
int startingIndex;
for( unsigned int i = 0; i < triangleCount; i+=3 )
{
startingIndex = i*6;
for( int j = 0; j < 3; ++j )
{
triangleVerts[j].x = vertices[triangles[i+j]*3];
triangleVerts[j].y = vertices[triangles[i+j]*3 + 1];
triangleVerts[j].z = vertices[triangles[i+j]*3 + 2];
vertexBufferData[startingIndex + j*6] = triangleVerts[j].x;
vertexBufferData[startingIndex + j*6 + 1] = triangleVerts[j].y;
vertexBufferData[startingIndex + j*6 + 2] = triangleVerts[j].z;
}
vec3s normal = glms_vec3_crossn(glms_vec3_sub(triangleVerts[0], triangleVerts[1]),
glms_vec3_sub(triangleVerts[0], triangleVerts[2]));
vertexBufferData[startingIndex + 3] = normal.x;
vertexBufferData[startingIndex + 4] = normal.y;
vertexBufferData[startingIndex + 5] = normal.z;
vertexBufferData[startingIndex + 9] = normal.x;
vertexBufferData[startingIndex + 10] = normal.y;
vertexBufferData[startingIndex + 11] = normal.z;
vertexBufferData[startingIndex + 15] = normal.x;
vertexBufferData[startingIndex + 16] = normal.y;
vertexBufferData[startingIndex + 17] = normal.z;
}
}

36
src/renderer.c Normal file
View File

@ -0,0 +1,36 @@
#include "renderer.h"
#include <cglm/struct.h>
void setVertexBufferData(GLfloat* vertexBufferData, GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount)
{
vec3s triangleVerts[3];
int startingIndex;
for( unsigned int i = 0; i < triangleCount; i+=3 )
{
startingIndex = i*6;
for( int j = 0; j < 3; ++j )
{
triangleVerts[j].x = vertices[triangles[i+j]*3];
triangleVerts[j].y = vertices[triangles[i+j]*3 + 1];
triangleVerts[j].z = vertices[triangles[i+j]*3 + 2];
vertexBufferData[startingIndex + j*6] = triangleVerts[j].x;
vertexBufferData[startingIndex + j*6 + 1] = triangleVerts[j].y;
vertexBufferData[startingIndex + j*6 + 2] = triangleVerts[j].z;
}
vec3s normal = glms_vec3_crossn(glms_vec3_sub(triangleVerts[0], triangleVerts[2]),
glms_vec3_sub(triangleVerts[0], triangleVerts[1]));
vertexBufferData[startingIndex + 3] = normal.x;
vertexBufferData[startingIndex + 4] = normal.y;
vertexBufferData[startingIndex + 5] = normal.z;
vertexBufferData[startingIndex + 9] = normal.x;
vertexBufferData[startingIndex + 10] = normal.y;
vertexBufferData[startingIndex + 11] = normal.z;
vertexBufferData[startingIndex + 15] = normal.x;
vertexBufferData[startingIndex + 16] = normal.y;
vertexBufferData[startingIndex + 17] = normal.z;
}
}

2
src/renderer.h Normal file
View File

@ -0,0 +1,2 @@
#include <GL/glew.h>
void setVertexBufferData(GLfloat* vertexBufferData, GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount);

View File

@ -1,15 +1,16 @@
#version 330 core
layout(location = 0) in vec3 aPos;
layout(location = 1) in vec3 aNormal;
out vec3 FragPos;
out vec3 Normal;
uniform mat4 MVP;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main(){
FragPos = vec3(model * vec4(aPos, 1.0));
Normal = mat3(transpose(inverse(model))) * aNormal;
gl_Position = MVP * vec4(aPos, 1.0);
gl_Position = projection * view * vec4(aPos, 1.0);
}

View File

@ -1,3 +1,6 @@
#include "shaders.h"
#include "textfiles.h"
GLuint loadShaders(const char* vertexShaderPath, const char* fragmentShaderPath)
{
char* vertexShaderCode = readTextFile( vertexShaderPath );
@ -61,4 +64,62 @@ GLuint loadShaders(const char* vertexShaderPath, const char* fragmentShaderPath)
return programID;
}
void shaderSetBool(GLuint ID, char* name, bool value)
{
glUniform1i(glGetUniformLocation(ID, name), (int) value);
}
void shaderSetInt(GLuint ID, char* name, int value)
{
glUniform1i(glGetUniformLocation(ID, name), value);
}
void shaderSetFloat(GLuint ID, char* name, float value)
{
glUniform1f(glGetUniformLocation(ID, name), value);
}
void shaderSetVec2(GLuint ID, char* name, vec2s* ref)
{
glUniform2fv(glGetUniformLocation(ID, name), 1, ref);
}
void shaderSet2f(GLuint ID, char* name, float x, float y)
{
glUniform2f(glGetUniformLocation(ID, name), x, y);
}
void shaderSetVec3(GLuint ID, char* name, vec3s* ref)
{
glUniform3fv(glGetUniformLocation(ID, name), 1, ref);
}
void shaderSet3f(GLuint ID, char* name, float x, float y, float z)
{
glUniform3f(glGetUniformLocation(ID, name), x, y, z);
}
void shaderSetVec4(GLuint ID, char* name, vec4s* ref)
{
glUniform4fv(glGetUniformLocation(ID, name), 1, ref);
}
void shaderSet4f(GLuint ID, char* name, float x, float y, float z, float w)
{
glUniform4f(glGetUniformLocation(ID, name), x, y, z, w);
}
void shaderSetMat2(GLuint ID, char* name, mat2s* ref)
{
glUniformMatrix2fv(glGetUniformLocation(ID, name), 1, GL_FALSE, ref);
}
void shaderSetMat3(GLuint ID, char* name, mat3s* ref)
{
glUniformMatrix3fv(glGetUniformLocation(ID, name), 1, GL_FALSE, ref);
}
void shaderSetMat4(GLuint ID, char* name, mat4s* ref)
{
glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, ref);
}

View File

@ -4,21 +4,19 @@
GLuint loadShaders(const char* vertexShaderPath, const char* fragmentShaderPath);
void use();
void shaderSetBool(GLuint ID, char* name, bool value);
void shaderSetInt(GLuint ID, char* name, int value);
void shaderSetFloat(GLuint ID, char* name, float value);
void setBool(char* name, bool value);
void setInt(char* name, int value);
void setFloat(char* name, float value);
void shaderSetVec2(GLuint ID, char* name, vec2s* ref);
void shaderSet2f(GLuint ID, char* name, float x, float y);
void setVec2(char* name, vec2s value);
void setVec3(char* name, float x, float y);
void shaderSetVec3(GLuint ID, char* name, vec3s* ref);
void shaderSet3f(GLuint ID, char* name, float x, float y, float z);
void setVec3(char* name, vec3s value);
void setVec3(char* name, float x, float y, float z);
void shaderSetVec4(GLuint ID, char* name, vec4s* ref);
void shaderSet4f(GLuint ID, char* name, float x, float y, float z, float w);
void setVec4(char* name, vec4s value);
void setVec4(char* name, float x, float y, float z, float w);
void setMat2(char* name, mat2s value);
void setMat3(char* name, mat3s value);
void setMat4(char* name, mat4s value);
void shaderSetMat2(GLuint ID, char* name, mat2s* ref);
void shaderSetMat3(GLuint ID, char* name, mat3s* ref);
void shaderSetMat4(GLuint ID, char* name, mat4s* ref);

View File

@ -4,6 +4,9 @@
#include <math.h>
#include <string.h>
#define MIN(i, j) (((i) < (j)) ? (i) : (j))
#define MAX(i, j) (((i) > (j)) ? (i) : (j))
#define ORDER 0
#define RSQRT5 0.4472135954999579f
#define TWICE_RSQRT5 0.8944271909999159f
@ -65,3 +68,8 @@ void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat
vertices[i + 2] = vertex.z;
}
}
unsigned int reducedSymmetricPair(unsigned int a, unsigned int b)
{
return ((a+b+1)*(a+b+1) - (a+b+1)&1) >> 2 + MIN(a,b);
}

View File

@ -19,3 +19,5 @@ typedef struct worldMesh
worldMesh* generateWorld(vec3s pos);
void populateIcosphere(worldMesh* mesh, unsigned char order, GLfloat radius);
void normalizeVertices(GLfloat* vertices, unsigned int vertexArrayCount, GLfloat radius);
unsigned int reducedSymmetricPair(unsigned int, unsigned int);