File: gr3_internals.h

package info (click to toggle)
gr-framework 0.73.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 11,600 kB
  • sloc: ansic: 87,413; cpp: 45,348; objc: 3,057; javascript: 2,647; makefile: 1,129; python: 1,000; sh: 991; yacc: 855; pascal: 554; fortran: 228
file content (362 lines) | stat: -rw-r--r-- 12,654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#ifndef GR3_INTERNALS_H_INCLUDED
#define GR3_INTERNALS_H_INCLUDED

typedef struct _GR3_LightSource_t_
{
  float x;
  float y;
  float z;
  float r;
  float g;
  float b;
} GR3_LightSource_t_;
#define MAX_NUM_LIGHTS 16

typedef struct _GR3_LightParameter_t_
{
  float ambient;
  float diffuse;
  float specular_exponent;
  float specular;
} GR3_LightParameter_t_;

#include "gr3_sr.h"
#ifndef M_PI
#define M_PI 3.141592653589793238462643383279
#endif

#ifndef isfinite
#define isfinite(x) ((x) - (x) == (x) - (x))
#endif

#if defined(EMSCRIPTEN)
#ifdef NO_GL
#define GR3_USE_SR
#define GLfloat float
#define GLuint unsigned int
#define GLint int
#else
#define GL_GLEXT_LEGACY
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#ifndef _MSC_VER
#include <unistd.h> /* for getpid() for tempfile names */
#else
#include <process.h>
#endif
#endif
#elif defined(__APPLE__)
/* Core OpenGL (CGL) on Mac OS X */
#define GR3_USE_CGL
#include "gr3_cgl.h"
#elif defined(__linux__) || defined(__FreeBSD__)
/* OpenGL Extension to the X Window System (GLX) on Linux */
#define GR3_USE_GLX
#include "gr3_glx.h"
#elif defined(_WIN32)
/* Windows */
#define GR3_USE_WIN
#include "gr3_win.h"
#else
#error "This operating system is currently not supported by gr3"
#endif

#define GR3_DO_INIT                        \
  do                                       \
    {                                      \
      if (!context_struct_.is_initialized) \
        {                                  \
          gr3_log_("auto-init");           \
          gr3_init(NULL);                  \
        }                                  \
    }                                      \
  while (0)

#ifndef DONT_USE_RETURN_ERROR
#define RETURN_ERROR(error) return _return_error_helper(error, __LINE__, __FILE__)
#endif


/*!
 * An instance of this struct contains all information from the
 * ::int list given to gr3_init() or the default values from
 * ::GR3_InitStruct_INITIALIZER.
 * \note Except for when gr3_init() is run, the only existing instance of this
 * struct should be the one in #context_struct_.
 */
typedef struct _GR3_InitStruct_t_
{
  int framebuffer_width;  /*!< The width of the framebuffer used for
                           generating images */
  int framebuffer_height; /*!< The height of the framebuffer used for
                           generating images */
  int num_threads;
} GR3_InitStruct_t_;

typedef enum _GR3_MeshType_t
{
  kMTNormalMesh,
  kMTIndexedMesh,
  kMTConeMesh,
  kMTSphereMesh,
  kMTCylinderMesh
} GR3_MeshType_t;

/*!
 * This union contains all information required for using a mesh. Either the
 * display list id, or the vertex buffer object id and number of vertices.
 * Instances of this unions are kept and
 * reference counted in the ::GR3_MeshList_t_.
 */
typedef struct _GR3_MeshData_t_
{
  GR3_MeshType_t type;
  union
  {
    int display_list_id; /*!< The OpenGL display list of the mesh. */
    unsigned int vertex_buffer_id;
    struct
    {
      unsigned int index_buffer_id;
      unsigned int vertex_buffer_id;
    } buffers;
  } data;
  float *vertices;
  float *normals;
  float *colors;
  int *indices;
  int number_of_vertices;
  int number_of_indices;
  vertex_fp *vertices_fp;
} GR3_MeshData_t_;


