diff --git a/include/model.h b/include/model.h index 43e7707..cf1fae3 100644 --- a/include/model.h +++ b/include/model.h @@ -22,6 +22,12 @@ int modelRegistry_register(ModelRegistry *, Model *); void modelRegistry_free(ModelRegistry *); GLfloat * model_applyTransformations(Model *); void model_colorFromPosition(Model *); +void model_colorRed(Model *); +void model_colorGreen(Model *); +void model_colorBlue(Model *); +void model_colorWhite(Model *); +void model_colorBlack(Model *); +Model *model_circle(int, GLfloat); #endif diff --git a/src/cx.c b/src/cx.c index 935e84c..4a5ad13 100644 --- a/src/cx.c +++ b/src/cx.c @@ -1,5 +1,26 @@ #include +static void +cx_glBindBuffer(GLfloat *render_buffer, GLuint buffer_address, + GLuint gl_index, GLint member_size, GLsizeiptr bufsize) { + + glBindBuffer(GL_ARRAY_BUFFER, buffer_address); + glBufferData(GL_ARRAY_BUFFER, bufsize, + render_buffer, GL_STATIC_DRAW); + + // 1rst attribute buffer : vertices + glEnableVertexAttribArray(gl_index); + glBindBuffer(GL_ARRAY_BUFFER, buffer_address); + glVertexAttribPointer( + gl_index, // attribute 0 in the pipeline + member_size, // size + GL_FLOAT, // type + GL_FALSE, // normalized? + 0, // stride + NULL // array buffer offset + ); +} + static int cx_glrender(GLFWwindow *window, GLuint programID, ModelRegistry *mr) { @@ -20,42 +41,17 @@ cx_glrender(GLFWwindow *window, GLuint programID, for (int i = 0; i < mr->model_count; i++) { + // Allocate the render buffer + // GL uses this to feed the GPU render_buffer = model_applyTransformations(mr->models[i]); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glBufferData(GL_ARRAY_BUFFER, mr->models[i]->bufsize*4*sizeof(GLfloat), - render_buffer, GL_STATIC_DRAW); - - // 1rst attribute buffer : vertices - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); - glVertexAttribPointer( - 0, // attribute 0 in the pipeline - 4, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - NULL // array buffer offset - ); - - glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); - glBufferData(GL_ARRAY_BUFFER, mr->models[i]->bufsize*3*sizeof(GLfloat), - mr->models[i]->colors, GL_STATIC_DRAW); - - // 1rst attribute buffer : vertices - glEnableVertexAttribArray(2); - glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); - glVertexAttribPointer( - 2, // attribute 0 in the pipeline - 3, // size - GL_FLOAT, // type - GL_FALSE, // normalized? - 0, // stride - NULL // array buffer offset - ); + cx_glBindBuffer(render_buffer, vertexbuffer, 0, 4, + mr->models[i]->bufsize*4*sizeof(GLfloat)); + cx_glBindBuffer(mr->models[i]->colors, colorbuffer, 2, 3, + mr->models[i]->bufsize*3*sizeof(GLfloat)); // Draw! - glDrawArrays(GL_TRIANGLES, 0, mr->models[i]->bufsize); // 3 indices starting at 0 -> 1 triangle + glDrawArrays(GL_TRIANGLES, 0, mr->models[i]->bufsize); glDisableVertexAttribArray(0); glDisableVertexAttribArray(2); @@ -101,7 +97,7 @@ cx_glinit(GLFWwindow **window) { glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Open a window and create its OpenGL context - *window = glfwCreateWindow(1280, 720, "CONTROL-X", NULL, NULL); + *window = glfwCreateWindow(1920, 1080, "CONTROL-X", NULL, NULL); if (*window == NULL) { fprintf(stderr, "Failed to open GLFW window.\n"); glfwTerminate(); @@ -137,30 +133,29 @@ cx_glrun(GLFWwindow *window) { // Establish a model registry ModelRegistry *mr; mr = modelRegistry_new(); - for (int i = 0; i < 3; i++) { + + // Fill the model registry with mesh models + for (int i = 0; i < 64; i++) { // Load model to render from file - Model *model = model_load("../3d_assets/triangle.obj"); - GLfloat *rotation_matrix = matrix_new(); + //Model *model = model_load("../3d_assets/triangle.obj"); + Model *model = model_circle(0, (GLfloat)1/64); GLfloat *translation_matrix = matrix_new(); + GLfloat *aspectRatio_matrix = matrix_new(); + aspectRatio_matrix[0] = (GLfloat)9/16; - rotation_matrix[0] = cos(M_PI*2/256); - rotation_matrix[8] = -sin(M_PI*2/256); - rotation_matrix[2] = sin(M_PI*2/256); - rotation_matrix[10] = cos(M_PI*2/256); + translation_matrix[3] = (((GLfloat)-1*16/9)*.90) + + ((GLfloat)1/32 * i * (((GLfloat)16/9))*.90); - translation_matrix[3] = -0.5 + (0.5 * i); + translation_matrix[7] = .90 - ((GLfloat)1/8 * i * .90); - model->transformations[0] = rotation_matrix; - model->transformations[1] = translation_matrix; + model->transformations[0] = translation_matrix; + model->transformations[1] = aspectRatio_matrix; model->transformation_count = 2; - model_colorFromPosition(model); + model_colorWhite(model); modelRegistry_register(mr, model); } - // Allocate the render buffer - // GL uses this to feed the GPU - // Remainder from cursor experiments, might be useful later double xpos, ypos; diff --git a/src/model.c b/src/model.c index 2bed4e6..9acb9fd 100644 --- a/src/model.c +++ b/src/model.c @@ -113,6 +113,34 @@ model_colorFromPosition(Model *self) { } } +void +model_colorRed(Model *self) { + for (int i = 0; i < self->bufsize; i++) { + self->colors[i*3] = 1.0f; + } +} +void +model_colorGreen(Model *self) { + for (int i = 0; i < self->bufsize; i++) { + self->colors[i*3+1] = 1.0f; + } +} +void +model_colorBlue(Model *self) { + for (int i = 0; i < self->bufsize; i++) { + self->colors[i*3+2] = 1.0f; + } +} + +void +model_colorWhite(Model *self) { + for (int i = 0; i < self->bufsize; i++) { + for (int j = 0; j < 3; j++) { + self->colors[i*3+j] = 1.0f; + } + } +} + Model * model_triangle(Model *self, int detail) { if (self == NULL) { @@ -122,11 +150,32 @@ model_triangle(Model *self, int detail) { } Model * -model_circle(Model *self, int detail) { +model_circle(int detail, GLfloat scale) { + Model *self; + self = model_new(96); if (self == NULL) { - self = model_new(36); + return NULL; } - return 0; + + int k = 0; + for (int i = 0; i < 96; i++) { + self->object[(i*4)+3] = 1.0f; + if (!(i%3)) { + continue; + } + if (!(k%2)) { + self->object[(i*4)+0] = cos((M_PI*2/96)*((GLfloat)i-1.0)) * scale; + self->object[(i*4)+1] = sin((M_PI*2/96)*((GLfloat)i-1.0)) * scale; + } else { + self->object[(i*4)+0] = cos((M_PI*2/96)*((GLfloat)i+1.0)) * scale; + self->object[(i*4)+1] = sin((M_PI*2/96)*((GLfloat)i+1.0)) * scale; + } + + k++; + self->object[(i*4)+3] = 1.0f; + } + + return self; } Model * @@ -196,7 +245,7 @@ int modelRegistry_register(ModelRegistry *self, Model *model) { if (self->model_count >= self->size) { self->size *= 2; - self->models = realloc(self->models, self->size); + self->models = realloc(self->models, self->size * sizeof(Model *)); if (self->models == NULL) { modelRegistry_free(self); return -1; diff --git a/src/neural.c b/src/neural.c index 8228062..e1345bf 100644 --- a/src/neural.c +++ b/src/neural.c @@ -38,7 +38,7 @@ void neural_randomize(Neural_Network *self) { // Does not randomize, just sets 0.5, but it doesn't matter for now. for (int i = 0; i < self->layers; i++) { - Neuron *n = &(self->n[i*self->layer_size]); + Neuron *n = &(self->n[self->layer_size*i]); for (int j = 0; j < self->layer_size; j++) { n[j].threshold = 0.5; }