1 /**
2  * @file
3  * @brief
4  */
5 
6 /*
7 All original material Copyright (C) 2002-2013 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 
24 */
25 
26 #pragma once
27 
28 #include "r_program.h"
29 #include "r_material.h"
30 #include "r_framebuffer.h"
31 #include "r_light.h"
32 #include "r_image.h"
33 
34 /**
35  * @brief Center position of skybox along z-axis. This is used to make sure we see only the inside of Skybox.
36  * @sa R_DrawStarfield
37  * @sa R_Setup2D
38  */
39 #define SKYBOX_DEPTH -9999.0f
40 
41 /* vertex arrays are used for many things */
42 #define GL_ARRAY_LENGTH_CHUNK 4096
43 extern const vec2_t default_texcoords[4];
44 
45 /** @brief texunits maintain multitexture state */
46 typedef struct gltexunit_s {
47 	bool enabled;		/**< GL_TEXTURE_2D on / off */
48 	GLenum texture;		/**< e.g. GL_TEXTURE0 */
49 	GLint texnum;		/**< e.g 123 */
50 	GLenum texenv;		/**< e.g. GL_MODULATE */
51 	GLfloat* texcoord_array;
52 	/* Size of the array above - it's dynamically reallocated */
53 	int array_size;
54 } gltexunit_t;
55 
56 #define MAX_GL_TEXUNITS		6
57 
58 /* these are defined for convenience */
59 #define texunit_0	        r_state.texunits[0]
60 #define texunit_1	        r_state.texunits[1]
61 #define texunit_2	        r_state.texunits[2]
62 #define texunit_3	        r_state.texunits[3]
63 #define texunit_4	        r_state.texunits[4]
64 #define texunit_5	        r_state.texunits[5]
65 
66 #define texunit_diffuse			texunit_0
67 #define texunit_lightmap		texunit_1
68 #define texunit_deluxemap		texunit_2
69 #define texunit_normalmap		texunit_3
70 #define texunit_glowmap			texunit_4
71 #define texunit_specularmap		texunit_5
72 #define texunit_roughnessmap	texunit_2
73 
74 #define COMPONENTS_VERTEX_ARRAY3D	3
75 #define COMPONENTS_VERTEX_ARRAY2D	2
76 #define COMPONENTS_COLOR_ARRAY		4
77 #define COMPONENTS_INDEX_ARRAY		1
78 #define COMPONENTS_NORMAL_ARRAY		3
79 #define COMPONENTS_TANGENT_ARRAY	4
80 #define COMPONENTS_TEXCOORD_ARRAY	2
81 
82 #define DOWNSAMPLE_PASSES	5
83 #define DOWNSAMPLE_SCALE	2
84 
85 #define fbo_screen			nullptr
86 #define fbo_render			r_state.renderBuffer
87 #define fbo_bloom0			r_state.bloomBuffer0
88 #define fbo_bloom1			r_state.bloomBuffer1
89 
90 #define default_program		nullptr
91 
92 struct mAliasMesh_s;
93 
94 typedef struct rstate_s {
95 	bool fullscreen;
96 
97 	/* arrays */
98 	GLfloat* vertex_array_3d;
99 	GLshort* vertex_array_2d;
100 	GLfloat* color_array;
101 	GLint* index_array;
102 	GLfloat* normal_array;
103 	GLfloat* tangent_array;
104 	GLfloat* next_vertex_array_3d;
105 	GLfloat* next_normal_array;
106 	GLfloat* next_tangent_array;
107 
108 	/* Size of all arrays above - it's dynamically reallocated */
109 	int array_size;
110 
111 	/* multitexture texunits */
112 	gltexunit_t texunits[MAX_GL_TEXUNITS];
113 
114 	/* texunit in use */
115 	gltexunit_t* active_texunit;
116 
117 	/* framebuffer objects*/
118 	r_framebuffer_t* renderBuffer;
119 	r_framebuffer_t* bloomBuffer0;
120 	r_framebuffer_t* bloomBuffer1;
121 	r_framebuffer_t* buffers0[DOWNSAMPLE_PASSES];
122 	r_framebuffer_t* buffers1[DOWNSAMPLE_PASSES];
123 	r_framebuffer_t* buffers2[DOWNSAMPLE_PASSES];
124 	bool frameBufferObjectsInitialized;
125 	const r_framebuffer_t* activeFramebuffer;
126 
127 	/* shaders */
128 	r_shader_t shaders[MAX_SHADERS];
129 	r_program_t programs[MAX_PROGRAMS];
130 	r_program_t* world_program;
131 	r_program_t* model_program;
132 	r_program_t* warp_program;
133 	r_program_t* geoscape_program;
134 	r_program_t* convolve_program;
135 	r_program_t* combine2_program;
136 	r_program_t* atmosphere_program;
137 	r_program_t* simple_glow_program;
138 	r_program_t* active_program;
139 
140 	/* blend function */
141 	GLenum blend_src, blend_dest;
142 
143 	const material_t* active_material;
144 
145 	/* states */
146 	bool shell_enabled;
147 	bool blend_enabled;
148 	bool multisample_enabled;
149 	bool color_array_enabled;
150 	bool alpha_test_enabled;
151 	bool stencil_test_enabled;
152 	bool lighting_enabled;
153 	bool warp_enabled;
154 	bool fog_enabled;
155 	bool blur_enabled;
156 	bool glowmap_enabled;
157 	bool draw_glow_enabled;
158 	bool dynamic_lighting_enabled;
159 	bool specularmap_enabled;
160 	bool roughnessmap_enabled;
161 	bool animation_enabled;
162 	bool renderbuffer_enabled; /**< renderbuffer vs screen as render target*/
163 
164 	const struct image_s* active_normalmap;
165 } rstate_t;
166 
167 extern rstate_t r_state;
168 
169 void R_SetDefaultState(void);
170 void R_Setup2D(void);
171 void R_Setup3D(void);
172 void R_ReallocateStateArrays(int size);
173 void R_ReallocateTexunitArray(gltexunit_t* texunit, int size);
174 
175 void R_TexEnv(GLenum value);
176 void R_TexOverride (vec4_t rgba);
177 void R_BlendFunc(GLenum src, GLenum dest);
178 
179 bool R_SelectTexture(gltexunit_t* texunit);
180 
181 void R_BindTextureDebug(int texnum, const char* file, int line, const char* function);
182 #define R_BindTexture(tn) R_BindTextureDebug(tn, __FILE__, __LINE__, __PRETTY_FUNCTION__)
183 void R_BindTextureForTexUnit(GLuint texnum, gltexunit_t* texunit);
184 void R_BindLightmapTexture(GLuint texnum);
185 void R_BindDeluxemapTexture(GLuint texnum);
186 void R_BindNormalmapTexture(GLuint texnum);
187 void R_BindBuffer(GLenum target, GLenum type, GLuint id);
188 void R_BindArray(GLenum target, GLenum type, const void* array);
189 void R_BindDefaultArray(GLenum target);
190 void R_EnableStencilTest(bool enable);
191 void R_EnableTexture(gltexunit_t* texunit, bool enable);
192 void R_EnableMultisample(bool enable);
193 void R_EnableBlend(bool enable);
194 void R_EnableAlphaTest(bool enable);
195 void R_EnableColorArray(bool enable);
196 bool R_EnableLighting(r_program_t* program, bool enable);
197 void R_EnableBumpmap(const struct image_s* normalmap);
198 void R_EnableWarp(r_program_t* program, bool enable);
199 void R_EnableBlur(r_program_t* program, bool enable, r_framebuffer_t* source, r_framebuffer_t* dest, int dir);
200 void R_EnableShell(bool enable);
201 void R_EnableFog(bool enable);
202 void R_EnableDrawAsGlow(bool enable);
203 void R_EnableGlowMap(const struct image_s* image);
204 void R_EnableSpecularMap(const struct image_s* image, bool enable);
205 void R_EnableRoughnessMap(const struct image_s* image, bool enable);
206 void R_SetupSpotLight(int index, const light_t* light);
207 void R_DisableSpotLight(int index);
208 void R_EnableAnimation(const struct mAliasMesh_s* mesh, float backlerp, bool enable);
209 
210 void R_UseMaterial (const material_t* material);
211