~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/glbook/Common/esUtil.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

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