/*!
 * This struct is for managing ::GR3_MeshData_t_ objects.
 * Instances of this array are meant to be kept in an array so they can be
 * accessed by an index. Each instance refers to the next free instance in the
 * list, so when the user needs a new one, a free one can be found just by
 * indexing the array. They are reference counted and are 'free' if there are
 * no references left and _GR3_MeshList_t_::refcount reaches zero. A user can
 * only get one reference to the mesh by calling gr3_createmesh() and therefore
 * should only be able to release one by calling gr3_deletemesh(). This is
 * realized by saving a flag (whether a mesh is marked for deletion or not)
 * in _GR3_MeshList_t_::marked_for_deletion.
 * \note Instances of this struct should only appear in the #context_struct_.
 * \note Reference counting is handled by calling gr3_meshaddreference_() and
 * gr3_meshremovereference_().
 */
typedef struct _GR3_MeshList_t_
{
  GR3_MeshData_t_ data;    /*!<  The data of the actual mesh managed by this
                            struct. */
  int refcount;            /*!< A reference counter. If refcount reaches zero,
                            the instance is unused or 'free' and can be used
                            for a different mesh. */
  int marked_for_deletion; /*!< A flag whether the user called
                            gr3_deletemesh() for this mesh. */
  int next_free;           /*!< If refcount is zero (so this object is free),
                            next_free is the index of the next free object
                            in the array it is stored in. */
} GR3_MeshList_t_;

/*!
 * Each call to gr3_drawmesh() gets saved in an instance of this struct, so it
 * can be used for later drawing. They form a linked-list, with each draw call
 * pointing to the next one.
 */
typedef struct _GR3_DrawList_t_
{
  int mesh;          /*!< The id of the mesh that should be drawn */
  float *positions;  /*!< A list of 3 * _GR3_DrawList_t_::n floats.
                      Each triple is one position
                      for one mesh to be drawn. */
  float *directions; /*!< A list of 3 * _GR3_DrawList_t_::n floats.
                      Each triple is one (forward) direction
                      for one mesh to be drawn. */
  float *ups;        /*!< A list of 3 * _GR3_DrawList_t_::n floats.
                      Each triple is one up vector
                      for one mesh to be drawn. */
  float *colors;     /*!< A list of 3 * _GR3_DrawList_t_::n floats.
                      Each triple is one color
                      for one mesh to be drawn. */
  float *scales;     /*!< A list of 3 * _GR3_DrawList_t_::n floats.
                      Each triple means the scaling factors
                      for one mesh to be drawn. */
  int n;             /*!< The number of meshes to be drawn. */
  int object_id;
  int alpha_mode;
  vertex_fp **vertices_fp;
  float *alphas;
  struct _GR3_DrawList_t_ *next; /*!< The pointer to the next GR3_DrawList_t_. */
} GR3_DrawList_t_;

typedef struct _TransparencyObject
{
  float r;
  float g;
  float b;
  float tr;
  float tg;
  float tb;
  float depth;
} _TransparencyObject;

typedef struct TransparencyVector
{
  int size;
  int max_size;
  _TransparencyObject *obj;
} TransparencyVector;

/*!
 * This struct holds all context data. All data that is dependent on gr3 to
 * be initialized is saved here. It is set up by gr3_init() and turned back
 * into its default state by gr3_terminate().
 * \note #context_struct_ should be the only instance of this struct.
 */
