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  virtual void useColor(uint8 r, uint8 g, uint8 b) = 0;
80  virtual void enableCulling(bool enabled) {};
81  virtual void polygonOffset(bool enabled) = 0;
82 
83  virtual Texture *createTexture(const Graphics::Surface *surface, bool is3D = false) = 0;
84  Graphics::Surface *convertImageFormatIfNecessary(Graphics::ManagedSurface *surface);
85 
86  virtual void freeTexture(Texture *texture) = 0;
87  virtual void drawTexturedRect2D(const Common::Rect &screenRect, const Common::Rect &textureRect, Texture *texture) = 0;
88 
89  virtual void renderSensorShoot(byte color, const Math::Vector3d sensor, const Math::Vector3d player, const Common::Rect &viewPort) = 0;
90  virtual void renderPlayerShootBall(byte color, const Common::Point &position, int frame, const Common::Rect &viewPort) = 0;
91  virtual void renderPlayerShootRay(byte color, const Common::Point &position, const Common::Rect &viewPort) = 0;
92 
93  virtual void renderCrossair(const Common::Point &crossairPosition) = 0;
94 
95  virtual void renderCube(const Math::Vector3d &position, const Math::Vector3d &size, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
96  virtual void renderRectangle(const Math::Vector3d &position, const Math::Vector3d &size, Common::Array<uint8> *colours, Common::Array<uint8> *ecolours, float offset = 0.0);
97  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);
98  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);
99  virtual void renderFace(const Common::Array<Math::Vector3d> &vertices) = 0;
100 
101  void setColorRemaps(ColorReMap *colorRemaps);
102  virtual void clear(uint8 r, uint8 g, uint8 b, bool ignoreViewport = false) = 0;
103  virtual void drawFloor(uint8 color) = 0;
104  virtual void drawBackground(uint8 color);
105 
106  void drawEclipse(uint8 color1, uint8 color2, float difference);
107  virtual void drawSkybox(Texture *texture, Math::Vector3d camera) {};
108  virtual void drawThunder(Texture *texture, Math::Vector3d camera, float size) {};
109  virtual void drawCelestialBody(Math::Vector3d position, float radius, uint8 color) {};
110 
111  Common::Rect viewport() const;
112  virtual Common::Point nativeResolution() { return Common::Point(_screenW, _screenH); }
113 
114  // palette
115  void readFromPalette(uint8 index, uint8 &r, uint8 &g, uint8 &b);
116  void setPaletteValue(uint8 index, uint8 r, uint8 g, uint8 b);
117  uint8 indexFromColor(uint8 r, uint8 g, uint8 b);
118  uint8 mapEGAColor(uint8 index);
119 
120  bool getRGBAt(uint8 index, uint8 ecolor, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
121  bool getRGBAtC64(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
122  bool getRGBAtCGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
123  bool getRGBAtCPC(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
124  bool getRGBAtEGA(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2);
125  bool getRGBAtZX(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
126  bool getRGBAtHercules(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1, uint8 &r2, uint8 &g2, uint8 &b2, byte *&stipple);
127  void extractCPCIndexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
128  void extractC64Indexes(uint8 cm1, uint8 cm2, uint8 &i1, uint8 &i2);
129 
130  void selectColorFromFourColorPalette(uint8 index, uint8 &r1, uint8 &g1, uint8 &b1);
131 
132  // Stipple
133  virtual void setStippleData(byte *data) {};
134  virtual void useStipple(bool enabled) {};
135  void scaleStipplePattern(byte originalPattern[128], byte newPattern[128]);
136 
137  byte _defaultStippleArray[128] = {
138  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
139  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
140  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
141  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
142  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
143  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
144  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
145  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
146  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
147  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
148  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
149  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
150  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
151  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
152  0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
153  0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
154  };
155 
156  byte *_variableStippleArray;
157 
158  float _skyNormals[16][3] = {
159  { 0.0, 0.0, 1.0 }, //front //0
160  { 0.0, 0.0, 1.0 }, //1
161  { 0.0, 0.0, 1.0 }, //2
162  { 0.0, 0.0, 1.0 }, //3
163  { 0.0, 0.0, -1.0 }, //back //0
164  { 0.0, 0.0, -1.0 }, //1
165  { 0.0, 0.0, -1.0 }, //2
166  { 0.0, 0.0, -1.0 }, //3
167  { -1.0, 0.0, 0.0 }, //left
168  { -1.0, 0.0, 0.0 },
169  { -1.0, 0.0, 0.0 },
170  { -1.0, 0.0, 0.0 },
171  { 1.0, 0.0, 0.0 }, //right
172  { 1.0, 0.0, 0.0 },
173  { 1.0, 0.0, 0.0 },
174  { 1.0, 0.0, 0.0 }
175  };
176 
177  float _skyUvs1008[16][2] = {
178  { 0.0f, 0.0f }, //1
179  { 0.0f, 2.0f }, //2
180  { 0.4f, 2.0f }, //3
181  { 0.4f, 0.0f }, //front //4
182 
183  { 0.0f, 2.0f }, //back //1
184  { 0.4f, 2.0f }, //2
185  { 0.4f, 0.0f }, //3
186  { 0.0f, 0.0f }, //4
187 
188  { 0.0f, 0.0f }, //left //1
189  { 0.4f, 0.0f }, //2
190  { 0.4f, 2.0f }, //3
191  { 0.0f, 2.0f }, //4
192 
193  { 0.4f, 0.0f }, //right //1
194  { 0.0f, 0.0f }, //2
195  { 0.0f, 2.0f }, //3
196  { 0.4f, 2.0f }, //4
197  };
198 
199  float _skyUvs672[16][2] = {
200  { 0.0f, 0.0f }, //1
201  { 0.0f, 2.0f }, //2
202  { 0.6f, 2.0f }, //3
203  { 0.6f, 0.0f }, //front //4
204 
205  { 0.0f, 2.0f }, //back //1
206  { 0.6f, 2.0f }, //2
207  { 0.6f, 0.0f }, //3
208  { 0.0f, 0.0f }, //4
209 
210  { 0.0f, 0.0f }, //left //1
211  { 0.6f, 0.0f }, //2
212  { 0.6f, 2.0f }, //3
213  { 0.0f, 2.0f }, //4
214 
215  { 0.6f, 0.0f }, //right //1
216  { 0.0f, 0.0f }, //2
217  { 0.0f, 2.0f }, //3
218  { 0.6f, 2.0f }, //4
219  };
220 
221  float _skyUvs128[16][2] = {
222  { 0.0f, 0.0f }, //1
223  { 0.0f, 2.0f }, //2
224  { 2.5f, 2.0f }, //3
225  { 2.5f, 0.0f }, //front //4
226 
227  { 0.0f, 2.0f }, //back //1
228  { 2.5f, 2.0f }, //2
229  { 2.5f, 0.0f }, //3
230  { 0.0f, 0.0f }, //4
231 
232  { 0.0f, 0.0f }, //left //1
233  { 2.5f, 0.0f }, //2
234  { 2.5f, 2.0f }, //3
235  { 0.0f, 2.0f }, //4
236 
237  { 2.5f, 0.0f }, //right //1
238  { 0.0f, 0.0f }, //2
239  { 0.0f, 2.0f }, //3
240  { 2.5f, 2.0f }, //4
241  };
242 
243  float _skyVertices[16][3] = {
244  { -81280.0, 8128.0, 81280.0 }, //1 // Vertex #0 front
245  { -81280.0, -8128.0, 81280.0 }, //2 // Vertex #1
246  { 81280.0, -8128.0, 81280.0 }, //3 // Vertex #2
247  { 81280.0, 8128.0, 81280.0 }, //4 // Vertex #3
248 
249  { 81280.0f, -8128.0f, -81280.0f }, // 1
250  { -81280.0f, -8128.0f, -81280.0f }, // 2
251  { -81280.0f, 8128.0f, -81280.0f }, // 3
252  { 81280.0f, 8128.0f, -81280.0f }, // 4
253 
254  { -81280.0f, 8128.0f, 81280.0f }, //left //1
255  { -81280.0f, 8128.0f, -81280.0f }, //2
256  { -81280.0f, -8128.0f, -81280.0f }, //3
257  { -81280.0f, -8128.0f, 81280.0f }, //4
258 
259  { 81280.0f, 8128.0f, -81280.0f }, //right //1
260  { 81280.0f, 8128.0f, 81280.0f }, //2
261  { 81280.0f, -8128.0f, 81280.0f },//3
262  { 81280.0f, -8128.0f, -81280.0f },//4
263  };
264 
265  unsigned int _skyIndices[24] = {
266  0, 1, 2, // front
267  0, 2, 3,
268 
269  4, 5, 6, // back
270  4, 6, 7,
271 
272  8, 9, 10, // left
273  8, 10, 11,
274 
275  12, 13, 14, // right
276  12, 14, 15,
277  };
278 
279  byte *_palette;
280  void setColorMap(ColorMap *colorMap_);
281  ColorMap *_colorMap;
282  ColorReMap *_colorRemaps;
283  void clearColorPairArray();
284  void fillColorPairArray();
285  byte _colorPair[16];
286  int _keyColor;
287  int _inkColor;
288  int _paperColor;
289  int _underFireBackgroundColor;
290  Common::Point _shakeOffset;
291  byte _stipples[16][128];
292 
293  int _scale;
294 
295  // debug flags
296  bool _debugRenderBoundingBoxes;
297  bool _debugRenderOcclusionBoxes;
298  bool _debugRenderWireframe;
299  bool _debugRenderNormals;
300  Common::Array<uint8> _debugHighlightObjectIDs;
301 
302  // for drawing bounding boxes
303  virtual void drawAABB(const Math::AABB &aabb, uint8 r, uint8 g, uint8 b) {}
304 
311  virtual void positionCamera(const Math::Vector3d &pos, const Math::Vector3d &interest, float rollAngle = 0.0f) = 0;
312  virtual void updateProjectionMatrix(float fov, float aspectRatio, float nearClipPlane, float farClipPlane) = 0;
313 
314  Math::Matrix4 getMvpMatrix() const { return _mvpMatrix; }
315  virtual Graphics::Surface *getScreenshot() = 0;
316  void flipVertical(Graphics::Surface *s);
317 
318  int _screenW;
319  int _screenH;
320  Common::RenderMode _renderMode;
321 
322  bool computeScreenViewport();
323 
324 protected:
325  Common::Rect _screenViewport;
326  Common::Rect _viewport;
327  Common::Rect _unscaledViewport;
328 
329  Math::Matrix4 _projectionMatrix;
330  Math::Matrix4 _modelViewMatrix;
331  Math::Matrix4 _mvpMatrix;
332 
333  Math::Frustum _frustum;
334 
335  Math::Matrix4 makeProjectionMatrix(float fov, float nearClipPlane, float farClipPlane) const;
336 };
337 
338 Graphics::RendererType determinateRenderType();
339 Renderer *CreateGfxOpenGL(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
340 Renderer *CreateGfxOpenGLShader(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
341 Renderer *CreateGfxTinyGL(int screenW, int screenH, Common::RenderMode renderMode);
342 Renderer *createRenderer(int screenW, int screenH, Common::RenderMode renderMode, bool authenticGraphics);
343 
344 } // End of namespace Freescape
345 
346 #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
Definition: gfx.h:63
Definition: rect.h:144
Definition: gfx.h:50