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  virtual void fillViewportStippled(uint8 r1, uint8 g1, uint8 b1, uint8 r2, uint8 g2, uint8 b2, byte *stipple) {}
122 
123  void drawEclipse(uint8 color1, uint8 color2, float difference);
124  virtual void drawSkybox(Texture *texture, Math::Vector3d camera) {};
125  virtual void drawThunder(Texture *texture, Math::Vector3d camera, float size) {};
126  virtual void drawCelestialBody(Math::Vector3d position, float radius, uint8 color) {};
127 
128  Common::Rect viewport() const;
129  virtual Common::Point nativeResolution() { return Common::Point(_screenW, _screenH); }
130 
131  // palette
132  void readFromPalette(uint8 index, uint8 &r, uint8 &g, uint8 &b);
133  void setPaletteValue(uint8 index, uint8 r, uint8 g, uint8 b);
134  uint8 indexFromColor(uint8 r, uint8 g, uint8 b);
135  uint8 mapEGAColor(uint8 index);
136 
137  bool getRGBAt(uint8 index, uint8 ecolor, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
138  bool getRGBAtC64(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
139  bool getRGBAtCGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
140  bool getRGBAtCPC(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
141  bool getRGBAtEGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2);
142  bool getRGBAtZX(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
143  bool getRGBAtHercules(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
144  void extractCPCIndexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
145  void extractC64Indexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
146 
147  void selectColorFromFourColorPalette(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1);
148 
149  // Stipple
150  virtual void setStippleData(byte *data) {};
151  virtual void useStipple(bool enabled) {};
152  void scaleStipplePattern(byte originalPattern[128], byte newPattern[128]);
153 
154  byte _defaultStippleArray[128] = {
155  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
156  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
157  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
158  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
159  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
160  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
161  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
162  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
163  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
164  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
165  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
166  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
167  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
168  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
169  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
170  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
171  };
172 
173  byte *_variableStippleArray;
174 
175  float _skyNormals[16][3] = {
176  { 0.0, 0.0, 1.0 }, //front //0
177  { 0.0, 0.0, 1.0 }, //1
178  { 0.0, 0.0, 1.0 }, //2
179  { 0.0, 0.0, 1.0 }, //3
180  { 0.0, 0.0, -1.0 }, //back //0
181  { 0.0, 0.0, -1.0 }, //1
182  { 0.0, 0.0, -1.0 }, //2
183  { 0.0, 0.0, -1.0 }, //3
184  { -1.0, 0.0, 0.0 }, //left
185  { -1.0, 0.0, 0.0 },
186  { -1.0, 0.0, 0.0 },
187  { -1.0, 0.0, 0.0 },
188  { 1.0, 0.0, 0.0 }, //right
189  { 1.0, 0.0, 0.0 },
190  { 1.0, 0.0, 0.0 },
191  { 1.0, 0.0, 0.0 }
192  };
193 
194  float _skyUvs1008[16][2] = {
195  { 0.0f, 0.0f }, //1
196  { 0.0f, 2.0f }, //2
197  { 0.4f, 2.0f }, //3
198  { 0.4f, 0.0f }, //front //4
199 
200  { 0.0f, 2.0f }, //back //1
201  { 0.4f, 2.0f }, //2
202  { 0.4f, 0.0f }, //3
203  { 0.0f, 0.0f }, //4
204 
205  { 0.0f, 0.0f }, //left //1
206  { 0.4f, 0.0f }, //2
207  { 0.4f, 2.0f }, //3
208  { 0.0f, 2.0f }, //4
209 
210  { 0.4f, 0.0f }, //right //1
211  { 0.0f, 0.0f }, //2
212  { 0.0f, 2.0f }, //3
213  { 0.4f, 2.0f }, //4
214  };
215 
216  float _skyUvs672[16][2] = {
217  { 0.0f, 0.0f }, //1
218  { 0.0f, 2.0f }, //2
219  { 0.6f, 2.0f }, //3
220  { 0.6f, 0.0f }, //front //4
221 
222  { 0.0f, 2.0f }, //back //1
223  { 0.6f, 2.0f }, //2
224  { 0.6f, 0.0f }, //3
225  { 0.0f, 0.0f }, //4
226 
227  { 0.0f, 0.0f }, //left //1
228  { 0.6f, 0.0f }, //2
229  { 0.6f, 2.0f }, //3
230  { 0.0f, 2.0f }, //4
231 
232  { 0.6f, 0.0f }, //right //1
233  { 0.0f, 0.0f }, //2
234  { 0.0f, 2.0f }, //3
235  { 0.6f, 2.0f }, //4
236  };
237 
238  float _skyUvs128[16][2] = {
239  { 0.0f, 0.0f }, //1
240  { 0.0f, 2.0f }, //2
241  { 2.5f, 2.0f }, //3
242  { 2.5f, 0.0f }, //front //4
243 
244  { 0.0f, 2.0f }, //back //1
245  { 2.5f, 2.0f }, //2
246  { 2.5f, 0.0f }, //3
247  { 0.0f, 0.0f }, //4
248 
249  { 0.0f, 0.0f }, //left //1
250  { 2.5f, 0.0f }, //2
251  { 2.5f, 2.0f }, //3
252  { 0.0f, 2.0f }, //4
253 
254  { 2.5f, 0.0f }, //right //1
255  { 0.0f, 0.0f }, //2
256  { 0.0f, 2.0f }, //3
257  { 2.5f, 2.0f }, //4
258  };
259 
260  float _skyVertices[16][3] = {
261  { -81280.0, 8128.0, 81280.0 }, //1 // Vertex #0 front
262  { -81280.0, -8128.0, 81280.0 }, //2 // Vertex #1
263  { 81280.0, -8128.0, 81280.0 }, //3 // Vertex #2
264  { 81280.0, 8128.0, 81280.0 }, //4 // Vertex #3
265 
266  { 81280.0f, -8128.0f, -81280.0f }, // 1
267  { -81280.0f, -8128.0f, -81280.0f }, // 2
268  { -81280.0f, 8128.0f, -81280.0f }, // 3
269  { 81280.0f, 8128.0f, -81280.0f }, // 4
270 
271  { -81280.0f, 8128.0f, 81280.0f }, //left //1
272  { -81280.0f, 8128.0f, -81280.0f }, //2
273  { -81280.0f, -8128.0f, -81280.0f }, //3
274  { -81280.0f, -8128.0f, 81280.0f }, //4
275 
276  { 81280.0f, 8128.0f, -81280.0f }, //right //1
277  { 81280.0f, 8128.0f, 81280.0f }, //2
278  { 81280.0f, -8128.0f, 81280.0f },//3
279  { 81280.0f, -8128.0f, -81280.0f },//4
280  };
281 
282  unsigned int _skyIndices[24] = {
283  0, 1, 2, // front
284  0, 2, 3,
285 
286  4, 5, 6, // back
287  4, 6, 7,
288 
289  8, 9, 10, // left
290  8, 10, 11,
291 
292  12, 13, 14, // right
293  12, 14, 15,
294  };
295 
296  byte *_palette;
297  void setColorMap(ColorMap *colorMap_);
298  ColorMap *_colorMap;
299  ColorReMap *_colorRemaps;
300  void clearColorPairArray();
301  void fillColorPairArray();
302  byte _colorPair[16];
303  int _keyColor;
304  int _inkColor;
305  int _paperColor;
306  int _underFireBackgroundColor;
307  int _fourColorBackground;
308  Common::Point _shakeOffset;
309  byte _stipples[16][128];
310 
311  // Amiga/Atari hardware palette cycling for pulsating surfaces.
312  // Castle Master: COLOR15, every 4 frames, per-area gated.
313  // Dark Side: COLOR5, every 2 frames, always active.
314  Common::Array<uint16> _colorCyclingTable;
315  int _colorCyclingIndex;
316  int _colorCyclingTimer; // -1 = disabled, >=0 = active
317  int _colorCyclingPaletteIndex; // which palette entry to cycle (5 or 15)
318  int _colorCyclingSpeed; // frames between changes (2 or 4)
319  void updateColorCycling();
320 
321  int _scale;
322 
323  // debug flags
324  bool _debugRenderBoundingBoxes;
325  bool _debugRenderOcclusionBoxes;
326  bool _debugRenderWireframe;
327  bool _debugRenderNormals;
328  Common::Array<uint8> _debugHighlightObjectIDs;
329 
330  // for drawing bounding boxes
331  virtual void drawAABB(const Math::AABB &aabb, uint8 r, uint8 g, uint8 b) {}
332 
339  virtual void positionCamera(const Math::Vector3d &pos, const Math::Vector3d &interest, float rollAngle = 0.0f) = 0;
340  virtual void updateProjectionMatrix(float fov, float aspectRatio, float nearClipPlane, float farClipPlane) = 0;
341 
342  Math::Matrix4 getMvpMatrix() const { return _mvpMatrix; }
343  virtual Graphics::Surface *getScreenshot() = 0;
344  void flipVertical(Graphics::Surface *s);
345 
346  int _screenW;
347  int _screenH;
348  Common::RenderMode _renderMode;
349 
350  bool computeScreenViewport();
351 
352 protected:
353  Common::Rect _screenViewport;
354  Common::Rect _viewport;
355  Common::Rect _unscaledViewport;
356 
357  Math::Matrix4 _projectionMatrix;
358  Math::Matrix4 _modelViewMatrix;
359  Math::Matrix4 _mvpMatrix;
360 
361  Math::Frustum _frustum;
362 
363  Math::Matrix4 makeProjectionMatrix(float fov, float nearClipPlane, float farClipPlane) const;
364  void applyStereoTint(uint8 &r, uint8 &g, uint8 &b) const;
365  void getStereoCamera(const Math::Vector3d &pos, const Math::Vector3d &interest, Math::Vector3d &eyePos, Math::Vector3d &eyeInterest) const;
366  float getStereoFrustumOffset(float nearClipPlane, bool mirroredProjection) const;
367 
368  StereoEye _stereoEye;
369  float _stereoSeparation; // half of the inter-ocular distance, in world units
370  float _stereoConvergence; // distance to the zero-parallax plane, in world units
371 };
372 
373 Graphics::RendererType determinateRenderType();
374 Renderer *CreateGfxOpenGL(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
375 Renderer *CreateGfxOpenGLShader(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
376 Renderer *CreateGfxTinyGL(int screenW, int screenH, Common::RenderMode renderMode);
377 Renderer *createRenderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
378 
379 } // End of namespace Freescape
380 
381 #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:536
StereoEye
Definition: gfx.h:85
Definition: gfx.h:63
Definition: rect.h:144
Definition: gfx.h:50