typedef struct _GR3_ContextStruct_t_
{
  GR3_InitStruct_t_ init_struct; /*!< The information given to gr3_init().
                                  \note This member and its members must not
                                  be changed outside of gr3_init() or
                                  gr3_terminate().
                                  \sa _GR3_InitStruct_t_ */

  int is_initialized; /*!< This flag is set to 1 if gr3 was
                       initialized successfully. */

  int gl_is_initialized; /*!< This flag is set to 1 if an OpenGL context
                          has been created successfully. */

  int convenience_is_initialized; /*!< This flag is set to 1 if the convenience
                                   layer has been initialized successfully. */

  void (*terminateGL)(void); /*!< This member holds a pointer to the
                              function which must be used for destroying
                              the OpenGL context.
                              \sa gr3_terminateGL_WIN_()
                              \sa gr3_terminateGL_GLX_()
                              \sa gr3_terminateGL_CGL_()
                              */

  int fbo_is_initialized; /*!< This flag is set to 1 if a framebuffer
                           object has been created successfully. */

  void (*terminateFBO)(void); /*!< This member holds a pointer to the
                               function which must be used for destroying
                               the framebuffer object.
                               \sa gr3_terminateFBO_ARB_()
                               \sa gr3_terminateFBO_EXT_()
                               */

  char *renderpath_string; /*!< This string holds information on the gr3
                            renderpath.
                            \sa gr3_getrenderpathstring()
                            \sa gr3_appendtorenderpathstring_()
                            */

  GR3_DrawList_t_ *draw_list_; /*!< This member holds a pointer to the first
                                element of the linked draw list or NULL. */

  GR3_MeshList_t_ *mesh_list_; /*!< This member holds a pointer to the mesh
                                list or NULL. */

  int mesh_list_first_free_; /*!< The index of the first free element
                              in the mesh list*/
  int mesh_list_capacity_;   /*!< The number of elements in the mesh
                              list */

  GLfloat view_matrix[4][4];    /*!< The view matrix used to transform vertices
                                 from world to eye space. */
  float vertical_field_of_view; /*!< The vertical field of view, used for the
                                 projection marix */
  float zNear;                  /*!< distance to the near clipping plane */
  float zFar;                   /*!< distance to the far clipping plane */
  float left;                   /*!< distance to the left plane of the orthographic projection */
  float right;                  /*!< distance to the right plane of the orthographic projection */
  float bottom;                 /*!< distance to the bottom plane of the orthographic projection */
  float top;                    /*!< distance to the top plane of the orthographic projection */

  int use_vbo;

  int cylinder_mesh;
  int sphere_mesh;
  int cone_mesh;
  int cube_mesh;
  GLfloat background_color[4];
  GLuint program;
  float camera_x;
  float camera_y;
  float camera_z;
  float center_x;
  float center_y;
  float center_z;
  float up_x;
  float up_y;
  float up_z;
  GLfloat *projection_matrix;
  int quality;
  int projection_type;
  int num_threads;
  int use_software_renderer;
  int option; /* cf. gr_surface_option_t in gr3_gr.c, used for the software renderer */
  int software_renderer_pixmaps_initalised;
  unsigned char *pixmaps[MAX_NUM_THREADS]; /* pixels to be drawn created by the Software Renderer */
  float *depth_buffers[MAX_NUM_THREADS];
  TransparencyVector *transparency_buffer[MAX_NUM_THREADS];
#ifndef NO_THREADS
  thread_t threads[MAX_NUM_THREADS];
#endif
  queue *queues[MAX_NUM_THREADS];
  int last_width;
  int last_height;
  float aspect_override;
  int num_lights;
  GR3_LightSource_t_ light_sources[MAX_NUM_LIGHTS];
  GR3_LightParameter_t_ light_parameters;
  int use_default_light_parameters;
  float clip_xmin;
  float clip_xmax;
  float clip_ymin;
  float clip_ymax;
  float clip_zmin;
  float clip_zmax;
  int alpha_mode; /*Shows the mode used to calculate transparency. 0 means there are no alpha value and everythings
                   * opaque, 1 means there is one alpha value and 2 means there is one alpha value per colorchannel*/
  int use_transparency;
} GR3_ContextStruct_t_;

extern GR3_ContextStruct_t_ context_struct_;

extern int gr3_error_;
extern int gr3_error_line_;
extern const char *gr3_error_file_;

#ifndef DONT_USE_RETURN_ERROR
static int _return_error_helper(int error, int line, const char *file)
{
  if (error)
    {
      gr3_error_ = error;
      gr3_error_line_ = line;
      gr3_error_file_ = file;
    }
  return error;
}
#endif

void gr3_log_(const char *log_message);
void gr3_appendtorenderpathstring_(const char *string);
void gr3_init_convenience(void);
void gr3_terminate_convenience(void);
int gr3_export_html_(const char *filename, int width, int height);
int gr3_export_pov_(const char *filename, int width, int height);
int gr3_getpovray_(char *bitmap, int width, int height, int use_alpha, int ssaa_factor);
int gr3_export_png_(const char *filename, int width, int height);
int gr3_readpngtomemory_(int *pixels, const char *pngfile, int width, int height);
int gr3_export_jpeg_(const char *filename, int width, int height);
int gr3_drawimage_gks_(float xmin, float xmax, float ymin, float ymax, int width, int height);
void gr3_sortindexedmeshdata(int mesh);
#endif