1 #ifndef _MATERIAL_H
2 #define _MATERIAL_H
3 
4 #include "graphics/grinternal.h"
5 #include "model/model.h"
6 
7 #include <array>
8 
9 enum class ComparisionFunction
10 {
11 	Never,
12 	Always,
13 	Less,
14 	Greater,
15 	Equal,
16 	NotEqual,
17 	LessOrEqual,
18 	GreaterOrEqual
19 };
20 
21 enum class StencilOperation {
22 	Keep,
23 	Zero,
24 	Replace,
25 	Increment,
26 	IncrementWrap,
27 	Decrement,
28 	DecrementWrap,
29 	Invert
30 };
31 
32 class material
33 {
34 public:
35 	struct clip_plane
36 	{
37 		bool enabled;
38 		vec3d normal;
39 		vec3d position;
40 	};
41 
42 	enum texture_type {
43 		TEX_TYPE_NORMAL,
44 		TEX_TYPE_XPARENT,
45 		TEX_TYPE_AABITMAP,
46 		TEX_TYPE_INTERFACE
47 	};
48 
49 	struct StencilFunc {
50 		ComparisionFunction compare = ComparisionFunction::Always;
51 		int ref = 1;
52 		uint32_t mask = 0xFFFFFFFF;
53 	};
54 
55 	struct StencilOp {
56 		StencilOperation stencilFailOperation = StencilOperation::Keep;
57 		StencilOperation depthFailOperation = StencilOperation::Keep;
58 		StencilOperation successOperation = StencilOperation::Keep;
59 	};
60 
61 	static const size_t NUM_BUFFER_BLENDS = 8;
62 
63  private:
64 	shader_type Sdr_type;
65 
66 	int Texture_maps[TM_NUM_TYPES];
67 	texture_type Tex_type;
68 
69 	clip_plane Clip_params;
70 	int Texture_addressing;
71 	gr_zbuffer_type Depth_mode;
72 
73 	gr_alpha_blend Blend_mode;
74 	bool Has_buffer_blends = false;
75 	std::array<gr_alpha_blend, NUM_BUFFER_BLENDS> Buffer_blend_mode;
76 
77 	bool Cull_mode;
78 	int Fill_mode;
79 	vec4 Clr;
80 	float Clr_scale;
81 	int Depth_bias;
82 	bvec4 Color_mask;
83 	bool Stencil_test = false;
84 	uint32_t Stencil_mask = 0xFF;
85 	StencilFunc Stencil_func;
86 	StencilOp Front_stencil_op;
87 	StencilOp Back_stencil_op;
88 
89 protected:
90 	void set_shader_type(shader_type init_sdr_type = SDR_TYPE_NONE);
91 public:
92 	material();
93 
94 	int get_shader_handle() const;
95 	virtual uint get_shader_flags() const;
96 
97 	void set_texture_map(int tex_type, int texture_num);
98 	int get_texture_map(int tex_type) const;
99 	bool is_textured() const;
100 
101 	void set_texture_type(texture_type t_type);
102 	int get_texture_type() const;
103 
104 	bool is_clipped() const;
105 	void set_clip_plane(const vec3d &normal, const vec3d &position);
106 	void set_clip_plane();
107 	const clip_plane& get_clip_plane() const;
108 
109 	void set_texture_addressing(int addressing);
110 	int get_texture_addressing() const;
111 
112 	void set_depth_mode(gr_zbuffer_type mode);
113 	gr_zbuffer_type get_depth_mode() const;
114 
115 	void set_cull_mode(bool mode);
116 	bool get_cull_mode() const;
117 
118 	void set_fill_mode(int mode);
119 	int get_fill_mode() const;
120 
121 	void set_color_mask(bool red, bool green, bool blue, bool alpha);
122 	const bvec4& get_color_mask() const;
123 
124 	void set_blend_mode(gr_alpha_blend mode);
125 	void set_blend_mode(int buffer, gr_alpha_blend mode);
126 	bool has_buffer_blend_modes() const;
127 	gr_alpha_blend get_blend_mode(int buffer = 0) const;
128 
129 	void set_depth_bias(int bias);
130 	int get_depth_bias() const;
131 
132 	void set_color(float red, float green, float blue, float alpha);
133 	void set_color(int r, int g, int b, int a);
134 	void set_color(color &clr_in);
135 	const vec4& get_color() const;
136 
137 	void set_color_scale(float scale);
138 	float get_color_scale() const;
139 
140 	void set_stencil_test(bool stencil);
141 	bool is_stencil_enabled() const;
142 
143 	void set_stencil_mask(uint32_t mask);
144 	uint32_t get_stencil_mask() const;
145 
146 	void set_stencil_func(ComparisionFunction compare, int ref, uint32_t mask);
147 	const StencilFunc& get_stencil_func() const;
148 
149 	void set_stencil_op(StencilOperation stencilFailOperation,
150 						StencilOperation depthFailOperation,
151 						StencilOperation successOperation);
152 
153 	void set_front_stencil_op(StencilOperation stencilFailOperation,
154 							  StencilOperation depthFailOperation,
155 							  StencilOperation successOperation);
156 	const StencilOp& get_front_stencil_op() const;
157 
158 	void set_back_stencil_op(StencilOperation stencilFailOperation,
159 							  StencilOperation depthFailOperation,
160 							  StencilOperation successOperation);
161 	const StencilOp& get_back_stencil_op() const;
162 };
163 
164 class model_material : public material
165 {
166  public:
167 	struct fog
168 	{
169 		bool enabled = false;
170 		int r = 0;
171 		int g = 0;
172 		int b = 0;
173 		float dist_near = -1.0f;
174 		float dist_far = -1.0f;
175 	};
176 
177  private:
178 	bool Desaturate = false;
179 
180 	bool Shadow_casting = false;
181 	bool Shadow_receiving = false;
182 	bool Batched = false;
183 
184 	bool Deferred = false;
185 	bool HDR = false;
186 	bool lighting = false;
187 	float Light_factor = 1.0f;
188 
189 	int Center_alpha = 0;
190 
191 	int Animated_effect = -1;
192 	float Animated_timer = 0.0f;
193 
194 	float Thrust_scale = -1.0f;
195 
196 	bool Team_color_set = false;
197 	team_color Tm_color;
198 
199 	bool Normal_alpha = false;
200 	float Normal_alpha_min = 0.0f;
201 	float Normal_alpha_max = 1.0f;
202 
203 	fog Fog_params;
204 
205 	float Outline_thickness = -1.0f;
206 
207 public:
208 	model_material();
209 
210 	void set_desaturation(bool enabled);
211 	bool is_desaturated() const;
212 
213 	void set_shadow_casting(bool enabled);
214 	bool is_shadow_casting() const;
215 
216 	void set_shadow_receiving(bool enabled);
217 	bool is_shadow_receiving() const;
218 
219 	void set_light_factor(float factor);
220 	float get_light_factor() const;
221 
222 	void set_lighting(bool mode);
223 	bool is_lit() const;
224 
225 	void set_deferred_lighting(bool enabled);
226 	void set_high_dynamic_range(bool enabled);
227 
228 	void set_center_alpha(int center_alpha);
229 	int get_center_alpha() const;
230 
231 	void set_thrust_scale(float scale = -1.0f);
232 	float get_thrust_scale() const;
233 
234 	void set_team_color(const team_color &Team_clr);
235 	void set_team_color();
236 	const team_color& get_team_color() const;
237 
238 	void set_animated_effect(int effect, float time);
239 	void set_animated_effect();
240 	int get_animated_effect() const;
241 	float get_animated_effect_time() const;
242 
243 	void set_normal_alpha(float min, float max);
244 	void set_normal_alpha();
245 	bool is_normal_alpha_active() const;
246 	float get_normal_alpha_min() const;
247 	float get_normal_alpha_max() const;
248 
249 	void set_outline_thickness(float thickness = -1.0f);
250 	float get_outline_thickness() const;
251 	bool uses_thick_outlines() const;
252 
253 	void set_batching(bool enabled);
254 	bool is_batched() const;
255 
256 	uint get_shader_flags() const override;
257 
258 	void set_fog(int r, int g, int b, float near, float far);
259 	void set_fog();
260 	bool is_fogged() const;
261 	const fog& get_fog() const;
262 };
263 
264 class particle_material : public material
265 {
266 	bool Point_sprite;
267 public:
268 	particle_material();
269 
270 	void set_point_sprite_mode(bool enabled);
271 	bool get_point_sprite_mode();
272 
273 	uint get_shader_flags() const override;
274 };
275 
276 class distortion_material: public material
277 {
278 	bool Thruster_render;
279 public:
280 	distortion_material();
281 
282 	void set_thruster_rendering(bool enabled);
283 	bool get_thruster_rendering();
284 };
285 
286 class shield_material : public material
287 {
288 	matrix Impact_orient;
289 	vec3d Impact_pos;
290 	float Impact_radius;
291 public:
292 	shield_material();
293 
294 	void set_impact_transform(matrix &orient, vec3d &pos);
295 	void set_impact_radius(float radius);
296 
297 	const matrix& get_impact_orient();
298 	const vec3d& get_impact_pos();
299 	float get_impact_radius();
300 };
301 
302 class movie_material : public material {
303 	int Ytex = -1;
304 	int Utex = -1;
305 	int Vtex = -1;
306  public:
307 	movie_material();
308 
309 	int getYtex() const;
310 	int getUtex() const;
311 	int getVtex() const;
312 
313 	void setYtex(int _Ytex);
314 	void setUtex(int _Utex);
315 	void setVtex(int _Vtex);
316 };
317 
318 class batched_bitmap_material : public material {
319   public:
320 	batched_bitmap_material();
321 };
322 
323 class nanovg_material : public material {
324  public:
325 	nanovg_material();
326 };
327 
328 class decal_material : public material {
329   public:
330 	decal_material();
331 
332 	uint get_shader_flags() const override;
333 };
334 
335 class interface_material : public material {
336 	vec2d offset;
337 
338 	float horizontalSwipeOff = -1.0f;
339 
340   public:
341 	interface_material();
342 
343 	void set_offset(const vec2d& new_offset);
344 	vec2d get_offset() const;
345 
346 	void set_horizontal_swipe(float hor_offset);
347 	float get_horizontal_swipe() const;
348 };
349 
350 gr_alpha_blend material_determine_blend_mode(int base_bitmap, bool is_transparent);
351 
352 gr_zbuffer_type material_determine_depth_mode(bool depth_testing, bool is_transparent);
353 
354 void material_set_interface(material* mat_info, int texture, bool blended, float alpha);
355 void material_set_rocket_interface(interface_material* mat_info,
356 	int texture,
357 	const vec2d& offset,
358 	float horizontal_swipe);
359 void material_set_unlit(material* mat_info, int texture, float alpha, bool blending, bool depth_testing);
360 void material_set_unlit_emissive(material* mat_info, int texture, float alpha, float color_scale);
361 void material_set_unlit_color(material* mat_info, int texture, color *clr, bool blending, bool depth_testing);
362 void material_set_unlit_color(material* mat_info, int texture, color *clr, float alpha, bool blending, bool depth_testing);
363 void material_set_unlit_volume(particle_material* mat_info, int texture, bool point_sprites);
364 void material_set_distortion(distortion_material *mat_info, int texture, bool thruster);
365 void material_set_movie(movie_material *mat_info, int y_bm, int u_bm, int v_bm);
366 void material_set_batched_bitmap(batched_bitmap_material* mat_info, int base_tex, float alpha, float color_scale);
367 void material_set_nanovg(nanovg_material* mat_info, int base_tex);
368 void material_set_decal(material* mat_info, int diffuse_tex, int glow_tex, int normal_tex);
369 
370 #endif
371