| TGX 1.1.2 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, texture-wrap, texture-clamp
59 SHADER_NOTEXTURE = (1 << 7),
60 SHADER_TEXTURE = (1 << 8),
61
62 // Shaders for texture quality: nearest, bilinear
63 SHADER_TEXTURE_NEAREST = (1 << 11),
64 SHADER_TEXTURE_BILINEAR = (1 << 12),
65
66 // Shaders for texture wrapping mode: wrap , clamp
67 SHADER_TEXTURE_WRAP_POW2 = (1 << 13),
68 SHADER_TEXTURE_CLAMP = (1 << 14)
69 };
70
71
73 inline constexpr Shader operator|(Shader a1, Shader a2) { return ((Shader)((int)a1 | (int)a2)); }
74
76 inline Shader& operator|=(Shader& a1, Shader a2) { a1 = a1 | a2; return a1; }
77
79 inline constexpr Shader operator&(Shader a1, Shader a2) { return ((Shader)((int)a1 & (int)a2)); }
80
82 inline Shader& operator&=(Shader& a1, Shader a2) { a1 = a1 & a2; return a1; }
83
85 inline constexpr Shader operator~(Shader a) { return (Shader)(~((int)a)); }
86
87
88
89 #define TGX_SHADER_MASK_PROJECTION (SHADER_PERSPECTIVE | SHADER_ORTHO)
90 #define TGX_SHADER_MASK_ZBUFFER (SHADER_NOZBUFFER | SHADER_ZBUFFER)
91 #define TGX_SHADER_MASK_SHADING (SHADER_FLAT | SHADER_GOURAUD | SHADER_UNLIT)
92 #define TGX_SHADER_MASK_TEXTURE (SHADER_NOTEXTURE | SHADER_TEXTURE)
93 #define TGX_SHADER_MASK_TEXTURE_QUALITY (SHADER_TEXTURE_BILINEAR | SHADER_TEXTURE_NEAREST)
94 #define TGX_SHADER_MASK_TEXTURE_MODE (SHADER_TEXTURE_WRAP_POW2 | SHADER_TEXTURE_CLAMP)
95 #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)
96
97 #define TGX_SHADER_SET_FLAGS(shader_type, flags) { shader_type = (flags); }
98 #define TGX_SHADER_ADD_FLAGS(shader_type, flags) { shader_type |= (flags);}
99 #define TGX_SHADER_REMOVE_FLAGS(shader_type, flags) { shader_type &= ~(flags); }
100 #define TGX_SHADER_HAS_ONE_FLAG(shader_type, flags) (shader_type & (flags))
101 #define TGX_SHADER_HAS_ALL_FLAGS(shader_type, flags) ((shader_type & (flags)) == flags)
102
103 #define TGX_SHADER_HAS_PERSPECTIVE(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_PERSPECTIVE))
104 #define TGX_SHADER_HAS_ORTHO(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_ORTHO))
105 #define TGX_SHADER_HAS_NOZBUFFER(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_NOZBUFFER))
106 #define TGX_SHADER_HAS_ZBUFFER(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_ZBUFFER))
107 #define TGX_SHADER_HAS_FLAT(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_FLAT))
108 #define TGX_SHADER_HAS_GOURAUD(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_GOURAUD))
109 #define TGX_SHADER_HAS_UNLIT(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_UNLIT))
110
112 #define TGX_SHADER_CAN_USE_FLAT_OR_UNLIT(enabled_shader_type, shader_type) \
113 (TGX_SHADER_HAS_UNLIT(enabled_shader_type) ? \
114 (TGX_SHADER_HAS_UNLIT(shader_type) || (TGX_SHADER_HAS_FLAT(enabled_shader_type) && !TGX_SHADER_HAS_GOURAUD(shader_type))) : \
115 TGX_SHADER_HAS_FLAT(enabled_shader_type))
116 #define TGX_SHADER_HAS_NOTEXTURE(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_NOTEXTURE))
117 #define TGX_SHADER_HAS_TEXTURE(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE))
118 #define TGX_SHADER_HAS_TEXTURE_NEAREST(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_NEAREST))
119 #define TGX_SHADER_HAS_TEXTURE_BILINEAR(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_BILINEAR))
120 #define TGX_SHADER_HAS_TEXTURE_WRAP_POW2(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_WRAP_POW2))
121 #define TGX_SHADER_HAS_TEXTURE_CLAMP(shader_type) (TGX_SHADER_HAS_ONE_FLAG(shader_type , SHADER_TEXTURE_CLAMP))
122
123 #define TGX_SHADER_REMOVE_PERSPECTIVE(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_PERSPECTIVE)
124 #define TGX_SHADER_REMOVE_ORTHO(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_ORTHO)
125 #define TGX_SHADER_REMOVE_NOZBUFFER(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_NOZBUFFER)
126 #define TGX_SHADER_REMOVE_ZBUFFER(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_ZBUFFER)
127 #define TGX_SHADER_REMOVE_FLAT(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_FLAT)
128 #define TGX_SHADER_REMOVE_GOURAUD(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_GOURAUD)
129 #define TGX_SHADER_REMOVE_UNLIT(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_UNLIT)
130 #define TGX_SHADER_REMOVE_NOTEXTURE(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_NOTEXTURE)
131 #define TGX_SHADER_REMOVE_TEXTURE(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE)
132 #define TGX_SHADER_REMOVE_TEXTURE_NEAREST(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_NEAREST)
133 #define TGX_SHADER_REMOVE_TEXTURE_BILINEAR(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_BILINEAR)
134 #define TGX_SHADER_REMOVE_TEXTURE_WRAP_POW2(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_WRAP_POW2)
135 #define TGX_SHADER_REMOVE_TEXTURE_CLAMP(shader_type) TGX_SHADER_REMOVE_FLAGS(shader_type , SHADER_TEXTURE_CLAMP)
136
137 #define TGX_SHADER_ADD_PERSPECTIVE(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_PERSPECTIVE)
138 #define TGX_SHADER_ADD_ORTHO(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_ORTHO)
139 #define TGX_SHADER_ADD_NOZBUFFER(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_NOZBUFFER)
140 #define TGX_SHADER_ADD_ZBUFFER(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_ZBUFFER)
141 #define TGX_SHADER_ADD_FLAT(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_FLAT)
142 #define TGX_SHADER_ADD_GOURAUD(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_GOURAUD)
143 #define TGX_SHADER_ADD_UNLIT(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_UNLIT)
144 #define TGX_SHADER_ADD_NOTEXTURE(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_NOTEXTURE)
145 #define TGX_SHADER_ADD_TEXTURE(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE)
146 #define TGX_SHADER_ADD_TEXTURE_NEAREST(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_NEAREST)
147 #define TGX_SHADER_ADD_TEXTURE_BILINEAR(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_BILINEAR)
148 #define TGX_SHADER_ADD_TEXTURE_WRAP_POW2(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_WRAP_POW2)
149 #define TGX_SHADER_ADD_TEXTURE_CLAMP(shader_type) TGX_SHADER_ADD_FLAGS(shader_type , SHADER_TEXTURE_CLAMP)
150
151
152 //forward declaration
153 template<typename color_t> class Image;
154
155
162 struct RasterizerVec4 : public tgx::fVec4
163 {
164 tgx::RGBf color;
165 tgx::fVec2 T;
166 float A;
167 };
168
169
170
177 template<typename color_t_im, typename color_t_tex, typename ZBUFFER_t, typename BLEND_OP = void> struct RasterizerParams
178 {
179 RGBf facecolor;
180 float opacity;
181 Image<color_t_im> * im;
182 ZBUFFER_t * zbuf;
183 const Image<color_t_tex>* tex;
184 color_t_tex mask_color;
185 int shader_type;
186 float wa;
187 float wb;
188 const BLEND_OP * p_blend_op;
189 };
190
191
192
193}
194
195
196#endif
197
198#endif
199
200
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:85
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
@ SHADER_TEXTURE
enable texture mapping
Definition: ShaderParams.h:60
tgx::SHADER_UNLIT
@ SHADER_UNLIT
enable unlit shading: no lighting computation
Definition: ShaderParams.h:56
tgx::SHADER_TEXTURE_BILINEAR
@ SHADER_TEXTURE_BILINEAR
use bilinear texture sampling
Definition: ShaderParams.h:64
tgx::SHADER_TEXTURE_CLAMP
@ SHADER_TEXTURE_CLAMP
texture have arbitrary dimensions and use clamping 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 >