Seg faults with planet generation, probaly struct pointer fix

This commit is contained in:
Adog64 2023-12-13 10:50:20 -05:00
parent fc47e35256
commit 5c579bed90
13 changed files with 112 additions and 107 deletions

Binary file not shown.

BIN
bin/imagefiles.o Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

19
src/collections.h Normal file
View File

@ -0,0 +1,19 @@
typedef struct intHashTableItem
{
int key;
int value;
}
typedef struct intHashTable
{
intHashTableItem** items;
int size;
int count;
}
intHashTable* createTable(int);
intHashTableItem* createIntHashTableItem(int key, int value);
void freeIntHashTableItem(intHashTableItem*);
void freeIntHashTable(intHashTable*);
void intHashTableInsert(intHashTable*, int, int);

View File

@ -1,7 +1,7 @@
#include "imagefiles.h" #include "imagefiles.h"
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
/*
bmp loadBitmapImage(char* path) bmp loadBitmapImage(char* path)
{ {
FILE* inFile; FILE* inFile;
@ -58,4 +58,4 @@ bmp loadBitmapImage(char* path)
fread(data, 1, imageSize, inFile); fread(data, 1, imageSize, inFile);
fclose(inFile); fclose(inFile);
} }*/

View File

@ -1,4 +1,5 @@
typedef struct bmp { typedef struct bmp
{
unsigned char header[54]; unsigned char header[54];
unsigned int dataPos; unsigned int dataPos;
unsigned int width, height; unsigned int width, height;

View File

@ -11,6 +11,8 @@ GLFWwindow* window;
#include <cglm/struct.h> #include <cglm/struct.h>
#include "worldgen.h"
#define WINW 800 #define WINW 800
#define WINH 450 #define WINH 450
@ -19,6 +21,9 @@ GLFWwindow* window;
#define SHADER_BUF_SIZE 1000 #define SHADER_BUF_SIZE 1000
GLuint loadShaders(const char*, const char*); GLuint loadShaders(const char*, const char*);
static GLfloat vertexBufferData[1000];
void setVertexBufferData(GLfloat*, unsigned int*, unsigned int);
int main() int main()
{ {
@ -72,93 +77,14 @@ int main()
vec3s center = {0.0f, 0.0f, 0.0f}; vec3s center = {0.0f, 0.0f, 0.0f};
vec3s up = {0.0f, 1.0f, 0.0f}; vec3s up = {0.0f, 1.0f, 0.0f};
static const GLfloat g_vertex_buffer_data[] = { vec3s planetPos = {0.0f, 0.0f, 0.0f};
// BAC worldMesh mesh = generateWorld(planetPos);
1.0f, 0.0f, 2.0f, setVertexBufferData(mesh.vertices, mesh.triangles, mesh.triangleArrayCount);
2.0f, 1.0f, 0.0f,
0.0f, 2.0f, 1.0f,
// BCH
1.0f, 0.0f, 2.0f,
0.0f, 2.0f, 1.0f,
-1.0f, 0.0f, 2.0f,
// BHF
1.0f, 0.0f, 2.0f,
-1.0f, 0.0f, 2.0f,
0.0f, -2.0f, 1.0f,
// BFG
1.0f, 0.0f, 2.0f,
0.0f, -2.0f, 1.0f,
2.0f, -1.0f, 0.0f,
// BGA
1.0f, 0.0f, 2.0f,
2.0f, -1.0f, 0.0f,
2.0f, 1.0f, 0.0f,
// AIC
2.0f, 1.0f, 0.0f,
0.0f, 2.0f, -1.0f,
0.0f, 2.0f, 1.0f,
// ICD
0.0f, 2.0f, -1.0f,
0.0f, 2.0f, 1.0f,
-2.0f, 1.0f, 0.0f,
// CDH
0.0f, 2.0f, 1.0f,
-2.0f, 1.0f, 0.0f,
-1.0f, 0.0f, 2.0f,
// DHJ
-2.0f, 1.0f, 0.0f,
-1.0f, 0.0f, 2.0f,
-2.0f, -1.0f, 0.0f,
// HJF
-1.0f, 0.0f, 2.0f,
-2.0f, -1.0f, 0.0f,
0.0f, -2.0f, 1.0f,
// JFL
-2.0f, -1.0f, 0.0f,
0.0f, -2.0f, 1.0f,
0.0f, -2.0f, -1.0f,
// FLG
0.0f, -2.0f, 1.0f,
0.0f, -2.0f, -1.0f,
2.0f, -1.0f, 0.0f,
// LGE
0.0f, -2.0f, -1.0f,
2.0f, -1.0f, 0.0f,
1.0f, 0.0f, -2.0f,
// GEA
2.0f, -1.0f, 0.0f,
1.0f, 0.0f, -2.0f,
2.0f, 1.0f, 0.0f,
// EAI
1.0f, 0.0f, -2.0f,
2.0f, 1.0f, 0.0f,
0.0f, 2.0f, -1.0f,
// KEI
-1.0f, 0.0f, -2.0f,
1.0f, 0.0f, -2.0f,
0.0f, 2.0f, -1.0f,
// KID
-1.0f, 0.0f, -2.0f,
0.0f, 2.0f, -1.0f,
-2.0f, 1.0f, 0.0f,
// KDJ
-1.0f, 0.0f, -2.0f,
-2.0f, 1.0f, 0.0f,
-2.0f, -1.0f, 0.0f,
// KJL
-1.0f, 0.0f, -2.0f,
-2.0f, -1.0f, 0.0f,
0.0f, -2.0f, -1.0f,
// KLE
-1.0f, 0.0f, -2.0f,
0.0f, -2.0f, -1.0f,
1.0f, 0.0f, -2.0f
};
GLuint vertexbuffer; GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer); glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(vertexBufferData), vertexBufferData, GL_STATIC_DRAW);
vec3s direction = {0.0f, 0.0f, -5.0f}; vec3s direction = {0.0f, 0.0f, -5.0f};
@ -197,8 +123,8 @@ int main()
} }
// center = glms_vec3_add(eye, direction); // center = glms_vec3_add(eye, direction);
eye.x = radius * (float) sin((double) theta); eye.x = radius * sinf(theta);
eye.z = radius * (float) cos((double) theta); eye.z = radius * cosf(theta);
mat4s view = glms_lookat(eye, center, up); mat4s view = glms_lookat(eye, center, up);
mat4s model = glms_mat4_identity(); mat4s model = glms_mat4_identity();
@ -301,3 +227,11 @@ GLuint loadShaders(const char* vertexShaderPath, const char* fragmentShaderPath)
glDeleteShader( fragmentShaderID ); glDeleteShader( fragmentShaderID );
return programID; return programID;
} }
void setVertexBufferData(GLfloat* vertices, unsigned int* triangles, unsigned int triangleCount)
{
printf("Loading %d triangles into buffer\n", triangleCount);
for( int i = 0; i < triangleCount; ++i )
{
}
}

