| TGX 1.1.4 A tiny 2D/3D graphics library optimized for 32 bits microcontrollers.

Loading...
Searching...
No Matches
ShaderParams.h
Go to the documentation of this file.
1
5//
6// Copyright 2020 Arvind Singh
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; If not, see <http://www.gnu.org/licenses/>.
20#ifndef _TGX_SHADERSPARAMS_H_
21#define _TGX_SHADERSPARAMS_H_
22
23
24// only C++, no plain C
25#ifdef __cplusplus
26
27
28#include "Misc.h"
29#include "Vec2.h"
30#include "Vec3.h"
31#include "Vec4.h"
32#include "Box2.h"
33#include "Color.h"
34
35#include <stdint.h>
36
37 namespace tgx
38{
39
43 enum Shader
44 {
45 // Shaders for projection type : Perspective or ortho.
46 SHADER_PERSPECTIVE = (1 << 0),
47 SHADER_ORTHO = (1 << 1),
48
49 // Shaders for depth buffer : Zbuffer or no-Zbuffer
50 SHADER_NOZBUFFER = (1 << 2),
51 SHADER_ZBUFFER = (1 << 3),
52
53 // Shaders for shading algorithm: unlit, flat or Gouraud
54 SHADER_FLAT = (1 << 4),
55 SHADER_GOURAUD = (1 << 5),
56 SHADER_UNLIT = (1 << 6),
57
58 // Shaders for texturing mode: no-texture, perspective-correct texture, affine texture
59 SHADER_NOTEXTURE = (1 << 7),
60 SHADER_TEXTURE = (1 << 8),
61 SHADER_TEXTURE_AFFINE = (1 << 9),
62
63 // Shaders for texture quality: nearest, bilinear
64 SHADER_TEXTURE_NEAREST = (1 << 11),
65 SHADER_TEXTURE_BILINEAR = (1 << 12),
66
67 // Shaders for texture wrapping mode: wrap , clamp
68 SHADER_TEXTURE_WRAP_POW2 = (1 << 13),
69 SHADER_TEXTURE_CLAMP = (1 << 14)
70 };
71
72
74 inline constexpr Shader operator|(Shader a1, Shader a2) { return ((Shader)((int)a1 | (int)a2)); }
75
77 inline Shader& operator|=(Shader& a1, Shader a2) { a1 = a1 | a2; return a1; }
78
80 inline constexpr Shader operator&(Shader a1, Shader a2) { return ((Shader)((int)a1 & (int)a2)); }
81
83 inline Shader& operator&=(Shader& a1, Shader a2) { a1 = a1 & a2; return a1; }
84
86 inline constexpr Shader operator~(Shader a) { return (Shader)(~((int)a)); }
87
88
89
90 #define TGX_SHADER_MASK_PROJECTION (SHADER_PERSPECTIVE | SHADER_ORTHO)
91 #define TGX_SHADER_MASK_ZBUFFER (SHADER_NOZBUFFER | SHADER_ZBUFFER)
92 #define TGX_SHADER_MASK_SHADING (SHADER_FLAT | SHADER_GOURAUD | SHADER_UNLIT)
93 #define TGX_SHADER_MASK_TEXTURE (SHADER_NOTEXTURE | SHADER_TEXTURE | SHADER_TEXTURE_AFFINE)
94 #define TGX_SHADER_MASK_TEXTURE_QUALITY (SHADER_TEXTURE_BILINEAR | SHADER_TEXTURE_NEAREST)
95 #define TGX_SHADER_MASK_TEXTURE_MODE (SHADER_TEXTURE_WRAP_POW2 | SHADER_TEXTURE_CLAMP)
96 #define TGX_SHADER_MASK_ALL (TGX_SHADER_MASK_PROJECTION | TGX_SHADER_MASK_ZBUFFER | TGX_SHADER_MASK_SHADING | TGX_SHADER_MASK_TEXTURE | TGX_SHADER_MASK_TEXTURE_QUALITY | TGX_SHADER_MASK_TEXTURE_MODE)
97
98 #define TGX_SHADER_SET_FLAGS(shader_type, flags) { shader_type = (flags); }
99 #define TGX_SHADER_ADD_FLAGS(shader_type, flags) { shader_type |= (flags);}
100 #define TGX_SHADER_REMOVE_FLAGS(shader_type, flags) { shader_type &= ~(flags); }
101 #define TGX_SHADER_HAS_ONE_FLAG(shader_type, flags) (shader_type & (flags))
102 #define TGX_SHADER_HAS_ALL_FLAGS(shader_type, flags) ((shader_type & (flags)) == flags)
103
104 #define TGX_SHADER_HAS_PERSPECTIVE(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_PERSPECTIVE))
105 #define TGX_SHADER_HAS_ORTHO(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_ORTHO))
106 #define TGX_SHADER_HAS_NOZBUFFER(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_NOZBUFFER))
107 #define TGX_SHADER_HAS_ZBUFFER(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_ZBUFFER))
108 #define TGX_SHADER_HAS_FLAT(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_FLAT))
109 #define TGX_SHADER_HAS_GOURAUD(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_GOURAUD))
110 #define TGX_SHADER_HAS_UNLIT(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_UNLIT))
111
113 #define TGX_SHADER_CAN_USE_FLAT_OR_UNLIT(enabled_shader_type, shader_type) \
114 (TGX_SHADER_HAS_UNLIT(enabled_shader_type) ? \
115 (TGX_SHADER_HAS_UNLIT(shader_type) || (TGX_SHADER_HAS_FLAT(enabled_shader_type) && !TGX_SHADER_HAS_GOURAUD(shader_type))) : \
116 TGX_SHADER_HAS_FLAT(enabled_shader_type))
117 #define TGX_SHADER_HAS_NOTEXTURE(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_NOTEXTURE))
118 #define TGX_SHADER_HAS_TEXTURE(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE))
119 #define TGX_SHADER_HAS_TEXTURE_AFFINE(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_AFFINE))
120 #define TGX_SHADER_HAS_TEXTURING_ENABLED(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , (SHADER_TEXTURE | SHADER_TEXTURE_AFFINE)))
121 #define TGX_SHADER_HAS_TEXTURE_NEAREST(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_NEAREST))
122 #define TGX_SHADER_HAS_TEXTURE_BILINEAR(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_BILINEAR))
123 #define TGX_SHADER_HAS_TEXTURE_WRAP_POW2(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_WRAP_POW2))
124 #define TGX_SHADER_HAS_TEXTURE_CLAMP(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_CLAMP))
125
126 #define TGX_SHADER_REMOVE_PERSPECTIVE(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_PERSPECTIVE)
127 #define TGX_SHADER_REMOVE_ORTHO(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_ORTHO)
128 #define TGX_SHADER_REMOVE_NOZBUFFER(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_NOZBUFFER)
129 #define TGX_SHADER_REMOVE_ZBUFFER(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_ZBUFFER)
130 #define TGX_SHADER_REMOVE_FLAT(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_FLAT)
131 #define TGX_SHADER_REMOVE_GOURAUD(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_GOURAUD)
132 #define TGX_SHADER_REMOVE_UNLIT(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_UNLIT)
133 #define TGX_SHADER_REMOVE_NOTEXTURE(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_NOTEXTURE)
134 #define TGX_SHADER_REMOVE_TEXTURE(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE)
135 #define TGX_SHADER_REMOVE_TEXTURE_AFFINE(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_AFFINE)
136 #define TGX_SHADER_REMOVE_TEXTURING_ENABLED(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , (SHADER_TEXTURE | SHADER_TEXTURE_AFFINE))
137 #define TGX_SHADER_REMOVE_TEXTURE_NEAREST(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_NEAREST)
138 #define TGX_SHADER_REMOVE_TEXTURE_BILINEAR(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_BILINEAR)
139 #define TGX_SHADER_REMOVE_TEXTURE_WRAP_POW2(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_WRAP_POW2)
140 #define TGX_SHADER_REMOVE_TEXTURE_CLAMP(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_CLAMP)
141
142 #define TGX_SHADER_ADD_PERSPECTIVE(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_PERSPECTIVE)
143 #define TGX_SHADER_ADD_ORTHO(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_ORTHO)
144 #define TGX_SHADER_ADD_NOZBUFFER(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_NOZBUFFER)
145 #define TGX_SHADER_ADD_ZBUFFER(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_ZBUFFER)
146 #define TGX_SHADER_ADD_FLAT(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_FLAT)
147 #define TGX_SHADER_ADD_GOURAUD(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_GOURAUD)
148 #define TGX_SHADER_ADD_UNLIT(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_UNLIT)
149 #define TGX_SHADER_ADD_NOTEXTURE(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_NOTEXTURE)
150 #define TGX_SHADER_ADD_TEXTURE(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE)
151 #define TGX_SHADER_ADD_TEXTURE_AFFINE(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_AFFINE)
152 #define TGX_SHADER_ADD_TEXTURE_NEAREST(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_NEAREST)
153 #define TGX_SHADER_ADD_TEXTURE_BILINEAR(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_BILINEAR)
154 #define TGX_SHADER_ADD_TEXTURE_WRAP_POW2(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_WRAP_POW2)
155 #define TGX_SHADER_ADD_TEXTURE_CLAMP(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_CLAMP)
156
157
158 //forward declaration
159 template<typename color_t> class Image;
160
161
168 struct RasterizerVec4 : public tgx::fVec4
169 {
170 tgx::RGBf color;
171 tgx::fVec2 T;
172 float A;
173 };
174
175
176
183 template<typename color_t_im, typename color_t_tex, typename ZBUFFER_t, typename BLEND_OP = void> struct RasterizerParams
184 {
185 RGBf facecolor;
186 float opacity;
187 Image<color_t_im> * im;
188 ZBUFFER_t * zbuf;
189 const Image<color_t_tex>* tex;
190 color_t_tex mask_color;
191 int shader_type;
192 float wa;
193 float wb;
194 const BLEND_OP * p_blend_op;
195 };
196
197
198
199}
200
201
202#endif
203
204#endif
205
206
Box2.h
2D box class
tgx::operator|=
Anchor & operator|=(Anchor &a1, Anchor a2)
Enable bitwise |= operator for enum Anchor.
Definition: Box2.h:97
tgx::operator|
constexpr Anchor operator|(Anchor a1, Anchor a2)
Enable bitwise | operator for enum Anchor.
Definition: Box2.h:94
tgx::operator&
constexpr Anchor operator&(Anchor a1, Anchor a2)
Enable bitwise & operator for enum Anchor.
Definition: Box2.h:100
tgx::operator&=
Anchor & operator&=(Anchor &a1, Anchor a2)
Enable bitwise &= operator for enum Anchor.
Definition: Box2.h:103
Color.h
Color classes [RGB565, RGB24, RGB32, RGB64, RGBf, HSV].
Misc.h
Utility/miscellaneous functions used throughout the library.
tgx::operator~
constexpr Shader operator~(Shader a)
Enable bitwise ~ operator for enum.
Definition: ShaderParams.h:86
tgx::Shader
Shader
List of shaders available for 3D graphics.
Definition: ShaderParams.h:44
tgx::SHADER_NOZBUFFER
@ SHADER_NOZBUFFER
disable Z-buffer testing
Definition: ShaderParams.h:50
tgx::SHADER_FLAT
@ SHADER_FLAT
enable flat shading
Definition: ShaderParams.h:54
tgx::SHADER_TEXTURE_NEAREST
@ SHADER_TEXTURE_NEAREST
use point sampling texture mapping
Definition: ShaderParams.h:64
tgx::SHADER_TEXTURE
@ SHADER_TEXTURE
enable perspective-correct texture mapping
Definition: ShaderParams.h:60
tgx::SHADER_UNLIT
@ SHADER_UNLIT
enable unlit shading: no lighting computation
Definition: ShaderParams.h:56
tgx::SHADER_TEXTURE_WRAP_POW2
@ SHADER_TEXTURE_WRAP_POW2
texture has power of 2 dimensions and use wrapping at edges
Definition: ShaderParams.h:68
tgx::SHADER_PERSPECTIVE
@ SHADER_PERSPECTIVE
enable Perspective projection
Definition: ShaderParams.h:46
Vec2.h
2D vector.
Vec3.h
3D vector.
Vec4.h
4D vector.
tgx::RGBf
Color in R,G,B float format.
Definition: Color.h:2405
tgx::Vec2< float >
tgx::Vec4< float >