Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsfbgl.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #ifndef MMSFBGL_H_
00034 #define MMSFBGL_H_
00035 
00036 #include "mmsgui/fb/mmskms.h"
00037 #include "mmstools/mmstypes.h"
00038 #include <stack>
00039 
00040 #ifdef __HAVE_OPENGL__
00041 
00042 #include <string.h>
00043 
00044 #ifdef __HAVE_GL2__
00045 #include <GL/glew.h>
00046 #include <GL/gl.h>
00047 #include <GL/glext.h>
00048 #endif
00049 
00050 #ifdef __HAVE_GLX__
00051 #include <GL/glx.h>
00052 #endif
00053 
00054 #ifdef __HAVE_GLES2__
00055 #include <GLES2/gl2.h>
00056 #include <GLES2/gl2ext.h>
00057 #endif
00058 
00059 #ifdef __HAVE_EGL__
00060 #include <EGL/egl.h>
00061 #endif
00062 
00063 #ifdef GL_HALF_FLOAT_OES
00064 #define __HAVE_OGL_HALF_FLOAT__
00065 #endif
00066 
00067 //! Wrapper class for all supported Open GL versions.
00068 /*!
00069 \author Jens Schneider
00070 */
00071 class MMSFBGL {
00072     private:
00073         // bind the "VSVertex" attribute to shader index 0
00074         #define MMSFBGL_VSV_LOC 0
00075 
00076         typedef enum {
00077             MMSFBGL_SHADER_TYPE_FRAGMENT_SHADER = 0,
00078             MMSFBGL_SHADER_TYPE_VERTEX_SHADER
00079         } MMSFBGL_SHADER_TYPE;
00080 
00081 #ifdef __HAVE_XLIB__
00082         Display *x_display;
00083         Window  x_window;
00084 #endif
00085 
00086 #ifdef __HAVE_GLX__
00087         XVisualInfo *xvi;
00088         GLXContext  glx_context;
00089 #endif
00090 
00091 #ifdef __HAVE_EGL__
00092         EGLDisplay eglDisplay;
00093         EGLConfig  eglConfig[10];
00094         EGLSurface eglSurface;
00095         EGLContext eglContext;
00096 #endif
00097 
00098         bool initialized;
00099 
00100         int screen_width;
00101         int screen_height;
00102 
00103         //! frame buffer object which is currently bound
00104         GLuint bound_fbo;
00105 
00106         //! vertex buffer object which is currently bound
00107         GLuint bound_vbo;
00108 
00109         //! index buffer object which is currently bound
00110         GLuint bound_ibo;
00111 
00112         //! program handle to the fragment and vertex shader used for drawing primitives
00113         GLuint po_draw;
00114 
00115         //! program handle to the fragment and vertex shader used for blitting
00116         GLuint po_blit;
00117 
00118         //! program handle to the fragment and vertex shader used for blitting with source modulation beforehand
00119         GLuint po_modulateblit;
00120 
00121         //! program handle to the fragment and vertex shader used for blitting from alpha
00122         GLuint po_blit_fromalpha;
00123 
00124         //! program handle to the fragment and vertex shader used for blitting from alpha source and modulation beforehand
00125         GLuint po_modulateblit_fromalpha;
00126 
00127         //! currently active program
00128         GLuint po_current;
00129 
00130         //! matrix location in the vertex shader
00131         GLint VSMatrixLoc;
00132 
00133         //! matrix location in the vertex shader initialized
00134         bool VSMatrixLoc_initialized;
00135 
00136         //! color location in the fragment shader
00137         GLint FSColorLoc;
00138 
00139         //! color location in the fragment shader initialized
00140         bool FSColorLoc_initialized;
00141 
00142         //! texture location in the fragment shader
00143         GLint FSTextureLoc;
00144 
00145         //! texture location in the fragment shader initialized
00146         bool FSTextureLoc_initialized;
00147 
00148         //! texture coordinates location in the vertex shader
00149         GLint VSTexCoordLoc;
00150 
00151         //! texture coordinates location in the vertex shader initialized
00152         bool VSTexCoordLoc_initialized;
00153 
00154         //! current matrix
00155         MMSMatrix   current_matrix;
00156 
00157         //! use kms graphics system
00158         bool useKMS;
00159 
00160 #ifdef __HAVE_KMS__
00161         DRM     *drm;
00162         DRM_FB  *drm_fb;
00163 #endif
00164 
00165         //! current color
00166         unsigned char   current_color_r;
00167         unsigned char   current_color_g;
00168         unsigned char   current_color_b;
00169         unsigned char   current_color_a;
00170 
00171         class MMSFBGLStackMatrix {
00172         public:
00173             MMSMatrix matrix;
00174             MMSFBGLStackMatrix(MMSMatrix matrix) {
00175                 memcpy(this->matrix, matrix, sizeof(MMSMatrix));
00176             }
00177             void getMatrix(MMSMatrix matrix) {
00178                 memcpy(matrix, this->matrix, sizeof(MMSMatrix));
00179             }
00180         };
00181 
00182         //! matrix stack
00183         std::stack<MMSFBGLStackMatrix> matrix_stack;
00184 
00185         void printImplementationInformation();
00186 
00187         bool getError(const char* where, int line = __LINE__);
00188 
00189         bool buildShader(MMSFBGL_SHADER_TYPE shader_type, const char *shader_code, GLuint *shader);
00190         bool linkProgram(GLuint fragment_shader, GLuint vertex_shader, GLuint *program);
00191         bool buildShaderProgram(const char *fragment_shader_code, const char *vertex_shader_code, GLuint *program);
00192         bool buildShaderProgram4Drawing(GLuint *program);
00193         bool buildShaderProgram4Blitting(GLuint *program);
00194         bool buildShaderProgram4ModulateBlitting(GLuint *program);
00195         bool buildShaderProgram4BlittingFromAlpha(GLuint *program);
00196         bool buildShaderProgram4ModulateBlittingFromAlpha(GLuint *program);
00197 
00198         void deleteShaders();
00199         bool initShaders();
00200 
00201         bool useShaderProgram4Drawing();
00202         bool useShaderProgram4Blitting();
00203         bool useShaderProgram4ModulateBlitting();
00204         bool useShaderProgram4BlittingFromAlpha();
00205         bool useShaderProgram4ModulateBlittingFromAlpha();
00206 
00207     public:
00208         MMSFBGL();
00209         ~MMSFBGL();
00210 
00211 #ifdef __HAVE_XLIB__
00212         bool init(Display *x_display, int x_screen, Window x_window, int w, int h);
00213 #else
00214         bool init();
00215 #endif
00216         bool terminate();
00217         bool getResolution(int *w, int *h);
00218         bool swap();
00219 
00220         bool genBuffer(GLuint *bo);
00221         bool deleteBuffer(GLuint bo);
00222         bool bindBuffer(GLenum target, GLuint bo);
00223         bool initVertexBuffer(GLuint vbo, unsigned int size, const GLvoid *data = NULL);
00224         bool initVertexSubBuffer(GLuint vbo, unsigned int offset, unsigned int size, const GLvoid *data);
00225         bool enableVertexBuffer(GLuint vbo);
00226         void disableVertexBuffer();
00227         bool initIndexBuffer(GLuint ibo, unsigned int size, const GLvoid *data = NULL);
00228         bool initIndexSubBuffer(GLuint ibo, unsigned int offset, unsigned int size, const GLvoid *data);
00229         bool enableIndexBuffer(GLuint ibo);
00230         void disableIndexBuffer();
00231 
00232         bool genTexture(GLuint *tex);
00233         bool deleteTexture(GLuint tex);
00234         bool bindTexture2D(GLuint tex);
00235         bool initTexture2D(GLuint tex, GLenum texture_format, void *buffer, GLenum buffer_format, int sw, int sh);
00236         bool initSubTexture2D(GLuint tex, void *buffer, GLenum buffer_format, int sw, int sh, int dx, int dy);
00237         bool enableTexture2D(GLuint tex);
00238         void disableTexture2D();
00239 
00240         bool genFrameBuffer(GLuint *fbo);
00241         bool deleteFrameBuffer(GLuint fbo);
00242         bool genRenderBuffer(GLuint *rbo);
00243         bool deleteRenderBuffer(GLuint rbo);
00244         bool attachTexture2FrameBuffer(GLuint fbo, GLuint tex);
00245         bool attachRenderBuffer2FrameBuffer(GLuint fbo, GLuint rbo, int width, int height);
00246 
00247         bool allocTexture(GLuint tex, int width, int height);
00248         bool allocFBO(GLuint fbo, GLuint tex, int width, int height);
00249         bool allocFBOandRBO(GLuint fbo, GLuint tex, GLuint rbo, int width, int height);
00250         bool freeFBO(GLuint fbo, GLuint tex, GLuint rbo = 0);
00251 
00252         bool bindFrameBuffer(GLuint ogl);
00253         bool setScissor(GLint x, GLint y, GLsizei width, GLsizei height);
00254         bool disableScissor();
00255 
00256         void enableBlend(GLenum srcRGB = GL_SRC_ALPHA, GLenum dstRGB = GL_ONE_MINUS_SRC_ALPHA,
00257                           GLenum srcAlpha = GL_ONE, GLenum dstAlpha = GL_ONE_MINUS_SRC_ALPHA);
00258         void disableBlend();
00259         void enableDepthTest(bool readonly = false);
00260         void disableDepthTest();
00261         void setDrawingMode();
00262         void setTexEnvReplace(GLenum format);
00263         void setTexEnvModulate(GLenum format);
00264         void disableArrays();
00265 
00266         bool setCurrentMatrix(MMSMatrix matrix);
00267         bool getCurrentMatrix(MMSMatrix matrix);
00268         bool scaleCurrentMatrix(GLfloat sx, GLfloat sy, GLfloat sz);
00269         bool translateCurrentMatrix(GLfloat tx, GLfloat ty, GLfloat tz);
00270         bool rotateCurrentMatrix(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
00271 
00272         bool getParallelProjectionMatrix(MMSMatrix result, float left, float right, float bottom, float top, float nearZ, float farZ);
00273         bool getCentralProjectionMatrix(MMSMatrix result, float left, float right, float bottom, float top, float nearZ, float farZ);
00274         bool getPerspectiveMatrix(MMSMatrix result, float fovy, float aspect, float nearZ, float farZ);
00275 
00276         bool setParallelProjection(float left, float right, float bottom, float top, float nearZ, float farZ);
00277         bool setCentralProjection(float left, float right, float bottom, float top, float nearZ, float farZ);
00278         bool setPerspective(float fovy, float aspect, float nearZ, float farZ);
00279 
00280         bool pushCurrentMatrix();
00281         bool popCurrentMatrix();
00282 
00283         bool clear(unsigned char r = 0x00, unsigned char g = 0x00, unsigned char b = 0x00, unsigned char a = 0x00);
00284         bool setColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
00285 
00286         bool drawLine2D(float x1, float y1, float x2, float y2);
00287         bool drawLine2Di(int x1, int y1, int x2, int y2);
00288 
00289         bool drawRectangle2D(float x1, float y1, float x2, float y2);
00290         bool drawRectangle2Di(int x1, int y1, int x2, int y2);
00291 
00292 
00293         bool fillTriangle(float x1, float y1, float z1,
00294                              float x2, float y2, float z2,
00295                              float x3, float y3, float z3);
00296         bool fillTriangle2D(float x1, float y1, float x2, float y2, float x3, float y3);
00297 
00298         bool fillRectangle2D(float x1, float y1, float x2, float y2);
00299         bool fillRectangle2Di(int x1, int y1, int x2, int y2);
00300 
00301 
00302         bool stretchBlit3D(GLuint src_tex, float sx1, float sy1, float sx2, float sy2,
00303                                           float dx1, float dy1, float dz1,
00304                                           float dx2, float dy2, float dz2,
00305                                           float dx3, float dy3, float dz3,
00306                                           float dx4, float dy4, float dz4);
00307 
00308         bool stretchBlit(GLuint src_tex, float sx1, float sy1, float sx2, float sy2,
00309                                           float dx1, float dy1, float dx2, float dy2);
00310         bool stretchBliti(GLuint src_tex, int sx1, int sy1, int sx2, int sy2, int sw, int sh,
00311                                            int dx1, int dy1, int dx2, int dy2);
00312 
00313 
00314 
00315         bool stretchBlitBuffer(void *buffer, float sx1, float sy1, float sx2, float sy2, int sw, int sh,
00316                                               float dx1, float dy1, float dx2, float dy2);
00317         bool stretchBlitBufferi(void *buffer, int sx1, int sy1, int sx2, int sy2, int sw, int sh,
00318                                            int dx1, int dy1, int dx2, int dy2);
00319 
00320         bool blitBuffer2Texture(GLuint dst_tex, bool realloc, void *buffer, int sw, int sh);
00321 
00322         bool drawElements(MMS_VERTEX_ARRAY *vertices, MMS_VERTEX_ARRAY *normals, MMS_VERTEX_ARRAY *texcoords,
00323                           MMS_INDEX_ARRAY *indices);
00324 
00325         bool drawElements(MMS_VERTEX_BUFFER *vertices, MMS_VERTEX_BUFFER *normals, MMS_VERTEX_BUFFER *texcoords,
00326                           MMS_INDEX_BUFFER *indices);
00327 };
00328 
00329 #endif
00330 
00331 #endif /* MMSFBGL_H_ */
00332 
00333 
00334 
00335 
00336 

Generated by doxygen