View File

@ -16,7 +16,7 @@ char* readTextFile(char* fname)
if( inFile == NULL ) if( inFile == NULL )
{ {
fprintf( stderr, "Failed open file, [%s].\n", fname ); fprintf( stderr, "Failed open file, [%s].\n", fname );
exit(); exit(1);
} }
fseek( inFile, 0, SEEK_END ); fseek( inFile, 0, SEEK_END );

View File

@ -1,24 +1,67 @@
#include "worldgen.h" #include "worldgen.h"
#include <cglm/struct.h> #include <cglm/struct.h>
#include <GL/glew.h> #include <GL/glew.h>
#include <math.h>
#include <string.h>
#define ORDER 1 #define ORDER 0
#define RSQRT5 0.4472135954999579 #define RSQRT5 0.4472135954999579f
#define TWICE_RSQRT5 = 0.8944271909999159 #define TWICE_RSQRT5 0.8944271909999159f
#define GOLDEN 1.618033988749895f
worldMesh generateWorld(vec3s position) worldMesh generateWorld(vec3s position)
{ {
// placeholder struct for world mesh // placeholder struct for world mesh
worldMesh mesh; world
mesh.count = 20 * ORDER * 3; // Icosahedron has 20 triangular sides
mesh.vertices = (GLfloat*) malloc(sizeof(GLfloat) * mesh.count);
populateIcosphere(mesh, ORDER, 1);
return mesh; return mesh;
} }
void populateIcosphere(worldMesh mesh, unsigned char order, float radius) void populateIcosphere(unsigned char order, GLfloat radius)
{ {
int T = powf(4, order);
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
GLfloat vertices[] = {
-1.0f, GOLDEN, 0.0f, 1.0f, GOLDEN, 0.0f, -1.0f, -GOLDEN, 0.0f, 1.0f, -GOLDEN, 0.0f,
0.0f, -1.0f, GOLDEN, 0.0f, 1.0f, GOLDEN, 0.0f, -1.0f, -GOLDEN, 0.0f, 1.0f, -GOLDEN,
GOLDEN, 0.0f, -1.0f, GOLDEN, 0.0f, 1.0f, -GOLDEN, 0.0f, -1.0f, -GOLDEN, 0.0f, 1.0f
};
unsigned int triangles[] = {
0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
11, 10, 2, 5, 11, 4, 1, 5, 9, 7, 1, 8, 10, 7, 6,
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)
{
printf("Normalizing %d vertices\n", vertexArrayCount);
vec3s vertex;
for( int i = 0; i < vertexArrayCount; i += 3)
{
vertex.x = vertices[i];
vertex.y = vertices[i + 1];
vertex.z = vertices[i + 2];
glms_vec3_normalize(vertex);
glms_vec3_scale(vertex, radius);
vertices[i] = vertex.x;
vertices[i + 1] = vertex.y;
vertices[i + 2] = vertex.z;
}
} }

View File

@ -1,9 +1,12 @@
#include <cglm/struct.h> #include <cglm/struct.h>
#include <GL/glew.h> #include <GL/glew.h>
typedef struct worldMesh { typedef struct worldMesh
long int count; {
unsigned int vertexArrayCount;
unsigned int triangleArrayCount;
GLfloat* vertices; GLfloat* vertices;
unsigned int* triangles;
} worldMesh; } worldMesh;
@ -14,3 +17,5 @@ typedef struct worldMesh {
* @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);

13
todo.md
View File

@ -2,12 +2,15 @@
## Priority List ## Priority List
1. Render stuff 1. Render stuff
1. Render a cube - [x] Render a cube
2. Render a bunch of cubes - [x] Render icosahedron
3. Diffuse lighting - [ ] Vertices to triangle mesh
4. Ray tracing? - [ ] Subdivide icosahedron
2. Procedural worlds 2. Procedural worlds
1. Optimize rendering for this many cubes - [ ] Morph vertices of the subdivided icosphere
- [ ] Terrain generation
- [ ] Planet scaling
- [ ]
3. Block game 3. Block game
1. World editing 1. World editing