00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef MMSFBGL_H_
00034 #define MMSFBGL_H_
00035
00036 #include "mmstools/mmstypes.h"
00037 #include <stack>
00038
00039 #ifdef __HAVE_OPENGL__
00040
00041 #include <string.h>
00042
00043 #ifdef __HAVE_GL2__
00044 #include <GL/glew.h>
00045 #include <GL/gl.h>
00046 #include <GL/glext.h>
00047 #endif
00048
00049 #ifdef __HAVE_GLX__
00050 #include <GL/glx.h>
00051 #endif
00052
00053 #ifdef __HAVE_GLES2__
00054 #include <GLES2/gl2.h>
00055 #include <GLES2/gl2ext.h>
00056 #endif
00057
00058 #ifdef __HAVE_EGL__
00059 #include <EGL/egl.h>
00060 #endif
00061
00062 #ifdef GL_HALF_FLOAT_OES
00063 #define __HAVE_OGL_HALF_FLOAT__
00064 #endif
00065
00066
00067
00068
00069
00070 class MMSFBGL {
00071 private:
00072
00073 #define MMSFBGL_VSV_LOC 0
00074
00075 typedef enum {
00076 MMSFBGL_SHADER_TYPE_FRAGMENT_SHADER = 0,
00077 MMSFBGL_SHADER_TYPE_VERTEX_SHADER
00078 } MMSFBGL_SHADER_TYPE;
00079
00080 #ifdef __HAVE_XLIB__
00081 Display *x_display;
00082 Window x_window;
00083 #endif
00084
00085 #ifdef __HAVE_GLX__
00086 XVisualInfo *xvi;
00087 GLXContext glx_context;
00088 #endif
00089
00090 #ifdef __HAVE_EGL__
00091 EGLDisplay eglDisplay;
00092 EGLConfig eglConfig[10];
00093 EGLSurface eglSurface;
00094 EGLContext eglContext;
00095 #endif
00096
00097 bool initialized;
00098
00099 int screen_width;
00100 int screen_height;
00101
00102
00103 GLuint bound_fbo;
00104
00105
00106 GLuint bound_vbo;
00107
00108
00109 GLuint bound_ibo;
00110
00111
00112 GLuint po_draw;
00113
00114
00115 GLuint po_blit;
00116
00117
00118 GLuint po_modulateblit;
00119
00120
00121 GLuint po_blit_fromalpha;
00122
00123
00124 GLuint po_modulateblit_fromalpha;
00125
00126
00127 GLuint po_current;
00128
00129
00130 GLint VSMatrixLoc;
00131
00132
00133 bool VSMatrixLoc_initialized;
00134
00135
00136 GLint FSColorLoc;
00137
00138
00139 bool FSColorLoc_initialized;
00140
00141
00142 GLint FSTextureLoc;
00143
00144
00145 bool FSTextureLoc_initialized;
00146
00147
00148 GLint VSTexCoordLoc;
00149
00150
00151 bool VSTexCoordLoc_initialized;
00152
00153
00154 MMSMatrix current_matrix;
00155
00156
00157 unsigned char current_color_r;
00158 unsigned char current_color_g;
00159 unsigned char current_color_b;
00160 unsigned char current_color_a;
00161
00162 class MMSFBGLStackMatrix {
00163 public:
00164 MMSMatrix matrix;
00165 MMSFBGLStackMatrix(MMSMatrix matrix) {
00166 memcpy(this->matrix, matrix, sizeof(MMSMatrix));
00167 }
00168 void getMatrix(MMSMatrix matrix) {
00169 memcpy(matrix, this->matrix, sizeof(MMSMatrix));
00170 }
00171 };
00172
00173
00174 std::stack<MMSFBGLStackMatrix> matrix_stack;
00175
00176 void printImplementationInformation();
00177
00178 bool getError(const char* where, int line = __LINE__);
00179
00180 bool buildShader(MMSFBGL_SHADER_TYPE shader_type, const char *shader_code, GLuint *shader);
00181 bool linkProgram(GLuint fragment_shader, GLuint vertex_shader, GLuint *program);
00182 bool buildShaderProgram(const char *fragment_shader_code, const char *vertex_shader_code, GLuint *program);
00183 bool buildShaderProgram4Drawing(GLuint *program);
00184 bool buildShaderProgram4Blitting(GLuint *program);
00185 bool buildShaderProgram4ModulateBlitting(GLuint *program);
00186 bool buildShaderProgram4BlittingFromAlpha(GLuint *program);
00187 bool buildShaderProgram4ModulateBlittingFromAlpha(GLuint *program);
00188
00189 void deleteShaders();
00190 bool initShaders();
00191
00192 bool useShaderProgram4Drawing();
00193 bool useShaderProgram4Blitting();
00194 bool useShaderProgram4ModulateBlitting();
00195 bool useShaderProgram4BlittingFromAlpha();
00196 bool useShaderProgram4ModulateBlittingFromAlpha();
00197
00198 public:
00199 MMSFBGL();
00200 ~MMSFBGL();
00201
00202 #ifdef __HAVE_XLIB__
00203 bool init(Display *x_display, int x_screen, Window x_window, int w, int h);
00204 #else
00205 bool init();
00206 #endif
00207 bool terminate();
00208 bool getResolution(int *w, int *h);
00209 bool swap();
00210
00211 bool genBuffer(GLuint *bo);
00212 bool deleteBuffer(GLuint bo);
00213 bool bindBuffer(GLenum target, GLuint bo);
00214 bool initVertexBuffer(GLuint vbo, unsigned int size, const GLvoid *data = NULL);
00215 bool initVertexSubBuffer(GLuint vbo, unsigned int offset, unsigned int size, const GLvoid *data);
00216 bool enableVertexBuffer(GLuint vbo);
00217 void disableVertexBuffer();
00218 bool initIndexBuffer(GLuint ibo, unsigned int size, const GLvoid *data = NULL);
00219 bool initIndexSubBuffer(GLuint ibo, unsigned int offset, unsigned int size, const GLvoid *data);
00220 bool enableIndexBuffer(GLuint ibo);
00221 void disableIndexBuffer();
00222
00223 bool genTexture(GLuint *tex);
00224 bool deleteTexture(GLuint tex);
00225 bool bindTexture2D(GLuint tex);
00226 bool initTexture2D(GLuint tex, GLenum texture_format, void *buffer, GLenum buffer_format, int sw, int sh);
00227 bool initSubTexture2D(GLuint tex, void *buffer, GLenum buffer_format, int sw, int sh, int dx, int dy);
00228 bool enableTexture2D(GLuint tex);
00229 void disableTexture2D();
00230
00231 bool genFrameBuffer(GLuint *fbo);
00232 bool deleteFrameBuffer(GLuint fbo);
00233 bool genRenderBuffer(GLuint *rbo);
00234 bool deleteRenderBuffer(GLuint rbo);
00235 bool attachTexture2FrameBuffer(GLuint fbo, GLuint tex);
00236 bool attachRenderBuffer2FrameBuffer(GLuint fbo, GLuint rbo, int width, int height);
00237
00238 bool allocTexture(GLuint tex, int width, int height);
00239 bool allocFBO(GLuint fbo, GLuint tex, int width, int height);
00240 bool allocFBOandRBO(GLuint fbo, GLuint tex, GLuint rbo, int width, int height);
00241 bool freeFBO(GLuint fbo, GLuint tex, GLuint rbo = 0);
00242
00243 bool bindFrameBuffer(GLuint ogl);
00244 bool setScissor(GLint x, GLint y, GLsizei width, GLsizei height);
00245 bool disableScissor();
00246
00247 void enableBlend(GLenum srcRGB = GL_SRC_ALPHA, GLenum dstRGB = GL_ONE_MINUS_SRC_ALPHA,
00248 GLenum srcAlpha = GL_ONE, GLenum dstAlpha = GL_ONE_MINUS_SRC_ALPHA);
00249 void disableBlend();
00250 void enableDepthTest(bool readonly = false);
00251 void disableDepthTest();
00252 void setDrawingMode();
00253 void setTexEnvReplace(GLenum format);
00254 void setTexEnvModulate(GLenum format);
00255 void disableArrays();
00256
00257 bool setCurrentMatrix(MMSMatrix matrix);
00258 bool getCurrentMatrix(MMSMatrix matrix);
00259 bool scaleCurrentMatrix(GLfloat sx, GLfloat sy, GLfloat sz);
00260 bool translateCurrentMatrix(GLfloat tx, GLfloat ty, GLfloat tz);
00261 bool rotateCurrentMatrix(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
00262
00263 bool getParallelProjectionMatrix(MMSMatrix result, float left, float right, float bottom, float top, float nearZ, float farZ);
00264 bool getCentralProjectionMatrix(MMSMatrix result, float left, float right, float bottom, float top, float nearZ, float farZ);
00265 bool getPerspectiveMatrix(MMSMatrix result, float fovy, float aspect, float nearZ, float farZ);
00266
00267 bool setParallelProjection(float left, float right, float bottom, float top, float nearZ, float farZ);
00268 bool setCentralProjection(float left, float right, float bottom, float top, float nearZ, float farZ);
00269 bool setPerspective(float fovy, float aspect, float nearZ, float farZ);
00270
00271 bool pushCurrentMatrix();
00272 bool popCurrentMatrix();
00273
00274 bool clear(unsigned char r = 0x00, unsigned char g = 0x00, unsigned char b = 0x00, unsigned char a = 0x00);
00275 bool setColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a);
00276
00277 bool drawLine2D(float x1, float y1, float x2, float y2);
00278 bool drawLine2Di(int x1, int y1, int x2, int y2);
00279
00280 bool drawRectangle2D(float x1, float y1, float x2, float y2);
00281 bool drawRectangle2Di(int x1, int y1, int x2, int y2);
00282
00283
00284 bool fillTriangle(float x1, float y1, float z1,
00285 float x2, float y2, float z2,
00286 float x3, float y3, float z3);
00287 bool fillTriangle2D(float x1, float y1, float x2, float y2, float x3, float y3);
00288
00289 bool fillRectangle2D(float x1, float y1, float x2, float y2);
00290 bool fillRectangle2Di(int x1, int y1, int x2, int y2);
00291
00292
00293 bool stretchBlit3D(GLuint src_tex, float sx1, float sy1, float sx2, float sy2,
00294 float dx1, float dy1, float dz1,
00295 float dx2, float dy2, float dz2,
00296 float dx3, float dy3, float dz3,
00297 float dx4, float dy4, float dz4);
00298
00299 bool stretchBlit(GLuint src_tex, float sx1, float sy1, float sx2, float sy2,
00300 float dx1, float dy1, float dx2, float dy2);
00301 bool stretchBliti(GLuint src_tex, int sx1, int sy1, int sx2, int sy2, int sw, int sh,
00302 int dx1, int dy1, int dx2, int dy2);
00303
00304
00305
00306 bool stretchBlitBuffer(void *buffer, float sx1, float sy1, float sx2, float sy2, int sw, int sh,
00307 float dx1, float dy1, float dx2, float dy2);
00308 bool stretchBlitBufferi(void *buffer, int sx1, int sy1, int sx2, int sy2, int sw, int sh,
00309 int dx1, int dy1, int dx2, int dy2);
00310
00311 bool blitBuffer2Texture(GLuint dst_tex, bool realloc, void *buffer, int sw, int sh);
00312
00313 bool drawElements(MMS_VERTEX_ARRAY *vertices, MMS_VERTEX_ARRAY *normals, MMS_VERTEX_ARRAY *texcoords,
00314 MMS_INDEX_ARRAY *indices);
00315
00316 bool drawElements(MMS_VERTEX_BUFFER *vertices, MMS_VERTEX_BUFFER *normals, MMS_VERTEX_BUFFER *texcoords,
00317 MMS_INDEX_BUFFER *indices);
00318 };
00319
00320 #endif
00321
00322 #endif
00323
00324
00325
00326
00327