Add dynamic model rendering utilities

This commit is contained in:
Marcel Plch 2024-10-08 11:29:52 +02:00
parent f4cae1e5c1
commit 6a5a7a3b95
Signed by: dormouse
GPG key ID: 2CA77596BC4BDFFE
4 changed files with 102 additions and 52 deletions

View file

@ -22,6 +22,12 @@ int modelRegistry_register(ModelRegistry *, Model *);
void modelRegistry_free(ModelRegistry *); void modelRegistry_free(ModelRegistry *);
GLfloat * model_applyTransformations(Model *); GLfloat * model_applyTransformations(Model *);
void model_colorFromPosition(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 #endif

View file

@ -1,5 +1,26 @@
#include <cx.h> #include <cx.h>
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 static int
cx_glrender(GLFWwindow *window, GLuint programID, cx_glrender(GLFWwindow *window, GLuint programID,
ModelRegistry *mr) { ModelRegistry *mr) {
@ -20,42 +41,17 @@ cx_glrender(GLFWwindow *window, GLuint programID,
for (int i = 0; i < mr->model_count; i++) { 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]); render_buffer = model_applyTransformations(mr->models[i]);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); cx_glBindBuffer(render_buffer, vertexbuffer, 0, 4,
glBufferData(GL_ARRAY_BUFFER, mr->models[i]->bufsize*4*sizeof(GLfloat), mr->models[i]->bufsize*4*sizeof(GLfloat));
render_buffer, GL_STATIC_DRAW); cx_glBindBuffer(mr->models[i]->colors, colorbuffer, 2, 3,
mr->models[i]->bufsize*3*sizeof(GLfloat));
// 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
);
// Draw! // 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(0);
glDisableVertexAttribArray(2); glDisableVertexAttribArray(2);
@ -101,7 +97,7 @@ cx_glinit(GLFWwindow **window) {
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// Open a window and create its OpenGL context // 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) { if (*window == NULL) {
fprintf(stderr, "Failed to open GLFW window.\n"); fprintf(stderr, "Failed to open GLFW window.\n");
glfwTerminate(); glfwTerminate();
@ -137,30 +133,29 @@ cx_glrun(GLFWwindow *window) {
// Establish a model registry // Establish a model registry
ModelRegistry *mr; ModelRegistry *mr;
mr = modelRegistry_new(); 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 // Load model to render from file
Model *model = model_load("../3d_assets/triangle.obj"); //Model *model = model_load("../3d_assets/triangle.obj");
GLfloat *rotation_matrix = matrix_new(); Model *model = model_circle(0, (GLfloat)1/64);
GLfloat *translation_matrix = matrix_new(); GLfloat *translation_matrix = matrix_new();
GLfloat *aspectRatio_matrix = matrix_new();
aspectRatio_matrix[0] = (GLfloat)9/16;
rotation_matrix[0] = cos(M_PI*2/256); translation_matrix[3] = (((GLfloat)-1*16/9)*.90)
rotation_matrix[8] = -sin(M_PI*2/256); + ((GLfloat)1/32 * i * (((GLfloat)16/9))*.90);
rotation_matrix[2] = sin(M_PI*2/256);
rotation_matrix[10] = cos(M_PI*2/256);
translation_matrix[3] = -0.5 + (0.5 * i); translation_matrix[7] = .90 - ((GLfloat)1/8 * i * .90);
model->transformations[0] = rotation_matrix; model->transformations[0] = translation_matrix;
model->transformations[1] = translation_matrix; model->transformations[1] = aspectRatio_matrix;
model->transformation_count = 2; model->transformation_count = 2;
model_colorFromPosition(model); model_colorWhite(model);
modelRegistry_register(mr, model); modelRegistry_register(mr, model);
} }
// Allocate the render buffer
// GL uses this to feed the GPU
// Remainder from cursor experiments, might be useful later // Remainder from cursor experiments, might be useful later
double xpos, ypos; double xpos, ypos;

View file

@ -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 *
model_triangle(Model *self, int detail) { model_triangle(Model *self, int detail) {
if (self == NULL) { if (self == NULL) {
@ -122,11 +150,32 @@ model_triangle(Model *self, int detail) {
} }
Model * Model *
model_circle(Model *self, int detail) { model_circle(int detail, GLfloat scale) {
Model *self;
self = model_new(96);
if (self == NULL) { 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 * Model *
@ -196,7 +245,7 @@ int
modelRegistry_register(ModelRegistry *self, Model *model) { modelRegistry_register(ModelRegistry *self, Model *model) {
if (self->model_count >= self->size) { if (self->model_count >= self->size) {
self->size *= 2; self->size *= 2;
self->models = realloc(self->models, self->size); self->models = realloc(self->models, self->size * sizeof(Model *));
if (self->models == NULL) { if (self->models == NULL) {
modelRegistry_free(self); modelRegistry_free(self);
return -1; return -1;

View file

@ -38,7 +38,7 @@ void
neural_randomize(Neural_Network *self) { neural_randomize(Neural_Network *self) {
// Does not randomize, just sets 0.5, but it doesn't matter for now. // Does not randomize, just sets 0.5, but it doesn't matter for now.
for (int i = 0; i < self->layers; i++) { 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++) { for (int j = 0; j < self->layer_size; j++) {
n[j].threshold = 0.5; n[j].threshold = 0.5;
} }