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