1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code ("Doom 3 Source Code").
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code 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.  See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code.  If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #ifndef __RENDERER_H__
30 #define __RENDERER_H__
31 
32 #include "framework/Common.h"
33 #include "renderer/Model.h"
34 
35 /*
36 ===============================================================================
37 
38 	idRenderSystem is responsible for managing the screen, which can have
39 	multiple idRenderWorld and 2D drawing done on it.
40 
41 ===============================================================================
42 */
43 
44 
45 // Contains variables specific to the OpenGL configuration being run right now.
46 // These are constant once the OpenGL subsystem is initialized.
47 typedef struct glconfig_s {
48 	const char			*renderer_string;
49 	const char			*vendor_string;
50 	const char			*version_string;
51 	const char			*extensions_string;
52 
53 	float				glVersion;				// atof( version_string )
54 
55 
56 	int					maxTextureSize;			// queried from GL
57 	int					maxTextureUnits;
58 	int					maxTextureCoords;
59 	int					maxTextureImageUnits;
60 	float				maxTextureAnisotropy;
61 
62 	int					colorBits, depthBits, stencilBits;
63 
64 	bool				multitextureAvailable;
65 	bool				textureCompressionAvailable;
66 	bool				anisotropicAvailable;
67 	bool				textureLODBiasAvailable;
68 	bool				textureEnvAddAvailable;
69 	bool				textureEnvCombineAvailable;
70 	bool				registerCombinersAvailable;
71 	bool				cubeMapAvailable;
72 	bool				envDot3Available;
73 	bool				texture3DAvailable;
74 	bool				sharedTexturePaletteAvailable;
75 	bool				ARBVertexBufferObjectAvailable;
76 	bool				ARBVertexProgramAvailable;
77 	bool				ARBFragmentProgramAvailable;
78 	bool				twoSidedStencilAvailable;
79 	bool				textureNonPowerOfTwoAvailable;
80 	bool				depthBoundsTestAvailable;
81 
82 	int					vidWidth, vidHeight;	// passed to R_BeginFrame
83 
84 	int					displayFrequency;
85 
86 	bool				isFullscreen;
87 
88 	bool				allowARB2Path;
89 
90 	bool				isInitialized;
91 } glconfig_t;
92 
93 
94 // font support
95 const int GLYPH_START			= 0;
96 const int GLYPH_END				= 255;
97 const int GLYPH_CHARSTART		= 32;
98 const int GLYPH_CHAREND			= 127;
99 const int GLYPHS_PER_FONT		= GLYPH_END - GLYPH_START + 1;
100 
101 typedef struct {
102 	int					height;			// number of scan lines
103 	int					top;			// top of glyph in buffer
104 	int					bottom;			// bottom of glyph in buffer
105 	int					pitch;			// width for copying
106 	int					xSkip;			// x adjustment
107 	int					imageWidth;		// width of actual image
108 	int					imageHeight;	// height of actual image
109 	float				s;				// x offset in image where glyph starts
110 	float				t;				// y offset in image where glyph starts
111 	float				s2;
112 	float				t2;
113 	const idMaterial *	glyph;			// shader with the glyph
114 	char				shaderName[32];
115 } glyphInfo_t;
116 
117 typedef struct {
118 	glyphInfo_t			glyphs [GLYPHS_PER_FONT];
119 	float				glyphScale;
120 	char				name[64];
121 } fontInfo_t;
122 
123 typedef struct {
124 	fontInfo_t			fontInfoSmall;
125 	fontInfo_t			fontInfoMedium;
126 	fontInfo_t			fontInfoLarge;
127 	int					maxHeight;
128 	int					maxWidth;
129 	int					maxHeightSmall;
130 	int					maxWidthSmall;
131 	int					maxHeightMedium;
132 	int					maxWidthMedium;
133 	int					maxHeightLarge;
134 	int					maxWidthLarge;
135 	char				name[64];
136 } fontInfoEx_t;
137 
138 const int SMALLCHAR_WIDTH		= 8;
139 const int SMALLCHAR_HEIGHT		= 16;
140 const int BIGCHAR_WIDTH			= 16;
141 const int BIGCHAR_HEIGHT		= 16;
142 
143 // all drawing is done to a 640 x 480 virtual screen size
144 // and will be automatically scaled to the real resolution
145 const int SCREEN_WIDTH			= 640;
146 const int SCREEN_HEIGHT			= 480;
147 
148 class idRenderWorld;
149 
150 
151 class idRenderSystem {
152 public:
153 
~idRenderSystem()154 	virtual					~idRenderSystem() {}
155 
156 	// set up cvars and basic data structures, but don't
157 	// init OpenGL, so it can also be used for dedicated servers
158 	virtual void			Init( void ) = 0;
159 
160 	// only called before quitting
161 	virtual void			Shutdown( void ) = 0;
162 
163 	virtual void			InitOpenGL( void ) = 0;
164 
165 	virtual void			ShutdownOpenGL( void ) = 0;
166 
167 	virtual bool			IsOpenGLRunning( void ) const = 0;
168 
169 	virtual bool			IsFullScreen( void ) const = 0;
170 	virtual int				GetScreenWidth( void ) const = 0;
171 	virtual int				GetScreenHeight( void ) const = 0;
172 
173 	// allocate a renderWorld to be used for drawing
174 	virtual idRenderWorld *	AllocRenderWorld( void ) = 0;
175 	virtual	void			FreeRenderWorld( idRenderWorld * rw ) = 0;
176 
177 	// All data that will be used in a level should be
178 	// registered before rendering any frames to prevent disk hits,
179 	// but they can still be registered at a later time
180 	// if necessary.
181 	virtual void			BeginLevelLoad( void ) = 0;
182 	virtual void			EndLevelLoad( void ) = 0;
183 
184 	// font support
185 	virtual bool			RegisterFont( const char *fontName, fontInfoEx_t &font ) = 0;
186 
187 	// GUI drawing just involves shader parameter setting and axial image subsections
188 	virtual void			SetColor( const idVec4 &rgba ) = 0;
189 	virtual void			SetColor4( float r, float g, float b, float a ) = 0;
190 
191 	virtual void			DrawStretchPic( const idDrawVert *verts, const glIndex_t *indexes, int vertCount, int indexCount, const idMaterial *material,
192 											bool clip = true, float min_x = 0.0f, float min_y = 0.0f, float max_x = 640.0f, float max_y = 480.0f ) = 0;
193 	virtual void			DrawStretchPic( float x, float y, float w, float h, float s1, float t1, float s2, float t2, const idMaterial *material ) = 0;
194 
195 	virtual void			DrawStretchTri ( idVec2 p1, idVec2 p2, idVec2 p3, idVec2 t1, idVec2 t2, idVec2 t3, const idMaterial *material ) = 0;
196 	virtual void			GlobalToNormalizedDeviceCoordinates( const idVec3 &global, idVec3 &ndc ) = 0;
197 	virtual void			GetGLSettings( int& width, int& height ) = 0;
198 	virtual void			PrintMemInfo( MemInfo_t *mi ) = 0;
199 
200 	virtual void			DrawSmallChar( int x, int y, int ch, const idMaterial *material ) = 0;
201 	virtual void			DrawSmallStringExt( int x, int y, const char *string, const idVec4 &setColor, bool forceColor, const idMaterial *material ) = 0;
202 	virtual void			DrawBigChar( int x, int y, int ch, const idMaterial *material ) = 0;
203 	virtual void			DrawBigStringExt( int x, int y, const char *string, const idVec4 &setColor, bool forceColor, const idMaterial *material ) = 0;
204 
205 	// dump all 2D drawing so far this frame to the demo file
206 	virtual void			WriteDemoPics() = 0;
207 
208 	// draw the 2D pics that were saved out with the current demo frame
209 	virtual void			DrawDemoPics() = 0;
210 
211 	// FIXME: add an interface for arbitrary point/texcoord drawing
212 
213 
214 	// a frame cam consist of 2D drawing and potentially multiple 3D scenes
215 	// window sizes are needed to convert SCREEN_WIDTH / SCREEN_HEIGHT values
216 	virtual void			BeginFrame( int windowWidth, int windowHeight ) = 0;
217 
218 	// if the pointers are not NULL, timing info will be returned
219 	virtual void			EndFrame( int *frontEndMsec, int *backEndMsec ) = 0;
220 
221 	// aviDemo uses this.
222 	// Will automatically tile render large screen shots if necessary
223 	// Samples is the number of jittered frames for anti-aliasing
224 	// If ref == NULL, session->updateScreen will be used
225 	// This will perform swapbuffers, so it is NOT an approppriate way to
226 	// generate image files that happen during gameplay, as for savegame
227 	// markers.  Use WriteRender() instead.
228 	virtual void			TakeScreenshot( int width, int height, const char *fileName, int samples, struct renderView_s *ref ) = 0;
229 
230 	// the render output can be cropped down to a subset of the real screen, as
231 	// for save-game reviews and split-screen multiplayer.  Users of the renderer
232 	// will not know the actual pixel size of the area they are rendering to
233 
234 	// the x,y,width,height values are in virtual SCREEN_WIDTH / SCREEN_HEIGHT coordinates
235 
236 	// to render to a texture, first set the crop size with makePowerOfTwo = true,
237 	// then perform all desired rendering, then capture to an image
238 	// if the specified physical dimensions are larger than the current cropped region, they will be cut down to fit
239 	virtual void			CropRenderSize( int width, int height, bool makePowerOfTwo = false, bool forceDimensions = false ) = 0;
240 	virtual void			CaptureRenderToImage( const char *imageName ) = 0;
241 	// fixAlpha will set all the alpha channel values to 0xff, which allows screen captures
242 	// to use the default tga loading code without having dimmed down areas in many places
243 	virtual void			CaptureRenderToFile( const char *fileName, bool fixAlpha = false ) = 0;
244 	virtual void			UnCrop() = 0;
245 
246 	// the image has to be already loaded ( most straightforward way would be through a FindMaterial )
247 	// texture filter / mipmapping / repeat won't be modified by the upload
248 	// returns false if the image wasn't found
249 	virtual bool			UploadImage( const char *imageName, const byte *data, int width, int height ) = 0;
250 };
251 
252 extern idRenderSystem *			renderSystem;
253 
254 //
255 // functions mainly intended for editor and dmap integration
256 //
257 
258 // returns the frustum planes in world space
259 void R_RenderLightFrustum( const struct renderLight_s &renderLight, idPlane lightFrustum[6] );
260 
261 // for use by dmap to do the carving-on-light-boundaries and for the editor for display
262 void R_LightProjectionMatrix( const idVec3 &origin, const idPlane &rearPlane, idVec4 mat[4] );
263 
264 // used by the view shot taker
265 void R_ScreenshotFilename( int &lastNumber, const char *base, idStr &fileName );
266 
267 #endif /* !__RENDERER_H__ */
268