/* * Gem-graph OpenGL experiments * * Desc: GL functions * * Copyright (C) 2023 Adrien Bourmault * Copyright (C) 2023 Jean Sirmai * * This file is part of Gem-graph. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /* * Writes values to describe a vertex at (x,y,z) intoq the vertex buffer * * @param coords GLfloat(x,y,z) * * @return void */ #include #include #include #include "base.h" //#include "ui.h" #include "graph_area.h" void graphics_draw_vertex (const int stack_id, GLfloat x, GLfloat y, GLfloat z) { //g_printerr("stack_id is %d\n", stack_id); //g_printerr("stack_id is %d\n", stack_id); //g_printerr("graphic_stack is at %p\n", graphic_stack); //g_printerr("graphic_stack[stack_id] is at %p\n", graphic_stack + stack_id); volatile struct graphic_stack_t *stack = &graphic_stack[stack_id]; //g_printerr("Currently stack->buffer_vertex_origin @ %p\n", stack->buffer_vertex_origin); //assert (stack->buffer_vertex_origin); stack->buffer_vertex_origin = g_realloc (stack->buffer_vertex_origin, (stack->buffer_vertex_size + 3) * sizeof(GLfloat)); //print_stack(stack_id); stack->buffer_vertex_origin[stack->buffer_vertex_size + 0] = x; stack->buffer_vertex_origin[stack->buffer_vertex_size + 1] = y; stack->buffer_vertex_origin[stack->buffer_vertex_size + 2] = z; stack->buffer_vertex_size += 3; } /* * Writes values to describe a color (r,g,b) into the color buffer * * @param color GLfloat(r,g,b) * * @return void */ void graphics_draw_color (const int stack_id, GLfloat r, GLfloat g, GLfloat b) { struct graphic_stack_t *stack = &graphic_stack[stack_id]; stack->buffer_colors_origin = g_realloc (stack->buffer_colors_origin, (stack->buffer_colors_size + 3) * sizeof(GLfloat)); assert (stack->buffer_colors_origin); stack->buffer_colors_origin[stack->buffer_colors_size + 0] = r; stack->buffer_colors_origin[stack->buffer_colors_size + 1] = g; stack->buffer_colors_origin[stack->buffer_colors_size + 2] = b; stack->buffer_colors_size += 3; } /* * Writes values to describe a line from a to b into the line buffer * * @param coords GLuint (a,b) * * @return void */ void graphics_draw_line (const int stack_id, GLuint a, GLuint b) { struct graphic_stack_t *stack = &graphic_stack[stack_id]; stack->buffer_lines_origin = g_realloc (stack->buffer_lines_origin, (stack->buffer_lines_size + 2) * sizeof(GLuint)); assert (stack->buffer_lines_origin); stack->buffer_lines_origin[stack->buffer_lines_size + 0] = a; stack->buffer_lines_origin[stack->buffer_lines_size + 1] = b; stack->buffer_lines_size += 2; } /* * Writes values to describe an (a,b,c) plan (triangle) into the plan buffer * * @param coords GLuint (a,b,c) * * @return void */ void graphics_draw_plan (const int stack_id, GLuint a, GLuint b, GLuint c) { struct graphic_stack_t *stack = &graphic_stack[stack_id]; stack->buffer_plans_origin = g_realloc (stack->buffer_plans_origin, (stack->buffer_plans_size + 3) * sizeof(GLuint)); assert (stack->buffer_plans_origin); stack->buffer_plans_origin[stack->buffer_plans_size + 0] = a; stack->buffer_plans_origin[stack->buffer_plans_size + 1] = b; stack->buffer_plans_origin[stack->buffer_plans_size + 2] = c; stack->buffer_plans_size += 3; } /* * Draws the current buffer to a gl_area * * @param gl_area, ptr to the gl_area widget * * @return void */ void graphics_draw(const int stack_id) { struct graphic_stack_t *stack = &graphic_stack[stack_id]; //g_printerr("[debug] graphics_draw() started\n"); //print_stack(stack_id); GLint cur_viewport[4]; glGetIntegerv(GL_VIEWPORT, cur_viewport); mat4 m = GLM_MAT4_IDENTITY_INIT; glm_rotate_x(m, glm_rad(stack->rotation_angles[X_AXIS]), m); glm_rotate_y(m, glm_rad(stack->rotation_angles[Y_AXIS]), m); glm_rotate_z(m, glm_rad(stack->rotation_angles[Z_AXIS]), m); mat4 v = GLM_MAT4_IDENTITY_INIT; // XXX define zoom and translations here ? mat4 p = GLM_MAT4_IDENTITY_INIT; //glm_ortho(-1.0f, +1.0f, -1.0f, +1.0f, -1.0f, +1.0f, p); glm_ortho_default((float)cur_viewport[2] / (float)cur_viewport[3], p); //glm_perspective_default((float)cur_viewport[2] / (float)cur_viewport[3], p); /* Use our shaders */ glUseProgram(stack->program); glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* Update the "mvp" matrix we use in the shader */ glUniformMatrix4fv(stack->m, 1, GL_FALSE, (float *)m); glUniformMatrix4fv(stack->v, 1, GL_FALSE, (float *)v); glUniformMatrix4fv(stack->p, 1, GL_FALSE, (float *)p); /* Use the vertices in our buffer */ glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, stack->position_buffer); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); // couleurs glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, stack->color_buffer); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0); //glEnable(GL_DEPTH_TEST); glDrawElements(GL_LINES, stack->buffer_lines_size, GL_UNSIGNED_INT, stack->buffer_lines_origin); glDrawElements(GL_TRIANGLES, stack->buffer_plans_size, GL_UNSIGNED_INT, stack->buffer_plans_origin); /* We finished using the buffers and program */ glDisableVertexAttribArray(0); glDisableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, 0); glUseProgram(0); glFlush(); //g_printerr("[debug] graphics_draw() ended\n"); }