FIFE  be64c707dea6b3250bd4355bf5c825d25920087d
renderbackendopengl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2019 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 #ifndef FIFE_VIDEO_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGL_H
23 #define FIFE_VIDEO_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGL_H
24 
25 // Standard C++ library includes
26 
27 // 3rd party library includes
28 
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "video/renderbackend.h"
34 
35 #include "fife_opengl.h"
36 
37 namespace FIFE {
38  class ScreenMode;
39 
44  public:
45  RenderBackendOpenGL(const SDL_Color& colorkey);
46  virtual ~RenderBackendOpenGL();
47  virtual const std::string& getName() const;
48  virtual void startFrame();
49  virtual void endFrame();
50  virtual void init(const std::string& driver);
51  virtual void clearBackBuffer();
52  virtual void setLightingModel(uint32_t lighting);
53  virtual uint32_t getLightingModel() const;
54  virtual void setLighting(float red, float green, float blue);
55  virtual void resetLighting();
56  virtual void resetStencilBuffer(uint8_t buffer);
57  virtual void changeBlending(int32_t scr, int32_t dst);
58 
59  virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon);
60  virtual void setScreenMode(const ScreenMode& mode);
61 
62  virtual Image* createImage(IResourceLoader* loader = 0);
63  virtual Image* createImage(const std::string& name, IResourceLoader* loader = 0);
64  virtual Image* createImage(const uint8_t* data, uint32_t width, uint32_t height);
65  virtual Image* createImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height);
66  virtual Image* createImage(SDL_Surface* surface);
67  virtual Image* createImage(const std::string& name, SDL_Surface* surface);
68 
69  virtual void renderVertexArrays();
70  virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgba);
71  virtual void addImageToArray(const Rect& rect, uint32_t id1, float const* st1, uint32_t id2, float const* st2, uint8_t alpha, uint8_t const* rgba);
72 
73  virtual void addImageToArrayZ(uint32_t id, const Rect& rect, float vertexZ, float const* st, uint8_t alpha, uint8_t const* rgba);
74  virtual void addImageToArrayZ(const Rect& rect, float vertexZ, uint32_t id1, float const* st1, uint32_t id2, float const* st2, uint8_t alpha, uint8_t const* rgba);
75 
76  virtual void changeRenderInfos(RenderDataType type, uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc, OverlayType otype = OVERLAY_TYPE_NONE);
77 
78  virtual void captureScreen(const std::string& filename);
79  virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height);
80 
81  virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
82  virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
83  virtual void drawThickLine(const Point& p1, const Point& p2, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
84  virtual void drawPolyLine(const std::vector<Point>& points, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
85  virtual void drawBezier(const std::vector<Point>& points, int32_t steps, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
86  virtual void drawTriangle(const Point& p1, const Point& p2, const Point& p3, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
87  virtual void drawRectangle(const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
88  virtual void fillRectangle(const Point& p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
89  virtual void drawQuad(const Point& p1, const Point& p2, const Point& p3, const Point& p4, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
90  virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
91  virtual void drawCircle(const Point& p, uint32_t radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
92  virtual void drawFillCircle(const Point& p, uint32_t radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
93  virtual void drawCircleSegment(const Point& p, uint32_t radius, int32_t sangle, int32_t eangle, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
94  virtual void drawFillCircleSegment(const Point& p, uint32_t radius, int32_t sangle, int32_t eangle, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
95  virtual void drawLightPrimitive(const Point& p, uint8_t intensity, float radius, int32_t subdivisions, float xstretch, float ystretch, uint8_t red, uint8_t green, uint8_t blue);
96 
97  virtual void enableScissorTest();
98  virtual void disableScissorTest();
99 
100  virtual void attachRenderTarget(ImagePtr& img, bool discard);
101  virtual void detachRenderTarget();
102 
103  virtual void renderGuiGeometry(const std::vector<GuiVertex>& vertices, const std::vector<int>& indices, const DoublePoint& translation, ImagePtr texture);
104 
105  void enableTextures(uint32_t texUnit);
106  void disableTextures(uint32_t texUnit);
107  void bindTexture(uint32_t texUnit, GLuint texId);
108  void bindTexture(GLuint textId);
109 
110  protected:
111  virtual void setClipArea(const Rect& cliparea, bool clear);
112 
113  void enableLighting();
114  void disableLighting();
115  void enableStencilTest();
116  void disableStencilTest();
117  void setStencilTest(uint8_t stencil_ref, GLenum stencil_op, GLenum stencil_func);
118  uint8_t getStencilRef() const;
119  void enableAlphaTest();
120  void disableAlphaTest();
121  void setAlphaTest(float ref_alpha);
122  void enableDepthTest();
123  void disableDepthTest();
124  void enableColorArray();
125  void disableColorArray();
126  void setEnvironmentalColor(uint32_t texUnit, const uint8_t* rgba);
127  void setVertexPointer(GLint size, GLsizei stride, const GLvoid* ptr);
128  void setColorPointer(GLsizei stride, const GLvoid* ptr);
129  void setTexCoordPointer(uint32_t texUnit, GLsizei stride, const GLvoid* ptr);
130 
132  void prepareForOverlays();
133 
134  void renderWithoutZ();
135 
136  void renderWithZ();
137  void renderWithZTest();
138  void renderWithColorAndZ();
140 
141  class RenderObject;
142 
143  struct RenderZObject {
144  GLuint texture_id;
145  //uint32_t elements;
146  };
147 
148  // for regular primitives with color and alpha
149  struct renderDataP {
150  GLfloat vertex[2];
151  GLubyte color[4];
152  };
153 
154  // for regular textured quads
155  struct renderDataT {
156  GLfloat vertex[2];
157  GLfloat texel[2];
158  };
159 
160  // for textured quads with alpha
161  struct renderDataTC {
162  GLfloat vertex[2];
163  GLfloat texel[2];
164  GLubyte color[4];
165  };
166 
167  // for multitextured quads
168  struct renderData2TC {
169  GLfloat vertex[2];
170  GLfloat texel[2];
171  GLfloat texel2[2];
172  GLubyte color[4];
173  };
174 
175  // for regular textured quads with z
176  struct renderDataZ {
177  GLfloat vertex[3];
178  GLfloat texel[2];
179  };
180 
181  // for textured quads with alpha and z
183  GLfloat vertex[3];
184  GLfloat texel[2];
185  GLubyte color[4];
186  };
187 
188  // for multitextured quads with z
189  struct renderData2TCZ {
190  GLfloat vertex[3];
191  GLfloat texel[2];
192  GLfloat texel2[2];
193  GLubyte color[4];
194  };
195 
197  GLuint texture_id;
201  };
203  std::vector<renderDataZ> m_renderZ_datas;
204  std::vector<RenderZObjectTest> m_renderZ_objects;
205 
206  // Vertex data structs that dont use the depth buffer / z
207  // vertex data source for primitives that dont use depth buffer and texture - described by m_renderObjects
208  std::vector<renderDataP> m_renderPrimitiveDatas;
209  // vertex data source for textured quads that dont use depth buffer and color/alpha - described by m_renderObjects
210  std::vector<renderDataT> m_renderTextureDatas;
211  // vertex data source for textured quads with color/alpha that dont use depth buffer - described by m_renderObjects
212  std::vector<renderDataTC> m_renderTextureColorDatas;
213  // // vertex data source for multitexture quads that dont use depth buffer - described by m_renderObjects
214  std::vector<renderData2TC> m_renderMultitextureDatas;
215  std::vector<RenderObject> m_renderObjects;
216 
217  // Dynamic index buffers
218  std::vector<uint32_t> m_pIndices;
219  std::vector<uint32_t> m_tIndices;
220  std::vector<uint32_t> m_tcIndices;
221  std::vector<uint32_t> m_tc2Indices;
222 
223  // Now the vertex data that do use the depth buffer / z
224  // vertex data source for textured quads that do use depth buffer but no color/alpha - described by m_renderTextureObjectsZ
225  std::vector<renderDataZ> m_renderTextureDatasZ;
226  std::vector<RenderZObject> m_renderTextureObjectsZ;
227 
228  // vertex data source for textured quads that do use depth buffer and color/alpha - described by m_renderTextureColorObjectsZ
229  std::vector<renderDataColorZ> m_renderTextureColorDatasZ;
230  std::vector<RenderZObject> m_renderTextureColorObjectsZ;
231 
232  // vertex data source for multitexture quads that do use depth buffer - described by m_renderMultitextureObjectsZ
233  std::vector<renderData2TCZ> m_renderMultitextureDatasZ;
234  std::vector<RenderObject> m_renderMultitextureObjectsZ;
235 
236  // vertex data source for other stuff, unlit quads like outlines and unlit demanded instances - described by m_renderForcedObjectsZ
237  std::vector<renderDataZ> m_renderForcedDatasZ;
238  std::vector<RenderObject> m_renderForcedObjectsZ;
239 
240  struct currentState {
241  // Textures
242  bool tex_enabled[4];
243  GLuint texture[4];
246  float alpha_test;
248 
249  // Pointers
250  const void* vertex_pointer;
251  const void* tex_pointer[4];
252  const void* color_pointer;
253 
254  // Stencil
257  GLint sten_buf;
258  GLenum sten_op;
259  GLenum sten_func;
260 
261  // Light
264 
265  // The rest
266  uint8_t env_color[4];
267  GLenum blend_src;
268  GLenum blend_dst;
273  } m_state;
274 
275  GLuint m_fbo_id;
278  std::vector<uint32_t> m_indices;
281  SDL_GLContext m_context;
282  };
283 
284 }
285 
286 #endif
Abstract interface for all the renderbackends.
virtual void setScreenMode(const ScreenMode &mode)
Sets the mainscreen display mode.
std::vector< RenderZObjectTest > m_renderZ_objects
virtual void setLighting(float red, float green, float blue)
Set colors for lighting.
Base Class for Images.
Definition: image.h:48
virtual void drawFillCircleSegment(const Point &p, uint32_t radius, int32_t sangle, int32_t eangle, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws a filled circle segment.
virtual void fillRectangle(const Point &p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws a filled axis parallel rectangle.
virtual void renderGuiGeometry(const std::vector< GuiVertex > &vertices, const std::vector< int > &indices, const DoublePoint &translation, ImagePtr texture)
Renders geometry required by gui.
virtual void drawFillCircle(const Point &p, uint32_t radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws a filled circle.
std::vector< renderDataP > m_renderPrimitiveDatas
std::vector< renderDataZ > m_renderForcedDatasZ
virtual void renderVertexArrays()
Render the Vertex Arrays, only for primitives (points, lines,...)
std::vector< RenderObject > m_renderMultitextureObjectsZ
std::vector< renderDataColorZ > m_renderTextureColorDatasZ
void disableTextures(uint32_t texUnit)
virtual const std::string & getName() const
The name of the renderbackend.
std::vector< RenderObject > m_renderForcedObjectsZ
virtual void init(const std::string &driver)
Initializes the backend.
virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Writes pixel to given position.
std::vector< renderData2TC > m_renderMultitextureDatas
virtual void drawQuad(const Point &p1, const Point &p2, const Point &p3, const Point &p4, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws quad between given points with given RGBA.
std::vector< uint32_t > m_tc2Indices
virtual void drawTriangle(const Point &p1, const Point &p2, const Point &p3, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws triangle between given points with given RGBA.
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws line between given points with given RGBA.
virtual void setClipArea(const Rect &cliparea, bool clear)
Sets given clip area into image.
virtual void drawVertex(const Point &p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws a quad that represents a vertex with given RGBA.
void setStencilTest(uint8_t stencil_ref, GLenum stencil_op, GLenum stencil_func)
void enableTextures(uint32_t texUnit)
std::vector< uint32_t > m_indices
static indices for vertex data with z
virtual void addImageToArray(uint32_t id, const Rect &rec, float const *st, uint8_t alpha, uint8_t const *rgba)
Add the Image data to the array.
void setVertexPointer(GLint size, GLsizei stride, const GLvoid *ptr)
virtual void drawCircleSegment(const Point &p, uint32_t radius, int32_t sangle, int32_t eangle, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws a circle segment.
virtual void setLightingModel(uint32_t lighting)
Initializes the light.
virtual Image * createImage(IResourceLoader *loader=0)
std::vector< uint32_t > m_pIndices
unsigned char uint8_t
Definition: core.h:38
virtual void resetLighting()
Reset lighting with default values.
virtual void disableScissorTest()
Disables scissor test on the render backend.
virtual void drawCircle(const Point &p, uint32_t radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws a circle.
virtual void captureScreen(const std::string &filename)
Creates a Screenshot and saves it to a file.
std::vector< renderDataT > m_renderTextureDatas
virtual uint32_t getLightingModel() const
Gets the current light model.
virtual void drawThickLine(const Point &p1, const Point &p2, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws line between given points with given RGBA and width.
virtual void clearBackBuffer()
Forces a clear of the backbuffer.
virtual void changeBlending(int32_t scr, int32_t dst)
Change the Blendingmodel.
virtual void drawBezier(const std::vector< Point > &points, int32_t steps, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws bezier curve between given points with given RGBA and width.
struct FIFE::RenderBackendOpenGL::currentState m_state
virtual void changeRenderInfos(RenderDataType type, uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc, OverlayType otype=OVERLAY_TYPE_NONE)
Dirty helper function to change the render infos.
unsigned short uint16_t
Definition: core.h:39
virtual void drawPolyLine(const std::vector< Point > &points, uint8_t width, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws lines between given points with given RGBA and width.
virtual void detachRenderTarget()
Detaches current render surface.
void setTexCoordPointer(uint32_t texUnit, GLsizei stride, const GLvoid *ptr)
std::vector< renderDataZ > m_renderZ_datas
std::vector< renderDataTC > m_renderTextureColorDatas
std::vector< renderData2TCZ > m_renderMultitextureDatasZ
void setAlphaTest(float ref_alpha)
virtual void createMainScreen(const ScreenMode &mode, const std::string &title, const std::string &icon)
Creates the mainscreen (the display window).
std::vector< renderDataZ > m_renderTextureDatasZ
std::vector< RenderObject > m_renderObjects
RenderZObjectTest * getRenderBufferObject(GLuint texture_id)
RenderBackendOpenGL(const SDL_Color &colorkey)
void setEnvironmentalColor(uint32_t texUnit, const uint8_t *rgba)
void setColorPointer(GLsizei stride, const GLvoid *ptr)
virtual void drawRectangle(const Point &p, uint16_t w, uint16_t h, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Draws an axis parallel rectangle.
std::vector< RenderZObject > m_renderTextureColorObjectsZ
virtual void drawLightPrimitive(const Point &p, uint8_t intensity, float radius, int32_t subdivisions, float xstretch, float ystretch, uint8_t red, uint8_t green, uint8_t blue)
Draws a light primitive that based on a triangle fan.
virtual void addImageToArrayZ(uint32_t id, const Rect &rect, float vertexZ, float const *st, uint8_t alpha, uint8_t const *rgba)
virtual void attachRenderTarget(ImagePtr &img, bool discard)
Attaches given image as a new render surface.
RenderDataType
unsigned int uint32_t
Definition: core.h:40
std::vector< RenderZObject > m_renderTextureObjectsZ
virtual void startFrame()
Called when a new frame starts.
std::vector< uint32_t > m_tcIndices
std::vector< uint32_t > m_tIndices
virtual void enableScissorTest()
Enables scissor test on the render backend.
The main class of the OpenGL-based renderer.
void bindTexture(uint32_t texUnit, GLuint texId)
virtual void endFrame()
Called when a frame is finished and ready to be displayed.
virtual void resetStencilBuffer(uint8_t buffer)
Reset stencil buffer with given value.