FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
renderbackendopengl.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 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 drawTriangle(const Point& p1, const Point& p2, const Point& p3, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
84  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);
85  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);
86  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);
87  virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
88  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);
89 
90  virtual void enableScissorTest();
91  virtual void disableScissorTest();
92 
93  virtual void attachRenderTarget(ImagePtr& img, bool discard);
94  virtual void detachRenderTarget();
95 
96  virtual void renderGuiGeometry(const std::vector<GuiVertex>& vertices, const std::vector<int>& indices, const DoublePoint& translation, ImagePtr texture);
97 
98  void enableTextures(uint32_t texUnit);
99  void disableTextures(uint32_t texUnit);
100  void bindTexture(uint32_t texUnit, GLuint texId);
101  void bindTexture(GLuint textId);
102 
103  protected:
104  virtual void setClipArea(const Rect& cliparea, bool clear);
105 
106  void enableLighting();
107  void disableLighting();
108  void enableStencilTest();
109  void disableStencilTest();
110  void setStencilTest(uint8_t stencil_ref, GLenum stencil_op, GLenum stencil_func);
111  uint8_t getStencilRef() const;
112  void enableAlphaTest();
113  void disableAlphaTest();
114  void setAlphaTest(float ref_alpha);
115  void enableDepthTest();
116  void disableDepthTest();
117  void enableColorArray();
118  void disableColorArray();
119  void setEnvironmentalColor(uint32_t texUnit, const uint8_t* rgba);
120  void setVertexPointer(GLint size, GLsizei stride, const GLvoid* ptr);
121  void setColorPointer(GLsizei stride, const GLvoid* ptr);
122  void setTexCoordPointer(uint32_t texUnit, GLsizei stride, const GLvoid* ptr);
123 
125  void prepareForOverlays();
126 
127  void renderWithoutZ();
128 
129  void renderWithZ();
130  void renderWithZTest();
131  void renderWithColorAndZ();
133 
134  class RenderObject;
135 
136  struct RenderZObject {
137  GLuint texture_id;
138  //uint32_t elements;
139  };
140 
141  // for regular primitives with color and alpha
142  struct renderDataP {
143  GLfloat vertex[2];
144  GLubyte color[4];
145  };
146 
147  // for regular textured quads
148  struct renderDataT {
149  GLfloat vertex[2];
150  GLfloat texel[2];
151  };
152 
153  // for textured quads with alpha
154  struct renderDataTC {
155  GLfloat vertex[2];
156  GLfloat texel[2];
157  GLubyte color[4];
158  };
159 
160  // for multitextured quads
161  struct renderData2TC {
162  GLfloat vertex[2];
163  GLfloat texel[2];
164  GLfloat texel2[2];
165  GLubyte color[4];
166  };
167 
168  // for regular textured quads with z
169  struct renderDataZ {
170  GLfloat vertex[3];
171  GLfloat texel[2];
172  };
173 
174  // for textured quads with alpha and z
176  GLfloat vertex[3];
177  GLfloat texel[2];
178  GLubyte color[4];
179  };
180 
181  // for multitextured quads with z
182  struct renderData2TCZ {
183  GLfloat vertex[3];
184  GLfloat texel[2];
185  GLfloat texel2[2];
186  GLubyte color[4];
187  };
188 
190  GLuint texture_id;
194  };
195  RenderZObjectTest* getRenderBufferObject(GLuint texture_id);
196  std::vector<renderDataZ> m_renderZ_datas;
197  std::vector<RenderZObjectTest> m_renderZ_objects;
198 
199  // Vertex data structs that dont use the depth buffer / z
200  // vertex data source for primitives that dont use depth buffer and texture - described by m_renderObjects
201  std::vector<renderDataP> m_renderPrimitiveDatas;
202  // vertex data source for textured quads that dont use depth buffer and color/alpha - described by m_renderObjects
203  std::vector<renderDataT> m_renderTextureDatas;
204  // vertex data source for textured quads with color/alpha that dont use depth buffer - described by m_renderObjects
205  std::vector<renderDataTC> m_renderTextureColorDatas;
206  // // vertex data source for multitexture quads that dont use depth buffer - described by m_renderObjects
207  std::vector<renderData2TC> m_renderMultitextureDatas;
208  std::vector<RenderObject> m_renderObjects;
209 
210  // Now the vertex data that do use the depth buffer / z
211  // vertex data source for textured quads that do use depth buffer but no color/alpha - described by m_renderTextureObjectsZ
212  std::vector<renderDataZ> m_renderTextureDatasZ;
213  std::vector<RenderZObject> m_renderTextureObjectsZ;
214 
215  // vertex data source for textured quads that do use depth buffer and color/alpha - described by m_renderTextureColorObjectsZ
216  std::vector<renderDataColorZ> m_renderTextureColorDatasZ;
217  std::vector<RenderZObject> m_renderTextureColorObjectsZ;
218 
219  // vertex data source for multitexture quads that do use depth buffer - described by m_renderMultitextureObjectsZ
220  std::vector<renderData2TCZ> m_renderMultitextureDatasZ;
221  std::vector<RenderObject> m_renderMultitextureObjectsZ;
222 
223  // vertex data source for other stuff, unlit quads like outlines and unlit demanded instances - described by m_renderForcedObjectsZ
224  std::vector<renderDataZ> m_renderForcedDatasZ;
225  std::vector<RenderObject> m_renderForcedObjectsZ;
226 
227  struct currentState {
228  // Textures
229  bool tex_enabled[4];
230  GLuint texture[4];
233  float alpha_test;
235 
236  // Pointers
237  const void* vertex_pointer;
238  const void* tex_pointer[4];
239  const void* color_pointer;
240 
241  // Stencil
244  GLint sten_buf;
245  GLenum sten_op;
246  GLenum sten_func;
247 
248  // Light
251 
252  // The rest
254  GLenum blend_src;
255  GLenum blend_dst;
260  } m_state;
261 
262  GLuint m_fbo_id;
265  };
266 
267 }
268 
269 #endif
270 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
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:47
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 uint32_t getLightingModel() const
Gets the current light model.
virtual void renderGuiGeometry(const std::vector< GuiVertex > &vertices, const std::vector< int > &indices, const DoublePoint &translation, ImagePtr texture)
Renders geometry required by gui.
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)
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.
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)
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 setLightingModel(uint32_t lighting)
Initializes the light.
virtual Image * createImage(IResourceLoader *loader=0)
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 captureScreen(const std::string &filename)
Creates a Screenshot and saves it to a file.
std::vector< renderDataT > m_renderTextureDatas
virtual void clearBackBuffer()
Forces a clear of the backbuffer.
virtual void changeBlending(int32_t scr, int32_t dst)
Change the Blendingmodel.
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 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.
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.
virtual const std::string & getName() const
The name of the renderbackend.