1 /** @file api_gl.h Doomsday graphics library.
2  * @ingroup gl
3  *
4  * @authors Copyright © 2003-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
5  * @authors Copyright © 2007-2013 Daniel Swanson <danij@dengine.net>
6  * @authors Copyright © 2006 Jamie Jones <jamie_jones_au@yahoo.com.au>
7  *
8  * @par License
9  * GPL: http://www.gnu.org/licenses/gpl.html
10  *
11  * <small>This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by the
13  * Free Software Foundation; either version 2 of the License, or (at your
14  * option) any later version. This program is distributed in the hope that it
15  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
16  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17  * Public License for more details. You should have received a copy of the GNU
18  * General Public License along with this program; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA</small>
21  */
22 
23 #ifndef DOOMSDAY_GL_H
24 #define DOOMSDAY_GL_H
25 
26 #include <de/rect.h>
27 #include "api_map.h"
28 #include "dd_types.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /**
35  * @defgroup gl Graphics Library
36  * @ingroup render
37  */
38 ///@{
39 
40 enum {
41     // Values
42     DGL_ACTIVE_TEXTURE = 1,
43 
44     DGL_CURRENT_COLOR_R,
45     DGL_CURRENT_COLOR_G,
46     DGL_CURRENT_COLOR_B,
47     DGL_CURRENT_COLOR_A,
48     DGL_CURRENT_COLOR_RGBA,
49 
50     DGL_FOG_MODE,
51     DGL_FOG_START,
52     DGL_FOG_END,
53     DGL_FOG_DENSITY,
54     DGL_FOG_COLOR,
55 
56     DGL_LINE_WIDTH,
57     DGL_POINT_SIZE,
58     DGL_ALPHA_LIMIT,
59 
60     // Matrices
61     DGL_MODELVIEW = 0x4000,
62     DGL_PROJECTION,
63     DGL_TEXTURE,
64 
65     // Caps
66     DGL_TEXTURE_2D = 0x5000,
67     DGL_SCISSOR_TEST,
68     DGL_FOG,
69     DGL_MODULATE_TEXTURE,
70     DGL_LINE_SMOOTH,
71     DGL_POINT_SMOOTH,
72     DGL_BLEND,
73     DGL_DEPTH_TEST,
74     DGL_DEPTH_WRITE,
75     DGL_ALPHA_TEST,
76 
77     DGL_TEXTURE0 = 0x5100,
78     DGL_TEXTURE1,
79 
80     // Blending functions
81     DGL_ZERO = 0x6000,
82     DGL_ONE,
83     DGL_DST_COLOR,
84     DGL_ONE_MINUS_DST_COLOR,
85     DGL_DST_ALPHA,
86     DGL_ONE_MINUS_DST_ALPHA,
87     DGL_SRC_COLOR,
88     DGL_ONE_MINUS_SRC_COLOR,
89     DGL_SRC_ALPHA,
90     DGL_ONE_MINUS_SRC_ALPHA,
91     DGL_ADD,
92     DGL_SUBTRACT,
93     DGL_REVERSE_SUBTRACT,
94 
95     // Comparison functions
96     DGL_NEVER = 0x7000,
97     DGL_ALWAYS,
98     DGL_EQUAL,
99     DGL_NOT_EQUAL,
100     DGL_LESS,
101     DGL_GREATER,
102     DGL_LEQUAL,
103     DGL_GEQUAL,
104 
105     // Miscellaneous
106     DGL_MIN_FILTER = 0xF000,
107     DGL_MAG_FILTER,
108     DGL_ANISO_FILTER,
109     DGL_NEAREST,
110     DGL_LINEAR,
111     DGL_NEAREST_MIPMAP_NEAREST,
112     DGL_LINEAR_MIPMAP_NEAREST,
113     DGL_NEAREST_MIPMAP_LINEAR,
114     DGL_LINEAR_MIPMAP_LINEAR,
115     DGL_CLAMP,
116     DGL_CLAMP_TO_EDGE,
117     DGL_REPEAT,
118     DGL_EXP,
119     DGL_EXP2,
120     DGL_NONE,
121     DGL_BACK,
122     DGL_FRONT,
123     DGL_FLUSH_BACKTRACE,
124 };
125 
126 // Types.
127 typedef unsigned char DGLubyte;
128 typedef int           DGLint;
129 typedef unsigned int  DGLuint;
130 typedef int           DGLsizei;
131 typedef double        DGLdouble;
132 typedef unsigned int  DGLenum;
133 
134 /// Texture formats.
135 typedef enum dgltexformat_e {
136     DGL_RGB,
137     DGL_RGBA,
138     DGL_COLOR_INDEX_8,
139     DGL_COLOR_INDEX_8_PLUS_A8,
140     DGL_LUMINANCE,
141     DGL_LUMINANCE_PLUS_A8
142 } dgltexformat_t;
143 
144 /// Primitive types.
145 typedef enum dglprimtype_e {
146     DGL_NO_PRIMITIVE,
147     DGL_LINES,
148     DGL_LINE_STRIP,
149     DGL_LINE_LOOP,
150     DGL_TRIANGLES,
151     DGL_TRIANGLE_FAN,
152     DGL_TRIANGLE_STRIP,
153     DGL_QUADS,
154     DGL_POINTS,
155 } dglprimtype_t;
156 
157 #define DDNUM_BLENDMODES    9
158 
159 /// Blending modes.
160 typedef enum blendmode_e {
161     BM_FIRST = -1,
162     BM_ZEROALPHA = BM_FIRST,
163     BM_NORMAL,
164     BM_ADD,
165     BM_DARK,
166     BM_SUBTRACT,
167     BM_REVERSE_SUBTRACT,
168     BM_MUL,
169     BM_INVERSE,
170     BM_INVERSE_MUL,
171     BM_ALPHA_SUBTRACT,
172     BM_LAST = BM_ALPHA_SUBTRACT
173 } blendmode_t;
174 
175 #define VALID_BLENDMODE(val) ((int)(val) >= BM_FIRST && (int)(val) <= BM_LAST)
176 
177 #define NUM_BLENDMODES       (10)
178 
179 typedef struct dgl_vertex_s {
180     float           xyz[4]; ///< The fourth is padding.
181 } dgl_vertex_t;
182 
183 typedef struct dgl_texcoord_s {
184     float           st[2];
185 } dgl_texcoord_t;
186 
187 typedef struct dgl_color_s {
188     byte            rgba[4];
189 } dgl_color_t;
190 
191 typedef struct {
192     DGLubyte        rgb[3];
193 } dgl_rgb_t;
194 
195 typedef struct {
196     DGLubyte        rgba[4];
197 } dgl_rgba_t;
198 
199 /// 2-vertex with texture coordinates, using floats.
200 typedef struct {
201     float           pos[2];
202     float           tex[2];
203 } dgl_ft2vertex_t;
204 
205 /// 3-vertex with texture coordinates, using floats.
206 typedef struct {
207     float           pos[3];
208     float           tex[2];
209 } dgl_ft3vertex_t;
210 
211 /// 3-vertex with texture coordinates and a color, using floats.
212 typedef struct {
213     float           pos[3];
214     float           tex[2];
215     float           color[4];
216 } dgl_fct3vertex_t;
217 
218 /// Colored 3-vertex, using floats.
219 typedef struct {
220     float           pos[3];
221     float           color[4];
222 } dgl_fc3vertex_t;
223 
224 /// @defgroup scaleModes  Scale Modes
225 /// @ingroup gl
226 /// @{
227 typedef enum {
228     SCALEMODE_FIRST = 0,
229     SCALEMODE_SMART_STRETCH = SCALEMODE_FIRST,
230     SCALEMODE_NO_STRETCH, // Never.
231     SCALEMODE_STRETCH, // Always.
232     SCALEMODE_LAST = SCALEMODE_STRETCH,
233     SCALEMODE_COUNT
234 } scalemode_t;
235 /// @}
236 
237 /**
238  * @defgroup borderedProjectionFlags  Bordered Projection Flags
239  * @ingroup apiFlags
240  * @{
241  */
242 #define BPF_OVERDRAW_MASK   0x1
243 #define BPF_OVERDRAW_CLIP   0x2
244 ///@}
245 
246 typedef struct {
247     int flags;
248     scalemode_t scaleMode;
249     int width, height;
250     int availWidth, availHeight;
251     dd_bool isPillarBoxed; /// @c false: align vertically instead.
252     float scaleFactor;
253 } dgl_borderedprojectionstate_t;
254 
DENG_API_TYPEDEF(GL)255 DENG_API_TYPEDEF(GL)
256 {
257     de_api_t api;
258 
259     int (*Enable)(int cap);
260     void (*Disable)(int cap);
261     void (*PushState)(void);
262     void (*PopState)(void);
263 
264     dd_bool (*GetIntegerv)(int name, int* vec);
265     int (*GetInteger)(int name);
266     dd_bool (*SetInteger)(int name, int value);
267     dd_bool (*GetFloatv)(int name, float* vec);
268     float (*GetFloat)(int name);
269     dd_bool (*SetFloat)(int name, float value);
270 
271     void (*Ortho)(float left, float top, float right, float bottom, float znear, float zfar);
272 
273     /**
274      * Change the current viewport scissor region.
275      *
276      * This function only sets the geometry. To enable the scissor use
277      * DGL_Enable(DGL_SCISSOR_TEST).
278      *
279      * @param rect  Geometry of the new scissor region. Coordinates are
280      *              in viewport space.
281      */
282     void (*SetScissor)(RectRaw const *rect);
283     void (*SetScissor2)(int x, int y, int width, int height);
284 
285     void (*MatrixMode)(DGLenum mode);
286     void (*PushMatrix)(void);
287     void (*PopMatrix)(void);
288     void (*LoadIdentity)(void);
289     void (*LoadMatrix)(float const *matrix4x4);
290 
291     void (*Translatef)(float x, float y, float z);
292     void (*Rotatef)(float angle, float x, float y, float z);
293     void (*Scalef)(float x, float y, float z);
294 
295     void (*Begin)(dglprimtype_t type);
296     void (*End)(void);
297 
298     void (*SetNoMaterial)(void);
299     void (*SetMaterialUI)(world_Material *mat, DGLint wrapS, DGLint wrapT);
300     void (*SetPatch)(patchid_t id, DGLint wrapS, DGLint wrapT);
301     void (*SetPSprite)(world_Material *mat);
302     void (*SetPSprite2)(world_Material *mat, int tclass, int tmap);
303     void (*SetRawImage)(lumpnum_t lumpNum, DGLint wrapS, DGLint wrapT);
304 
305     void (*BlendOp)(int op);
306     void (*BlendFunc)(int param1, int param2);
307     void (*BlendMode)(blendmode_t mode);
308 
309     void (*Color3ub)(DGLubyte r, DGLubyte g, DGLubyte b);
310     void (*Color3ubv)(const DGLubyte* vec);
311     void (*Color4ub)(DGLubyte r, DGLubyte g, DGLubyte b, DGLubyte a);
312     void (*Color4ubv)(const DGLubyte* vec);
313     void (*Color3f)(float r, float g, float b);
314     void (*Color3fv)(const float* vec);
315     void (*Color4f)(float r, float g, float b, float a);
316     void (*Color4fv)(const float* vec);
317 
318     void (*TexCoord2f)(byte target, float s, float t);
319     void (*TexCoord2fv)(byte target, float const *vec);
320 
321     void (*Vertex2f)(float x, float y);
322     void (*Vertex2fv)(const float* vec);
323     void (*Vertex3f)(float x, float y, float z);
324     void (*Vertex3fv)(const float* vec);
325     void (*Vertices2ftv)(int num, const dgl_ft2vertex_t* vec);
326     void (*Vertices3ftv)(int num, const dgl_ft3vertex_t* vec);
327     void (*Vertices3fctv)(int num, const dgl_fct3vertex_t* vec);
328 
329     void (*DrawLine)(float x1, float y1, float x2, float y2, float r, float g, float b, float a);
330 
331     void (*DrawRect)(const RectRaw* rect);
332     void (*DrawRect2)(int x, int y, int w, int h);
333 
334     void (*DrawRectf)(const RectRawf* rect);
335     void (*DrawRectf2)(double x, double y, double w, double h);
336     void (*DrawRectf2Color)(double x, double y, double w, double h, float r, float g, float b, float a);
337     void (*DrawRectf2Tiled)(double x, double y, double w, double h, int tw, int th);
338 
339     void (*DrawCutRectfTiled)(const RectRawf* rect, int tw, int th, int txoff, int tyoff, const RectRawf* cutRect);
340     void (*DrawCutRectf2Tiled)(double x, double y, double w, double h, int tw, int th, int txoff, int tyoff,
341                                double cx, double cy, double cw, double ch);
342 
343     void (*DrawQuadOutline)(const Point2Raw* tl, const Point2Raw* tr, const Point2Raw* br,
344                             const Point2Raw* bl, const float color[4]);
345     void (*DrawQuad2Outline)(int tlX, int tlY, int trX, int trY, int brX, int brY, int blX, int blY,
346                              const float color[4]);
347 
348     DGLuint (*NewTextureWithParams)(dgltexformat_t format, int width, int height,
349                                     const uint8_t* pixels, int flags, int minFilter, int magFilter,
350                                     int anisoFilter, int wrapS, int wrapT);
351 
352     /**
353      * \todo The following routines should not be necessary once materials can
354      * be created dynamically.
355      */
356     int (*Bind)(DGLuint texture);
357 
358     void (*DeleteTextures)(int num, const DGLuint* names);
359 
360     void (*Fogi)(DGLenum property, int value);
361     void (*Fogf)(DGLenum property, float value);
362     void (*Fogfv)(DGLenum property, float const *values);
363 
364     void (*UseFog)(int yes);
365 
366     void (*SetFilter)(dd_bool enable);
367     void (*SetFilterColor)(float r, float g, float b, float a);
368     void (*ConfigureBorderedProjection2)(dgl_borderedprojectionstate_t* bp, int flags, int width, int height, int availWidth, int availHeight, scalemode_t overrideMode, float stretchEpsilon);
369     void (*ConfigureBorderedProjection)(dgl_borderedprojectionstate_t* bp, int flags, int width, int height, int availWidth, int availHeight, scalemode_t overrideMode);
370     void (*BeginBorderedProjection)(dgl_borderedprojectionstate_t* bp);
371     void (*EndBorderedProjection)(dgl_borderedprojectionstate_t* bp);
372 
373     /**
374      * Disable the color filter and clear PostFX (for consoleplayer).
375      */
376     void (*ResetViewEffects)();
377 }
378 DENG_API_T(GL);
379 
380 #ifndef DENG_NO_API_MACROS_GL
381 #define DGL_Enable          _api_GL.Enable
382 #define DGL_Disable         _api_GL.Disable
383 #define DGL_PushState       _api_GL.PushState
384 #define DGL_PopState        _api_GL.PopState
385 #define DGL_GetIntegerv     _api_GL.GetIntegerv
386 #define DGL_GetInteger      _api_GL.GetInteger
387 #define DGL_SetInteger      _api_GL.SetInteger
388 #define DGL_GetFloatv       _api_GL.GetFloatv
389 #define DGL_GetFloat        _api_GL.GetFloat
390 #define DGL_SetFloat        _api_GL.SetFloat
391 #define DGL_Ortho           _api_GL.Ortho
392 #define DGL_SetScissor      _api_GL.SetScissor
393 #define DGL_SetScissor2     _api_GL.SetScissor2
394 #define DGL_MatrixMode      _api_GL.MatrixMode
395 #define DGL_PushMatrix      _api_GL.PushMatrix
396 #define DGL_PopMatrix       _api_GL.PopMatrix
397 #define DGL_LoadIdentity    _api_GL.LoadIdentity
398 #define DGL_LoadMatrix      _api_GL.LoadMatrix
399 #define DGL_Translatef      _api_GL.Translatef
400 #define DGL_Rotatef         _api_GL.Rotatef
401 #define DGL_Scalef          _api_GL.Scalef
402 #define DGL_Begin           _api_GL.Begin
403 #define DGL_End             _api_GL.End
404 #define DGL_SetNoMaterial   _api_GL.SetNoMaterial
405 #define DGL_SetMaterialUI   _api_GL.SetMaterialUI
406 #define DGL_SetPatch        _api_GL.SetPatch
407 #define DGL_SetPSprite      _api_GL.SetPSprite
408 #define DGL_SetPSprite2     _api_GL.SetPSprite2
409 #define DGL_SetRawImage     _api_GL.SetRawImage
410 #define DGL_BlendOp         _api_GL.BlendOp
411 #define DGL_BlendFunc       _api_GL.BlendFunc
412 #define DGL_BlendMode       _api_GL.BlendMode
413 #define DGL_Color3ub        _api_GL.Color3ub
414 #define DGL_Color3ubv       _api_GL.Color3ubv
415 #define DGL_Color4ub        _api_GL.Color4ub
416 #define DGL_Color4ubv       _api_GL.Color4ubv
417 #define DGL_Color3f         _api_GL.Color3f
418 #define DGL_Color3fv        _api_GL.Color3fv
419 #define DGL_Color4f         _api_GL.Color4f
420 #define DGL_Color4fv        _api_GL.Color4fv
421 #define DGL_TexCoord2f      _api_GL.TexCoord2f
422 #define DGL_TexCoord2fv     _api_GL.TexCoord2fv
423 #define DGL_Vertex2f        _api_GL.Vertex2f
424 #define DGL_Vertex2fv       _api_GL.Vertex2fv
425 #define DGL_Vertex3f        _api_GL.Vertex3f
426 #define DGL_Vertex3fv       _api_GL.Vertex3fv
427 #define DGL_Vertices2ftv            _api_GL.Vertices2ftv
428 #define DGL_Vertices3ftv            _api_GL.Vertices3ftv
429 #define DGL_Vertices3fctv           _api_GL.Vertices3fctv
430 #define DGL_DrawLine                _api_GL.DrawLine
431 #define DGL_DrawRect                _api_GL.DrawRect
432 #define DGL_DrawRect2               _api_GL.DrawRect2
433 #define DGL_DrawRectf               _api_GL.DrawRectf
434 #define DGL_DrawRectf2              _api_GL.DrawRectf2
435 #define DGL_DrawRectf2Color         _api_GL.DrawRectf2Color
436 #define DGL_DrawRectf2Tiled         _api_GL.DrawRectf2Tiled
437 #define DGL_DrawCutRectfTiled       _api_GL.DrawCutRectfTiled
438 #define DGL_DrawCutRectf2Tiled      _api_GL.DrawCutRectf2Tiled
439 #define DGL_DrawQuadOutline         _api_GL.DrawQuadOutline
440 #define DGL_DrawQuad2Outline        _api_GL.DrawQuad2Outline
441 #define DGL_NewTextureWithParams    _api_GL.NewTextureWithParams
442 #define DGL_Bind                    _api_GL.Bind
443 #define DGL_DeleteTextures          _api_GL.DeleteTextures
444 #define DGL_Fogi            _api_GL.Fogi
445 #define DGL_Fogf            _api_GL.Fogf
446 #define DGL_Fogfv           _api_GL.Fogfv
447 #define GL_UseFog                   _api_GL.UseFog
448 #define GL_SetFilter                _api_GL.SetFilter
449 #define GL_SetFilterColor           _api_GL.SetFilterColor
450 #define GL_ConfigureBorderedProjection2 _api_GL.ConfigureBorderedProjection2
451 #define GL_ConfigureBorderedProjection  _api_GL.ConfigureBorderedProjection
452 #define GL_BeginBorderedProjection  _api_GL.BeginBorderedProjection
453 #define GL_EndBorderedProjection    _api_GL.EndBorderedProjection
454 #define GL_ResetViewEffects         _api_GL.ResetViewEffects
455 #endif
456 
457 #if defined __DOOMSDAY__ && defined __CLIENT__
458 DENG_USING_API(GL);
459 #endif
460 
461 ///@}
462 
463 #ifdef __cplusplus
464 } // extern "C"
465 #endif
466 
467 #endif /* DOOMSDAY_GL_H */
468