remove deprecated luminance texture generation
[voxelands-alt.git] / inc / graphics.h
blobebaf6cb7ee4f529cbe36c25ace4f8e5e7be3e7b3
1 #ifndef _GRAPHICS_H_
2 #define _GRAPHICS_H_
4 #include "common.h"
5 #include "file.h"
6 #define _WM_EXPOSE_ALL
7 #include "wm.h"
8 #include "array.h"
10 /* set a pixel colour on an image */
11 #define SETPX(img,x,y,clr) (((uint32_t*)img->pixels)[x+(y*img->w)] = clr)
12 #define SETPXI(img,i,clr) (((uint32_t*)img->pixels)[i] = clr)
13 /* get a pixel colour from an image */
14 #define GETPX(img,x,y) (((uint32_t*)img->pixels)[x+(y*img->w)])
15 #define GETPXI(img,i) (((uint32_t*)img->pixels)[i])
17 #define SGN(x) ((x<0)?-1:((x>0)?1:0))
19 /* convert colour struct to pixel */
20 #define C2P(c) (((((uint32_t)((c).a))&0x000000FF)<<24)|((((uint32_t)((c).r))&0x000000FF)<<16)|((((uint32_t)((c).g))&0x000000FF)<<8)|((((uint32_t)((c).b))&0x000000FF)))
22 /* get the value of one component of a pixel */
23 #define RED(clr) (((clr)&0x00FF0000)>>16)
24 #define GREEN(clr) (((clr)&0x0000FF00)>>8)
25 #define BLUE(clr) (((clr)&0x000000FF))
26 #define ALPHA(clr) (((clr)&0xFF000000)>>24)
28 #define AFLOAT(__a,__i) (((float*)(__a))[__i])
29 #define AUINT(__a,__i) (((unsigned int*)(__a))[__i])
31 #define MATOPT_NONE 0x00
32 #define MATOPT_GET 0x01
33 #define MATOPT_BFCULL 0x02 /* back face culling */
34 #define MATOPT_UPNORMAL 0x04 /* forces normals to up vector */
35 #define MATOPT_ALPHA_BLEND 0x08 /* use alpha blending GL_ONE_MINUS_SRC_ALPHA */
36 #define MATOPT_ADDITIVE_BLEND 0x10 /* use additive blending GL_ONE */
37 #define MATOPT_ALPHA_TEST 0x20 /* semi-caps alpha values (signed distance field) */
38 #define MATOPT_SDF_ALPHA 0x40 /* pseudo signed distance field */
40 #define RD2_NONE 0x00
41 #define RD2_LINE 0x01
42 #define RD2_QUAD 0x02
43 #define RD2_TEXT 0x03
45 #define UI_ALIGN_LEFT 0
46 #define UI_ALIGN_CENTRE -1
47 #define UI_ALIGN_RIGHT -2
48 #define UI_ALIGN_TOP 0
49 #define UI_ALIGN_MIDDLE -1
50 #define UI_ALIGN_BOTTOM -2
51 #define UI_ALIGN_MOUSE -3
52 #define UI_ALIGN_MOUSE_CENTRE -4
53 #define UI_DEFAULT -5
55 #define TB_OPT_NONE 0x00
56 #define TB_OPT_NUMERIC 0x01
58 #ifndef _HAVE_FCOLOUR_TYPE
59 #define _HAVE_FCOLOUR_TYPE
60 typedef struct fcolour_s {
61 float r;
62 float g;
63 float b;
64 float a;
65 } fcolour_t;
66 #endif
68 #ifndef _HAVE_IMAGE_TYPE
69 #define _HAVE_IMAGE_TYPE
70 typedef struct image_s {
71 int w;
72 int h;
73 uint8_t* pixels;
74 } image_t;
75 #endif
77 #ifndef _HAVE_TEXTURE_TYPE
78 #define _HAVE_TEXTURE_TYPE
79 typedef struct texture_s {
80 struct texture_s *prev;
81 struct texture_s *next;
82 GLuint glid;
83 int state;
84 int id;
85 char name[100];
86 int w;
87 int h;
88 GLfloat xf;
89 GLfloat yf;
90 image_t *px;
91 } texture_t;
92 #endif
94 #ifndef _HAVE_MATERIAL_TYPE
95 #define _HAVE_MATERIAL_TYPE
96 typedef struct material_s {
97 struct material_s *prev;
98 struct material_s *next;
99 int id;
100 char name[100];
101 uint32_t options;
102 texture_t *tex;
103 float shininess;
104 float reflectivity;
105 float bumpiness;
106 } material_t;
107 #endif
109 #ifndef _HAVE_VAO_TYPE
110 #define _HAVE_VAO_TYPE
111 typedef struct vao_s {
112 int state;
113 GLuint list;
114 GLuint vertices;
115 GLuint normals;
116 GLuint texcoords;
117 GLuint indices;
118 } vao_t;
119 #endif
121 #ifndef _HAVE_MESH_TYPE
122 #define _HAVE_MESH_TYPE
123 typedef struct mesh_s {
124 material_t *mat;
125 vao_t vao;
126 colour_t *col;
127 array_t *v; /* vertices */
128 array_t *n; /* normals */
129 array_t *t; /* texcoords */
130 array_t *i; /* indices */
131 array_t *w; /* weights */
132 array_t *m; /* weight map */
133 GLenum mode;
134 int option;
135 float radius;
136 aabox_t bounds;
137 } mesh_t;
138 #endif
140 #ifndef _HAVE_ANIM_STATE_TYPE
141 #define _HAVE_ANIM_STATE_TYPE
142 typedef struct anim_state_s {
143 int skeleton;
144 int frame;
145 float value;
146 } anim_state_t;
147 #endif
149 #ifndef _HAVE_WEIGHT_TYPE
150 #define _HAVE_WEIGHT_TYPE
151 typedef struct weight_s {
152 int joint;
153 float bias;
154 v3_t pos;
155 } __attribute__((packed)) weight_t;
156 #endif
158 #ifndef _HAVE_JOINT_TYPE
159 #define _HAVE_JOINT_TYPE
160 typedef struct joint_s {
161 v3_t pos;
162 quaternion_t rot;
163 } __attribute__((packed)) joint_t;
164 #endif
166 #ifndef _HAVE_SKELETON_TYPE
167 #define _HAVE_SKELETON_TYPE
168 typedef struct skeleton_s {
169 char* name;
170 float fps;
171 array_t *frames;
172 } __attribute__((packed)) skeleton_t;
173 #endif
175 #ifndef _HAVE_PRE_VERTEX_TYPE
176 #define _HAVE_PRE_VERTEX_TYPE
177 typedef struct pre_vertex_s {
178 array_t *vertex;
179 GLuint vbo;
180 } pre_vertex_t;
181 #endif
183 #ifndef _HAVE_FRAME_TYPE
184 #define _HAVE_FRAME_TYPE
185 typedef struct frame_s {
186 array_t *joints;
187 array_t *pre_vertex;
188 } frame_t;
189 #endif
191 #ifndef _HAVE_MODEL_TYPE
192 #define _HAVE_MODEL_TYPE
193 struct object_s;
195 typedef struct model_s {
196 struct model_s *prev;
197 struct model_s *next;
198 char* name;
199 void *data;
200 array_t *meshes;
201 array_t *skeletons;
202 int (*step)(struct object_s *);
203 } model_t;
204 #endif
206 #ifndef _HAVE_OBJECT_TYPE
207 #define _HAVE_OBJECT_TYPE
208 typedef struct object_s {
209 struct object_s *prev;
210 struct object_s *next;
211 model_t *m;
212 array_t *meshes;
213 int ignore;
214 int drop;
215 int id;
216 v3_t pos;
217 v3_t rot;
218 v3_t scale;
219 float radius;
220 aabox_t bounds;
221 array_t *abounds;
222 anim_state_t anim;
223 } object_t;
224 #endif
226 #ifndef _HAVE_OBJECT2D_TYPE
227 #define _HAVE_OBJECT2D_TYPE
228 typedef struct object2d_s {
229 struct object2d_s *prev;
230 struct object2d_s *next;
231 int builtin;
232 array_t *meshes;
233 v3_t pos;
234 } object2d_t;
235 #endif
237 #ifndef _HAVE_SHADER_TYPE
238 #define _HAVE_SHADER_TYPE
239 typedef struct shader_s {
240 struct shader_s *prev;
241 struct shader_s *next;
242 char name[256];
243 GLuint program;
244 GLuint vert;
245 GLuint frag;
246 GLuint geom;
247 uint8_t active;
248 } shader_t;
249 #endif
251 #ifndef _HAVE_FONTCHAR_TYPE
252 #define _HAVE_FONTCHAR_TYPE
253 typedef struct fontchar_s {
254 float w;
255 float h;
256 float a;
257 float x[2];
258 float y[2];
259 } fontchar_t;
260 #endif
262 #ifndef _HAVE_FONTPAGE_TYPE
263 #define _HAVE_FONTPAGE_TYPE
264 typedef struct fontpage_s {
265 GLubyte *pixels;
266 GLuint glid;
267 array_t sizes;
268 } fontpage_t;
269 #endif
271 #ifndef _HAVE_FONT_TYPE
272 #define _HAVE_FONT_TYPE
273 typedef struct font_s {
274 char* token;
275 char* file;
276 array_t pages;
277 } font_t;
278 #endif
280 #ifndef _HAVE_RENDERTEXT_TYPE
281 #define _HAVE_RENDERTEXT_TYPE
282 typedef struct rendertext_s {
283 uint32_t page;
284 uint32_t state;
285 GLuint glid;
286 GLuint vao;
287 GLuint vbo[2];
288 array_t v;
289 array_t t;
290 } rendertext_t;
291 #endif
293 #ifndef _HAVE_TEXTBUFFER_TYPE
294 #define _HAVE_TEXTBUFFER_TYPE
295 typedef struct textbuffer_s {
296 uint32_t* str;
297 uint32_t length;
298 uint32_t size;
299 int mx;
300 int my;
301 int mc;
302 float x;
303 float y;
304 float nx;
305 float ny;
306 int cw;
307 int ch;
308 font_t *font;
309 int font_size;
310 uint32_t options;
311 array_t data;
312 } textbuffer_t;
313 #endif
315 /* defined in image.c */
316 image_t *image_load(char* type, char* file);
317 image_t *image_load_frommem(file_t *f);
318 image_t *image_load_fromscreen(int x, int y, int w, int h, int alpha);
319 image_t *image_rgba(int x, int y, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
320 image_t *image_copy(image_t *img);
321 void image_clear(image_t *img);
322 void image_draw_rect(image_t *img, colour_t *c, int width, rect_t *area);
323 void image_draw_line(image_t *img, colour_t *c, int width, rect_t *from, rect_t *to);
324 void image_fill_rect(image_t *img, colour_t *c, rect_t *area);
325 void image_copy_area(image_t *dest, image_t *src, rect_t *to, rect_t *from);
326 void image_blit(image_t *dest, image_t *src, rect_t *to, rect_t *from);
327 void image_colourise(image_t *img, colour_t *c, rect_t *area);
329 /* defined in image_bmp.c */
330 int image_is_bmp(file_t *f);
331 int image_load_bmp(file_t *f, image_t *p);
332 int image_save_bmp(image_t *p, char* file);
334 /* defined in image_png.c */
335 int image_is_png(file_t *f);
336 int image_load_png(file_t *f, image_t *p);
337 int image_save_png(image_t *p, char* file);
339 /* defined in image_tga.c */
340 int image_is_tga(file_t *f);
341 int image_load_tga(file_t *f, image_t *p);
342 int image_save_tga(image_t *p, char* file);
344 /* defined in material.c */
345 material_t *mat_create(void);
346 void mat_free(material_t *mat);
347 material_t *mat_from_tex(texture_t *tex);
348 material_t *mat_from_pixels(image_t *p);
349 material_t *mat_update_pixels(material_t *mat, image_t *p);
350 material_t *mat_from_image(char* type, char* file);
351 material_t *mat_from_colour(colour_t *c);
352 int mat_bind_with_opts(GLuint tex, uint32_t options);
353 void mat_use(material_t *mat, shader_t *shader);
354 void mat_shininess(material_t *mat, float shininess, float reflectivity);
355 void mat_bumpiness(material_t *mat, float bumpiness);
356 void mat_name(material_t *mat, char* name);
357 void mat_tex(material_t *mat, texture_t *tex);
358 void mat_tex_file(material_t *mat, char* type, char* file);
359 uint32_t mat_options(material_t *mat, uint32_t opt);
361 /* defined in texture.c */
362 int tex_generate(texture_t *tex);
363 texture_t *tex_create(void);
364 void tex_free(texture_t *tex);
365 texture_t *tex_from_image(char* type, char* file);
366 texture_t *tex_from_pixels(image_t *px);
367 texture_t *tex_update_pixels(texture_t *tex, image_t *px);
368 texture_t *tex_from_rgba(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a);
370 /* defined in opengl.c */
371 int opengl_has_anisotropic(void);
372 float opengl_max_anisotropic(void);
373 int opengl_has_bilinear(void);
374 int opengl_has_trilinear(void);
375 int opengl_has_mipmap(void);
376 int opengl_has_particles(void);
377 int opengl_particles_max(void);
378 int opengl_has_psdf(void);
379 char* opengl_error_string(GLenum e);
381 /* defined in mesh.c */
382 mesh_t *mesh_create(GLenum mode);
383 void mesh_free(mesh_t *m);
384 mesh_t *mesh_create_material(material_t *mat);
385 mesh_t *mesh_create_colour(colour_t *c);
386 mesh_t *mesh_copy(mesh_t *om);
387 int mesh_push_point(mesh_t *m, v3_t *v1);
388 int mesh_push_polypoint(mesh_t *m, v3_t *v, v2_t *t);
389 int mesh_push_polypoint_n(mesh_t *m, v3_t *v, v3_t *n, v2_t *t);
390 void mesh_push_poly(mesh_t *m, v3_t *v1, v3_t *v2, v3_t *v3);
391 void mesh_push_quad(mesh_t *m, v3_t *v1, v3_t *v2, v3_t *v3, v3_t *v4);
392 void mesh_calc_normals(mesh_t *m);
394 /* defined in object.c */
395 object_t *object_create(v3_t *pos);
396 void object_rotate(object_t *o, GLfloat x, GLfloat y, GLfloat z);
397 void object_scale(object_t *o, GLfloat x, GLfloat y, GLfloat z);
398 void object_add_material(object_t *o, material_t *mat);
399 void object_add_colour(object_t *o, colour_t *c);
400 void object_add_mat_poly(object_t *o, material_t *mat, v3_t *v1, v3_t *v2, v3_t *v3);
401 void object_add_mat_quad(object_t *o, material_t *mat, v3_t *v1, v3_t *v2, v3_t *v3, v3_t *v4);
402 void object_set_skeleton(object_t *o, int skel);
403 void object_set_frame(object_t *o, int frame);
404 int object_advance_frame(object_t *o, float dtime);
405 void object_calc_normals(object_t *o);
407 /* defined in render.c */
408 void render_pre(void);
409 void render_post(void);
410 float client_dtime(void);
412 /* defined in render2d.c */
413 void render2d_line(material_t *m, float x, float y, float ex, float ey);
414 void render2d_quad_mat(material_t *m, float x, float y, float w, float h);
415 void render2d_text(float x, float y, int w, int h, int font, int size, char* str);
416 void render2d_textbuffer(textbuffer_t *t);
417 void render2d(void);
419 /* defined in render3d.c */
420 object_t *render3d_object_find(int id);
421 void render3d_object_free(object_t *o);
422 object_t *render3d_object_create(void);
423 object_t *render3d_model(model_t *mod, v3_t *pos);
424 object_t *render3d_mesh(mesh_t *m, v3_t *pos);
425 object_t *render3d_cube(float scale, v3_t *pos, material_t *mat);
426 void render3d_set_projection_matrix(void);
427 void render3d(void);
429 /* defined in shader.c */
430 shader_t *shader_create(char* name);
431 void shader_free(shader_t *s);
432 int shader_attribute(shader_t *s, int att, char* name);
433 int shader_uniform_int(shader_t *s, char* name, int value);
434 int shader_uniform_float(shader_t *s, char* name, float value);
435 int shader_uniform_v2(shader_t *s, char* name, v2_t *value);
436 int shader_uniform_v3(shader_t *s, char* name, v3_t *value);
437 int shader_uniform_matrix(shader_t *s, char* name, matrix_t *value);
438 int shader_enable(shader_t *s);
439 int shader_disable(shader_t *s);
441 /* defined in font.c */
442 int font_load(char* file, char* token);
443 font_t *font_get(int id);
444 font_t *font_find(char* token);
445 int font_get_id(char* token);
447 /* defined in font_ttf.c */
448 int font_load_ttf(font_t *f, char* file);
449 int font_ttf_get_char(font_t *f, uint32_t ch, fontchar_t **fc, GLuint *glid);
451 /* defined in textbuffer.c */
452 void textbuffer_init(textbuffer_t *t, font_t *f, int size, float x, float y, int max_x, int max_y, int max_c, uint32_t options);
453 textbuffer_t *textbuffer_create(font_t *f, int size, float x, float y, int max_x, int max_y, int max_c, uint32_t options);
454 void textbuffer_clear(textbuffer_t *t);
455 void textbuffer_free(textbuffer_t *t);
456 int textbuffer_adjust(textbuffer_t *t, font_t *f, int size, float x, float y, int max_x, int max_y, int max_c, uint32_t options);
457 int textbuffer_addstr(textbuffer_t *t, char* str);
458 int textbuffer_addchar(textbuffer_t *t, uint32_t ch);
459 int textbuffer_get_dimensions(textbuffer_t *t, int sizes[2]);
461 #endif