Basilisk CFD
Adaptive Cartesian mesh PDE framework
Loading...
Searching...
No Matches
vertexbuffer.h
Go to the documentation of this file.
1/** @file vertexbuffer.h
2 */
3/**
4# Vertex buffers
5
6They are used to store the vertex coordinates, normals and colors
7computed by the the OpenGL commands typically used by [draw.h]().
8
9These vertex buffers are the minimal information required to render
10the objects.
11
12In combination with the [tiny implementation](gl/fb_tiny.c) this
13allows to generate geometries using OpenGL commands but without any
14OpenGL library. */
15
16struct {
17 // public
19 float modelview[16];
20 int type; // type = 0 -> lines, type = 1 -> mesh
21 int dim;
23 bool visible; // visible = true > only traverse visible vertices
24
25 // private
28 int state;
29} VertexBuffer = {
30 .visible = false, // traverse all vertices by default
31 .modelview = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }
32};
33
34static void vertex_buffer_push_index (unsigned int i)
35{
36 i -= VertexBuffer.vertex;
37 array_append (VertexBuffer.index, &i, sizeof(unsigned int));
38}
39
41{
42 VertexBuffer.nvertex = 0;
43 VertexBuffer.type = -1;
44 VertexBuffer.dim = -1;
45 VertexBuffer.position = array_new();
46 VertexBuffer.normal = array_new();
47 VertexBuffer.color = array_new();
48 VertexBuffer.index = array_new();
49}
50
52{
53 array_free (VertexBuffer.position);
54 VertexBuffer.position = NULL;
55 array_free (VertexBuffer.normal);
56 VertexBuffer.normal = NULL;
58 VertexBuffer.color = NULL;
60 VertexBuffer.index = NULL;
61}
62
63static void vertex_buffer_glBegin (unsigned int state)
64{
65 if (VertexBuffer.index) {
66
68
69 bview * view = get_view();
70
71 float q[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
72 - view->tx, - view->ty, 3, 1 };
73 matrix_multiply (q, VertexBuffer.modelview);
74 for (int i = 0; i < 16; i++)
75 VertexBuffer.modelview[i] = q[i];
76
77 gl_build_rotmatrix ((float (*)[4])q, view->quat);
78 swap (float, q[1], q[4]);
79 swap (float, q[2], q[8]);
80 swap (float, q[6], q[9]);
81 matrix_multiply (q, VertexBuffer.modelview);
82 for (int i = 0; i < 16; i++)
83 VertexBuffer.modelview[i] = q[i];
84
85 VertexBuffer.state = state;
86 switch (state) {
87 case GL_LINE_LOOP:
88 VertexBuffer.line_loop = VertexBuffer.nvertex;
89 break;
90 case GL_LINES:
91 VertexBuffer.lines = VertexBuffer.nvertex;
92 break;
93 case GL_LINE_STRIP:
94 VertexBuffer.line_strip = VertexBuffer.nvertex;
95 break;
96 case GL_QUADS:
97 VertexBuffer.quads = VertexBuffer.nvertex;
98 break;
99 case GL_POLYGON:
100 VertexBuffer.polygon = VertexBuffer.nvertex;
101 break;
102 case GL_TRIANGLE_FAN:
103 VertexBuffer.fan = VertexBuffer.nvertex;
104 break;
105 default:
106 fprintf (stderr, "glBegin (%d) not implemented yet\n", state);
107 break;
108 }
109 }
110 else
111 glBegin (state);
112}
113
115{
116 if (VertexBuffer.index) {
117 int type = -1;
118 switch (VertexBuffer.state) {
119
120 case GL_LINE_LOOP:
121 for (int i = VertexBuffer.line_loop; i < VertexBuffer.nvertex - 1; i++) {
124 }
127 type = 0;
128 break;
129
130 case GL_LINES:
131 for (int i = VertexBuffer.lines; i < VertexBuffer.nvertex; i += 2) {
134 }
135 type = 0;
136 break;
137
138 case GL_LINE_STRIP:
139 for (int i = VertexBuffer.line_strip; i < VertexBuffer.nvertex - 1; i++) {
142 }
143 type = 0;
144 break;
145
146 case GL_QUADS:
147 for (int i = VertexBuffer.quads; i < VertexBuffer.nvertex; i += 4)
148 for (int j = 1; j <= 2; j++) {
152 }
153 type = 1;
154 break;
155
156 case GL_POLYGON:
157 for (int j = 1; j <= VertexBuffer.nvertex - VertexBuffer.polygon - 2;
158 j++) {
162 }
163 type = 1;
164 break;
165
166 case GL_TRIANGLE_FAN:
167 for (int i = VertexBuffer.fan + 1; i < VertexBuffer.nvertex - 1; i++) {
171 }
172 type = 1;
173 break;
174
175 default:
176 break;
177 }
178 VertexBuffer.state = 0;
179 if (VertexBuffer.type >= 0 && type >= 0) {
180 // cannot mix lines and surfaces primitives
181 assert (VertexBuffer.type == type);
182 }
183 else
184 VertexBuffer.type = type;
185 }
186 else
187 glEnd();
188}
189
190static void vertex_buffer_glColor3f (float r, float g, float b)
191{
192 if (VertexBuffer.color) {
193 struct { float x, y, z; } color = {r, g, b}; // fixme: use r,g,b directly
194 array_append (VertexBuffer.color, &color, 3*sizeof(float));
195 }
196 else
197 glColor3f (r, g, b);
198}
199
200static void vertex_buffer_glNormal3d (double nx, double ny, double nz)
201{
202 if (VertexBuffer.normal) {
203 struct { float x, y, z; } normal = {nx, ny, nz};
204 array_append (VertexBuffer.normal, &normal, 3*sizeof(float));
205 }
206 else
207 glNormal3d (nx, ny, nz);
208}
209
210static void vertex_buffer_glVertex3d (double x, double y, double z)
211{
212 if (VertexBuffer.position) {
213 if (VertexBuffer.dim < 3)
214 VertexBuffer.dim = 3;
215 float v[4] = {x, y, z, 1.};
216 vector_multiply (v, VertexBuffer.modelview);
217 array_append (VertexBuffer.position, v, 3*sizeof(float));
218 VertexBuffer.nvertex++;
219 }
220 else
221 glVertex3d (x, y, z);
222}
223
224static void vertex_buffer_glVertex2d (double x, double y)
225{
226 if (VertexBuffer.position) {
227 if (VertexBuffer.dim < 2)
228 VertexBuffer.dim = 2;
229 float v[4] = {x, y, 0, 1.};
230 vector_multiply (v, VertexBuffer.modelview);
231 array_append (VertexBuffer.position, v, 3*sizeof(float));
232 VertexBuffer.nvertex++;
233 }
234 else
235 glVertex3d (x, y, 0.);
236}
237
238/**
239Here we overload the default OpenGL commands, in order to call the
240corresponding vertex buffer operations defined above. */
241
242#define glBegin vertex_buffer_glBegin
243#define glEnd vertex_buffer_glEnd
244#define glVertex2d vertex_buffer_glVertex2d
245#define glVertex2f vertex_buffer_glVertex2d
246#define glVertex3d vertex_buffer_glVertex3d
247#define glColor3f vertex_buffer_glColor3f
248#define glNormal3d vertex_buffer_glNormal3d
vector g[]
We store the combined pressure gradient and acceleration field in g*.
Definition all-mach.h:65
vector q[]
The primitive variables are the momentum , pressure , density , (face) specific volume ,...
Definition all-mach.h:44
Array * array_new()
Definition array.h:10
void array_free(Array *a)
Definition array.h:18
void * array_append(Array *a, void *elem, size_t size)
Definition array.h:24
int y
Definition common.h:76
int x
Definition common.h:76
int z
Definition common.h:76
#define swap(type, a, b)
Definition common.h:19
#define assert(a)
Definition config.h:107
void view(float tx=0., float ty=0., float fov=0., float quat[4]={0}, float sx=1., float sy=1., float sz=1., unsigned width=800, unsigned height=800, unsigned samples=4, float bg[3]={0}, float theta=0., float phi=0., float psi=0., bool relative=false, float tz=0., float near=0., float far=0., float res=0., char *camera=NULL, MapFunc map=NULL, int cache=0, float p1x=0., float p1y=0., float p2x=0., float p2y=0., bview *view1=NULL)
Definition draw.h:52
*cs[i, 0, 0] a *[i -1, 0, 0] j
Definition embed.h:88
double v[2]
Definition embed.h:383
scalar int i
Definition embed.h:74
#define glGetFloatv
Definition glad.h:1987
#define GL_QUADS
Definition glad.h:150
#define GL_TRIANGLE_FAN
Definition glad.h:149
#define GL_LINE_STRIP
Definition glad.h:146
#define GL_LINES
Definition glad.h:144
#define GL_MODELVIEW_MATRIX
Definition glad.h:409
#define GL_POLYGON
Definition glad.h:330
#define GL_LINE_LOOP
Definition glad.h:145
size *double * b
Definition array.h:5
Definition view.h:141
int line_loop
#define glColor3f
#define glBegin
Here we overload the default OpenGL commands, in order to call the corresponding vertex buffer operat...
int line_strip
#define glNormal3d
int nvertex
float modelview[16]
int dim
static void vertex_buffer_glVertex2d(double x, double y)
void vertex_buffer_free()
int vertex
int polygon
int state
void vertex_buffer_setup()
static void vertex_buffer_glEnd()
static void vertex_buffer_glColor3f(float r, float g, float b)
int fan
int lines
bool visible
int quads
struct @17 VertexBuffer
Array * index
static void vertex_buffer_glNormal3d(double nx, double ny, double nz)
Array * normal
int type
static void vertex_buffer_push_index(unsigned int i)
#define glEnd
#define glVertex3d
Array * position
static void vertex_buffer_glVertex3d(double x, double y, double z)
Array * color
static void vertex_buffer_glBegin(unsigned int state)
bview * get_view()
Definition view.h:238