Home | History | Annotate | Download | only in Common
      1 //
      2 // Book:      OpenGL(R) ES 2.0 Programming Guide
      3 // Authors:   Aaftab Munshi, Dan Ginsburg, Dave Shreiner
      4 // ISBN-10:   0321502795
      5 // ISBN-13:   9780321502797
      6 // Publisher: Addison-Wesley Professional
      7 // URLs:      http://safari.informit.com/9780321563835
      8 //            http://www.opengles-book.com
      9 //
     10 
     11 //
     12 /// \file ESUtil.h
     13 /// \brief A utility library for OpenGL ES.  This library provides a
     14 ///        basic common framework for the example applications in the
     15 ///        OpenGL ES 2.0 Programming Guide.
     16 //
     17 #ifndef ESUTIL_H
     18 #define ESUTIL_H
     19 
     20 ///
     21 //  Includes
     22 //
     23 #include <GLES2/gl2.h>
     24 #include <GLES2/gl2ext.h>
     25 #include <EGL/egl.h>
     26 #include <EGL/eglext.h>
     27 
     28 #ifdef __cplusplus
     29 
     30 extern "C" {
     31 #endif
     32 
     33 
     34 ///
     35 //  Macros
     36 //
     37 #define ESUTIL_API  __cdecl
     38 #define ESCALLBACK  __cdecl
     39 
     40 
     41 /// esCreateWindow flag - RGB color buffer
     42 #define ES_WINDOW_RGB           0
     43 /// esCreateWindow flag - ALPHA color buffer
     44 #define ES_WINDOW_ALPHA         1
     45 /// esCreateWindow flag - depth buffer
     46 #define ES_WINDOW_DEPTH         2
     47 /// esCreateWindow flag - stencil buffer
     48 #define ES_WINDOW_STENCIL       4
     49 /// esCreateWindow flag - multi-sample buffer
     50 #define ES_WINDOW_MULTISAMPLE   8
     51 /// esCreateWindow flag - EGL_POST_SUB_BUFFER_NV supported.
     52 #define ES_WINDOW_POST_SUB_BUFFER_SUPPORTED 16
     53 
     54 ///
     55 // Types
     56 //
     57 
     58 typedef struct
     59 {
     60     GLfloat   m[4][4];
     61 } ESMatrix;
     62 
     63 typedef struct
     64 {
     65    /// Put your user data here...
     66    void*       userData;
     67 
     68    /// Window width
     69    GLint       width;
     70 
     71    /// Window height
     72    GLint       height;
     73 
     74    /// Window handle
     75    EGLNativeWindowType  hWnd;
     76 
     77    /// EGL display
     78    EGLDisplay  eglDisplay;
     79 
     80    /// EGL context
     81    EGLContext  eglContext;
     82 
     83    /// EGL surface
     84    EGLSurface  eglSurface;
     85 
     86    /// Callbacks
     87    void (ESCALLBACK *drawFunc) ( void* );
     88    void (ESCALLBACK *keyFunc) ( void*, unsigned char, int, int );
     89    void (ESCALLBACK *updateFunc) ( void*, float deltaTime );
     90 } ESContext;
     91 
     92 
     93 ///
     94 //  Extensions
     95 //
     96 
     97 extern PFNEGLCREATEIMAGEKHRPROC eglCreateImageKHR;
     98 extern PFNEGLDESTROYIMAGEKHRPROC eglDestroyImageKHR;
     99 
    100 extern PFNEGLPOSTSUBBUFFERNVPROC eglPostSubBufferNV;
    101 
    102 extern PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
    103 
    104 extern PFNGLDELETEFENCESNVPROC glDeleteFencesNV;
    105 extern PFNGLGENFENCESNVPROC glGenFencesNV;
    106 extern PFNGLGETFENCEIVNVPROC glGetFenceivNV;
    107 extern PFNGLISFENCENVPROC glIsFenceNV;
    108 extern PFNGLFINISHFENCENVPROC glFinishFenceNV;
    109 extern PFNGLSETFENCENVPROC glSetFenceNV;
    110 extern PFNGLTESTFENCENVPROC glTestFenceNV;
    111 
    112 ///
    113 //  Public Functions
    114 //
    115 
    116 //
    117 ///
    118 /// \brief Initialize ES framework context.  This must be called before calling any other functions.
    119 /// \param esContext Application context
    120 //
    121 void ESUTIL_API esInitContext ( ESContext *esContext );
    122 
    123 //
    124 /// \brief Create a window with the specified parameters
    125 /// \param esContext Application context
    126 /// \param title Name for title bar of window
    127 /// \param width Width in pixels of window to create
    128 /// \param height Height in pixels of window to create
    129 /// \param flags Bitfield for the window creation flags
    130 ///         ES_WINDOW_RGB     - specifies that the color buffer should have R,G,B channels
    131 ///         ES_WINDOW_ALPHA   - specifies that the color buffer should have alpha
    132 ///         ES_WINDOW_DEPTH   - specifies that a depth buffer should be created
    133 ///         ES_WINDOW_STENCIL - specifies that a stencil buffer should be created
    134 ///         ES_WINDOW_MULTISAMPLE - specifies that a multi-sample buffer should be created
    135 ///         ES_WINDOW_POST_SUB_BUFFER_SUPPORTED - specifies that EGL_POST_SUB_BUFFER_NV is supported.
    136 /// \return GL_TRUE if window creation is succesful, GL_FALSE otherwise
    137 GLboolean ESUTIL_API esCreateWindow ( ESContext *esContext, LPCTSTR title, GLint width, GLint height, GLuint flags );
    138 
    139 //
    140 /// \brief Start the main loop for the OpenGL ES application
    141 /// \param esContext Application context
    142 //
    143 void ESUTIL_API esMainLoop ( ESContext *esContext );
    144 
    145 //
    146 /// \brief Register a draw callback function to be used to render each frame
    147 /// \param esContext Application context
    148 /// \param drawFunc Draw callback function that will be used to render the scene
    149 //
    150 void ESUTIL_API esRegisterDrawFunc ( ESContext *esContext, void (ESCALLBACK *drawFunc) ( ESContext* ) );
    151 
    152 //
    153 /// \brief Register an update callback function to be used to update on each time step
    154 /// \param esContext Application context
    155 /// \param updateFunc Update callback function that will be used to render the scene
    156 //
    157 void ESUTIL_API esRegisterUpdateFunc ( ESContext *esContext, void (ESCALLBACK *updateFunc) ( ESContext*, float ) );
    158 
    159 //
    160 /// \brief Register an keyboard input processing callback function
    161 /// \param esContext Application context
    162 /// \param keyFunc Key callback function for application processing of keyboard input
    163 //
    164 void ESUTIL_API esRegisterKeyFunc ( ESContext *esContext,
    165                                     void (ESCALLBACK *drawFunc) ( ESContext*, unsigned char, int, int ) );
    166 //
    167 /// \brief Log a message to the debug output for the platform
    168 /// \param formatStr Format string for error log.
    169 //
    170 void ESUTIL_API esLogMessage ( const char *formatStr, ... );
    171 
    172 //
    173 ///
    174 /// \brief Load a shader, check for compile errors, print error messages to output log
    175 /// \param type Type of shader (GL_VERTEX_SHADER or GL_FRAGMENT_SHADER)
    176 /// \param shaderSrc Shader source string
    177 /// \return A new shader object on success, 0 on failure
    178 //
    179 GLuint ESUTIL_API esLoadShader ( GLenum type, const char *shaderSrc );
    180 
    181 //
    182 ///
    183 /// \brief Load a vertex and fragment shader, create a program object, link program.
    184 ///        Errors output to log.
    185 /// \param vertShaderSrc Vertex shader source code
    186 /// \param fragShaderSrc Fragment shader source code
    187 /// \return A new program object linked with the vertex/fragment shader pair, 0 on failure
    188 //
    189 GLuint ESUTIL_API esLoadProgram ( const char *vertShaderSrc, const char *fragShaderSrc );
    190 
    191 
    192 //
    193 /// \brief Generates geometry for a sphere.  Allocates memory for the vertex data and stores
    194 ///        the results in the arrays.  Generate index list for a TRIANGLE_STRIP
    195 /// \param numSlices The number of slices in the sphere
    196 /// \param vertices If not NULL, will contain array of float3 positions
    197 /// \param normals If not NULL, will contain array of float3 normals
    198 /// \param texCoords If not NULL, will contain array of float2 texCoords
    199 /// \param indices If not NULL, will contain the array of indices for the triangle strip
    200 /// \return The number of indices required for rendering the buffers (the number of indices stored in the indices array
    201 ///         if it is not NULL ) as a GL_TRIANGLE_STRIP
    202 //
    203 int ESUTIL_API esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **normals,
    204                              GLfloat **texCoords, GLushort **indices );
    205 
    206 //
    207 /// \brief Generates geometry for a cube.  Allocates memory for the vertex data and stores
    208 ///        the results in the arrays.  Generate index list for a TRIANGLES
    209 /// \param scale The size of the cube, use 1.0 for a unit cube.
    210 /// \param vertices If not NULL, will contain array of float3 positions
    211 /// \param normals If not NULL, will contain array of float3 normals
    212 /// \param texCoords If not NULL, will contain array of float2 texCoords
    213 /// \param indices If not NULL, will contain the array of indices for the triangle strip
    214 /// \return The number of indices required for rendering the buffers (the number of indices stored in the indices array
    215 ///         if it is not NULL ) as a GL_TRIANGLES
    216 //
    217 int ESUTIL_API esGenCube ( float scale, GLfloat **vertices, GLfloat **normals,
    218                            GLfloat **texCoords, GLushort **indices );
    219 
    220 //
    221 /// \brief Loads a 24-bit TGA image from a file
    222 /// \param fileName Name of the file on disk
    223 /// \param width Width of loaded image in pixels
    224 /// \param height Height of loaded image in pixels
    225 ///  \return Pointer to loaded image.  NULL on failure.
    226 //
    227 char* ESUTIL_API esLoadTGA ( char *fileName, int *width, int *height );
    228 
    229 
    230 //
    231 /// \brief multiply matrix specified by result with a scaling matrix and return new matrix in result
    232 /// \param result Specifies the input matrix.  Scaled matrix is returned in result.
    233 /// \param sx, sy, sz Scale factors along the x, y and z axes respectively
    234 //
    235 void ESUTIL_API esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz);
    236 
    237 //
    238 /// \brief multiply matrix specified by result with a translation matrix and return new matrix in result
    239 /// \param result Specifies the input matrix.  Translated matrix is returned in result.
    240 /// \param tx, ty, tz Scale factors along the x, y and z axes respectively
    241 //
    242 void ESUTIL_API esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz);
    243 
    244 //
    245 /// \brief multiply matrix specified by result with a rotation matrix and return new matrix in result
    246 /// \param result Specifies the input matrix.  Rotated matrix is returned in result.
    247 /// \param angle Specifies the angle of rotation, in degrees.
    248 /// \param x, y, z Specify the x, y and z coordinates of a vector, respectively
    249 //
    250 void ESUTIL_API esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
    251 
    252 //
    253 // \brief multiply matrix specified by result with a perspective matrix and return new matrix in result
    254 /// \param result Specifies the input matrix.  new matrix is returned in result.
    255 /// \param left, right Coordinates for the left and right vertical clipping planes
    256 /// \param bottom, top Coordinates for the bottom and top horizontal clipping planes
    257 /// \param nearZ, farZ Distances to the near and far depth clipping planes.  Both distances must be positive.
    258 //
    259 void ESUTIL_API esFrustum(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ);
    260 
    261 //
    262 /// \brief multiply matrix specified by result with a perspective matrix and return new matrix in result
    263 /// \param result Specifies the input matrix.  new matrix is returned in result.
    264 /// \param fovy Field of view y angle in degrees
    265 /// \param aspect Aspect ratio of screen
    266 /// \param nearZ Near plane distance
    267 /// \param farZ Far plane distance
    268 //
    269 void ESUTIL_API esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float farZ);
    270 
    271 //
    272 /// \brief multiply matrix specified by result with a perspective matrix and return new matrix in result
    273 /// \param result Specifies the input matrix.  new matrix is returned in result.
    274 /// \param left, right Coordinates for the left and right vertical clipping planes
    275 /// \param bottom, top Coordinates for the bottom and top horizontal clipping planes
    276 /// \param nearZ, farZ Distances to the near and far depth clipping planes.  These values are negative if plane is behind the viewer
    277 //
    278 void ESUTIL_API esOrtho(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ);
    279 
    280 //
    281 /// \brief perform the following operation - result matrix = srcA matrix * srcB matrix
    282 /// \param result Returns multiplied matrix
    283 /// \param srcA, srcB Input matrices to be multiplied
    284 //
    285 void ESUTIL_API esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB);
    286 
    287 //
    288 //// \brief return an indentity matrix
    289 //// \param result returns identity matrix
    290 //
    291 void ESUTIL_API esMatrixLoadIdentity(ESMatrix *result);
    292 
    293 #ifdef __cplusplus
    294 }
    295 #endif
    296 
    297 #endif // ESUTIL_H
    298