FIFE  2008.0
renderbackendopengle.h
1 /***************************************************************************
2  * Copyright (C) 2005-2011 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_RENDERBACKENDOPENGLE_H
23 #define FIFE_VIDEO_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGLE_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 #include "video/opengl/fife_opengl.h"
35 
36 namespace FIFE {
37  class ScreenMode;
38 
43  public:
44  RenderBackendOpenGLe(const SDL_Color& colorkey);
45  virtual ~RenderBackendOpenGLe();
46  virtual const std::string& getName() const;
47  virtual void startFrame();
48  virtual void endFrame();
49  virtual void init(const std::string& driver);
50  virtual void clearBackBuffer();
51  virtual void setLightingModel(uint32_t lighting);
52  virtual uint32_t getLightingModel() const;
53  virtual void setLighting(float red, float green, float blue);
54  virtual void resetLighting();
55  virtual void resetStencilBuffer(uint8_t buffer);
56  virtual void changeBlending(int32_t scr, int32_t dst);
57 
58  virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon);
59  virtual void setScreenMode(const ScreenMode& mode);
60 
61  virtual Image* createImage(IResourceLoader* loader = 0);
62  virtual Image* createImage(const std::string& name, IResourceLoader* loader = 0);
63  virtual Image* createImage(const uint8_t* data, uint32_t width, uint32_t height);
64  virtual Image* createImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height);
65  virtual Image* createImage(SDL_Surface* surface);
66  virtual Image* createImage(const std::string& name, SDL_Surface* surface);
67 
68  virtual void renderVertexArrays();
69  virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgb);
70  virtual void addImageToArrayZ(uint32_t id, const Rect& rec, float vertexZ, float const* st, uint8_t alpha, bool forceNewBatch, uint8_t const* rgb);
71  virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc);
72  virtual void captureScreen(const std::string& filename);
73  virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height);
74 
75  virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
76  virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
77  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);
78  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);
79  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);
80  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);
81  virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
82  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);
83 
84  virtual void attachRenderTarget(ImagePtr& img, bool discard);
85  virtual void detachRenderTarget();
86 
87  void enableTextures(uint32_t texUnit);
88  void disableTextures(uint32_t texUnit);
89  void bindTexture(uint32_t texUnit, GLuint texId);
90  void bindTexture(GLuint textId);
91 
92  static const float zfar;
93  static const float znear;
94 
95  protected:
96  virtual void setClipArea(const Rect& cliparea, bool clear);
97 
98  void enableLighting();
99  void disableLighting();
100  void enableStencilTest();
101  void disableStencilTest();
102  void setStencilTest(uint8_t stencil_ref, GLenum stencil_op, GLenum stencil_func);
103  void enableAlphaTest();
104  void disableAlphaTest();
105  void setAlphaTest(float ref_alpha);
106  void enableDepthTest();
107  void disableDepthTest();
108  void setEnvironmentalColor(const uint8_t* rgb);
109  void setVertexPointer(GLsizei stride, const GLvoid* ptr);
110  void setColorPointer(GLsizei stride, const GLvoid* ptr);
111  void setTexCoordPointer(uint32_t texUnit, GLsizei stride, const GLvoid* ptr);
112  void enableScissorTest();
113  void disableScissorTest();
114 
115  void renderWithZ();
116  void renderWithoutZ();
117 
118  GLuint m_mask_overlays;
119  void prepareForOverlays();
120 
121  // only for textured quads
122  struct RenderZData {
123  GLfloat vertex[3];
124  GLfloat texel[2];
125  };
126 
127  // structure for colored overlay
128  struct RenderZData2T {
129  GLfloat vertex[3];
130  GLfloat texel[2];
131  GLfloat texel2[2];
132  GLubyte color[4];
133  };
134 
135  // rest (data for objects that don't utilise depth buffer)
136  struct RenderData {
137  GLfloat vertex[2];
138  GLfloat texel[2];
139  GLubyte color[4];
140  };
141 
142  // describe chunk of batched (by texture) quads
143  struct RenderZObject {
144  GLuint texture_id;
145  uint32_t elements;
146  uint32_t index;
147  uint32_t max_size;
148  };
149  class RenderObject;
150 
151  RenderZObject* getRenderBufferObject(GLuint texture_id, bool unlit = false);
152 
153  // main source of vertex data - described by m_renderZ_objects
154  std::vector<RenderZData> m_renderZ_datas;
155  std::vector<RenderZObject> m_renderZ_objects;
156 
157  // vertex data source for transparent primitives - described by m_render_trans_objects
158  std::vector<RenderZData2T> m_render_trans_datas;
159  std::vector<RenderObject> m_render_trans_objects;
160 
161  // vertex data source for colored overlays - described by m_render_objects2T
162  std::vector<RenderZData2T> m_render_datas2T;
163  std::vector<RenderObject> m_render_objects2T;
164 
165  // note: vertex data comes from m_renderZ_datas
166  std::vector<RenderZObject> m_renderZ_objects_forced;
167 
168  // vertex data source for primitives that dont use depth buffer - described by m_render_objects
169  std::vector<RenderData> m_render_datas;
170  std::vector<RenderObject> m_render_objects;
171 
172  struct currentState {
173  // Textures
174  bool tex_enabled[2];
175  GLuint texture[2];
176  uint32_t active_tex;
177  uint32_t active_client_tex;
178 
179  // Light
180  uint32_t lightmodel;
181  bool light_enabled;
182 
183  // Stencil
184  bool sten_enabled;
185  uint8_t sten_ref;
186  GLint sten_buf;
187  GLenum sten_op;
188  GLenum sten_func;
189 
190  // The rest
191  uint8_t env_color[3];
192  GLenum blend_src;
193  GLenum blend_dst;
194  bool alpha_enabled;
195  bool depth_enabled;
196  bool scissor_test;
197  } m_state;
198 
199  GLuint m_fbo_id;
200  ImagePtr m_img_target;
201  bool m_target_discard;
202  };
203 }
204 
205 #endif
206 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
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)
virtual void attachRenderTarget(ImagePtr &img, bool discard)
virtual void init(const std::string &driver)
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)
virtual void changeBlending(int32_t scr, int32_t dst)
virtual void createMainScreen(const ScreenMode &mode, const std::string &title, const std::string &icon)
virtual void drawLine(const Point &p1, const Point &p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
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)
virtual void setLighting(float red, float green, float blue)
virtual const std::string & getName() const
virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc)
virtual void setScreenMode(const ScreenMode &mode)
virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
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)
virtual void setLightingModel(uint32_t lighting)
virtual void addImageToArray(uint32_t id, const Rect &rec, float const *st, uint8_t alpha, uint8_t const *rgb)
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)
virtual void captureScreen(const std::string &filename)
virtual void drawVertex(const Point &p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
virtual uint32_t getLightingModel() const
virtual void resetStencilBuffer(uint8_t buffer)
virtual void setClipArea(const Rect &cliparea, bool clear)
credit to phoku for his NodeDisplay example which the visitor code is adapted from ( he coded the qua...
Definition: soundclip.cpp:39