WIP: essais
This commit is contained in:
parent
a000fcc414
commit
2a0ed92cfc
|
@ -14,9 +14,9 @@
|
|||
GtkWidget *window;
|
||||
|
||||
// OpenGL objects
|
||||
GLuint vao, vbo, ebo, instance_vbo, arrow_instance_vbo;
|
||||
GLuint arrow_vao, arrow_vbo, arrow_ebo;
|
||||
GLuint edge_ebo;
|
||||
GLuint vao, arrow_vao;
|
||||
GLuint vbo, arrow_vbo, cubes_instance_vbo, arrow_instance_vbo;
|
||||
GLuint ebo, arrow_ebo, edge_ebo;
|
||||
GLuint shader_program;
|
||||
unsigned int num_instances = N_INSTANCE;
|
||||
mat4 instance_matrices[N_INSTANCE];
|
||||
|
@ -39,9 +39,9 @@ mat4 projection, view;
|
|||
// Function Prototypes zone ---------------------------------------------------
|
||||
|
||||
GLuint create_shader_program ();
|
||||
void setup_buffers ();
|
||||
void setup_cubes_buffers ();
|
||||
void setup_arrow_buffers ();
|
||||
void setup_instance_data ();
|
||||
void setup_cube_instance_data ();
|
||||
void setup_arrow_instance_data();
|
||||
static void on_realize (GtkGLArea *area);
|
||||
static gboolean on_render (GtkGLArea *area, GdkGLContext *context);
|
||||
|
@ -253,7 +253,7 @@ GLuint create_shader_program ()
|
|||
}
|
||||
|
||||
// Setup OpenGL buffers (VBO, VAO, EBO for cubes and edges)
|
||||
void setup_buffers ()
|
||||
void setup_cubes_buffers ()
|
||||
{
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
@ -280,29 +280,8 @@ void setup_buffers ()
|
|||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void setup_arrow_buffers() {
|
||||
glGenVertexArrays(1, &arrow_vao);
|
||||
glBindVertexArray(arrow_vao);
|
||||
|
||||
// Vertex buffer for arrows
|
||||
glGenBuffers(1, &arrow_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, arrow_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(arrow_vertices), arrow_vertices, GL_STATIC_DRAW);
|
||||
|
||||
// Index buffer for arrows
|
||||
glGenBuffers(1, &arrow_ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrow_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(arrow_indices), arrow_indices, GL_STATIC_DRAW);
|
||||
|
||||
// Vertex attributes
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
// Setup instance data for cubes (random positions)
|
||||
void setup_instance_data ()
|
||||
void setup_cube_instance_data ()
|
||||
{
|
||||
g_message("[%s] setting up cube instance data...",
|
||||
__func__);
|
||||
|
@ -312,10 +291,10 @@ void setup_instance_data ()
|
|||
g_message("[%s] random seed initialized",
|
||||
__func__);
|
||||
|
||||
glGenBuffers(1, &instance_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, instance_vbo);
|
||||
glGenBuffers(1, &cubes_instance_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, cubes_instance_vbo);
|
||||
|
||||
g_message("[%s] instance_vbo buffer bound",
|
||||
g_message("[%s] cubes_instance_vbo buffer bound",
|
||||
__func__);
|
||||
|
||||
for (unsigned int i = 0; i < num_instances; i++) {
|
||||
|
@ -330,6 +309,7 @@ void setup_instance_data ()
|
|||
|
||||
glm_translate(instance_matrices[i], position);
|
||||
}
|
||||
|
||||
g_message("[%s] generated %d cube instances",
|
||||
__func__,
|
||||
num_instances);
|
||||
|
@ -355,7 +335,30 @@ void setup_instance_data ()
|
|||
__func__);
|
||||
}
|
||||
|
||||
void setup_arrow_instance_data() {
|
||||
void setup_arrow_buffers()
|
||||
{
|
||||
glGenVertexArrays(1, &arrow_vao);
|
||||
glBindVertexArray(arrow_vao);
|
||||
|
||||
// Vertex buffer for arrows
|
||||
glGenBuffers(1, &arrow_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, arrow_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(arrow_vertices), arrow_vertices, GL_STATIC_DRAW);
|
||||
|
||||
// Index buffer for arrows
|
||||
glGenBuffers(1, &arrow_ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrow_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(arrow_indices), arrow_indices, GL_STATIC_DRAW);
|
||||
|
||||
// Vertex attributes
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
||||
glEnableVertexAttribArray(0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void setup_arrow_instance_data()
|
||||
{
|
||||
// Define indices for the arrows (lines from center to cube faces)
|
||||
GLuint arrow_indices[] = {
|
||||
0, 1, // Arrow pointing to right face
|
||||
|
@ -366,18 +369,12 @@ void setup_arrow_instance_data() {
|
|||
0, 6 // Arrow pointing to back face
|
||||
};
|
||||
|
||||
// Generate Vertex Array Object for arrows
|
||||
glGenVertexArrays(1, &arrow_vao);
|
||||
glBindVertexArray(arrow_vao);
|
||||
|
||||
// Generate Vertex Buffer Object for arrows
|
||||
GLuint arrow_vbo;
|
||||
glGenBuffers(1, &arrow_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, arrow_vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(arrow_vertices), arrow_vertices, GL_STATIC_DRAW);
|
||||
|
||||
// Generate Element Buffer Object for arrows
|
||||
GLuint arrow_ebo;
|
||||
glGenBuffers(1, &arrow_ebo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrow_ebo);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(arrow_indices), arrow_indices, GL_STATIC_DRAW);
|
||||
|
@ -436,9 +433,9 @@ static void on_realize(GtkGLArea *area)
|
|||
|
||||
// Create shader program and setup buffers
|
||||
shader_program = create_shader_program();
|
||||
setup_buffers();
|
||||
setup_cubes_buffers();
|
||||
setup_arrow_buffers();
|
||||
setup_instance_data();
|
||||
setup_cube_instance_data();
|
||||
setup_arrow_instance_data(); // Setup arrow data
|
||||
|
||||
// Set the background color
|
||||
|
@ -475,7 +472,7 @@ static gboolean on_render(GtkGLArea *area, GdkGLContext *context)
|
|||
}
|
||||
|
||||
// Update the instance matrix buffer with the modified instance matrices
|
||||
glBindBuffer(GL_ARRAY_BUFFER, instance_vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, cubes_instance_vbo);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, num_instances * sizeof(mat4), instance_matrices);
|
||||
|
||||
// Draw the cubes with the updated instance matrices
|
||||
|
@ -500,6 +497,7 @@ static gboolean on_render(GtkGLArea *area, GdkGLContext *context)
|
|||
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, (const GLfloat *)view);
|
||||
|
||||
// Draw the arrows
|
||||
glBindVertexArray(arrow_vao);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, arrow_ebo);
|
||||
glDrawElementsInstanced(GL_LINES, 12, GL_UNSIGNED_INT, 0, num_instances);
|
||||
|
||||
|
@ -597,3 +595,4 @@ int main (int argc, char *argv[])
|
|||
|
||||
return status;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue