Fusion travail perso dans dev/graphics
This commit is contained in:
commit
b36730db97
|
@ -0,0 +1,145 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<gem-graph-model version="0.2.1">
|
||||
|
||||
<identity>
|
||||
<name>Test 21-03-2023</name>
|
||||
|
||||
<owner>J</owner>
|
||||
|
||||
<owner_id>J</owner_id>
|
||||
|
||||
<date>time_stamp</date>
|
||||
|
||||
<version>1.0</version>
|
||||
|
||||
<g_ref id="Cause" date="time_stamp" author="J" lang="en">Ref</g_ref>
|
||||
</identity>
|
||||
|
||||
<parameters id="texte" date="0" author="J">
|
||||
<simulation>
|
||||
<max_thread>0</max_thread>
|
||||
<max_cycles>9</max_cycles>
|
||||
|
||||
</simulation>
|
||||
|
||||
<space-param>
|
||||
<!-- loop_on_zero="true" anything moved before zero reenters at the end of space-->
|
||||
<!-- loop_on_zero="false" anything moved before zero is lost-->
|
||||
<!-- loop_on_max="true" anything moved beyond max reenters at the zero of space-->
|
||||
<!-- loop_on_max="false" anything moved beyond max is lost-->
|
||||
|
||||
<dimension x="22" y="4" z="8"/>
|
||||
|
||||
<!-- Site_multiplicity = number of sites in a space unit. -->
|
||||
<!-- Each site points towards a neighbouring space unit. -->
|
||||
<!-- Several arrows can be stacked in the same site. -->
|
||||
|
||||
<site_multiplicity>3</site_multiplicity>
|
||||
|
||||
</space-param>
|
||||
|
||||
</parameters>
|
||||
|
||||
<!-- Model objects definition -->
|
||||
|
||||
<objects id="only dimers in this model" date="time_stamp" author="J">
|
||||
<object id="a dimer" date="time_stamp" author="J">
|
||||
<arrow site="1" weight="1" x="0"/>
|
||||
<arrow site="2" weight="1" x="1"/>
|
||||
</object>
|
||||
|
||||
</objects>
|
||||
|
||||
|
||||
<!-- Saved space description (initial space is sequence 0) -->
|
||||
|
||||
<savedstates id="texte" date="time_stamp" author="J">
|
||||
<state id="initial" date="time_stamp" author="J">
|
||||
<arrow site="1" weight="1" x="0"/>
|
||||
<arrow site="2" weight="1" x="1"/>
|
||||
<arrow site="1" weight="1" x="10"/>
|
||||
<arrow site="2" weight="1" x="11"/>
|
||||
<arrow site="1" weight="1" x="20"/>
|
||||
<arrow site="2" weight="1" x="21"/>
|
||||
|
||||
<!-- Three dimers 1-1 are drawn in the global space
|
||||
at locations (0,1) (10,11) and (20,21) -->
|
||||
|
||||
</state>
|
||||
|
||||
</savedstates>
|
||||
|
||||
<!-- Model transitions definition (rules) -->
|
||||
<!-- In this version : <xs:sequence minOccurs="0" maxOccurs="unbounded">-->
|
||||
<!-- Transitions should be edited by hand and written in a 'human-readable' format -->
|
||||
|
||||
<conditions_tree>
|
||||
<condition_anonym site="1" weight="1" x="0">
|
||||
<condition_anonym site="2" weight="1" x="1">
|
||||
<condition_anonym site="1" weight="0" x="2">
|
||||
<transition_anonym id="move_a_dimer_to_east"
|
||||
date="time_stamp"
|
||||
author="J"
|
||||
probability="0.5"/>
|
||||
<arrow site="1" weight="0" x="0"/>
|
||||
<arrow site="2" weight="0" x="1"/>
|
||||
<arrow site="1" weight="1" x="1"/>
|
||||
<arrow site="2" weight="1" x="2"/>
|
||||
</condition_anonym>
|
||||
<condition_anonym site="2" weight="0" x="-1">
|
||||
<transition_anonym id="move_a_dimer_to_west"
|
||||
date="time_stamp"
|
||||
author="J"
|
||||
probability="0.5"/>
|
||||
<arrow site="1" weight="0" x="0"/>
|
||||
<arrow site="2" weight="0" x="1"/>
|
||||
<arrow site="2" weight="1" x="0"/>
|
||||
<arrow site="1" weight="1" x="1"/>
|
||||
</condition_anonym>
|
||||
</condition_anonym>
|
||||
</condition_anonym>
|
||||
</conditions_tree>
|
||||
|
||||
<conditionslist id="random walk of dimers" date="time_stamp" author="J">
|
||||
|
||||
<condition site="1" weight="1" node_id="1" parent="0" x="0"/>
|
||||
<condition site="2" weight="1" node_id="2" parent="1" x="1"/>
|
||||
<!-- as soon as conditions 1 and 2 are satisfied, a dimer is identified at atation (0,1). -->
|
||||
|
||||
<condition site="1" weight="0" node_id="3" parent="2" x="2"/>
|
||||
<!-- as soon as condition 3 is satisfied,
|
||||
the neighbouring space unit to East of the dimer is empty
|
||||
and the dimer identified by conditions (1,2) can be moved to East. -->
|
||||
|
||||
<condition site="2" weight="0" node_id="4" parent="2" x="-1"/>
|
||||
<!-- as soon as condition 4 is satisfied,
|
||||
the neighbouring space unit to West of the dimer is empty
|
||||
and the dimer identified by conditions (1,2) can be moved to West. -->
|
||||
|
||||
</conditionslist>
|
||||
|
||||
<transitionslist>
|
||||
|
||||
<transition id="move_a_dimer_to_east" date="time_stamp" author="J"
|
||||
parent="3" probability="1">
|
||||
|
||||
<arrow site="1" weight="0" x="0"/>
|
||||
<arrow site="2" weight="0" x="1"/>
|
||||
<arrow site="1" weight="1" x="1"/>
|
||||
<arrow site="2" weight="1" x="2"/>
|
||||
|
||||
</transition>
|
||||
|
||||
<transition id="move_a_dimer_to_west" date="time_stamp" author="J"
|
||||
parent="4" probability="1">
|
||||
|
||||
<arrow site="1" weight="0" x="0"/>
|
||||
<arrow site="2" weight="0" x="1"/>
|
||||
<arrow site="2" weight="1" x="0"/>
|
||||
<arrow site="1" weight="1" x="1"/>
|
||||
|
||||
</transition>
|
||||
|
||||
</transitionslist>
|
||||
|
||||
</gem-graph-model>
|
|
@ -0,0 +1,235 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
|
||||
<!-- https://www.freeformatter.com/xml-validator-xsd.html -->
|
||||
<xs:attribute name="id" type="xs:string"/>
|
||||
<xs:attribute name="node_id" type="xs:integer"/>
|
||||
<xs:attribute name="parent" type="xs:integer"/>
|
||||
<xs:attribute name="date" type="xs:integer"/>
|
||||
<xs:attribute name="name" type="xs:string"/>
|
||||
<xs:attribute name="lang" type="xs:string"/>
|
||||
<xs:attribute name="owner" type="xs:string"/>
|
||||
<xs:attribute name="owner_id" type="xs:integer"/>
|
||||
<xs:attribute name="author" type="xs:string"/>
|
||||
<xs:attribute name="version" type="xs:float"/>
|
||||
<xs:attribute name="probability" type="xs:integer"/>
|
||||
<xs:attribute name="site" type="xs:integer" default="0"/>
|
||||
<xs:attribute name="weight" type="xs:integer" default="0"/>
|
||||
<xs:attribute name="u" type="xs:integer"/>
|
||||
<xs:attribute name="v" type="xs:integer"/>
|
||||
<xs:attribute name="w" type="xs:integer"/>
|
||||
<xs:attribute name="x" type="xs:integer"/>
|
||||
<xs:attribute name="y" type="xs:integer"/>
|
||||
<xs:attribute name="z" type="xs:integer"/>
|
||||
<xs:attribute name="t" type="xs:integer"/>
|
||||
|
||||
|
||||
<xs:attributeGroup name="id_date_author">
|
||||
<xs:attribute ref="id"/>
|
||||
<xs:attribute ref="date"/>
|
||||
<xs:attribute ref="author"/>
|
||||
</xs:attributeGroup>
|
||||
|
||||
<xs:element name="g_ref">
|
||||
<xs:complexType>
|
||||
<xs:simpleContent>
|
||||
<xs:extension base="xs:string">
|
||||
<xs:attributeGroup ref="id_date_author"/>
|
||||
<xs:attribute ref="lang" use="required"/>
|
||||
</xs:extension>
|
||||
</xs:simpleContent>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="quote" substitutionGroup="g_ref"/>
|
||||
|
||||
<xs:element name="dimension">
|
||||
<xs:complexType>
|
||||
<xs:attribute ref="u" use="optional"/>
|
||||
<xs:attribute ref="v" use="optional"/>
|
||||
<xs:attribute ref="w" use="optional"/>
|
||||
<xs:attribute ref="x" use="optional"/>
|
||||
<xs:attribute ref="y" use="optional"/>
|
||||
<xs:attribute ref="z" use="optional"/>
|
||||
<xs:attribute ref="t" use="optional"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="arrow">
|
||||
<xs:complexType>
|
||||
<xs:attribute ref="site" use="required"/>
|
||||
<xs:attribute ref="weight" use="required"/>
|
||||
<xs:attribute ref="u" use="optional"/>
|
||||
<xs:attribute ref="v" use="optional"/>
|
||||
<xs:attribute ref="w" use="optional"/>
|
||||
<xs:attribute ref="x" use="required"/>
|
||||
<xs:attribute ref="y" use="optional"/>
|
||||
<xs:attribute ref="z" use="optional"/>
|
||||
<xs:attribute ref="t" use="optional"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="condition">
|
||||
<xs:complexType>
|
||||
<xs:attribute ref="site" use="required"/>
|
||||
<xs:attribute ref="weight" use="required"/>
|
||||
<xs:attribute ref="node_id" use="required"/>
|
||||
<xs:attribute ref="parent" use="required"/>
|
||||
<xs:attribute ref="u" use="optional"/>
|
||||
<xs:attribute ref="v" use="optional"/>
|
||||
<xs:attribute ref="w" use="optional"/>
|
||||
<xs:attribute ref="x" use="required"/>
|
||||
<xs:attribute ref="y" use="optional"/>
|
||||
<xs:attribute ref="z" use="optional"/>
|
||||
<xs:attribute ref="t" use="optional"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="condition_anonym">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="0" maxOccurs="unbounded">
|
||||
<xs:element ref="condition_anonym"/>
|
||||
</xs:sequence>
|
||||
<xs:sequence minOccurs="0" maxOccurs="unbounded">
|
||||
<xs:element ref="arrow"/>
|
||||
</xs:sequence>
|
||||
<xs:attribute ref="site" use="required"/>
|
||||
<xs:attribute ref="weight" use="required"/>
|
||||
<xs:attribute ref="u" use="optional"/>
|
||||
<xs:attribute ref="v" use="optional"/>
|
||||
<xs:attribute ref="w" use="optional"/>
|
||||
<xs:attribute ref="x" use="required"/>
|
||||
<xs:attribute ref="y" use="optional"/>
|
||||
<xs:attribute ref="z" use="optional"/>
|
||||
<xs:attribute ref="t" use="optional"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="state">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="1" maxOccurs="unbounded">
|
||||
<xs:element ref="arrow"/>
|
||||
</xs:sequence>
|
||||
<xs:attributeGroup ref="id_date_author"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="object" substitutionGroup="state"/>
|
||||
|
||||
<xs:element name="transition">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="1" maxOccurs="unbounded">
|
||||
<xs:element minOccurs="0" ref="quote"/>
|
||||
<xs:element ref="arrow"/>
|
||||
</xs:sequence>
|
||||
<xs:attributeGroup ref="id_date_author"/>
|
||||
<xs:attribute ref="parent" use="required"/>
|
||||
<xs:attribute ref="probability" use="required"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="transition_anonym">
|
||||
<xs:complexType>
|
||||
<xs:attributeGroup ref="id_date_author"/>
|
||||
<xs:attribute ref="probability" use="required"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="gem-graph-model">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="1" maxOccurs="1">
|
||||
<xs:element name="identity">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="1" maxOccurs="1">
|
||||
<xs:element name="name" type="xs:string"/>
|
||||
<xs:element name="owner" type="xs:string"/>
|
||||
<xs:element name="owner_id" type="xs:integer"/>
|
||||
<xs:element name="date" type="xs:integer"/>
|
||||
<xs:element name="version" type="xs:float"/>
|
||||
<xs:element ref="g_ref" minOccurs="1" maxOccurs="1"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="parameters">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="1" maxOccurs="1">
|
||||
|
||||
<xs:element name="simulation">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="1" maxOccurs="1">
|
||||
<xs:element name="max_thread" type="xs:integer"/>
|
||||
<xs:element name="max_cycles" type="xs:integer"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="space-param">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="1" maxOccurs="1">
|
||||
<xs:element ref="dimension"/>
|
||||
<xs:element name="site_multiplicity" type="xs:integer"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
</xs:sequence>
|
||||
<xs:attributeGroup ref="id_date_author"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="objects">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="1" maxOccurs="unbounded">
|
||||
<xs:element ref="object"/>
|
||||
</xs:sequence>
|
||||
<xs:attributeGroup ref="id_date_author"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="savedstates">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="0" maxOccurs="unbounded">
|
||||
<xs:element ref="state"/>
|
||||
</xs:sequence>
|
||||
<xs:attributeGroup ref="id_date_author"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="conditionslist">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="0" maxOccurs="unbounded">
|
||||
<xs:element minOccurs="0" ref="quote"/>
|
||||
<xs:element ref="condition"/>
|
||||
</xs:sequence>
|
||||
<xs:attributeGroup ref="id_date_author"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="transitionslist">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="0" maxOccurs="unbounded">
|
||||
<xs:element ref="transition"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
<xs:element name="conditionstree">
|
||||
<xs:complexType>
|
||||
<xs:sequence minOccurs="0" maxOccurs="unbounded">
|
||||
<xs:element ref="condition_anonym"/>
|
||||
</xs:sequence>
|
||||
<xs:sequence minOccurs="0" maxOccurs="1">
|
||||
<xs:element ref="transition_anonym"/>
|
||||
</xs:sequence>
|
||||
<xs:sequence minOccurs="0" maxOccurs="unbounded">
|
||||
<xs:element ref="arrow"/>
|
||||
</xs:sequence>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
|
||||
</xs:sequence>
|
||||
<xs:attribute ref="version" use="required"/>
|
||||
</xs:complexType>
|
||||
</xs:element>
|
||||
</xs:schema>
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: OpenGL utils header
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
bool compute_arrow_2D(int state_size,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx, GLubyte *plan_ndx,
|
||||
int vgap, int igap, int cgap,
|
||||
int weight, int site, int x, int y, int z);
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: OpenGL utils header
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
bool compute_arrow_3D(int state_size,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx, GLubyte *plan_ndx,
|
||||
int vgap, int igap, int cgap,
|
||||
int weight, int site, int x, int y, int z);
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: OpenGL utils header
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
int graphics_compute_lines(int line_indices_nb,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx, int lines_nb,
|
||||
int dim, int state_size,
|
||||
int vertex_nb,
|
||||
int colors_nb,
|
||||
int arrows_nb);
|
||||
|
||||
int graphics_compute_plans(int plan_indices_nb,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *plan_ndx, int plans_nb,
|
||||
int dim, int state_size,
|
||||
int vertex_nb,
|
||||
int colors_nb,
|
||||
int arrows_nb);
|
||||
|
|
@ -33,14 +33,15 @@
|
|||
*/
|
||||
struct gl_area_entry {
|
||||
char name[16];
|
||||
GLuint vao;
|
||||
GLuint position_buffer;
|
||||
GLuint color_buffer;
|
||||
GLuint program;
|
||||
GLuint m;
|
||||
GLuint v;
|
||||
GLuint p;
|
||||
GLuint indices_nb;
|
||||
GLuint vao; // init_buffers
|
||||
GLuint position_buffer; // shutdown, draw
|
||||
GLuint color_buffer; // shutdown, draw
|
||||
GLuint program; // shutdown, init_shaders, draw
|
||||
GLuint m; // init_shaders, draw
|
||||
GLuint v; // init_shaders, draw
|
||||
GLuint p; // init_shaders, draw
|
||||
GLuint line_indices_nb; // init_buffers, draw, compute_space, arrow_2D, 3D
|
||||
GLuint plan_indices_nb; // init_buffers, draw, compute_space, arrow_2D, 3D
|
||||
};
|
||||
|
||||
/*
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: OpenGL utils header
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
bool compute_space_2D(int state_size,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx,
|
||||
bool pref_mark_unit_space_zero, int dgap);
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: GL functions
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
bool compute_space_3D(int state_size,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx,
|
||||
bool pref_mark_unit_space_zero, int dgap);
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: GL functions
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <epoxy/gl.h>
|
||||
//#include <GL/glu.h>
|
||||
|
||||
bool compute_arrow_2D(int state_size,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx, GLubyte *plan_ndx,
|
||||
int vgap, int igap, int cgap,
|
||||
int weight, int site, int x, int y, int z){
|
||||
|
||||
float zero = 0.0f;
|
||||
float center = (1.0f / state_size) * (2 * x - state_size + 2);
|
||||
// float tip = center + (2 * site - 1) * (1.0f / state_size);
|
||||
float base = center + (2 * site - 1) * (0.1f / state_size);
|
||||
|
||||
*(vertex_base + vgap + 0) = center + (2 * site - 1) * (1.0f / state_size - 0.01f);
|
||||
*(vertex_base + vgap + 1) = zero;
|
||||
*(vertex_base + vgap + 2) = zero;
|
||||
|
||||
*(vertex_base + vgap + 3) = base;
|
||||
*(vertex_base + vgap + 4) = 0.4f / state_size;
|
||||
*(vertex_base + vgap + 5) = zero;
|
||||
|
||||
*(vertex_base + vgap + 6) = base;
|
||||
*(vertex_base + vgap + 7) = - 0.4f / state_size;
|
||||
*(vertex_base + vgap + 8) = zero;
|
||||
|
||||
|
||||
*(line_ndx + igap + 0) = vgap / 3 + 0;
|
||||
*(line_ndx + igap + 1) = vgap / 3 + 1;
|
||||
*(line_ndx + igap + 2) = vgap / 3 + 0;
|
||||
*(line_ndx + igap + 3) = vgap / 3 + 2;
|
||||
*(line_ndx + igap + 4) = vgap / 3 + 1;
|
||||
*(line_ndx + igap + 5) = vgap / 3 + 2;
|
||||
|
||||
*(plan_ndx + 0) = vgap / 3 + 0;
|
||||
*(plan_ndx + 1) = vgap / 3 + 1;
|
||||
*(plan_ndx + 2) = vgap / 3 + 2;
|
||||
|
||||
*(plan_ndx + 3) = vgap / 3 + 0;
|
||||
*(plan_ndx + 4) = vgap / 3 + 1;
|
||||
*(plan_ndx + 5) = vgap / 3 + 2;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -0,0 +1,104 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: GL functions
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <epoxy/gl.h>
|
||||
//#include <GL/glu.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define A 0
|
||||
bool compute_arrow_3D(int state_size,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx, GLubyte *plan_ndx,
|
||||
int vgap, int igap, int cgap,
|
||||
int weight, int site, int x, int y, int z){
|
||||
|
||||
float zero = 0.0f;
|
||||
float center = (1.0f / state_size) * (2 * x - state_size + 2);
|
||||
float tip = center + (2 * site - 1) * (1.0f / state_size);
|
||||
float base = center + (2 * site - 1) * (0.1f / state_size);
|
||||
|
||||
if (A) printf("compute_arrow_3D vgap = %d\n", vgap);
|
||||
|
||||
*(vertex_base + vgap + 0) = center + (2 * site - 1) * (1.0f / state_size - 0.01f);
|
||||
*(vertex_base + vgap + 1) = zero;
|
||||
*(vertex_base + vgap + 2) = zero;
|
||||
|
||||
*(vertex_base + vgap + 3) = base;
|
||||
*(vertex_base + vgap + 4) = 0.4f / state_size;
|
||||
*(vertex_base + vgap + 5) = 0.4f / state_size;
|
||||
|
||||
*(vertex_base + vgap + 6) = base;
|
||||
*(vertex_base + vgap + 7) = 0.4f / state_size;
|
||||
*(vertex_base + vgap + 8) = - 0.4f / state_size;
|
||||
|
||||
*(vertex_base + vgap + 9) = base;
|
||||
*(vertex_base + vgap + 10) = - 0.4f / state_size;
|
||||
*(vertex_base + vgap + 11) = - 0.4f / state_size;
|
||||
|
||||
*(vertex_base + vgap + 12) = base;
|
||||
*(vertex_base + vgap + 13) = - 0.4f / state_size;
|
||||
*(vertex_base + vgap + 14) = 0.4f / state_size;
|
||||
|
||||
if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\
|
||||
x, site, center, tip, base); // vgap=%d igap=%d
|
||||
|
||||
*(line_ndx + igap + 0) = vgap / 3 + 0;
|
||||
*(line_ndx + igap + 1) = vgap / 3 + 1;
|
||||
|
||||
*(line_ndx + igap + 2) = vgap / 3 + 0;
|
||||
*(line_ndx + igap + 3) = vgap / 3 + 2;
|
||||
|
||||
*(line_ndx + igap + 4) = vgap / 3 + 0;
|
||||
*(line_ndx + igap + 5) = vgap / 3 + 3;
|
||||
|
||||
*(line_ndx + igap + 6) = vgap / 3 + 0;
|
||||
*(line_ndx + igap + 7) = vgap / 3 + 4;
|
||||
|
||||
*(line_ndx + igap + 8) = vgap / 3 + 1;
|
||||
*(line_ndx + igap + 9) = vgap / 3 + 2;
|
||||
|
||||
*(line_ndx + igap + 10) = vgap / 3 + 2;
|
||||
*(line_ndx + igap + 11) = vgap / 3 + 3;
|
||||
|
||||
*(line_ndx + igap + 12) = vgap / 3 + 3;
|
||||
*(line_ndx + igap + 13) = vgap / 3 + 4;
|
||||
|
||||
*(line_ndx + igap + 14) = vgap / 3 + 4;
|
||||
*(line_ndx + igap + 15) = vgap / 3 + 1;
|
||||
|
||||
|
||||
*(plan_ndx + 0) = vgap / 3 + 0;
|
||||
*(plan_ndx + 1) = vgap / 3 + 1;
|
||||
*(plan_ndx + 2) = vgap / 3 + 2;
|
||||
|
||||
*(plan_ndx + 3) = vgap / 3 + 0;
|
||||
*(plan_ndx + 4) = vgap / 3 + 1;
|
||||
*(plan_ndx + 5) = vgap / 3 + 2;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
|
@ -1,978 +0,0 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: GL functions
|
||||
*
|
||||
* Copyright (C) 2023 Arthur Menges <arthur.menges@a-lec.org>
|
||||
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <epoxy/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <GL/glext.h>
|
||||
#include <glib-2.0/glib.h>
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "../../include/base.h"
|
||||
#include "../../include/ui.h"
|
||||
#include "../../include/graphics.h"
|
||||
|
||||
#define VERTEX_SHADER_FILE "src/shaders/shader.vert"
|
||||
#define FRAG_SHADER_FILE "src/shaders/shader.frag"
|
||||
#define K 0
|
||||
|
||||
GLfloat *vertex_base = NULL;
|
||||
GLfloat *color_base = NULL;
|
||||
GLubyte *indices = NULL;
|
||||
int dim = 0;
|
||||
int state_size = 0;
|
||||
int vertex_nb = 0;
|
||||
int colors_nb = 0;
|
||||
int segments_nb = 0;
|
||||
int arrows_nb = 0;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Prints verbose human-readable debug messages
|
||||
*
|
||||
* @param source, XXX
|
||||
* type, XXX
|
||||
* id, XXX
|
||||
* severity, XXX
|
||||
* length, XXX
|
||||
* msg, XXX
|
||||
* data, XXX
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
static void graphics_debug_callback(GLenum source, GLenum type, GLuint id,
|
||||
GLenum severity, GLsizei length,
|
||||
const GLchar *msg, const void *data)
|
||||
{
|
||||
const char *errsource;
|
||||
const char *errtype;
|
||||
const char *errseverity;
|
||||
const GLubyte *string;
|
||||
GLenum code;
|
||||
|
||||
switch (source) {
|
||||
case GL_DEBUG_SOURCE_API:
|
||||
errsource = "API";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_SOURCE_WINDOW_SYSTEM:
|
||||
errsource = "WINDOW SYSTEM";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_SOURCE_SHADER_COMPILER:
|
||||
errsource = "SHADER COMPILER";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_SOURCE_THIRD_PARTY:
|
||||
errsource = "THIRD PARTY";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_SOURCE_APPLICATION:
|
||||
errsource = "APPLICATION";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_SOURCE_OTHER:
|
||||
errsource = "UNKNOWN";
|
||||
break;
|
||||
|
||||
default:
|
||||
errsource = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case GL_DEBUG_TYPE_ERROR:
|
||||
errtype = "ERROR";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR:
|
||||
errtype = "DEPRECATED BEHAVIOR";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR:
|
||||
errtype = "UNDEFINED BEHAVIOR";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_TYPE_PORTABILITY:
|
||||
errtype = "PORTABILITY";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_TYPE_PERFORMANCE:
|
||||
errtype = "PERFORMANCE";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_TYPE_OTHER:
|
||||
errtype = "OTHER";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_TYPE_MARKER:
|
||||
errtype = "MARKER";
|
||||
break;
|
||||
|
||||
default:
|
||||
errtype = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
switch (severity) {
|
||||
case GL_DEBUG_SEVERITY_HIGH:
|
||||
errseverity = "CRITICAL";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_SEVERITY_MEDIUM:
|
||||
errseverity = "ERROR";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_SEVERITY_LOW:
|
||||
errseverity = "WARNING";
|
||||
break;
|
||||
|
||||
case GL_DEBUG_SEVERITY_NOTIFICATION:
|
||||
errseverity = "INFO";
|
||||
break;
|
||||
|
||||
default:
|
||||
errseverity = "UNKNOWN";
|
||||
break;
|
||||
}
|
||||
|
||||
code = glGetError();
|
||||
string = gluErrorString(code);
|
||||
|
||||
printf("[%s] %s (%s) from %s: %s\n",
|
||||
errseverity, string, errtype, errsource, msg);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Dynamic array of ptrs to dynamically allocated gl_area_entry
|
||||
*/
|
||||
struct gl_area_entry **gl_area_array = NULL;
|
||||
|
||||
|
||||
/*
|
||||
* Find gl_area_entry from a gl_area ptr
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
*
|
||||
* @return entry ptr
|
||||
*/
|
||||
struct gl_area_entry *find_entry_from_ptr(const char *name)
|
||||
{
|
||||
struct gl_area_entry **cur;
|
||||
|
||||
// Check uninitialized and quit
|
||||
if (gl_area_array == NULL) return NULL;
|
||||
|
||||
// Seek in array
|
||||
cur = gl_area_array;
|
||||
while(*cur) {
|
||||
if (strncmp((*cur)->name, name, sizeof((*cur)->name)) == 0) return *cur;
|
||||
cur++;
|
||||
}
|
||||
|
||||
// Nothing found
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the gl_area_entry size
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return size of array
|
||||
*/
|
||||
static inline short gl_area_size(void)
|
||||
{
|
||||
struct gl_area_entry **cur;
|
||||
short size;
|
||||
|
||||
// Check uninitialized and quit
|
||||
if (gl_area_array == NULL) return 0;
|
||||
|
||||
cur = gl_area_array;
|
||||
while(*cur) {
|
||||
cur++;
|
||||
size++;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initializes a gl_area
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
* @return true if initialized
|
||||
*/
|
||||
bool graphics_init(const char *gl_area)
|
||||
{
|
||||
short array_size;
|
||||
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
// Check if not already initialized
|
||||
if (find_entry_from_ptr(gl_area)) {
|
||||
errno = EEXIST;
|
||||
perror("gl_area_array already exists");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get actual array size
|
||||
array_size = gl_area_size();
|
||||
|
||||
// If it does not exist, allocs it
|
||||
if (gl_area_array == NULL) {
|
||||
gl_area_array = g_malloc0(sizeof(struct gl_area_entry *) * 2);
|
||||
|
||||
// If it does exist, g_reallocs it
|
||||
} else {
|
||||
gl_area_array = g_realloc(gl_area_array,
|
||||
(array_size + 1)
|
||||
* sizeof(struct gl_area_entry *));
|
||||
if (gl_area_array == NULL) {
|
||||
perror("Not enough memory to allocate gl_area_array");
|
||||
return false;
|
||||
}
|
||||
|
||||
explicit_bzero(gl_area_array + array_size * sizeof(struct gl_area_entry *),
|
||||
sizeof(struct gl_area_entry *));
|
||||
}
|
||||
|
||||
// Alloc new entry
|
||||
gl_area_array[array_size] = g_malloc0(sizeof(struct gl_area_entry));
|
||||
|
||||
strcpy(gl_area_array[array_size]->name, gl_area);
|
||||
|
||||
if (!graphics_init_shaders(gl_area)) return false;
|
||||
|
||||
graphics_init_buffers(gl_area);
|
||||
|
||||
glDebugMessageCallback(graphics_debug_callback, NULL);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Shutdowns a gl_area
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
* @return true if success
|
||||
*/
|
||||
bool graphics_shutdown(const void *gl_area)
|
||||
{
|
||||
struct gl_area_entry *entry;
|
||||
|
||||
entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
if (entry == NULL) {
|
||||
errno = EFAULT;
|
||||
perror("gl_area_array entry not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
glDeleteBuffers(1, &entry->position_buffer);
|
||||
glDeleteBuffers(1, &entry->color_buffer);
|
||||
glDeleteProgram(entry->program);
|
||||
|
||||
// Liberate
|
||||
g_free(entry);
|
||||
|
||||
entry = NULL;
|
||||
|
||||
gl_area_array = g_realloc(gl_area_array,
|
||||
(gl_area_size())
|
||||
* sizeof(struct gl_area_entry *));
|
||||
|
||||
g_free(vertex_base);
|
||||
g_free(color_base);
|
||||
g_free(indices);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Initializes an identity matrix
|
||||
*
|
||||
* @param res, target ptr
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
static inline void compute_i(float *res)
|
||||
{
|
||||
/* initialize to the identity matrix */
|
||||
res[0] = 1.f; res[4] = 0.f; res[8] = 0.f; res[12] = 0.f;
|
||||
res[1] = 0.f; res[5] = 1.f; res[9] = 0.f; res[13] = 0.f;
|
||||
res[2] = 0.f; res[6] = 0.f; res[10] = 1.f; res[14] = 0.f;
|
||||
res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f;
|
||||
}
|
||||
|
||||
/*
|
||||
* Computes the projection matrix
|
||||
*
|
||||
* @param res, target ptr
|
||||
* phi, XXX
|
||||
* theta, XXX
|
||||
* psi, XXX
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
static inline void compute_mvp(float *res, float phi, float theta, float psi)
|
||||
{
|
||||
float x = phi *(G_PI / 180.f);
|
||||
float y = theta *(G_PI / 180.f);
|
||||
float z = psi *(G_PI / 180.f);
|
||||
float c1 = cosf(x), s1 = sinf(x);
|
||||
float c2 = cosf(y), s2 = sinf(y);
|
||||
float c3 = cosf(z), s3 = sinf(z);
|
||||
float c3c2 = c3 * c2;
|
||||
float s3c1 = s3 * c1;
|
||||
float c3s2s1 = c3 * s2 * s1;
|
||||
float s3s1 = s3 * s1;
|
||||
float c3s2c1 = c3 * s2 * c1;
|
||||
float s3c2 = s3 * c2;
|
||||
float c3c1 = c3 * c1;
|
||||
float s3s2s1 = s3 * s2 * s1;
|
||||
float c3s1 = c3 * s1;
|
||||
float s3s2c1 = s3 * s2 * c1;
|
||||
float c2s1 = c2 * s1;
|
||||
float c2c1 = c2 * c1;
|
||||
|
||||
compute_i(res);
|
||||
|
||||
/* apply all three rotations using the three matrices:
|
||||
*
|
||||
* ⎡ c3 s3 0 ⎤ ⎡ c2 0 -s2 ⎤ ⎡ 1 0 0 ⎤
|
||||
* ⎢ -s3 c3 0 ⎥ ⎢ 0 1 0 ⎥ ⎢ 0 c1 s1 ⎥
|
||||
* ⎣ 0 0 1 ⎦ ⎣ s2 0 c2 ⎦ ⎣ 0 -s1 c1 ⎦
|
||||
*/
|
||||
res[0] = c3c2; res[4] = s3c1 + c3s2s1; res[8] = s3s1 - c3s2c1; res[12] = 0.f;
|
||||
res[1] = -s3c2; res[5] = c3c1 - s3s2s1; res[9] = c3s1 + s3s2c1; res[13] = 0.f;
|
||||
res[2] = s2; res[6] = -c2s1; res[10] = c2c1; res[14] = 0.f;
|
||||
res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f;
|
||||
}
|
||||
|
||||
/*
|
||||
* Created and compile a shader
|
||||
*
|
||||
* @param type, shader type
|
||||
* src, source code of shader
|
||||
*
|
||||
* @return shader id
|
||||
*/
|
||||
static inline GLuint create_shader(int type, const char *src)
|
||||
{
|
||||
GLuint shader;
|
||||
int status;
|
||||
|
||||
shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &src, NULL);
|
||||
glCompileShader(shader);
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
||||
if(status == GL_FALSE) {
|
||||
int log_len;
|
||||
char *buffer;
|
||||
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
|
||||
|
||||
buffer = g_malloc(log_len + 1);
|
||||
glGetShaderInfoLog(shader, log_len, NULL, buffer);
|
||||
|
||||
g_warning("Compile failure in %s shader:\n%s",
|
||||
type == GL_VERTEX_SHADER ? "vertex" : "fragment",
|
||||
buffer);
|
||||
|
||||
g_free(buffer);
|
||||
|
||||
glDeleteShader(shader);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Initializes the shaders of a gl_area and link them to a program
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
*
|
||||
* @return true if initialized
|
||||
*/
|
||||
bool graphics_init_shaders(const void *gl_area)
|
||||
{
|
||||
char *vertex_shader;
|
||||
char *fragment_shader;
|
||||
struct gl_area_entry *entry;
|
||||
int status;
|
||||
GLuint vertex, fragment;
|
||||
GLuint program = 0;
|
||||
GLuint m = 0;
|
||||
GLuint v = 0;
|
||||
GLuint p = 0;
|
||||
|
||||
entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
if (entry == NULL) {
|
||||
errno = EFAULT;
|
||||
perror("gl_area_array entry not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load vertex shader file
|
||||
vertex_shader = read_file(VERTEX_SHADER_FILE);
|
||||
if (vertex_shader == NULL)
|
||||
return false;
|
||||
vertex = create_shader(GL_VERTEX_SHADER, vertex_shader);
|
||||
|
||||
if(vertex == 0) {
|
||||
entry->program = 0;
|
||||
g_free(vertex_shader);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load fragment shader file
|
||||
fragment_shader = read_file(FRAG_SHADER_FILE);
|
||||
if (fragment_shader == NULL)
|
||||
return false;
|
||||
fragment = create_shader(GL_FRAGMENT_SHADER, fragment_shader);
|
||||
|
||||
if(fragment == 0) {
|
||||
glDeleteShader(vertex);
|
||||
entry->program = 0;
|
||||
g_free(vertex_shader);
|
||||
g_free(fragment_shader);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Link shaders to program
|
||||
program = glCreateProgram();
|
||||
glAttachShader(program, vertex);
|
||||
glAttachShader(program, fragment);
|
||||
|
||||
glLinkProgram(program);
|
||||
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &status);
|
||||
|
||||
if(status == GL_FALSE) {
|
||||
int log_len;
|
||||
char *buffer;
|
||||
|
||||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
|
||||
|
||||
buffer = g_malloc(log_len + 1);
|
||||
glGetProgramInfoLog(program, log_len, NULL, buffer);
|
||||
|
||||
g_warning("Linking failure:\n%s", buffer);
|
||||
|
||||
g_free(buffer);
|
||||
|
||||
glDeleteProgram(program);
|
||||
program = 0;
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
|
||||
g_free(vertex_shader);
|
||||
g_free(fragment_shader);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Get the location of the "mvp" uniform */
|
||||
m = glGetUniformLocation(program, "model_matrix");
|
||||
v = glGetUniformLocation(program, "view_matrix");
|
||||
p = glGetUniformLocation(program, "projection_matrix");
|
||||
|
||||
glDetachShader(program, vertex);
|
||||
glDetachShader(program, fragment);
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
|
||||
entry->program = program;
|
||||
entry->m = m;
|
||||
entry->v = v;
|
||||
entry->p = p;
|
||||
|
||||
g_free(vertex_shader);
|
||||
g_free(fragment_shader);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#define A 0
|
||||
static bool compute_arrow_2D(struct gl_area_entry *entry, int state_size, int vgap, int igap, int weight, int site, int x, int y, int z){
|
||||
|
||||
float zero = 0.0f;
|
||||
float center = (1.0f / state_size) * (2 * x - state_size + 2);
|
||||
float tip = center + (2 * site - 1) * (1.0f / state_size);
|
||||
float base = center + (2 * site - 1) * (0.1f / state_size);
|
||||
|
||||
printf("compute_arrow_2D vgap = %d vertex_nb = %d\n", vgap, vertex_nb);
|
||||
|
||||
assert(vgap < vertex_nb);
|
||||
assert(vgap + 9 < vertex_nb);
|
||||
|
||||
*(vertex_base + vgap + 0) = center + (2 * site - 1) * (1.0f / state_size - 0.01f);
|
||||
*(vertex_base + vgap + 1) = zero;
|
||||
*(vertex_base + vgap + 2) = zero;
|
||||
|
||||
*(vertex_base + vgap + 3) = base;
|
||||
*(vertex_base + vgap + 4) = 0.4f / state_size;
|
||||
*(vertex_base + vgap + 5) = zero;
|
||||
|
||||
*(vertex_base + vgap + 6) = base;
|
||||
*(vertex_base + vgap + 7) = - 0.4f / state_size;
|
||||
*(vertex_base + vgap + 8) = zero;
|
||||
|
||||
if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\
|
||||
x, site, center, tip, base); // vgap=%d igap=%d
|
||||
|
||||
/* *(color_base + vgap + 0) = 0.2f; // cyan */
|
||||
/* *(color_base + vgap + 1) = 0.8f; */
|
||||
/* *(color_base + vgap + 2) = 0.8f; */
|
||||
|
||||
/* *(color_base + vgap + 3) = 0.8f; // magenta */
|
||||
/* *(color_base + vgap + 4) = 0.2f; */
|
||||
/* *(color_base + vgap + 5) = 0.8f; */
|
||||
|
||||
/* *(color_base + vgap + 6) = 0.8f; // jaune */
|
||||
/* *(color_base + vgap + 7) = 0.8f; */
|
||||
/* *(color_base + vgap + 8) = 0.2f; */
|
||||
|
||||
assert(igap < entry->indices_nb);
|
||||
assert(igap + 5 < entry->indices_nb);
|
||||
|
||||
*(indices + igap + 0) = vgap / 3 + 0;
|
||||
*(indices + igap + 1) = vgap / 3 + 1;
|
||||
*(indices + igap + 2) = vgap / 3 + 0;
|
||||
*(indices + igap + 3) = vgap / 3 + 2;
|
||||
*(indices + igap + 4) = vgap / 3 + 1;
|
||||
*(indices + igap + 5) = vgap / 3 + 2;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool compute_arrow_3D(struct gl_area_entry *entry, int state_size, int vgap, int igap, int weight, int site, int x, int y, int z){
|
||||
|
||||
float zero = 0.0f;
|
||||
float center = (1.0f / state_size) * (2 * x - state_size + 2);
|
||||
float tip = center + (2 * site - 1) * (1.0f / state_size);
|
||||
float base = center + (2 * site - 1) * (0.1f / state_size);
|
||||
|
||||
printf("compute_arrow_3D vgap = %d vertex_nb = %d\n", vgap, vertex_nb);
|
||||
|
||||
assert(vgap < vertex_nb);
|
||||
assert(vgap + 9 < vertex_nb);
|
||||
|
||||
*(vertex_base + vgap + 0) = center + (2 * site - 1) * (1.0f / state_size - 0.01f);
|
||||
*(vertex_base + vgap + 1) = zero;
|
||||
*(vertex_base + vgap + 2) = zero;
|
||||
|
||||
*(vertex_base + vgap + 3) = base;
|
||||
*(vertex_base + vgap + 4) = 0.4f / state_size;
|
||||
*(vertex_base + vgap + 5) = 0.4f / state_size;
|
||||
|
||||
*(vertex_base + vgap + 6) = base;
|
||||
*(vertex_base + vgap + 7) = 0.4f / state_size;
|
||||
*(vertex_base + vgap + 8) = - 0.4f / state_size;
|
||||
|
||||
*(vertex_base + vgap + 9) = base;
|
||||
*(vertex_base + vgap + 10) = - 0.4f / state_size;
|
||||
*(vertex_base + vgap + 11) = - 0.4f / state_size;
|
||||
|
||||
*(vertex_base + vgap + 12) = base;
|
||||
*(vertex_base + vgap + 13) = - 0.4f / state_size;
|
||||
*(vertex_base + vgap + 14) = 0.4f / state_size;
|
||||
|
||||
if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\
|
||||
x, site, center, tip, base); // vgap=%d igap=%d
|
||||
|
||||
assert(igap < entry->indices_nb);
|
||||
assert(igap + 5 < entry->indices_nb);
|
||||
|
||||
*(indices + igap + 0) = vgap / 3 + 0;
|
||||
*(indices + igap + 1) = vgap / 3 + 1;
|
||||
|
||||
*(indices + igap + 2) = vgap / 3 + 0;
|
||||
*(indices + igap + 3) = vgap / 3 + 2;
|
||||
|
||||
*(indices + igap + 4) = vgap / 3 + 0;
|
||||
*(indices + igap + 5) = vgap / 3 + 3;
|
||||
|
||||
*(indices + igap + 6) = vgap / 3 + 0;
|
||||
*(indices + igap + 7) = vgap / 3 + 4;
|
||||
|
||||
*(indices + igap + 8) = vgap / 3 + 1;
|
||||
*(indices + igap + 9) = vgap / 3 + 2;
|
||||
|
||||
*(indices + igap + 10) = vgap / 3 + 2;
|
||||
*(indices + igap + 11) = vgap / 3 + 3;
|
||||
|
||||
*(indices + igap + 12) = vgap / 3 + 3;
|
||||
*(indices + igap + 13) = vgap / 3 + 4;
|
||||
|
||||
*(indices + igap + 14) = vgap / 3 + 4;
|
||||
*(indices + igap + 15) = vgap / 3 + 1;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
#define V 1
|
||||
#define I 0
|
||||
#define EDGE 0.999999
|
||||
static bool compute_space_2D(struct gl_area_entry *entry, int state_size){
|
||||
|
||||
float x = 0;
|
||||
// for (int k = 0; k < state_size + 1; k++) <=> + 1 car
|
||||
// on passe du nombre de cases = (n)
|
||||
// au nombre de séparations entre les cases + les deux bords = (n + 1)
|
||||
|
||||
if (V) printf("vertices ");
|
||||
for (int k = 0; k < state_size; k++){ // barres verticales
|
||||
//
|
||||
x = ((state_size % 2) * (state_size / 2 - k)
|
||||
+ (state_size % 2 - 1) * (k + 0.5f - state_size / 2)) / state_size * 2 * EDGE;
|
||||
|
||||
if (V) printf("[%1.1f] ", x);
|
||||
|
||||
assert(k * 6 < vertex_nb);
|
||||
assert(k * 6 + 5 < vertex_nb);
|
||||
|
||||
*(vertex_base + k * 6 + 0) = - x;
|
||||
*(vertex_base + k * 6 + 1) = 1.0f / state_size;
|
||||
*(vertex_base + k * 6 + 2) = 0.0f;
|
||||
|
||||
*(vertex_base + k * 6 + 3) = - x;
|
||||
*(vertex_base + k * 6 + 4) = - 1.0f / state_size;
|
||||
*(vertex_base + k * 6 + 5) = 0.0f;
|
||||
}
|
||||
|
||||
if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size);
|
||||
|
||||
*(indices + 0) = 0;
|
||||
*(indices + 1) = state_size * 2 - 2; // barre horizontale du bas
|
||||
|
||||
*(indices + 2) = 1;
|
||||
*(indices + 3) = state_size * 2 - 1; // barre horizontale du haut
|
||||
|
||||
for (int k = 0; k < state_size * 2; k++){
|
||||
|
||||
assert(k < entry->indices_nb);
|
||||
assert(k + 4 < entry->indices_nb);
|
||||
|
||||
*(indices + k + 4) = k;
|
||||
}
|
||||
|
||||
/* *(indices + state_size * 2 + 6 + 4) = 0; // diagonales pour marquer la case zéro */
|
||||
/* *(indices + state_size * 2 + 6 + 5) = 3; */
|
||||
/* *(indices + state_size * 2 + 6 + 6) = 1; */
|
||||
/* *(indices + state_size * 2 + 6 + 7) = 2; */
|
||||
|
||||
if (I) printf("indices (%d - state_size(0)) x (%d - state_size(1)) ", 0, 1);
|
||||
for (int v = 0; v < state_size + 2; v++) {
|
||||
if (I) printf("(%d-%d) ", *(indices + v), *(indices + v + 1));
|
||||
}
|
||||
|
||||
if (I) printf(" n = 4 + (%d x 2)\n", state_size + 2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static bool compute_space_3D(struct gl_area_entry *entry, int state_size){
|
||||
|
||||
float x = 0;
|
||||
// for (int k = 0; k < state_size + 1; k++) <=> + 1 car
|
||||
// on passe du nombre de cases = (n)
|
||||
// au nombre de séparations entre les cases + les deux bords = (n + 1)
|
||||
|
||||
if (V) printf("vertices ");
|
||||
for (int k = 0; k < state_size; k++){ // barres verticales
|
||||
//
|
||||
x = ((state_size % 2) * (state_size / 2 - k)
|
||||
+ (state_size % 2 - 1) * (k + 0.5f - state_size / 2)) / state_size * 2 * EDGE;
|
||||
|
||||
if (V) printf("[%1.1f] ", x);
|
||||
|
||||
assert(k * 12 < vertex_nb);
|
||||
assert(k * 12 + 11 < vertex_nb);
|
||||
assert(k * 12 < colors_nb);
|
||||
assert(k * 12 + 11 < colors_nb);
|
||||
|
||||
*(vertex_base + k * 12 + 0) = - x;
|
||||
*(vertex_base + k * 12 + 1) = 1.0f / state_size;
|
||||
*(vertex_base + k * 12 + 2) = 1.0f / state_size;
|
||||
|
||||
*(vertex_base + k * 12 + 3) = - x;
|
||||
*(vertex_base + k * 12 + 4) = - 1.0f / state_size;
|
||||
*(vertex_base + k * 12 + 5) = - 1.0f / state_size;
|
||||
|
||||
*(vertex_base + k * 12 + 6) = - x;
|
||||
*(vertex_base + k * 12 + 7) = 1.0f / state_size;
|
||||
*(vertex_base + k * 12 + 8) = - 1.0f / state_size;
|
||||
|
||||
*(vertex_base + k * 12 + 9) = - x;
|
||||
*(vertex_base + k * 12 + 10) = - 1.0f / state_size;
|
||||
*(vertex_base + k * 12 + 11) = 1.0f / state_size;
|
||||
|
||||
*(color_base + k * 12 + 0) = 0.8f; // rouge
|
||||
*(color_base + k * 12 + 1) = 0.2f;
|
||||
*(color_base + k * 12 + 2) = 0.2f;
|
||||
|
||||
*(color_base + k * 12 + 3) = 0.8f; // jaune
|
||||
*(color_base + k * 12 + 4) = 0.8f;
|
||||
*(color_base + k * 12 + 5) = 0.2f;
|
||||
|
||||
*(color_base + k * 12 + 6) = 0.2f; // vert
|
||||
*(color_base + k * 12 + 7) = 0.8f;
|
||||
*(color_base + k * 12 + 8) = 0.2f;
|
||||
|
||||
*(color_base + k * 12 + 9) = 0.8f; // magenta
|
||||
*(color_base + k * 12 + 10) = 0.2f;
|
||||
*(color_base + k * 12 + 11) = 0.8f;
|
||||
|
||||
/* 0.8, 0.8, 0.8, // blanc */
|
||||
/* 0.8, 0.8, 0.2, // jaune */
|
||||
/* 0.8, 0.2, 0.2, // rouge */
|
||||
/* 0.2, 0.2, 0.2, // noir */
|
||||
/* 0.2, 0.2, 0.2, // gris */
|
||||
/* 0.2, 0.8, 0.8, // cyan */
|
||||
/* 0.2, 0.8, 0.2, // vert */
|
||||
/* 0.8, 0.2, 0.8, // magenta */
|
||||
|
||||
}
|
||||
|
||||
if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size);
|
||||
|
||||
*(indices + 4) = 0;
|
||||
*(indices + 5) = state_size * 4 - 4; // barre horizontale du bas arr
|
||||
|
||||
*(indices + 6) = 1;
|
||||
*(indices + 7) = state_size * 4 - 3; // barre horizontale du haut arr
|
||||
|
||||
*(indices + 0) = 2;
|
||||
*(indices + 1) = state_size * 4 - 2; // barre horizontale du bas av
|
||||
|
||||
*(indices + 2) = 3;
|
||||
*(indices + 3) = state_size * 4 - 1; // barre horizontale du haut av
|
||||
|
||||
for (int k = 8; k <= state_size * 8; k += 8){ // for (int k = 8; k < state_size * 8; k += 8){
|
||||
|
||||
|
||||
assert(k < entry->indices_nb);
|
||||
assert(k + 8 < entry->indices_nb);
|
||||
|
||||
*(indices + k + 0) = k / 2 - 4; // + 0;
|
||||
*(indices + k + 1) = k / 2 - 2; // + 2;
|
||||
|
||||
*(indices + k + 2) = k / 2 - 3; // + 1;
|
||||
*(indices + k + 3) = k / 2 - 1; // + 3;
|
||||
|
||||
*(indices + k + 4) = k / 2 - 4; // + 0;
|
||||
*(indices + k + 5) = k / 2 - 1; // + 3;
|
||||
|
||||
*(indices + k + 6) = k / 2 - 3; // + 1;
|
||||
*(indices + k + 7) = k / 2 - 2; // + 2;
|
||||
}
|
||||
|
||||
|
||||
*(indices + entry->indices_nb - 4) = 0 + 0; // diagonales pour marquer la case zéro
|
||||
*(indices + entry->indices_nb - 3) = 0 + 1;
|
||||
|
||||
*(indices + entry->indices_nb - 2) = 0 + 2; // diagonales pour marquer la case zéro
|
||||
*(indices + entry->indices_nb - 1) = 0 + 3;
|
||||
|
||||
|
||||
if (I) printf("indices (%d - state_size(0)) x (%d - state_size(1)) ", 0, 1);
|
||||
for (int v = 0; v < state_size + 2; v++) {
|
||||
if (I) printf("(%d-%d) ", *(indices + v), *(indices + v + 1));
|
||||
}
|
||||
|
||||
if (I) printf(" n = 4 + (%d x 2)\n", state_size + 2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Initializes the buffer of a gl_area
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
* Note : indices[] is defined in graphics_cube.h
|
||||
* @return void
|
||||
*/
|
||||
|
||||
|
||||
void graphics_init_buffers(const void *gl_area)
|
||||
{
|
||||
struct gl_area_entry *entry;
|
||||
GLuint vao, vertex_buffer, color_buffer;
|
||||
int vgap = 0, igap = 0, cgap = 0;
|
||||
|
||||
entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
//{1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21};
|
||||
dim = 1;
|
||||
state_size = 13; // 2 < state_size < 32
|
||||
arrows_nb = 4;
|
||||
|
||||
vertex_nb = (state_size + 1) * 6 + arrows_nb * 9;
|
||||
vertex_nb = (state_size + 1) * 12 + arrows_nb * 15;
|
||||
colors_nb = (state_size + 1) * 12 + arrows_nb * 0; // TODO
|
||||
segments_nb = (2 + state_size) + (arrows_nb * 3);
|
||||
segments_nb = (4 + state_size * 4) + (arrows_nb * 8) + 2; // + 2; pour les 2 diagonales
|
||||
|
||||
entry->indices_nb = segments_nb * 2;
|
||||
|
||||
printf("Initialization of buffers with %u indices, %u vertices and %u colors & state_size = %d\n",
|
||||
entry->indices_nb, vertex_nb, colors_nb, state_size);
|
||||
|
||||
//XXX g_malloc
|
||||
vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2);
|
||||
color_base = g_malloc0(colors_nb * sizeof(GLfloat) * 2);
|
||||
indices = g_malloc0(segments_nb * 2 * sizeof(GLubyte) * 2);
|
||||
|
||||
// compute_space_2D(entry, state_size); vgap += (6 * state_size); igap += state_size * 2 + 4; cgap += (6 * state_size);
|
||||
|
||||
compute_space_3D(entry, state_size); vgap += (12 * state_size); igap += state_size * 8 + 8; cgap += (12 * state_size);
|
||||
|
||||
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 0, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_2D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 0, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
//compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
//compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
|
||||
// We only use one VAO, so we always keep it bound
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
glGenBuffers(1, &vertex_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_nb * sizeof(vertex_base[0]), vertex_base, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// colors
|
||||
glGenBuffers(1, &color_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(color_base[0]), color_base, GL_STATIC_DRAW); // TODO check : color_base[0] or color_base ?
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
if(entry != NULL) {
|
||||
entry->vao = vao;
|
||||
entry->position_buffer = vertex_buffer;
|
||||
entry->color_buffer = color_buffer;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Draws the current buffer to a gl_area
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
void graphics_draw(const void *gl_area)
|
||||
{
|
||||
float m[16];
|
||||
float v[16];
|
||||
float p[16];
|
||||
struct gl_area_entry *entry;
|
||||
|
||||
entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
if (entry == NULL) {
|
||||
errno = EFAULT;
|
||||
perror("gl_area_array entry not found");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/* Compute the model view projection matrix using the
|
||||
* rotation angles specified through the GtkRange widgets
|
||||
*/
|
||||
compute_mvp(p,
|
||||
rotation_angles[X_AXIS],
|
||||
rotation_angles[Y_AXIS],
|
||||
rotation_angles[Z_AXIS]);
|
||||
compute_i(m);
|
||||
compute_i(v);
|
||||
|
||||
glClearColor(0.1f, 0.1f, 0.1f, 0.15f); // couleur du 'fond'
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
/* Use our shaders */
|
||||
glUseProgram(entry->program);
|
||||
|
||||
/* Update the "mvp" matrix we use in the shader */
|
||||
glUniformMatrix4fv(entry->m, 1, GL_FALSE, &m[0]);
|
||||
glUniformMatrix4fv(entry->v, 1, GL_FALSE, &v[0]);
|
||||
glUniformMatrix4fv(entry->p, 1, GL_FALSE, &p[0]);
|
||||
|
||||
/* Use the vertices in our buffer */
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, entry->position_buffer);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0);
|
||||
|
||||
// couleurs
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, entry->color_buffer);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
glDrawElements(GL_LINES, entry->indices_nb, GL_UNSIGNED_BYTE, indices);
|
||||
|
||||
/* We finished using the buffers and program */
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glUseProgram(0);
|
||||
|
||||
glFlush();
|
||||
}
|
||||
|
|
@ -0,0 +1,362 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: OpenGL utils header
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
//#pragma once
|
||||
#include <unistd.h>
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <epoxy/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <GL/glext.h>
|
||||
#include <glib-2.0/glib.h>
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "../../include/base.h"
|
||||
#include "../../include/ui.h"
|
||||
#include "../../include/graphics_control.h"
|
||||
#include "../../include/graphics_compute.h"
|
||||
#include "../../include/space_2D.h"
|
||||
#include "../../include/space_3D.h"
|
||||
#include "../../include/arrow_2D.h"
|
||||
#include "../../include/arrow_3D.h"
|
||||
|
||||
#define A 0
|
||||
int graphics_compute_lines(int line_indices_nb,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx, int lines_nb,
|
||||
int dim, int state_size,
|
||||
int vertex_nb,
|
||||
int colors_nb,
|
||||
int arrows_nb)
|
||||
{
|
||||
/// struct gl_area_entry *entry;
|
||||
/// GLuint vao, vertex_buffer, color_buffer;
|
||||
int vgap = 0, igap = 0, pgap = 0, cgap = 0;
|
||||
|
||||
/// entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
// {1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21}; < from model.xml
|
||||
dim = 1;
|
||||
state_size = 7; // 2 < state_size < 32
|
||||
arrows_nb = 4; // assert : leur emplacement doit être fonction de state_size
|
||||
|
||||
vertex_nb = (state_size + 1) * 6 + arrows_nb * 9;
|
||||
vertex_nb = (state_size + 1) * 12 + arrows_nb * 15;
|
||||
colors_nb = (state_size + 1) * 12 + arrows_nb * 0; // TODO
|
||||
// lines_nb = (2 + state_size) + (arrows_nb * 3); // cas 2D
|
||||
lines_nb = (4 + state_size * 4) + (arrows_nb * 8) + 2; // + 2; pour les 2 diagonales
|
||||
//// plans_nb = 8; //(4 + state_size * 4) + (arrows_nb * 8) + 2; // bhuingfyunfyuguinlgi svbysbubsyu qvyqytqujtvcttcef
|
||||
|
||||
line_indices_nb = lines_nb * 2;
|
||||
|
||||
printf("Initialization of buffers with %u line_ndx, %u vertices and %u colors & state_size = %d\n",
|
||||
line_indices_nb, vertex_nb, colors_nb, state_size);
|
||||
|
||||
// g_malloc
|
||||
// vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2);
|
||||
// color_base = g_malloc0(colors_nb * sizeof(GLfloat) * 2);
|
||||
// line_ndx = g_malloc0(lines_nb * 2 * sizeof(GLubyte) * 2);
|
||||
//// plan_ndx = g_malloc0(plans_nb * 3 * sizeof(GLubyte) * 2);
|
||||
|
||||
assert(state_size * 6 < vertex_nb); assert(state_size * 6 + 5 < vertex_nb);
|
||||
// compute_space_2D(state_size, vertex_base, line_ndx);
|
||||
// vgap += (6 * state_size); igap += state_size * 2 + 4; cgap += (6 * state_size);
|
||||
|
||||
*(line_ndx + state_size * 2 + 6 + 4) = 0; // diagonales pour marquer la case zéro
|
||||
*(line_ndx + state_size * 2 + 6 + 5) = 3;
|
||||
*(line_ndx + state_size * 2 + 6 + 6) = 1;
|
||||
*(line_ndx + state_size * 2 + 6 + 7) = 2;
|
||||
|
||||
for (int k = 0; k < state_size; k++){
|
||||
assert(k * 12 < vertex_nb); assert(k * 12 + 11 < vertex_nb);
|
||||
assert(k * 12 < colors_nb); assert(k * 12 + 11 < colors_nb);
|
||||
assert(k + 8 < line_indices_nb); assert(k + 16 < line_indices_nb);
|
||||
}
|
||||
|
||||
compute_space_3D(state_size, vertex_base, color_base, line_ndx,
|
||||
0,4); // patch provisoire
|
||||
// pref_mark_unit_space_zero, dgap){
|
||||
vgap += (12 * state_size); igap += state_size * 8 + 8; cgap += (12 * state_size);
|
||||
|
||||
/* assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); */
|
||||
/* assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); */
|
||||
/* compute_arrow_2D(state_size, vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, vgap, igap, cgap, 1, 0, 0, 0, 0); */
|
||||
/* // if (A) printf("compute_arrow_2D vgap = %d vertex_nb = %d\n", vgap, vertex_nb); */
|
||||
/* // if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\ */
|
||||
/* // x, site, center, tip, base); // vgap=%d igap=%d */
|
||||
/* vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); */
|
||||
/* assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); */
|
||||
/* compute_arrow_2D(state_size, vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, vgap, igap, cgap, 1, 1, 2, 0, 0); */
|
||||
/* vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); */
|
||||
/* assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); */
|
||||
/* compute_arrow_2D(state_size, vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, vgap, igap, cgap, 1, 0, 3, 0, 0); */
|
||||
/* vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); */
|
||||
/* assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); */
|
||||
/* compute_arrow_2D(state_size, vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, vgap, igap, cgap, 1, 1, state_size - 2, 0, 0); */
|
||||
/* vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb);
|
||||
assert(igap < line_indices_nb); assert(igap + 5 < line_indices_nb);
|
||||
|
||||
|
||||
compute_arrow_3D(state_size,
|
||||
vertex_base, color_base,
|
||||
line_ndx, 0, // plan_ndx,
|
||||
vgap, igap, cgap,
|
||||
1, 0, 0, 0, 0);
|
||||
vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
|
||||
compute_arrow_3D(state_size,
|
||||
vertex_base, color_base,
|
||||
line_ndx, 0, // plan_ndx,
|
||||
vgap, igap, cgap,
|
||||
1, 1, 2, 0, 0);
|
||||
vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
|
||||
compute_arrow_3D(state_size,
|
||||
vertex_base, color_base,
|
||||
line_ndx, 0, // plan_ndx,
|
||||
vgap, igap, cgap,
|
||||
1, 0, 3, 0, 0);
|
||||
vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
|
||||
compute_arrow_3D(state_size,
|
||||
vertex_base, color_base,
|
||||
line_ndx, 0, // plan_ndx,
|
||||
vgap, igap, cgap,
|
||||
1, 1, 3, 0, 0);
|
||||
vgap += 15; igap += 16; cgap += 15; ++arrows_nb;
|
||||
|
||||
return line_indices_nb;
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 0, state_size - 2, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 1, state_size - 2, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/// glGenBuffers(1, &vertex_buffer);
|
||||
/// glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
/// glBufferData(GL_ARRAY_BUFFER, vertex_nb * sizeof(vertex_base[0]), vertex_base, GL_STATIC_DRAW);
|
||||
/// glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
|
||||
// colors
|
||||
/// glGenBuffers(1, &color_buffer);
|
||||
/// glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
|
||||
/// glBufferData(GL_ARRAY_BUFFER, colors_nb * sizeof(color_base), color_base, GL_STATIC_DRAW);
|
||||
/// glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// We only use one VAO, so we always keep it bound
|
||||
/// glGenVertexArrays(1, &vao);
|
||||
/// glBindVertexArray(vao);
|
||||
|
||||
/// if(entry != NULL) {
|
||||
/// entry->vao = vao;
|
||||
/// entry->position_buffer = vertex_buffer;
|
||||
/// entry->color_buffer = color_buffer;
|
||||
/// }
|
||||
}
|
||||
|
||||
#define A 0
|
||||
int graphics_compute_plans(int plan_indices_nb,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *plan_ndx, int plans_nb,
|
||||
int dim, int state_size,
|
||||
int vertex_nb,
|
||||
int colors_nb,
|
||||
int arrows_nb)
|
||||
{
|
||||
/// int line_indices_nb = 0; // AD HOC !
|
||||
/// struct gl_area_entry *entry;
|
||||
/// GLuint vao, vertex_buffer, color_buffer;
|
||||
int vgap = 0, igap = 0, pgap = 0, cgap = 0;
|
||||
|
||||
/// entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
// {1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21}; < from model.xml
|
||||
dim = 1;
|
||||
state_size = 7; // 2 < state_size < 32
|
||||
arrows_nb = 4; // assert : leur emplacement doit être fonction de state_size
|
||||
|
||||
vertex_nb = (state_size + 1) * 6 + arrows_nb * 9;
|
||||
vertex_nb = (state_size + 1) * 12 + arrows_nb * 15;
|
||||
colors_nb = (state_size + 1) * 12 + arrows_nb * 0; // TODO
|
||||
// lines_nb = (2 + state_size) + (arrows_nb * 3); // cas 2D
|
||||
//// lines_nb = (4 + state_size * 4) + (arrows_nb * 8) + 2; // + 2; pour les 2 diagonales
|
||||
plans_nb = 8; //(4 + state_size * 4) + (arrows_nb * 8) + 2; // bhuingfyunfyuguinlgi svbysbubsyu qvyqytqujtvcttcef
|
||||
|
||||
//// plan_indices_nb = plans_nb * 3;
|
||||
|
||||
printf("Initialization of buffers with %u plan_ndx, %u vertices and %u colors & state_size = %d\n",
|
||||
plan_indices_nb, vertex_nb, colors_nb, state_size);
|
||||
|
||||
// g_malloc
|
||||
//// vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2);
|
||||
//// color_base = g_malloc0(colors_nb * sizeof(GLfloat) * 2);
|
||||
//// line_ndx = g_malloc0(lines_nb * 2 * sizeof(GLubyte) * 2);
|
||||
plan_ndx = g_malloc0(plans_nb * 3 * sizeof(GLubyte) * 2);
|
||||
|
||||
assert(state_size * 6 < vertex_nb); assert(state_size * 6 + 5 < vertex_nb);
|
||||
// compute_space_2D(state_size, vertex_base, line_ndx);
|
||||
// vgap += (6 * state_size); igap += state_size * 2 + 4; cgap += (6 * state_size);
|
||||
|
||||
/* *(line_ndx + state_size * 2 + 6 + 4) = 0; // diagonales pour marquer la case zéro */
|
||||
/* *(line_ndx + state_size * 2 + 6 + 5) = 3; */
|
||||
/* *(line_ndx + state_size * 2 + 6 + 6) = 1; */
|
||||
/* *(line_ndx + state_size * 2 + 6 + 7) = 2; */
|
||||
|
||||
for (int k = 0; k < state_size; k++){
|
||||
assert(k * 12 < vertex_nb); assert(k * 12 + 11 < vertex_nb);
|
||||
assert(k * 12 < colors_nb); assert(k * 12 + 11 < colors_nb);
|
||||
// assert(k + 8 < line_indices_nb); assert(k + 16 < line_indices_nb);
|
||||
|
||||
compute_space_3D(state_size, vertex_base, color_base, plan_ndx,
|
||||
0,4); // patch provisoire
|
||||
//pref_mark_unit_space_zero, dgap){
|
||||
|
||||
vgap += (12 * state_size); igap += state_size * 8 + 8; cgap += (12 * state_size);
|
||||
|
||||
/* assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); */
|
||||
/* assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); */
|
||||
/* compute_arrow_2D(state_size, vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, vgap, igap, cgap, 1, 0, 0, 0, 0); */
|
||||
/* // if (A) printf("compute_arrow_2D vgap = %d vertex_nb = %d\n", vgap, vertex_nb); */
|
||||
/* // if (A) printf("x = %d site = %d center = %f tip = %f base = %f\n",\ */
|
||||
/* // x, site, center, tip, base); // vgap=%d igap=%d */
|
||||
/* vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); */
|
||||
/* assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); */
|
||||
/* compute_arrow_2D(state_size, vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, vgap, igap, cgap, 1, 1, 2, 0, 0); */
|
||||
/* vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); */
|
||||
/* assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); */
|
||||
/* compute_arrow_2D(state_size, vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, vgap, igap, cgap, 1, 0, 3, 0, 0); */
|
||||
/* vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb); */
|
||||
/* assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb); */
|
||||
/* compute_arrow_2D(state_size, vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, vgap, igap, cgap, 1, 1, state_size - 2, 0, 0); */
|
||||
/* vgap += 9; igap += 6; cgap += 15; ++arrows_nb; */
|
||||
|
||||
assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb);
|
||||
// assert(igap < line_indices_nb); assert(igap + 5 < line_indices_nb);
|
||||
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, 0, // plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 0, 0, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, 0, // plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 1, 2, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, 0, // plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 0, 3, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, 0, // plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 1, 3, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
return plan_indices_nb;
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 0, state_size - 2, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 1, state_size - 2, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
/// glGenBuffers(1, &vertex_buffer);
|
||||
/// glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
/// glBufferData(GL_ARRAY_BUFFER, vertex_nb * sizeof(vertex_base[0]), vertex_base, GL_STATIC_DRAW);
|
||||
/// glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
|
||||
// colors
|
||||
/// glGenBuffers(1, &color_buffer);
|
||||
/// glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
|
||||
/// glBufferData(GL_ARRAY_BUFFER, colors_nb * sizeof(color_base), color_base, GL_STATIC_DRAW);
|
||||
/// glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// We only use one VAO, so we always keep it bound
|
||||
/// glGenVertexArrays(1, &vao);
|
||||
/// glBindVertexArray(vao);
|
||||
|
||||
/// if(entry != NULL) {
|
||||
/// entry->vao = vao;
|
||||
/// entry->position_buffer = vertex_buffer;
|
||||
/// entry->color_buffer = color_buffer;
|
||||
/// }
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,701 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: GL functions
|
||||
*
|
||||
* Copyright (C) 2023 Arthur Menges <arthur.menges@a-lec.org>
|
||||
* Copyright (C) 2023 Adrien Bourmault <neox@a-lec.org>
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <fcntl.h>
|
||||
#include <epoxy/gl.h>
|
||||
#include <GL/glu.h>
|
||||
#include <GL/glext.h>
|
||||
#include <glib-2.0/glib.h>
|
||||
#include <math.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "../../include/base.h"
|
||||
#include "../../include/ui.h"
|
||||
#include "../../include/graphics_control.h"
|
||||
#include "../../include/space_2D.h"
|
||||
#include "../../include/space_3D.h"
|
||||
#include "../../include/arrow_2D.h"
|
||||
#include "../../include/arrow_3D.h"
|
||||
|
||||
#define VERTEX_SHADER_FILE "src/shaders/shader.vert"
|
||||
#define FRAG_SHADER_FILE "src/shaders/shader.frag"
|
||||
#define K 0
|
||||
|
||||
GLfloat *vertex_base = NULL;
|
||||
GLfloat *color_base = NULL;
|
||||
GLubyte *line_ndx = NULL;
|
||||
GLubyte *plan_ndx = NULL;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Prints verbose human-readable debug messages
|
||||
*
|
||||
* @param source, XXX
|
||||
* type, XXX
|
||||
* id, XXX
|
||||
* severity, XXX
|
||||
* length, XXX
|
||||
* msg, XXX
|
||||
* data, XXX
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
static void graphics_debug_callback(GLenum source, GLenum type, GLuint id,
|
||||
GLenum severity, GLsizei length,
|
||||
const GLchar *msg, const void *data)
|
||||
{
|
||||
const char *errsource;
|
||||
const char *errtype;
|
||||
const char *errseverity;
|
||||
const GLubyte *string;
|
||||
GLenum code;
|
||||
|
||||
switch (source) {
|
||||
case GL_DEBUG_SOURCE_API: errsource = "API"; break;
|
||||
case GL_DEBUG_SOURCE_WINDOW_SYSTEM: errsource = "WINDOW SYSTEM"; break;
|
||||
case GL_DEBUG_SOURCE_SHADER_COMPILER: errsource = "SHADER COMPILER"; break;
|
||||
case GL_DEBUG_SOURCE_THIRD_PARTY: errsource = "THIRD PARTY"; break;
|
||||
case GL_DEBUG_SOURCE_APPLICATION: errsource = "APPLICATION"; break;
|
||||
case GL_DEBUG_SOURCE_OTHER: errsource = "UNKNOWN"; break;
|
||||
default: errsource = "UNKNOWN"; break;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case GL_DEBUG_TYPE_ERROR: errtype = "ERROR"; break;
|
||||
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: errtype = "DEPRECATED BEHAVIOR";break;
|
||||
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: errtype = "UNDEFINED BEHAVIOR"; break;
|
||||
case GL_DEBUG_TYPE_PORTABILITY: errtype = "PORTABILITY"; break;
|
||||
case GL_DEBUG_TYPE_PERFORMANCE: errtype = "PERFORMANCE"; break;
|
||||
case GL_DEBUG_TYPE_OTHER: errtype = "OTHER"; break;
|
||||
case GL_DEBUG_TYPE_MARKER: errtype = "MARKER"; break;
|
||||
default: errtype = "UNKNOWN"; break;
|
||||
}
|
||||
|
||||
switch (severity) {
|
||||
case GL_DEBUG_SEVERITY_HIGH: errseverity = "CRITICAL"; break;
|
||||
case GL_DEBUG_SEVERITY_MEDIUM: errseverity = "ERROR"; break;
|
||||
case GL_DEBUG_SEVERITY_LOW: errseverity = "WARNING"; break;
|
||||
case GL_DEBUG_SEVERITY_NOTIFICATION: errseverity = "INFO"; break;
|
||||
default: errseverity = "UNKNOWN"; break;
|
||||
}
|
||||
|
||||
code = glGetError();
|
||||
string = gluErrorString(code);
|
||||
|
||||
printf("[%s] %s (%s) from %s: %s\n",
|
||||
errseverity, string, errtype, errsource, msg);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Dynamic array of ptrs to dynamically allocated gl_area_entry
|
||||
*/
|
||||
struct gl_area_entry **gl_area_array = NULL;
|
||||
|
||||
|
||||
/*
|
||||
* Find gl_area_entry from a gl_area ptr
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
*
|
||||
* @return entry ptr
|
||||
*/
|
||||
struct gl_area_entry *find_entry_from_ptr(const char *name)
|
||||
{
|
||||
struct gl_area_entry **cur;
|
||||
|
||||
// Check uninitialized and quit
|
||||
if (gl_area_array == NULL) return NULL;
|
||||
|
||||
// Seek in array
|
||||
cur = gl_area_array;
|
||||
while(*cur) {
|
||||
if (strncmp((*cur)->name, name, sizeof((*cur)->name)) == 0) return *cur;
|
||||
cur++;
|
||||
}
|
||||
|
||||
// Nothing found
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Find the gl_area_entry size
|
||||
*
|
||||
* @param void
|
||||
*
|
||||
* @return size of array
|
||||
*/
|
||||
static inline short gl_area_size(void)
|
||||
{
|
||||
struct gl_area_entry **cur;
|
||||
short size;
|
||||
|
||||
// Check uninitialized and quit
|
||||
if (gl_area_array == NULL) return 0;
|
||||
|
||||
cur = gl_area_array;
|
||||
while(*cur) {
|
||||
cur++;
|
||||
size++;
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initializes a gl_area
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
* @return true if initialized
|
||||
*/
|
||||
bool graphics_init(const char *gl_area)
|
||||
{
|
||||
short array_size;
|
||||
|
||||
glEnable(GL_DEBUG_OUTPUT);
|
||||
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
|
||||
// Check if not already initialized
|
||||
if (find_entry_from_ptr(gl_area)) {
|
||||
errno = EEXIST;
|
||||
perror("gl_area_array already exists");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Get actual array size
|
||||
array_size = gl_area_size();
|
||||
|
||||
// If it does not exist, allocs it
|
||||
if (gl_area_array == NULL) {
|
||||
gl_area_array = g_malloc0(sizeof(struct gl_area_entry *) * 2);
|
||||
|
||||
// If it does exist, g_reallocs it
|
||||
} else {
|
||||
gl_area_array = g_realloc(gl_area_array,
|
||||
(array_size + 1)
|
||||
* sizeof(struct gl_area_entry *));
|
||||
if (gl_area_array == NULL) {
|
||||
perror("Not enough memory to allocate gl_area_array");
|
||||
return false;
|
||||
}
|
||||
|
||||
explicit_bzero(gl_area_array + array_size * sizeof(struct gl_area_entry *),
|
||||
sizeof(struct gl_area_entry *));
|
||||
}
|
||||
|
||||
// Alloc new entry
|
||||
gl_area_array[array_size] = g_malloc0(sizeof(struct gl_area_entry));
|
||||
|
||||
strcpy(gl_area_array[array_size]->name, gl_area);
|
||||
|
||||
if (!graphics_init_shaders(gl_area)) return false;
|
||||
|
||||
graphics_init_buffers(gl_area);
|
||||
|
||||
glDebugMessageCallback(graphics_debug_callback, NULL);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* Shutdowns a gl_area
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
* @return true if success
|
||||
*/
|
||||
bool graphics_shutdown(const void *gl_area)
|
||||
{
|
||||
struct gl_area_entry *entry;
|
||||
|
||||
entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
if (entry == NULL) {
|
||||
errno = EFAULT;
|
||||
perror("gl_area_array entry not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
glDeleteBuffers(1, &entry->position_buffer);
|
||||
glDeleteBuffers(1, &entry->color_buffer);
|
||||
glDeleteProgram(entry->program);
|
||||
|
||||
// Liberate
|
||||
g_free(entry);
|
||||
|
||||
entry = NULL;
|
||||
|
||||
gl_area_array = g_realloc(gl_area_array,
|
||||
(gl_area_size())
|
||||
* sizeof(struct gl_area_entry *));
|
||||
|
||||
g_free(vertex_base);
|
||||
g_free(color_base);
|
||||
g_free(line_ndx);
|
||||
g_free(plan_ndx);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Initializes an identity matrix
|
||||
*
|
||||
* @param res, target ptr
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
static inline void compute_i(float *res)
|
||||
{
|
||||
/* initialize to the identity matrix */
|
||||
res[0] = 1.f; res[4] = 0.f; res[8] = 0.f; res[12] = 0.f;
|
||||
res[1] = 0.f; res[5] = 1.f; res[9] = 0.f; res[13] = 0.f;
|
||||
res[2] = 0.f; res[6] = 0.f; res[10] = 1.f; res[14] = 0.f;
|
||||
res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f;
|
||||
}
|
||||
|
||||
/*
|
||||
* Computes the projection matrix
|
||||
*
|
||||
* @param res, target ptr
|
||||
* phi, XXX
|
||||
* theta, XXX
|
||||
* psi, XXX
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
static inline void compute_mvp(float *res, float phi, float theta, float psi)
|
||||
{
|
||||
float x = phi *(G_PI / 180.f);
|
||||
float y = theta *(G_PI / 180.f);
|
||||
float z = psi *(G_PI / 180.f);
|
||||
float c1 = cosf(x), s1 = sinf(x);
|
||||
float c2 = cosf(y), s2 = sinf(y);
|
||||
float c3 = cosf(z), s3 = sinf(z);
|
||||
float c3c2 = c3 * c2;
|
||||
float s3c1 = s3 * c1;
|
||||
float c3s2s1 = c3 * s2 * s1;
|
||||
float s3s1 = s3 * s1;
|
||||
float c3s2c1 = c3 * s2 * c1;
|
||||
float s3c2 = s3 * c2;
|
||||
float c3c1 = c3 * c1;
|
||||
float s3s2s1 = s3 * s2 * s1;
|
||||
float c3s1 = c3 * s1;
|
||||
float s3s2c1 = s3 * s2 * c1;
|
||||
float c2s1 = c2 * s1;
|
||||
float c2c1 = c2 * c1;
|
||||
|
||||
compute_i(res);
|
||||
|
||||
/* apply all three rotations using the three matrices:
|
||||
*
|
||||
* ⎡ c3 s3 0 ⎤ ⎡ c2 0 -s2 ⎤ ⎡ 1 0 0 ⎤
|
||||
* ⎢ -s3 c3 0 ⎥ ⎢ 0 1 0 ⎥ ⎢ 0 c1 s1 ⎥
|
||||
* ⎣ 0 0 1 ⎦ ⎣ s2 0 c2 ⎦ ⎣ 0 -s1 c1 ⎦
|
||||
*/
|
||||
res[0] = c3c2; res[4] = s3c1 + c3s2s1; res[8] = s3s1 - c3s2c1; res[12] = 0.f;
|
||||
res[1] = -s3c2; res[5] = c3c1 - s3s2s1; res[9] = c3s1 + s3s2c1; res[13] = 0.f;
|
||||
res[2] = s2; res[6] = -c2s1; res[10] = c2c1; res[14] = 0.f;
|
||||
res[3] = 0.f; res[7] = 0.f; res[11] = 0.f; res[15] = 1.f;
|
||||
}
|
||||
|
||||
/*
|
||||
* Created and compile a shader
|
||||
*
|
||||
* @param type, shader type
|
||||
* src, source code of shader
|
||||
*
|
||||
* @return shader id
|
||||
*/
|
||||
static inline GLuint create_shader(int type, const char *src)
|
||||
{
|
||||
GLuint shader;
|
||||
int status;
|
||||
|
||||
shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &src, NULL);
|
||||
glCompileShader(shader);
|
||||
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
||||
if(status == GL_FALSE) {
|
||||
int log_len;
|
||||
char *buffer;
|
||||
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_len);
|
||||
|
||||
buffer = g_malloc(log_len + 1);
|
||||
glGetShaderInfoLog(shader, log_len, NULL, buffer);
|
||||
|
||||
g_warning("Compile failure in %s shader:\n%s",
|
||||
type == GL_VERTEX_SHADER ? "vertex" : "fragment",
|
||||
buffer);
|
||||
|
||||
g_free(buffer);
|
||||
|
||||
glDeleteShader(shader);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return shader;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Initializes the shaders of a gl_area and link them to a program
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
*
|
||||
* @return true if initialized
|
||||
*/
|
||||
bool graphics_init_shaders(const void *gl_area)
|
||||
{
|
||||
char *vertex_shader;
|
||||
char *fragment_shader;
|
||||
struct gl_area_entry *entry;
|
||||
int status;
|
||||
GLuint vertex, fragment;
|
||||
GLuint program = 0;
|
||||
GLuint m = 0;
|
||||
GLuint v = 0;
|
||||
GLuint p = 0;
|
||||
|
||||
entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
if (entry == NULL) {
|
||||
errno = EFAULT;
|
||||
perror("gl_area_array entry not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load vertex shader file
|
||||
vertex_shader = read_file(VERTEX_SHADER_FILE);
|
||||
if (vertex_shader == NULL)
|
||||
return false;
|
||||
vertex = create_shader(GL_VERTEX_SHADER, vertex_shader);
|
||||
|
||||
if(vertex == 0) {
|
||||
entry->program = 0;
|
||||
g_free(vertex_shader);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load fragment shader file
|
||||
fragment_shader = read_file(FRAG_SHADER_FILE);
|
||||
if (fragment_shader == NULL)
|
||||
return false;
|
||||
fragment = create_shader(GL_FRAGMENT_SHADER, fragment_shader);
|
||||
|
||||
if(fragment == 0) {
|
||||
glDeleteShader(vertex);
|
||||
entry->program = 0;
|
||||
g_free(vertex_shader);
|
||||
g_free(fragment_shader);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Link shaders to program
|
||||
program = glCreateProgram();
|
||||
glAttachShader(program, vertex);
|
||||
glAttachShader(program, fragment);
|
||||
|
||||
glLinkProgram(program);
|
||||
|
||||
glGetProgramiv(program, GL_LINK_STATUS, &status);
|
||||
|
||||
if(status == GL_FALSE) {
|
||||
int log_len;
|
||||
char *buffer;
|
||||
|
||||
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &log_len);
|
||||
|
||||
buffer = g_malloc(log_len + 1);
|
||||
glGetProgramInfoLog(program, log_len, NULL, buffer);
|
||||
|
||||
g_warning("Linking failure:\n%s", buffer);
|
||||
|
||||
g_free(buffer);
|
||||
|
||||
glDeleteProgram(program);
|
||||
program = 0;
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
|
||||
g_free(vertex_shader);
|
||||
g_free(fragment_shader);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Get the location of the "mvp" uniform */
|
||||
m = glGetUniformLocation(program, "model_matrix");
|
||||
v = glGetUniformLocation(program, "view_matrix");
|
||||
p = glGetUniformLocation(program, "projection_matrix");
|
||||
|
||||
glDetachShader(program, vertex);
|
||||
glDetachShader(program, fragment);
|
||||
|
||||
glDeleteShader(vertex);
|
||||
glDeleteShader(fragment);
|
||||
|
||||
entry->program = program;
|
||||
entry->m = m;
|
||||
entry->v = v;
|
||||
entry->p = p;
|
||||
|
||||
g_free(vertex_shader);
|
||||
g_free(fragment_shader);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
int dim = 0;
|
||||
int state_size = 0;
|
||||
int vertex_nb = 0;
|
||||
int colors_nb = 0;
|
||||
int lines_nb = 0;
|
||||
int plans_nb = 0;
|
||||
int arrows_nb = 0;
|
||||
bool pref_3D_xor_2D_space = 0; // default view == 0 == 3D
|
||||
bool pref_3D_xor_2D_arrows = 0; // default view == 0 == 3D
|
||||
bool pref_mark_unit_space_zero = 0; // default == 0 == no mark
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
static void get_model_data_and_user_preferences(){
|
||||
// The model data (arrows) to use ( from model.xml)
|
||||
// { 1, 1, 0, 1, 2, 1, 1, 1, 10, 1, 2, 11, 1, 1, 20, 1, 2, 21 };
|
||||
|
||||
dim = 1;
|
||||
state_size = 9; // 2 < state_size < 32
|
||||
arrows_nb = 4; // assert : leur emplacement doit être fonction de state_size
|
||||
pref_3D_xor_2D_space = 0; // default == 0 == 3D
|
||||
pref_3D_xor_2D_arrows = 1; // default == 0 == 3D
|
||||
pref_mark_unit_space_zero = 1; // default == 0 == no mark
|
||||
}
|
||||
|
||||
static void compute_vertex_colors_lines_plans_nb(int dim,
|
||||
int state_size,
|
||||
int arrows_nb,
|
||||
bool pref_3D_xor_2D_space,
|
||||
bool pref_3D_xor_2D_arrows,
|
||||
bool pref_mark_unit_space_zero,
|
||||
struct gl_area_entry *entry){
|
||||
if (pref_3D_xor_2D_space) vertex_nb = (state_size + 1) * 6 + arrows_nb * 9; // case 2D
|
||||
else vertex_nb = (state_size + 1) * 12 + arrows_nb * 15; // case 3D
|
||||
|
||||
if (pref_3D_xor_2D_space) lines_nb = (2 + state_size) + (arrows_nb * 3); // case 2D
|
||||
else lines_nb = (4 + state_size * 4) + (arrows_nb * 8); // case 3D
|
||||
|
||||
if (pref_mark_unit_space_zero) lines_nb += 2; // 2 diagonal lines
|
||||
//
|
||||
plans_nb = 8; // (4 + state_size * 4) + (arrows_nb * 8) + 2;
|
||||
|
||||
entry->line_indices_nb = lines_nb * 2;
|
||||
entry->plan_indices_nb = plans_nb * 3;
|
||||
|
||||
colors_nb = (state_size + 1) * 12 + arrows_nb * 0; // TODO
|
||||
|
||||
assert(state_size * 6 < vertex_nb); assert(state_size * 6 + 5 < vertex_nb);
|
||||
|
||||
printf("Prefer : mark_unit_space_zero = %d pref_3D_xor_2D_space = %d state_size = %d\n",
|
||||
pref_mark_unit_space_zero, pref_3D_xor_2D_space, state_size);
|
||||
|
||||
printf("Initialization of buffers with %u line_ndx, %u vertices and %u colors & state_size = %d\n",
|
||||
entry->line_indices_nb, vertex_nb, colors_nb, state_size);
|
||||
}
|
||||
|
||||
/* Initializes the buffer of a gl_area
|
||||
* Calls according to the user preferences
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
* Note : line_ndx[] is no more defined in graphics_cube.h
|
||||
* @return void
|
||||
*/
|
||||
#define A 0
|
||||
void graphics_init_buffers(const void *gl_area)
|
||||
{
|
||||
struct gl_area_entry *entry;
|
||||
entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
get_model_data_and_user_preferences();
|
||||
compute_vertex_colors_lines_plans_nb(dim, state_size, arrows_nb,
|
||||
pref_3D_xor_2D_space,
|
||||
pref_3D_xor_2D_arrows,
|
||||
pref_mark_unit_space_zero,
|
||||
entry);
|
||||
|
||||
// g_malloc
|
||||
vertex_base = g_malloc0(vertex_nb * sizeof(GLfloat) * 2);
|
||||
color_base = g_malloc0(colors_nb * sizeof(GLfloat) * 2);
|
||||
line_ndx = g_malloc0(lines_nb * 2 * sizeof(GLubyte) * 2);
|
||||
plan_ndx = g_malloc0(plans_nb * 3 * sizeof(GLubyte) * 2);
|
||||
|
||||
|
||||
int vgap = 0, igap = 0, cgap = 0, dgap = 0;
|
||||
|
||||
if (pref_mark_unit_space_zero) dgap = 4; // 4 vertices for 2 diagonal lines
|
||||
vgap += (6 * state_size);
|
||||
igap += state_size * 2 + dgap;
|
||||
cgap += (6 * state_size);
|
||||
|
||||
if (pref_3D_xor_2D_space) compute_space_2D(state_size,
|
||||
vertex_base, color_base,
|
||||
line_ndx,
|
||||
pref_mark_unit_space_zero, dgap);
|
||||
|
||||
else compute_space_3D(state_size,
|
||||
vertex_base, color_base,
|
||||
line_ndx,
|
||||
pref_mark_unit_space_zero, dgap);
|
||||
|
||||
for (int k = 0; k < state_size; k++){
|
||||
assert(k * 12 < vertex_nb); assert(k * 12 + 11 < vertex_nb);
|
||||
assert(k * 12 < colors_nb); assert(k * 12 + 11 < colors_nb);
|
||||
assert(k + 8 < entry->line_indices_nb); assert(k + 16 < entry->line_indices_nb);
|
||||
}
|
||||
|
||||
assert(vgap < vertex_nb); assert(vgap + 9 < vertex_nb);
|
||||
assert(igap < entry->line_indices_nb); assert(igap + 5 < entry->line_indices_nb);
|
||||
|
||||
|
||||
/* compute_arrow_3D(state_size, */
|
||||
/* vertex_base, color_base, */
|
||||
/* line_ndx, plan_ndx, */
|
||||
/* vgap, igap, cgap, */
|
||||
/* 1, 0, 0, 0, 0); */
|
||||
/* vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, 3, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_3D(entry, state_size, vgap, igap, 1, 0, state_size - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
/* compute_arrow_3D(entry, state_size, vgap, igap, 1, 1, state_size - 2, 0, 0); vgap += 15; igap += 16; cgap += 15; ++arrows_nb; */
|
||||
|
||||
GLuint vao, vertex_buffer, color_buffer;
|
||||
|
||||
glGenBuffers(1, &vertex_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, vertex_nb * sizeof(vertex_base[0]), vertex_base, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
|
||||
// colors
|
||||
glGenBuffers(1, &color_buffer);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
|
||||
glBufferData(GL_ARRAY_BUFFER, colors_nb * sizeof(color_base), color_base, GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
|
||||
// We only use one VAO, so we always keep it bound
|
||||
glGenVertexArrays(1, &vao);
|
||||
glBindVertexArray(vao);
|
||||
|
||||
if(entry != NULL) {
|
||||
entry->vao = vao;
|
||||
entry->position_buffer = vertex_buffer;
|
||||
entry->color_buffer = color_buffer;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Draws the current buffer to a gl_area
|
||||
*
|
||||
* @param gl_area, ptr to the gl_area widget
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
void graphics_draw(const void *gl_area)
|
||||
{
|
||||
float m[16];
|
||||
float v[16];
|
||||
float p[16];
|
||||
struct gl_area_entry *entry;
|
||||
|
||||
entry = find_entry_from_ptr(gl_area);
|
||||
|
||||
if (entry == NULL) {
|
||||
errno = EFAULT;
|
||||
perror("gl_area_array entry not found");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Compute the model view projection matrix using the
|
||||
* rotation angles specified through the GtkRange widgets
|
||||
*/
|
||||
compute_mvp(p,
|
||||
rotation_angles[X_AXIS],
|
||||
rotation_angles[Y_AXIS],
|
||||
rotation_angles[Z_AXIS]);
|
||||
compute_i(m);
|
||||
compute_i(v);
|
||||
|
||||
/* Use our shaders */
|
||||
glUseProgram(entry->program);
|
||||
|
||||
glClearColor(0, 0, 0, 0.2f); // glClearColor(0.3f, 0, 0, 0.8f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
/* Update the "mvp" matrix we use in the shader */
|
||||
glUniformMatrix4fv(entry->m, 1, GL_FALSE, &m[0]);
|
||||
glUniformMatrix4fv(entry->v, 1, GL_FALSE, &v[0]);
|
||||
glUniformMatrix4fv(entry->p, 1, GL_FALSE, &p[0]);
|
||||
|
||||
/* Use the vertices in our buffer */
|
||||
glEnableVertexAttribArray(0);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, entry->position_buffer);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,(void*)0);
|
||||
|
||||
// couleurs
|
||||
glEnableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, entry->color_buffer);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0,(void*)0);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
glDrawElements(GL_LINES, entry->line_indices_nb, GL_UNSIGNED_BYTE, line_ndx);
|
||||
glDrawElements(GL_TRIANGLES, entry->plan_indices_nb, GL_UNSIGNED_BYTE, plan_ndx);
|
||||
|
||||
/* We finished using the buffers and program */
|
||||
glDisableVertexAttribArray(0);
|
||||
glDisableVertexAttribArray(1);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||
glUseProgram(0);
|
||||
|
||||
glFlush();
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: GL functions
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <epoxy/gl.h>
|
||||
|
||||
#define V 1
|
||||
#define I 0
|
||||
#define EDGE 0.999999
|
||||
bool compute_space_2D(int state_size,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx,
|
||||
bool pref_mark_unit_space_zero, int dgap){
|
||||
|
||||
float x = 0;
|
||||
// for (int k = 0; k < state_size + 1; k++) <=> + 1
|
||||
// car on passe du nombre de cases = (n)
|
||||
// au nombre de séparations entre les cases + les deux bords = (n + 1)
|
||||
|
||||
if (V) printf("vertices ");
|
||||
for (int k = 0; k < state_size; k++){ // barres verticales
|
||||
//
|
||||
x = ((state_size % 2) * (state_size / 2 - k)
|
||||
+ (state_size % 2 - 1) * (k + 0.5f - state_size / 2)) / state_size * 2 * EDGE;
|
||||
|
||||
if (V) printf("[%1.1f] ", x);
|
||||
|
||||
*(vertex_base + k * 6 + 0) = - x;
|
||||
*(vertex_base + k * 6 + 1) = 1.0f / state_size;
|
||||
*(vertex_base + k * 6 + 2) = 0.0f;
|
||||
|
||||
*(vertex_base + k * 6 + 3) = - x;
|
||||
*(vertex_base + k * 6 + 4) = - 1.0f / state_size;
|
||||
*(vertex_base + k * 6 + 5) = 0.0f;
|
||||
}
|
||||
|
||||
if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size);
|
||||
|
||||
*(line_ndx + 0) = 0;
|
||||
*(line_ndx + 1) = state_size * 2 - 2; // barre horizontale du bas
|
||||
|
||||
*(line_ndx + 2) = 1;
|
||||
*(line_ndx + 3) = state_size * 2 - 1; // barre horizontale du haut
|
||||
|
||||
for (int k = 0; k < state_size * 2; k++) *(line_ndx + k + 4) = k;
|
||||
|
||||
if (I) printf("line_ndx (%d - state_size(0)) x (%d - state_size(1)) ", 0, 1);
|
||||
for (int v = 0; v < state_size + 2; v++) {
|
||||
if (I) printf("(%d-%d) ", *(line_ndx + v), *(line_ndx + v + 1));
|
||||
}
|
||||
|
||||
if (pref_mark_unit_space_zero) { // diagonales pour marquer la case zéro
|
||||
*(line_ndx + state_size * 2 + 6 + dgap + 0) = 0;
|
||||
*(line_ndx + state_size * 2 + 6 + dgap + 1) = 3;
|
||||
*(line_ndx + state_size * 2 + 6 + dgap + 2) = 1;
|
||||
*(line_ndx + state_size * 2 + 6 + dgap + 3) = 2;
|
||||
}
|
||||
|
||||
if (I) printf(" n = 4 + (%d x 2)\n", state_size + 2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
|
@ -0,0 +1,156 @@
|
|||
/*
|
||||
* Gem-graph OpenGL experiments
|
||||
*
|
||||
* Desc: GL functions
|
||||
*
|
||||
* Copyright (C) 2023 Jean Sirmai <jean@a-lec.org>
|
||||
*
|
||||
* 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <epoxy/gl.h>
|
||||
|
||||
#define V 1
|
||||
#define I 1
|
||||
#define EDGE 0.999999
|
||||
|
||||
bool compute_space_3D(int state_size,
|
||||
GLfloat *vertex_base, GLfloat *color_base,
|
||||
GLubyte *line_ndx,
|
||||
bool pref_mark_unit_space_zero, int dgap){
|
||||
|
||||
float x = 0;
|
||||
// for (int k = 0; k < state_size + 1; k++) <=> + 1 car
|
||||
// on passe du nombre de cases = (n)
|
||||
// au nombre de séparations entre les cases + les deux bords = (n + 1)
|
||||
|
||||
if (V) printf("vertices ");
|
||||
for (int k = 0; k < state_size; k++){ // barres verticales
|
||||
//
|
||||
x = ((state_size % 2) * (state_size / 2 - k)
|
||||
+ (state_size % 2 - 1) * (k + 0.5f - state_size / 2)) / state_size * 2 * EDGE;
|
||||
|
||||
if (V) printf("[%1.1f] ", x);
|
||||
|
||||
*(vertex_base + k * 12 + 0) = - x;
|
||||
*(vertex_base + k * 12 + 1) = 1.0f / state_size;
|
||||
*(vertex_base + k * 12 + 2) = 1.0f / state_size;
|
||||
|
||||
*(vertex_base + k * 12 + 3) = - x;
|
||||
*(vertex_base + k * 12 + 4) = - 1.0f / state_size;
|
||||
*(vertex_base + k * 12 + 5) = - 1.0f / state_size;
|
||||
|
||||
*(vertex_base + k * 12 + 6) = - x;
|
||||
*(vertex_base + k * 12 + 7) = 1.0f / state_size;
|
||||
*(vertex_base + k * 12 + 8) = - 1.0f / state_size;
|
||||
|
||||
*(vertex_base + k * 12 + 9) = - x;
|
||||
*(vertex_base + k * 12 + 10) = - 1.0f / state_size;
|
||||
*(vertex_base + k * 12 + 11) = 1.0f / state_size;
|
||||
|
||||
*(color_base + k * 12 + 0) = 1 - (k / state_size);
|
||||
*(color_base + k * 12 + 1) = 0.2f;
|
||||
*(color_base + k * 12 + 2) = (k / state_size);
|
||||
|
||||
*(color_base + k * 12 + 3) = 0.8f;
|
||||
*(color_base + k * 12 + 4) = 0.8f;
|
||||
*(color_base + k * 12 + 5) = 0.2f;
|
||||
|
||||
*(color_base + k * 12 + 6) = 0.8f;
|
||||
*(color_base + k * 12 + 7) = (k / state_size);
|
||||
*(color_base + k * 12 + 8) = 0.2f;
|
||||
|
||||
*(color_base + k * 12 + 9) = 1 - (k / state_size);
|
||||
*(color_base + k * 12 + 10) = 0.2f;
|
||||
*(color_base + k * 12 + 11) = 0.9f;
|
||||
|
||||
// if (k == 0) for (int j = 0; j < 12; j++) *(color_base + j) = 0.2f;
|
||||
|
||||
/* 0.8, 0.8, 0.8, // blanc */
|
||||
/* 0.8, 0.8, 0.2, // jaune */
|
||||
/* 0.8, 0.2, 0.2, // rouge */
|
||||
/* 0.2, 0.2, 0.2, // noir */
|
||||
/* 0.2, 0.2, 0.2, // gris */
|
||||
/* 0.2, 0.8, 0.8, // cyan */
|
||||
/* 0.2, 0.8, 0.2, // vert */
|
||||
/* 0.8, 0.2, 0.8, // magenta */
|
||||
|
||||
}
|
||||
|
||||
if (V) printf(" n = %d x 2 côté = [%1.1f]\n", state_size + 1, 2.0f / state_size);
|
||||
|
||||
*(line_ndx + 4) = 0;
|
||||
*(line_ndx + 5) = state_size * 4 - 4; // barre horizontale du bas arr
|
||||
|
||||
*(line_ndx + 6) = 1;
|
||||
*(line_ndx + 7) = state_size * 4 - 3; // barre horizontale du haut arr
|
||||
|
||||
*(line_ndx + 0) = 2;
|
||||
*(line_ndx + 1) = state_size * 4 - 2; // barre horizontale du bas av
|
||||
|
||||
*(line_ndx + 2) = 3;
|
||||
*(line_ndx + 3) = state_size * 4 - 1; // barre horizontale du haut av
|
||||
|
||||
|
||||
/* *(plan_ndx + 0) = 0; */
|
||||
/* *(plan_ndx + 1) = 1; */
|
||||
/* *(plan_ndx + 2) = 2; */
|
||||
|
||||
/* *(plan_ndx + 3) = 0; */
|
||||
/* *(plan_ndx + 4) = 1; */
|
||||
/* *(plan_ndx + 5) = 3; */
|
||||
|
||||
|
||||
for (int k = 8; k <= state_size * 8; k += 8){ // for (int k = 8; k < state_size * 8; k += 8){
|
||||
|
||||
|
||||
*(line_ndx + k + 0) = k / 2 - 4; // + 0;
|
||||
*(line_ndx + k + 1) = k / 2 - 2; // + 2;
|
||||
|
||||
*(line_ndx + k + 2) = k / 2 - 3; // + 1;
|
||||
*(line_ndx + k + 3) = k / 2 - 1; // + 3;
|
||||
|
||||
*(line_ndx + k + 4) = k / 2 - 4; // + 0;
|
||||
*(line_ndx + k + 5) = k / 2 - 1; // + 3;
|
||||
|
||||
*(line_ndx + k + 6) = k / 2 - 3; // + 1;
|
||||
*(line_ndx + k + 7) = k / 2 - 2; // + 2;
|
||||
}
|
||||
|
||||
if (pref_mark_unit_space_zero) {
|
||||
|
||||
*(line_ndx + (2 + state_size) * 8 - 4) = 0 + 0; // diagonales pour marquer la case zéro
|
||||
*(line_ndx + (2 + state_size) * 8 - 3) = 0 + 1;
|
||||
|
||||
*(line_ndx + (2 + state_size) * 8 - 2) = 0 + 2; // diagonales pour marquer la case zéro
|
||||
*(line_ndx + (2 + state_size) * 8 - 1) = 0 + 3;
|
||||
}
|
||||
|
||||
if (I) printf("line_ndx ");
|
||||
for (int v = 0; v < state_size + 2; v++) {
|
||||
if (I) printf("(%d-%d) ", *(line_ndx + v), *(line_ndx + v + 1));
|
||||
}
|
||||
|
||||
if (I) printf(" n = 4 + (%d x 2) dgap = %d\n", state_size + 2, dgap);
|
||||
if (I) printf("pref_mark_unit_space_zero = %d ", pref_mark_unit_space_zero);
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -12,5 +12,5 @@ out vec4 color;
|
|||
void main(void)
|
||||
{
|
||||
gl_Position = projection_matrix * view_matrix * model_matrix * vec4(in_position, 1);
|
||||
color = vec4(1 * in_color.rgb, 1);
|
||||
color = vec4(1 * in_color.rgb, 0.2f);
|
||||
}
|
|
@ -25,7 +25,7 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#include "../../include/base.h"
|
||||
#include "../../include/graphics.h"
|
||||
#include "../../include/graphics_control.h"
|
||||
#include "../../include/ui.h"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue