renderbackendopengle.h

00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by the FIFE team                              *
00003  *   http://www.fifengine.net                                              *
00004  *   This file is part of FIFE.                                            *
00005  *                                                                         *
00006  *   FIFE is free software; you can redistribute it and/or                 *
00007  *   modify it under the terms of the GNU Lesser General Public            *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2.1 of the License, or (at your option) any later version.    *
00010  *                                                                         *
00011  *   This library is distributed in the hope that it will be useful,       *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00014  *   Lesser General Public License for more details.                       *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Lesser General Public      *
00017  *   License along with this library; if not, write to the                 *
00018  *   Free Software Foundation, Inc.,                                       *
00019  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
00020  ***************************************************************************/
00021 
00022 #ifndef FIFE_VIDEO_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGLE_H
00023 #define FIFE_VIDEO_RENDERBACKENSD_OPENGL_RENDERBACKENDOPENGLE_H
00024 
00025 // Standard C++ library includes
00026 
00027 // 3rd party library includes
00028 
00029 // FIFE includes
00030 // These includes are split up in two parts, separated by one empty line
00031 // First block: files included from the FIFE root src directory
00032 // Second block: files included from the same folder
00033 #include "video/renderbackend.h"
00034 #include "video/opengl/fife_opengl.h"
00035 
00036 namespace FIFE {
00037     class ScreenMode;
00038 
00042     class RenderBackendOpenGLe : public RenderBackend {
00043     public:
00044         RenderBackendOpenGLe(const SDL_Color& colorkey);
00045         virtual ~RenderBackendOpenGLe();
00046         virtual const std::string& getName() const;
00047         virtual void startFrame();
00048         virtual void endFrame();
00049         virtual void init(const std::string& driver);
00050         virtual void clearBackBuffer();
00051         virtual void setLightingModel(uint32_t lighting);
00052         virtual uint32_t getLightingModel() const;
00053         virtual void setLighting(float red, float green, float blue);
00054         virtual void resetLighting();
00055         virtual void resetStencilBuffer(uint8_t buffer);
00056         virtual void changeBlending(int32_t scr, int32_t dst);
00057 
00058         virtual void createMainScreen(const ScreenMode& mode, const std::string& title, const std::string& icon);
00059         virtual void setScreenMode(const ScreenMode& mode);
00060 
00061         virtual Image* createImage(IResourceLoader* loader = 0);
00062         virtual Image* createImage(const std::string& name, IResourceLoader* loader = 0);
00063         virtual Image* createImage(const uint8_t* data, uint32_t width, uint32_t height);
00064         virtual Image* createImage(const std::string& name, const uint8_t* data, uint32_t width, uint32_t height);
00065         virtual Image* createImage(SDL_Surface* surface);
00066         virtual Image* createImage(const std::string& name, SDL_Surface* surface);
00067 
00068         virtual void renderVertexArrays();
00069         virtual void addImageToArray(uint32_t id, const Rect& rec, float const* st, uint8_t alpha, uint8_t const* rgb);
00070         virtual void addImageToArrayZ(uint32_t id, const Rect& rec, float vertexZ, float const* st, uint8_t alpha, bool forceNewBatch, uint8_t const* rgb);
00071         virtual void changeRenderInfos(uint16_t elements, int32_t src, int32_t dst, bool light, bool stentest, uint8_t stenref, GLConstants stenop, GLConstants stenfunc);
00072         virtual void captureScreen(const std::string& filename);
00073         virtual void captureScreen(const std::string& filename, uint32_t width, uint32_t height);
00074 
00075         virtual bool putPixel(int32_t x, int32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
00076         virtual void drawLine(const Point& p1, const Point& p2, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
00077         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);
00078         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);
00079         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);
00080         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);
00081         virtual void drawVertex(const Point& p, const uint8_t size, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255);
00082         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);
00083 
00084         virtual void attachRenderTarget(ImagePtr& img, bool discard);
00085         virtual void detachRenderTarget();
00086 
00087         void enableTextures(uint32_t texUnit);
00088         void disableTextures(uint32_t texUnit);
00089         void bindTexture(uint32_t texUnit, GLuint texId);
00090         void bindTexture(GLuint textId);
00091 
00092         static const float zfar;
00093         static const float znear;
00094 
00095     protected:
00096         virtual void setClipArea(const Rect& cliparea, bool clear);
00097 
00098         void enableLighting();
00099         void disableLighting();
00100         void enableStencilTest();
00101         void disableStencilTest();
00102         void setStencilTest(uint8_t stencil_ref, GLenum stencil_op, GLenum stencil_func);
00103         void enableAlphaTest();
00104         void disableAlphaTest();
00105         void setAlphaTest(float ref_alpha);
00106         void enableDepthTest();
00107         void disableDepthTest();
00108         void setEnvironmentalColor(const uint8_t* rgb);
00109         void setVertexPointer(GLsizei stride, const GLvoid* ptr);
00110         void setColorPointer(GLsizei stride, const GLvoid* ptr);
00111         void setTexCoordPointer(uint32_t texUnit, GLsizei stride, const GLvoid* ptr);
00112         void enableScissorTest();
00113         void disableScissorTest();
00114 
00115         void renderWithZ();
00116         void renderWithoutZ();
00117 
00118         GLuint m_mask_overlays;
00119         void prepareForOverlays();
00120         
00121         // only for textured quads
00122         struct RenderZData {
00123             GLfloat vertex[3];
00124             GLfloat texel[2];
00125         };
00126 
00127         // structure for colored overlay
00128         struct RenderZData2T {
00129             GLfloat vertex[3];
00130             GLfloat texel[2];
00131             GLfloat texel2[2];
00132             GLubyte color[4];
00133         };
00134 
00135         // rest (data for objects that don't utilise depth buffer)
00136         struct RenderData {
00137             GLfloat vertex[2];
00138             GLfloat texel[2];
00139             GLubyte color[4];
00140         };
00141 
00142         // describe chunk of batched (by texture) quads 
00143         struct RenderZObject {
00144             GLuint texture_id;
00145             uint32_t elements;
00146             uint32_t index;
00147             uint32_t max_size;
00148         };
00149         class RenderObject;
00150 
00151         RenderZObject* getRenderBufferObject(GLuint texture_id, bool unlit = false);
00152 
00153         // main source of vertex data - described by m_renderZ_objects
00154         std::vector<RenderZData> m_renderZ_datas;
00155         std::vector<RenderZObject> m_renderZ_objects;
00156 
00157         // vertex data source for transparent primitives - described by m_render_trans_objects
00158         std::vector<RenderZData2T> m_render_trans_datas;
00159         std::vector<RenderObject> m_render_trans_objects;
00160 
00161         // vertex data source for colored overlays - described by m_render_objects2T
00162         std::vector<RenderZData2T> m_render_datas2T;
00163         std::vector<RenderObject> m_render_objects2T;
00164 
00165         // note: vertex data comes from m_renderZ_datas
00166         std::vector<RenderZObject> m_renderZ_objects_forced;
00167 
00168         // vertex data source for primitives that dont use depth buffer - described by m_render_objects
00169         std::vector<RenderData> m_render_datas;
00170         std::vector<RenderObject> m_render_objects;
00171 
00172         struct currentState {
00173             // Textures
00174             bool tex_enabled[2];
00175             GLuint texture[2];
00176             uint32_t active_tex;
00177             uint32_t active_client_tex;
00178 
00179             // Light
00180             uint32_t lightmodel;
00181             bool light_enabled;
00182 
00183             // Stencil
00184             bool sten_enabled;
00185             uint8_t sten_ref;
00186             GLint sten_buf;
00187             GLenum sten_op;
00188             GLenum sten_func;
00189 
00190             // The rest
00191             uint8_t env_color[3];
00192             GLenum blend_src;
00193             GLenum blend_dst;
00194             bool alpha_enabled;
00195             bool depth_enabled;
00196             bool scissor_test;
00197         } m_state;
00198 
00199         GLuint m_fbo_id;
00200         ImagePtr m_img_target;
00201         bool m_target_discard;
00202     };
00203 }
00204 
00205 #endif
00206 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */