ScummVM API documentation
gfx.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef FREESCAPE_GFX_H
23 #define FREESCAPE_GFX_H
24 
25 #include "common/hashmap.h"
26 #include "common/rendermode.h"
27 #include "common/rect.h"
28 
29 #include "graphics/pixelformat.h"
30 #include "graphics/managed_surface.h"
31 #include "graphics/renderer.h"
32 #include "math/frustum.h"
33 #include "math/vector3d.h"
34 
35 namespace Freescape {
36 
37 #define kVertexArraySize 128
38 #define kCoordsArraySize 4
39 
40 typedef Common::Array<byte *> ColorMap;
41 typedef Common::HashMap<int, int> ColorReMap;
42 
43 class Renderer;
44 
45 const Graphics::PixelFormat getRGBAPixelFormat();
46 byte getCPCPixelMode1(byte cpc_byte, int index);
47 byte getCPCPixelMode0(byte cpc_byte, int index);
48 byte getCPCPixel(byte cpc_byte, int index, bool mode1);
49 
50 class Texture {
51 public:
52  Texture(){ _width = 0; _height = 0; };
53  virtual ~Texture(){};
54 
55  uint _width;
56  uint _height;
57  Graphics::PixelFormat _format;
58 
59  virtual void update(const Graphics::Surface *surface) = 0;
60  virtual void updatePartial(const Graphics::Surface *surface, const Common::Rect &rect) = 0;
61 };
62 
63 class Renderer {
64 public:
65  Renderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
66  virtual ~Renderer();
67 
68  Graphics::PixelFormat _texturePixelFormat;
69  bool _isAccelerated;
70  bool _authenticGraphics;
71 
72  virtual void init() = 0;
73  virtual void setViewport(const Common::Rect &rect) = 0;
74 
78  virtual void flipBuffer() {}
79 
85  enum StereoEye {
86  kStereoEyeNone = 0,
87  kStereoEyeLeft = -1,
88  kStereoEyeRight = 1,
89  kStereoEyeFlatAnaglyph = 2
90  };
91  virtual void setStereoEye(StereoEye eye) { _stereoEye = eye; }
92  void setStereoParameters(float separation, float convergence);
93 
94  virtual void useColor(uint8 r, uint8 g, uint8 b) = 0;
95  virtual void enableCulling(bool enabled) {};
96  virtual void polygonOffset(bool enabled) = 0;
97 
98  virtual Texture *createTexture(const Graphics::Surface *surface, bool is3D = false) = 0;
99  Graphics::Surface *convertImageFormatIfNecessary(Graphics::ManagedSurface *surface);
100 
101  virtual void freeTexture(Texture *texture) = 0;
102  virtual void drawTexturedRect2D(const Common::Rect &screenRect, const Common::Rect &textureRect, Texture *texture) = 0;
103 
104  virtual void renderSensorShoot(byte color, const Math::Vector3d sensor, const Math::Vector3d player, const Common::Rect &viewPort) = 0;
105  virtual void renderPlayerShootBall(byte color, const Common::Point &position, int frame, const Common::Rect &viewPort) = 0;
106  virtual void renderPlayerShootRay(byte color, const Common::Point &position, const Common::Rect &viewPort) = 0;
107 
108  virtual void renderCrossair(const Common::Point &crossairPosition) = 0;
109 
110  virtual void renderCube(const Math::Vector3d &position, const Math::Vector3d &size, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
111  virtual void renderRectangle(const Math::Vector3d &position, const Math::Vector3d &size, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
112  virtual void renderPolygon(const Math::Vector3d &origin, const Math::Vector3d &size, const Common::Array<float> *ordinates, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
113  virtual void renderPyramid(const Math::Vector3d &origin, const Math::Vector3d &size, const Common::Array<float> *ordinates, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, int type);
114  virtual void renderFace(const Common::Array<Math::Vector3d> &vertices) = 0;
115 
116  void setColorRemaps(ColorReMap *colorRemaps);
117  virtual void clear(uint8 r, uint8 g, uint8 b, bool ignoreViewport = false) = 0;
118  virtual void clearDepthBuffer(bool ignoreViewport = false) {}
119  virtual void drawFloor(uint8 color) = 0;
120  virtual void drawBackground(uint8 color);
121 
122  void drawEclipse(uint8 color1, uint8 color2, float difference);
123  virtual void drawSkybox(Texture *texture, Math::Vector3d camera) {};
124  virtual void drawThunder(Texture *texture, Math::Vector3d camera, float size) {};
125  virtual void drawCelestialBody(Math::Vector3d position, float radius, uint8 color) {};
126 
127  Common::Rect viewport() const;
128  virtual Common::Point nativeResolution() { return Common::Point(_screenW, _screenH); }
129 
130  // palette
131  void readFromPalette(uint8 index, uint8 &r, uint8 &g, uint8 &b);
132  void setPaletteValue(uint8 index, uint8 r, uint8 g, uint8 b);
133  uint8 indexFromColor(uint8 r, uint8 g, uint8 b);
134  uint8 mapEGAColor(uint8 index);
135 
136  bool getRGBAt(uint8 index, uint8 ecolor, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
137  bool getRGBAtC64(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
138  bool getRGBAtCGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
139  bool getRGBAtCPC(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
140  bool getRGBAtEGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2);
141  bool getRGBAtZX(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
142  bool getRGBAtHercules(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
143  void extractCPCIndexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
144  void extractC64Indexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
145 
146  void selectColorFromFourColorPalette(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1);
147 
148  // Stipple
149  virtual void setStippleData(byte *data) {};
150  virtual void useStipple(bool enabled) {};
151  void scaleStipplePattern(byte originalPattern[128], byte newPattern[128]);
152 
153  byte _defaultStippleArray[128] = {
154  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
155  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
156  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
157  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
158  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
159  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
160  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
161  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
162  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
163  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
164  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
165  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
166  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
167  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
168  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
169  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
170  };
171 
172  byte *_variableStippleArray;
173 
174  float _skyNormals[16][3] = {
175  { 0.0, 0.0, 1.0 }, //front //0
176  { 0.0, 0.0, 1.0 }, //1
177  { 0.0, 0.0, 1.0 }, //2
178  { 0.0, 0.0, 1.0 }, //3
179  { 0.0, 0.0, -1.0 }, //back //0
180  { 0.0, 0.0, -1.0 }, //1
181  { 0.0, 0.0, -1.0 }, //2
182  { 0.0, 0.0, -1.0 }, //3
183  { -1.0, 0.0, 0.0 }, //left
184  { -1.0, 0.0, 0.0 },
185  { -1.0, 0.0, 0.0 },
186  { -1.0, 0.0, 0.0 },
187  { 1.0, 0.0, 0.0 }, //right
188  { 1.0, 0.0, 0.0 },
189  { 1.0, 0.0, 0.0 },
190  { 1.0, 0.0, 0.0 }
191  };
192 
193  float _skyUvs1008[16][2] = {
194  { 0.0f, 0.0f }, //1
195  { 0.0f, 2.0f }, //2
196  { 0.4f, 2.0f }, //3
197  { 0.4f, 0.0f }, //front //4
198 
199  { 0.0f, 2.0f }, //back //1
200  { 0.4f, 2.0f }, //2
201  { 0.4f, 0.0f }, //3
202  { 0.0f, 0.0f }, //4
203 
204  { 0.0f, 0.0f }, //left //1
205  { 0.4f, 0.0f }, //2
206  { 0.4f, 2.0f }, //3
207  { 0.0f, 2.0f }, //4
208 
209  { 0.4f, 0.0f }, //right //1
210  { 0.0f, 0.0f }, //2
211  { 0.0f, 2.0f }, //3
212  { 0.4f, 2.0f }, //4
213  };
214 
215  float _skyUvs672[16][2] = {
216  { 0.0f, 0.0f }, //1
217  { 0.0f, 2.0f }, //2
218  { 0.6f, 2.0f }, //3
219  { 0.6f, 0.0f }, //front //4
220 
221  { 0.0f, 2.0f }, //back //1
222  { 0.6f, 2.0f }, //2
223  { 0.6f, 0.0f }, //3
224  { 0.0f, 0.0f }, //4
225 
226  { 0.0f, 0.0f }, //left //1
227  { 0.6f, 0.0f }, //2
228  { 0.6f, 2.0f }, //3
229  { 0.0f, 2.0f }, //4
230 
231  { 0.6f, 0.0f }, //right //1
232  { 0.0f, 0.0f }, //2
233  { 0.0f, 2.0f }, //3
234  { 0.6f, 2.0f }, //4
235  };
236 
237  float _skyUvs128[16][2] = {
238  { 0.0f, 0.0f }, //1
239  { 0.0f, 2.0f }, //2
240  { 2.5f, 2.0f }, //3
241  { 2.5f, 0.0f }, //front //4
242 
243  { 0.0f, 2.0f }, //back //1
244  { 2.5f, 2.0f }, //2
245  { 2.5f, 0.0f }, //3
246  { 0.0f, 0.0f }, //4
247 
248  { 0.0f, 0.0f }, //left //1
249  { 2.5f, 0.0f }, //2
250  { 2.5f, 2.0f }, //3
251  { 0.0f, 2.0f }, //4
252 
253  { 2.5f, 0.0f }, //right //1
254  { 0.0f, 0.0f }, //2
255  { 0.0f, 2.0f }, //3
256  { 2.5f, 2.0f }, //4
257  };
258 
259  float _skyVertices[16][3] = {
260  { -81280.0, 8128.0, 81280.0 }, //1 // Vertex #0 front
261  { -81280.0, -8128.0, 81280.0 }, //2 // Vertex #1
262  { 81280.0, -8128.0, 81280.0 }, //3 // Vertex #2
263  { 81280.0, 8128.0, 81280.0 }, //4 // Vertex #3
264 
265  { 81280.0f, -8128.0f, -81280.0f }, // 1
266  { -81280.0f, -8128.0f, -81280.0f }, // 2
267  { -81280.0f, 8128.0f, -81280.0f }, // 3
268  { 81280.0f, 8128.0f, -81280.0f }, // 4
269 
270  { -81280.0f, 8128.0f, 81280.0f }, //left //1
271  { -81280.0f, 8128.0f, -81280.0f }, //2
272  { -81280.0f, -8128.0f, -81280.0f }, //3
273  { -81280.0f, -8128.0f, 81280.0f }, //4
274 
275  { 81280.0f, 8128.0f, -81280.0f }, //right //1
276  { 81280.0f, 8128.0f, 81280.0f }, //2
277  { 81280.0f, -8128.0f, 81280.0f },//3
278  { 81280.0f, -8128.0f, -81280.0f },//4
279  };
280 
281  unsigned int _skyIndices[24] = {
282  0, 1, 2, // front
283  0, 2, 3,
284 
285  4, 5, 6, // back
286  4, 6, 7,
287 
288  8, 9, 10, // left
289  8, 10, 11,
290 
291  12, 13, 14, // right
292  12, 14, 15,
293  };
294 
295  byte *_palette;
296  void setColorMap(ColorMap *colorMap_);
297  ColorMap *_colorMap;
298  ColorReMap *_colorRemaps;
299  void clearColorPairArray();
300  void fillColorPairArray();
301  byte _colorPair[16];
302  int _keyColor;
303  int _inkColor;
304  int _paperColor;
305  int _underFireBackgroundColor;
306  Common::Point _shakeOffset;
307  byte _stipples[16][128];
308 
309  // Amiga/Atari hardware palette cycling for pulsating surfaces.
310  // Castle Master: COLOR15, every 4 frames, per-area gated.
311  // Dark Side: COLOR5, every 2 frames, always active.
312  Common::Array<uint16> _colorCyclingTable;
313  int _colorCyclingIndex;
314  int _colorCyclingTimer; // -1 = disabled, >=0 = active
315  int _colorCyclingPaletteIndex; // which palette entry to cycle (5 or 15)
316  int _colorCyclingSpeed; // frames between changes (2 or 4)
317  void updateColorCycling();
318 
319  int _scale;
320 
321  // debug flags
322  bool _debugRenderBoundingBoxes;
323  bool _debugRenderOcclusionBoxes;
324  bool _debugRenderWireframe;
325  bool _debugRenderNormals;
326  Common::Array<uint8> _debugHighlightObjectIDs;
327 
328  // for drawing bounding boxes
329  virtual void drawAABB(const Math::AABB &aabb, uint8 r, uint8 g, uint8 b) {}
330 
337  virtual void positionCamera(const Math::Vector3d &pos, const Math::Vector3d &interest, float rollAngle = 0.0f) = 0;
338  virtual void updateProjectionMatrix(float fov, float aspectRatio, float nearClipPlane, float farClipPlane) = 0;
339 
340  Math::Matrix4 getMvpMatrix() const { return _mvpMatrix; }
341  virtual Graphics::Surface *getScreenshot() = 0;
342  void flipVertical(Graphics::Surface *s);
343 
344  int _screenW;
345  int _screenH;
346  Common::RenderMode _renderMode;
347 
348  bool computeScreenViewport();
349 
350 protected:
351  Common::Rect _screenViewport;
352  Common::Rect _viewport;
353  Common::Rect _unscaledViewport;
354 
355  Math::Matrix4 _projectionMatrix;
356  Math::Matrix4 _modelViewMatrix;
357  Math::Matrix4 _mvpMatrix;
358 
359  Math::Frustum _frustum;
360 
361  Math::Matrix4 makeProjectionMatrix(float fov, float nearClipPlane, float farClipPlane) const;
362  void applyStereoTint(uint8 &r, uint8 &g, uint8 &b) const;
363  void getStereoCamera(const Math::Vector3d &pos, const Math::Vector3d &interest, Math::Vector3d &eyePos, Math::Vector3d &eyeInterest) const;
364  float getStereoFrustumOffset(float nearClipPlane, bool mirroredProjection) const;
365 
366  StereoEye _stereoEye;
367  float _stereoSeparation; // half of the inter-ocular distance, in world units
368  float _stereoConvergence; // distance to the zero-parallax plane, in world units
369 };
370 
371 Graphics::RendererType determinateRenderType();
372 Renderer *CreateGfxOpenGL(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
373 Renderer *CreateGfxOpenGLShader(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
374 Renderer *CreateGfxTinyGL(int screenW, int screenH, Common::RenderMode renderMode);
375 Renderer *createRenderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
376 
377 } // End of namespace Freescape
378 
379 #endif // FREESCAPE_GFX_H
Definition: managed_surface.h:51
virtual void flipBuffer()
Definition: gfx.h:78
Definition: surface.h:67
RendererType
Definition: renderer.h:45
Definition: area.h:36
Definition: pixelformat.h:138
RenderMode
Definition: rendermode.h:48
Definition: rect.h:524
StereoEye
Definition: gfx.h:85
Definition: gfx.h:63
Definition: rect.h:144
Definition: gfx.h:50