From edfe78a36ed912cdb77fd45ddb38f37e676b3049 Mon Sep 17 00:00:00 2001 From: Marcel Plch Date: Sat, 6 Jul 2024 18:04:24 +0200 Subject: [PATCH] A whole bunch of unrelated tweaks. --- 3d_assets/cube.obj | 26 ++++++++ 3d_assets/cube_rotated.obj | 24 +++++++ triangle.obj => 3d_assets/triangle.obj | 0 include/tensor.h | 7 +- ncurses/void | 0 shaders/SimpleFragmentShader.fragmentshader | 3 +- shaders/SimpleVertexShader.vertexshader | 10 +-- src/cx.c | 74 +++++++++++++++------ src/main.c | 8 ++- src/model.c | 28 ++++---- src/neural.c | 3 +- src/tensor.c | 28 ++++---- 12 files changed, 149 insertions(+), 62 deletions(-) create mode 100644 3d_assets/cube.obj create mode 100644 3d_assets/cube_rotated.obj rename triangle.obj => 3d_assets/triangle.obj (100%) delete mode 100644 ncurses/void diff --git a/3d_assets/cube.obj b/3d_assets/cube.obj new file mode 100644 index 0000000..d71c8bf --- /dev/null +++ b/3d_assets/cube.obj @@ -0,0 +1,26 @@ +# Blender 4.1.1 +# www.blender.org +mtllib cube.mtl +o Cube +v 0.370694 0.370694 -0.370694 +v 0.370694 -0.370694 -0.370694 +v 0.370694 0.370694 0.370694 +v 0.370694 -0.370694 0.370694 +v -0.370694 0.370694 -0.370694 +v -0.370694 -0.370694 -0.370694 +v -0.370694 0.370694 0.370694 +v -0.370694 -0.370694 0.370694 +s 0 +usemtl Material +f 5 3 1 +f 3 8 4 +f 7 6 8 +f 2 8 6 +f 1 4 2 +f 5 2 6 +f 5 7 3 +f 3 7 8 +f 7 5 6 +f 2 4 8 +f 1 3 4 +f 5 1 2 diff --git a/3d_assets/cube_rotated.obj b/3d_assets/cube_rotated.obj new file mode 100644 index 0000000..e54a1e1 --- /dev/null +++ b/3d_assets/cube_rotated.obj @@ -0,0 +1,24 @@ +# Blender 4.1.1 +# www.blender.org +o Cube +v 0.732743 -0.117712 -0.338262 +v 0.295571 -0.722522 0.236198 +v 0.507624 0.592343 0.237987 +v 0.070452 -0.012468 0.812447 +v -0.070452 0.012468 -0.812447 +v -0.507624 -0.592343 -0.237987 +v -0.295571 0.722522 -0.236198 +v -0.732743 0.117712 0.338262 +s 0 +f 5 3 1 +f 3 8 4 +f 7 6 8 +f 2 8 6 +f 1 4 2 +f 5 2 6 +f 5 7 3 +f 3 7 8 +f 7 5 6 +f 2 4 8 +f 1 3 4 +f 5 1 2 diff --git a/triangle.obj b/3d_assets/triangle.obj similarity index 100% rename from triangle.obj rename to 3d_assets/triangle.obj diff --git a/include/tensor.h b/include/tensor.h index ca01589..e495dbc 100644 --- a/include/tensor.h +++ b/include/tensor.h @@ -1,12 +1,11 @@ #ifndef MATRIX_H #define MATRIX_H -GLfloat *matrix_new(void); +float *matrix_new(void); -GLfloat *matrix_multip(GLfloat *, GLfloat *); +float *matrix_multip(float *, float *); -GLfloat *matrix_transform(GLfloat *, int, - GLfloat *); +float *matrix_transform(float *, int, float *); #endif diff --git a/ncurses/void b/ncurses/void deleted file mode 100644 index e69de29..0000000 diff --git a/shaders/SimpleFragmentShader.fragmentshader b/shaders/SimpleFragmentShader.fragmentshader index 80c966b..d426a82 100644 --- a/shaders/SimpleFragmentShader.fragmentshader +++ b/shaders/SimpleFragmentShader.fragmentshader @@ -1,8 +1,9 @@ #version 330 core +in vec3 colorF; out vec3 color; void main() { - color = vec3(1, 1, 1); + color = colorF; } diff --git a/shaders/SimpleVertexShader.vertexshader b/shaders/SimpleVertexShader.vertexshader index 2f13fbf..a0fee1f 100644 --- a/shaders/SimpleVertexShader.vertexshader +++ b/shaders/SimpleVertexShader.vertexshader @@ -2,13 +2,13 @@ // Input vertex data, different for all executions of this shader. layout(location = 0) in vec4 position; -out float colorF; +layout(location = 1) in vec4 normal; +out vec3 colorF; void main() { - if (position.z <= 0.0) - colorF = 0; - else - colorF = 1; + colorF.x = position.x; + colorF.y = position.y; + colorF.z = position.z; gl_Position = position; } diff --git a/src/cx.c b/src/cx.c index 22c8a35..0545e5d 100644 --- a/src/cx.c +++ b/src/cx.c @@ -4,16 +4,17 @@ int cx_glinit(GLFWwindow **window) { // Initialise GLFW if(!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW\n"); - getchar(); 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); // To make MacOS happy; should not be needed glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + // To make MacOS happy; should not be needed + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + // Open a window and create its OpenGL context *window = glfwCreateWindow(1280, 720, "CONTROL-X", NULL, NULL); if (*window == NULL) { @@ -58,19 +59,21 @@ int cx_glrun(GLFWwindow *window) { // Load model to render from file Model *model; - model = model_load("../triangle.obj"); + model = model_load("../3d_assets/cube.obj"); // Allocate the render buffer // GL uses this to feed the GPU GLfloat *render_buffer; render_buffer = malloc(model->bufsize * 4 * sizeof(GLfloat)); - memcpy(render_buffer, model->object, model->bufsize * 4 * sizeof(GLfloat)); + memcpy(render_buffer, model->object, + model->bufsize * 4 * sizeof(GLfloat)); // Bind the render buffer to OpenGL GLuint vertexbuffer; glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), render_buffer, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), + render_buffer, GL_STATIC_DRAW); // 1rst attribute buffer : vertices glEnableVertexAttribArray(0); @@ -89,16 +92,17 @@ int cx_glrun(GLFWwindow *window) { glfwGetCursorPos(window, &xpos, &ypos); GLfloat *rotation_matrix = matrix_new(); - GLfloat *projection_matrix = matrix_new(); + GLfloat *translation_matrix = matrix_new(); + // Temporary storage of transformation results - GLfloat *temp_buffer; - GLfloat *projected_buffer; + GLfloat *temp_buffer[2]; - projection_matrix[14] = -1.0f; - projection_matrix[0] = (GLfloat)9/16; // Widescreen FOV + translation_matrix[3] = 0.5f; + int t = 0; + do { // Clear the screen. It's not mentioned before Tutorial 02, // but it can cause flickering, so it's there nonetheless. @@ -106,24 +110,23 @@ int cx_glrun(GLFWwindow *window) { // Use our shader glUseProgram(programID); - time_t t = time(NULL); - rotation_matrix[0] = cos(M_PI*2/60*(t%60)); - rotation_matrix[4] = -sin(M_PI*2/60*(t%60)); - rotation_matrix[1] = sin(M_PI*2/60*(t%60)); - rotation_matrix[5] = cos(M_PI*2/60*(t%60)); + rotation_matrix[0] = cos(M_PI*2/256*(t%256)); + rotation_matrix[8] = -sin(M_PI*2/256*(t%256)); + rotation_matrix[2] = sin(M_PI*2/256*(t%256)); + rotation_matrix[10] = cos(M_PI*2/256*(t%256)); // BANANA, ROH-TAH-TEH - temp_buffer = matrix_transform(model->object, model->bufsize, rotation_matrix); + temp_buffer[0] = matrix_transform(model->object, model->bufsize, rotation_matrix); + temp_buffer[1] = matrix_transform(temp_buffer[0], model->bufsize, translation_matrix); // Guess I'm just projecting. - projected_buffer = matrix_transform(temp_buffer, model->bufsize, projection_matrix); + free(temp_buffer[0]); - memcpy(render_buffer, projected_buffer, model->bufsize * 4 * sizeof(GLfloat)); - free(temp_buffer); - free(projected_buffer); + memcpy(render_buffer, temp_buffer[1], model->bufsize * 4 * sizeof(GLfloat)); + free(temp_buffer[1]); glBufferData(GL_ARRAY_BUFFER, model->bufsize*4*sizeof(GLfloat), render_buffer, GL_STATIC_DRAW); @@ -149,18 +152,45 @@ int cx_glrun(GLFWwindow *window) { glfwSwapBuffers(window); glfwPollEvents(); - // Check if the ESC key was pressed or the window was closed + temp_buffer[0] = matrix_transform(model->object, model->bufsize, translation_matrix); + + t++; usleep(1000000/60); + // Check if the ESC key was pressed or the window was closed } while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && !glfwWindowShouldClose(window)); // Close OpenGL window and terminate GLFW glfwTerminate(); free(rotation_matrix); - free(projection_matrix); model_free(model); free(render_buffer); return 0; } + +int cx_nninit(Neural_Network **nn) { + // Allocate a Neural Network + *nn = neural_new(64, 1); + if(!*nn) { + fprintf(stderr, "Failed to initialize Neural Network.\n"); + return -1; + } + + // Populate the neural network with sensible values. + neural_randomize(*nn); + + return 0; +} + +int cx_nnrun(Neural_Network *nn) { + + // Establish a neural interface. + float *input_buffer = malloc(64*sizeof(float)); + float *output_buffer; + + output_buffer = neural_process(nn, input_buffer); + return 0; +} + diff --git a/src/main.c b/src/main.c index 1ec9085..f971510 100644 --- a/src/main.c +++ b/src/main.c @@ -10,11 +10,17 @@ int main(void) { GLFWwindow *window; + Neural_Network *nn; + int retval; + + if (cx_glinit(&window)) { return -1; } + if (cx_nninit(&nn)) { + return -1; + } - int retval; retval = cx_glrun(window); return retval; } diff --git a/src/model.c b/src/model.c index f211a7e..890661d 100644 --- a/src/model.c +++ b/src/model.c @@ -2,17 +2,17 @@ Model * model_new(size_t size) { - Model *model = calloc(1, sizeof(Model)); - model->object = calloc((size ? size : 1) *4 , sizeof(GLfloat)); - model->bufsize = size; - return model; + Model *self = calloc(1, sizeof(Model)); + self->object = calloc((size ? size : 1) *4 , sizeof(GLfloat)); + self->bufsize = size; + return self; } Model * model_load(const char *path) { - Model *model; - GLfloat *vertices; + Model *self; + float *vertices; int *faces; size_t vertcount, facecount; char type, check; @@ -20,7 +20,7 @@ model_load(const char *path) { f = fopen(path, "r"); vertices = malloc(3 * sizeof(GLfloat)); - faces = malloc(3*sizeof(int *)); + faces = malloc(3*sizeof(int)); type = 0; vertcount = 0; facecount = 0; @@ -38,8 +38,8 @@ model_load(const char *path) { vertcount++; } else if (type == 'f') { - faces = realloc(faces, (facecount+1)*3*sizeof(GLfloat)); - check = fscanf(f, "%d %d %d\n", &(faces[facecount*3]), + faces = realloc(faces, (facecount+1)*3*sizeof(int)); + check = fscanf(f, "%i %i %i\n", &(faces[facecount*3]), &(faces[facecount*3+1]), &(faces[facecount*3+2])); facecount++; @@ -50,18 +50,20 @@ model_load(const char *path) { } } while(check != EOF); - model = model_new(facecount*3); + self = model_new(facecount*4); for (int i = 0; i < facecount; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { - model->object[i*12+j*4+k] = vertices[(faces[i*3+j]-1)*3+k]; + self->object[i*12+j*4+k] = vertices[(faces[i*3+j]-1)*3+k]; + printf("%f, ", vertices[(faces[i*3+j]-1)*3+k]); } - model->object[i*12+j*4+3] = 1; + printf("\n"); + self->object[i*12+j*4+3] = 1; } } free(vertices); free(faces); - return model; + return self; } int diff --git a/src/neural.c b/src/neural.c index 16b741c..8228062 100644 --- a/src/neural.c +++ b/src/neural.c @@ -9,7 +9,7 @@ neural_new(size_t layer_size, size_t layers) { self->layer_size = layer_size; self->layers = layers; self->n = calloc(layer_size*layers, sizeof(Neuron)); - + for (int j = 0; j < layers; j++) { n = &(self->n[j*layer_size]); for (int i = 0; i < layers; i++) { @@ -32,7 +32,6 @@ neural_new(size_t layer_size, size_t layers) { } return self; - } void diff --git a/src/tensor.c b/src/tensor.c index 0dd589b..bb2c65e 100644 --- a/src/tensor.c +++ b/src/tensor.c @@ -1,10 +1,10 @@ #include "cx.h" -GLfloat * +float * matrix_new() { - GLfloat *mat; + float *mat; - mat = calloc(16, sizeof(GLfloat)); + mat = calloc(16, sizeof(float)); for (int i = 0; i < 4; i++) { mat[i*4+i] = 1; @@ -13,10 +13,10 @@ matrix_new() { return mat; } -GLfloat * -matrix_multip(GLfloat *mat1, GLfloat *mat2) { - GLfloat *result; - GLfloat dot_prod; +float * +matrix_multip(float *mat1, float *mat2) { + float *result; + float dot_prod; result = matrix_new(); @@ -34,13 +34,13 @@ matrix_multip(GLfloat *mat1, GLfloat *mat2) { return result; } -GLfloat * -matrix_transform(GLfloat *vects, int vectcount, - GLfloat *mat) { - GLfloat dot_prod; - GLfloat *result; +float * +matrix_transform(float *vects, int vectcount, + float *mat) { + float dot_prod; + float *result; - result = calloc(vectcount*4, sizeof(GLfloat)); + result = calloc(vectcount*4, sizeof(float)); for (int k = 0; k < vectcount; k++) { for (int j = 0; j < 4; j++) { @@ -51,7 +51,7 @@ matrix_transform(GLfloat *vects, int vectcount, result[j+k*4] = dot_prod; } if (result[k*4+3] != 0.0f) { - GLfloat div = result[k*4+3]; + float div = result[k*4+3]; for (int i = 0; i < 4; i++) { result[k*4+i] /= div; }