From f7c1695a068d43a0313daa36e8bda3bb1b43f008 Mon Sep 17 00:00:00 2001 From: "ajwong@chromium.org" Date: Wed, 6 Jan 2010 23:44:36 +0000 Subject: Revert "Resubmit 35500: Added an application framework for demos. Ported hellotriangle example in OpenGL ES book to use the application framework." This reverts commit r35647. TBR: alok git-svn-id: svn://svn.chromium.org/chrome/trunk/src@35665 0039d316-1c4b-4281-b951-d872f2087c98 --- .../Chapter_2/Hello_Triangle/Hello_Triangle.c | 167 ++++++++++++++----- .../Chapter_2/Hello_Triangle/Hello_Triangle.h | 46 ------ .../gles_book_examples/Common/Include/esUtil.h | 133 ++++++++++++--- .../gles_book_examples/Common/Include/esUtil_win.h | 36 +++- .../Common/Source/Win32/esUtil_TGA.c | 3 - .../Common/Source/Win32/esUtil_win32.c | 183 +++++++++++++++++++++ .../gles_book_examples/Common/Source/esShader.c | 4 +- .../gles_book_examples/Common/Source/esShapes.c | 9 +- .../gles_book_examples/Common/Source/esTransform.c | 26 ++- .../gles_book_examples/Common/Source/esUtil.c | 182 +++++++++++++++++++- .../gles_book_examples/gles_book_examples.gyp | 49 ------ 11 files changed, 649 insertions(+), 189 deletions(-) delete mode 100644 third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.h create mode 100644 third_party/gles_book_examples/Common/Source/Win32/esUtil_win32.c delete mode 100644 third_party/gles_book_examples/gles_book_examples.gyp (limited to 'third_party') diff --git a/third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.c b/third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.c index 740ffe3..6b12a59 100644 --- a/third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.c +++ b/third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.c @@ -14,18 +14,70 @@ // a minimal vertex/fragment shader. The purpose of this // example is to demonstrate the basic concepts of // OpenGL ES 2.0 rendering. +#include +#include "esUtil.h" -#include "Hello_Triangle.h" +typedef struct +{ + // Handle to a program object + GLuint programObject; -#include +} UserData; /// -// Initialize the shader and program object +// Create a shader object, load the shader source, and +// compile the shader. // -int htInit ( ESContext *esContext ) +GLuint LoadShader ( GLenum type, const char *shaderSrc ) { - HTUserData *userData = esContext->userData; + GLuint shader; + GLint compiled; + + // Create the shader object + shader = glCreateShader ( type ); + + if ( shader == 0 ) + return 0; + + // Load the shader source + glShaderSource ( shader, 1, &shaderSrc, NULL ); + + // Compile the shader + glCompileShader ( shader ); + + // Check the compile status + glGetShaderiv ( shader, GL_COMPILE_STATUS, &compiled ); + + if ( !compiled ) + { + GLint infoLen = 0; + + glGetShaderiv ( shader, GL_INFO_LOG_LENGTH, &infoLen ); + + if ( infoLen > 1 ) + { + char* infoLog = malloc (sizeof(char) * infoLen ); + + glGetShaderInfoLog ( shader, infoLen, NULL, infoLog ); + esLogMessage ( "Error compiling shader:\n%s\n", infoLog ); + + free ( infoLog ); + } + + glDeleteShader ( shader ); + return 0; + } + return shader; + +} + +/// +// Initialize the shader and program object +// +int Init ( ESContext *esContext ) +{ + UserData *userData = esContext->userData; GLbyte vShaderStr[] = "attribute vec4 vPosition; \n" "void main() \n" @@ -33,28 +85,62 @@ int htInit ( ESContext *esContext ) " gl_Position = vPosition; \n" "} \n"; - // TODO(alokp): Shaders containing "precision" do not compile. GLbyte fShaderStr[] = + "precision mediump float;\n"\ "void main() \n" "{ \n" " gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n" "} \n"; - // TODO(alokp): Client-side vertex arrays do not work. - GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f }; + GLuint vertexShader; + GLuint fragmentShader; + GLuint programObject; + GLint linked; - userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); - if ( userData->programObject == 0 ) return FALSE; + // Load the vertex/fragment shaders + vertexShader = LoadShader ( GL_VERTEX_SHADER, vShaderStr ); + fragmentShader = LoadShader ( GL_FRAGMENT_SHADER, fShaderStr ); + + // Create the program object + programObject = glCreateProgram ( ); + + if ( programObject == 0 ) + return 0; + + glAttachShader ( programObject, vertexShader ); + glAttachShader ( programObject, fragmentShader ); // Bind vPosition to attribute 0 - glBindAttribLocation ( userData->programObject, 0, "vPosition" ); + glBindAttribLocation ( programObject, 0, "vPosition" ); + + // Link the program + glLinkProgram ( programObject ); - glGenBuffers ( 1, &userData->vbo ); - glBindBuffer ( GL_ARRAY_BUFFER, userData->vbo ); - glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices), NULL, GL_STATIC_DRAW ); - glBufferSubData ( GL_ARRAY_BUFFER, 0, sizeof(vVertices), vVertices ); + // Check the link status + glGetProgramiv ( programObject, GL_LINK_STATUS, &linked ); + + if ( !linked ) + { + GLint infoLen = 0; + + glGetProgramiv ( programObject, GL_INFO_LOG_LENGTH, &infoLen ); + + if ( infoLen > 1 ) + { + char* infoLog = malloc (sizeof(char) * infoLen ); + + glGetProgramInfoLog ( programObject, infoLen, NULL, infoLog ); + esLogMessage ( "Error linking program:\n%s\n", infoLog ); + + free ( infoLog ); + } + + glDeleteProgram ( programObject ); + return FALSE; + } + + // Store the program object + userData->programObject = programObject; glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); return TRUE; @@ -63,10 +149,13 @@ int htInit ( ESContext *esContext ) /// // Draw a triangle using the shader pair created in Init() // -void htDraw ( ESContext *esContext ) +void Draw ( ESContext *esContext ) { - HTUserData *userData = esContext->userData; - + UserData *userData = esContext->userData; + GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f, + -0.5f, -0.5f, 0.0f, + 0.5f, -0.5f, 0.0f }; + // Set the viewport glViewport ( 0, 0, esContext->width, esContext->height ); @@ -77,33 +166,29 @@ void htDraw ( ESContext *esContext ) glUseProgram ( userData->programObject ); // Load the vertex data - glBindBuffer ( GL_ARRAY_BUFFER, userData->vbo ); + glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, vVertices ); glEnableVertexAttribArray ( 0 ); - glVertexAttribPointer ( 0, 3, GL_FLOAT, GL_FALSE, 0, 0 ); glDrawArrays ( GL_TRIANGLES, 0, 3 ); - // Nothing is drawn or application crashes without glFlush. - // TODO(alokp): glFlush should not be necessary with SwapBuffers(). - glFlush(); + eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); } -/// -// Cleanup -// -void htShutDown ( ESContext *esContext ) + +int main ( int argc, char *argv[] ) { - HTUserData *userData = esContext->userData; + ESContext esContext; + UserData userData; - // Delete program object - if ( userData->programObject != 0 ) - { - glDeleteProgram ( userData->programObject ); - userData->programObject = 0; - } - if ( userData->vbo != 0 ) - { - glDeleteBuffers ( 1, &userData->vbo ); - userData->vbo = 0; - } + esInitContext ( &esContext ); + esContext.userData = &userData; + + esCreateWindow ( &esContext, "Hello Triangle", 320, 240, ES_WINDOW_RGB ); + + if ( !Init ( &esContext ) ) + return 0; + + esRegisterDrawFunc ( &esContext, Draw ); + + esMainLoop ( &esContext ); } diff --git a/third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.h b/third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.h deleted file mode 100644 index 930d486..0000000 --- a/third_party/gles_book_examples/Chapter_2/Hello_Triangle/Hello_Triangle.h +++ /dev/null @@ -1,46 +0,0 @@ -// -// Book: OpenGL(R) ES 2.0 Programming Guide -// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner -// ISBN-10: 0321502795 -// ISBN-13: 9780321502797 -// Publisher: Addison-Wesley Professional -// URLs: http://safari.informit.com/9780321563835 -// http://www.opengles-book.com -// - -// -// Hello_Triangle.h -// -// This is a simple example that draws a single triangle with -// a minimal vertex/fragment shader. The purpose of this -// example is to demonstrate the basic concepts of -// OpenGL ES 2.0 rendering. - -#ifndef HELLO_TRIANGLE_H -#define HELLO_TRIANGLE_H - -#include "esUtil.h" - -#ifdef __cplusplus -extern "C" { -#endif // __cplusplus - -typedef struct -{ - // Handle to a program object - GLuint programObject; - // Handle to vbo object - GLuint vbo; - -} HTUserData; - -extern int htInit ( ESContext *esContext ); - -extern void htDraw ( ESContext *esContext ); - -extern void htShutDown ( ESContext *esContext ); - -#ifdef __cplusplus -} -#endif // __cplusplus -#endif // HELLO_TRIANGLE_H diff --git a/third_party/gles_book_examples/Common/Include/esUtil.h b/third_party/gles_book_examples/Common/Include/esUtil.h index 2a0adfc..f1ab1bf 100644 --- a/third_party/gles_book_examples/Common/Include/esUtil.h +++ b/third_party/gles_book_examples/Common/Include/esUtil.h @@ -21,17 +21,36 @@ // Includes // #include +#include #ifdef __cplusplus + extern "C" { -#endif // __cplusplus +#endif + + +/// +// Macros +// +#define ESUTIL_API __cdecl +#define ESCALLBACK __cdecl + -#ifndef FALSE -#define FALSE 0 -#endif // FALSE -#ifndef TRUE -#define TRUE 1 -#endif // TRUE +/// esCreateWindow flag - RGB color buffer +#define ES_WINDOW_RGB 0 +/// esCreateWindow flag - ALPHA color buffer +#define ES_WINDOW_ALPHA 1 +/// esCreateWindow flag - depth buffer +#define ES_WINDOW_DEPTH 2 +/// esCreateWindow flag - stencil buffer +#define ES_WINDOW_STENCIL 4 +/// esCreateWindow flat - multi-sample buffer +#define ES_WINDOW_MULTISAMPLE 8 + + +/// +// Types +// typedef struct { @@ -48,20 +67,84 @@ typedef struct /// Window height GLint height; + + /// Window handle + EGLNativeWindowType hWnd; + + /// EGL display + EGLDisplay eglDisplay; + + /// EGL context + EGLContext eglContext; + + /// EGL surface + EGLSurface eglSurface; + + /// Callbacks + void (ESCALLBACK *drawFunc) ( void* ); + void (ESCALLBACK *keyFunc) ( void*, unsigned char, int, int ); + void (ESCALLBACK *updateFunc) ( void*, float deltaTime ); } ESContext; + +/// +// Public Functions +// + // /// /// \brief Initialize ES framework context. This must be called before calling any other functions. /// \param esContext Application context // -extern void esInitContext ( ESContext *esContext ); +void ESUTIL_API esInitContext ( ESContext *esContext ); // +/// \brief Create a window with the specified parameters +/// \param esContext Application context +/// \param title Name for title bar of window +/// \param width Width in pixels of window to create +/// \param height Height in pixels of window to create +/// \param flags Bitfield for the window creation flags +/// ES_WINDOW_RGB - specifies that the color buffer should have R,G,B channels +/// ES_WINDOW_ALPHA - specifies that the color buffer should have alpha +/// ES_WINDOW_DEPTH - specifies that a depth buffer should be created +/// ES_WINDOW_STENCIL - specifies that a stencil buffer should be created +/// ES_WINDOW_MULTISAMPLE - specifies that a multi-sample buffer should be created +/// \return GL_TRUE if window creation is succesful, GL_FALSE otherwise +GLboolean ESUTIL_API esCreateWindow ( ESContext *esContext, const char *title, GLint width, GLint height, GLuint flags ); + +// +/// \brief Start the main loop for the OpenGL ES application +/// \param esContext Application context +// +void ESUTIL_API esMainLoop ( ESContext *esContext ); + +// +/// \brief Register a draw callback function to be used to render each frame +/// \param esContext Application context +/// \param drawFunc Draw callback function that will be used to render the scene +// +void ESUTIL_API esRegisterDrawFunc ( ESContext *esContext, void (ESCALLBACK *drawFunc) ( ESContext* ) ); + +// +/// \brief Register an update callback function to be used to update on each time step +/// \param esContext Application context +/// \param updateFunc Update callback function that will be used to render the scene +// +void ESUTIL_API esRegisterUpdateFunc ( ESContext *esContext, void (ESCALLBACK *updateFunc) ( ESContext*, float ) ); + +// +/// \brief Register an keyboard input processing callback function +/// \param esContext Application context +/// \param keyFunc Key callback function for application processing of keyboard input +// +void ESUTIL_API esRegisterKeyFunc ( ESContext *esContext, + void (ESCALLBACK *drawFunc) ( ESContext*, unsigned char, int, int ) ); +// /// \brief Log a message to the debug output for the platform /// \param formatStr Format string for error log. // -extern void esLogMessage ( const char *formatStr, ... ); +void ESUTIL_API esLogMessage ( const char *formatStr, ... ); // /// @@ -70,7 +153,7 @@ extern void esLogMessage ( const char *formatStr, ... ); /// \param shaderSrc Shader source string /// \return A new shader object on success, 0 on failure // -extern GLuint esLoadShader ( GLenum type, const char *shaderSrc ); +GLuint ESUTIL_API esLoadShader ( GLenum type, const char *shaderSrc ); // /// @@ -80,7 +163,7 @@ extern GLuint esLoadShader ( GLenum type, const char *shaderSrc ); /// \param fragShaderSrc Fragment shader source code /// \return A new program object linked with the vertex/fragment shader pair, 0 on failure // -extern GLuint esLoadProgram ( const char *vertShaderSrc, const char *fragShaderSrc ); +GLuint ESUTIL_API esLoadProgram ( const char *vertShaderSrc, const char *fragShaderSrc ); // @@ -94,8 +177,8 @@ extern GLuint esLoadProgram ( const char *vertShaderSrc, const char *fragShaderS /// \return The number of indices required for rendering the buffers (the number of indices stored in the indices array /// if it is not NULL ) as a GL_TRIANGLE_STRIP // -extern int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **normals, - GLfloat **texCoords, GLuint **indices ); +int ESUTIL_API esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **normals, + GLfloat **texCoords, GLuint **indices ); // /// \brief Generates geometry for a cube. Allocates memory for the vertex data and stores @@ -108,8 +191,8 @@ extern int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloa /// \return The number of indices required for rendering the buffers (the number of indices stored in the indices array /// if it is not NULL ) as a GL_TRIANGLES // -extern int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, - GLfloat **texCoords, GLuint **indices ); +int ESUTIL_API esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, + GLfloat **texCoords, GLuint **indices ); // /// \brief Loads a 24-bit TGA image from a file @@ -118,7 +201,7 @@ extern int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, /// \param height Height of loaded image in pixels /// \return Pointer to loaded image. NULL on failure. // -extern char* esLoadTGA ( char *fileName, int *width, int *height ); +char* ESUTIL_API esLoadTGA ( char *fileName, int *width, int *height ); // @@ -126,14 +209,14 @@ extern char* esLoadTGA ( char *fileName, int *width, int *height ); /// \param result Specifies the input matrix. Scaled matrix is returned in result. /// \param sx, sy, sz Scale factors along the x, y and z axes respectively // -extern void esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz); +void ESUTIL_API esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz); // /// \brief multiply matrix specified by result with a translation matrix and return new matrix in result /// \param result Specifies the input matrix. Translated matrix is returned in result. /// \param tx, ty, tz Scale factors along the x, y and z axes respectively // -extern void esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz); +void ESUTIL_API esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz); // /// \brief multiply matrix specified by result with a rotation matrix and return new matrix in result @@ -141,7 +224,7 @@ extern void esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz); /// \param angle Specifies the angle of rotation, in degrees. /// \param x, y, z Specify the x, y and z coordinates of a vector, respectively // -extern void esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z); +void ESUTIL_API esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z); // // \brief multiply matrix specified by result with a perspective matrix and return new matrix in result @@ -150,7 +233,7 @@ extern void esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfl /// \param bottom, top Coordinates for the bottom and top horizontal clipping planes /// \param nearZ, farZ Distances to the near and far depth clipping planes. Both distances must be positive. // -extern void esFrustum(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ); +void ESUTIL_API esFrustum(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ); // /// \brief multiply matrix specified by result with a perspective matrix and return new matrix in result @@ -160,7 +243,7 @@ extern void esFrustum(ESMatrix *result, float left, float right, float bottom, f /// \param nearZ Near plane distance /// \param farZ Far plane distance // -extern void esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float farZ); +void ESUTIL_API esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float farZ); // /// \brief multiply matrix specified by result with a perspective matrix and return new matrix in result @@ -169,23 +252,23 @@ extern void esPerspective(ESMatrix *result, float fovy, float aspect, float near /// \param bottom, top Coordinates for the bottom and top horizontal clipping planes /// \param nearZ, farZ Distances to the near and far depth clipping planes. These values are negative if plane is behind the viewer // -extern void esOrtho(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ); +void ESUTIL_API esOrtho(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ); // /// \brief perform the following operation - result matrix = srcA matrix * srcB matrix /// \param result Returns multiplied matrix /// \param srcA, srcB Input matrices to be multiplied // -extern void esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB); +void ESUTIL_API esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB); // //// \brief return an indentity matrix //// \param result returns identity matrix // -extern void esMatrixLoadIdentity(ESMatrix *result); +void ESUTIL_API esMatrixLoadIdentity(ESMatrix *result); #ifdef __cplusplus } -#endif // __cplusplus +#endif #endif // ESUTIL_H \ No newline at end of file diff --git a/third_party/gles_book_examples/Common/Include/esUtil_win.h b/third_party/gles_book_examples/Common/Include/esUtil_win.h index cf3acdf..4017118 100644 --- a/third_party/gles_book_examples/Common/Include/esUtil_win.h +++ b/third_party/gles_book_examples/Common/Include/esUtil_win.h @@ -15,9 +15,41 @@ #ifndef ESUTIL_WIN_H #define ESUTIL_WIN_H +/// +// Includes +// + #ifdef __cplusplus + extern "C" { -#endif // __cplusplus +#endif + + +/// +// Macros +// + +/// +// Types +// + +/// +// Public Functions +// + +/// +// WinCreate() +// +// Create Win32 instance and window +// +GLboolean WinCreate ( ESContext *esContext, const char *title ); + +/// +// WinLoop() +// +// Start main windows loop +// +void WinLoop ( ESContext *esContext ); /// // WinTGALoad() @@ -28,6 +60,6 @@ int WinTGALoad ( const char *fileName, char **buffer, int *width, int *height ); #ifdef __cplusplus } -#endif // __cplusplus +#endif #endif // ESUTIL_WIN_H \ No newline at end of file diff --git a/third_party/gles_book_examples/Common/Source/Win32/esUtil_TGA.c b/third_party/gles_book_examples/Common/Source/Win32/esUtil_TGA.c index 9936a4f..4617cdd 100644 --- a/third_party/gles_book_examples/Common/Source/Win32/esUtil_TGA.c +++ b/third_party/gles_book_examples/Common/Source/Win32/esUtil_TGA.c @@ -12,10 +12,7 @@ // // This file contains the Win32 implementation of a TGA image loader -#ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN -#endif // WIN32_LEAN_AND_MEAN - #include #include #include diff --git a/third_party/gles_book_examples/Common/Source/Win32/esUtil_win32.c b/third_party/gles_book_examples/Common/Source/Win32/esUtil_win32.c new file mode 100644 index 0000000..73dcaad --- /dev/null +++ b/third_party/gles_book_examples/Common/Source/Win32/esUtil_win32.c @@ -0,0 +1,183 @@ +// +// Book: OpenGL(R) ES 2.0 Programming Guide +// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner +// ISBN-10: 0321502795 +// ISBN-13: 9780321502797 +// Publisher: Addison-Wesley Professional +// URLs: http://safari.informit.com/9780321563835 +// http://www.opengles-book.com +// + +// esUtil_win32.c +// +// This file contains the Win32 implementation of the windowing functions. + + +/// +// Includes +// +#define WIN32_LEAN_AND_MEAN +#include +#include "esUtil.h" + +////////////////////////////////////////////////////////////////// +// +// Private Functions +// +// + +/// +// ESWindowProc() +// +// Main window procedure +// +LRESULT WINAPI ESWindowProc ( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) +{ + LRESULT lRet = 1; + + switch (uMsg) + { + case WM_CREATE: + break; + + case WM_PAINT: + { + ESContext *esContext = (ESContext*)(LONG_PTR) GetWindowLongPtr ( hWnd, GWL_USERDATA ); + + if ( esContext && esContext->drawFunc ) + esContext->drawFunc ( esContext ); + + ValidateRect( esContext->hWnd, NULL ); + } + break; + + case WM_DESTROY: + PostQuitMessage(0); + break; + + case WM_CHAR: + { + POINT point; + ESContext *esContext = (ESContext*)(LONG_PTR) GetWindowLongPtr ( hWnd, GWL_USERDATA ); + + GetCursorPos( &point ); + + if ( esContext && esContext->keyFunc ) + esContext->keyFunc ( esContext, (unsigned char) wParam, + (int) point.x, (int) point.y ); +} + break; + + default: + lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); + break; + } + + return lRet; +} + +////////////////////////////////////////////////////////////////// +// +// Public Functions +// +// + +/// +// WinCreate() +// +// Create Win32 instance and window +// +GLboolean WinCreate ( ESContext *esContext, const char *title ) +{ + WNDCLASS wndclass = {0}; + DWORD wStyle = 0; + RECT windowRect; + HINSTANCE hInstance = GetModuleHandle(NULL); + + + wndclass.style = CS_OWNDC; + wndclass.lpfnWndProc = (WNDPROC)ESWindowProc; + wndclass.hInstance = hInstance; + wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wndclass.lpszClassName = "opengles2.0"; + + if (!RegisterClass (&wndclass) ) + return FALSE; + + wStyle = WS_VISIBLE | WS_POPUP | WS_BORDER | WS_SYSMENU | WS_CAPTION; + + // Adjust the window rectangle so that the client area has + // the correct number of pixels + windowRect.left = 0; + windowRect.top = 0; + windowRect.right = esContext->width; + windowRect.bottom = esContext->height; + + AdjustWindowRect ( &windowRect, wStyle, FALSE ); + + + + esContext->hWnd = CreateWindow( + "opengles2.0", + title, + wStyle, + 0, + 0, + windowRect.right - windowRect.left, + windowRect.bottom - windowRect.top, + NULL, + NULL, + hInstance, + NULL); + + // Set the ESContext* to the GWL_USERDATA so that it is available to the + // ESWindowProc + SetWindowLongPtr ( esContext->hWnd, GWL_USERDATA, (LONG) (LONG_PTR) esContext ); + + + if ( esContext->hWnd == NULL ) + return GL_FALSE; + + ShowWindow ( esContext->hWnd, TRUE ); + + return GL_TRUE; +} + +/// +// winLoop() +// +// Start main windows loop +// +void WinLoop ( ESContext *esContext ) +{ + MSG msg = { 0 }; + int done = 0; + DWORD lastTime = GetTickCount(); + + while (!done) + { + int gotMsg = (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0); + DWORD curTime = GetTickCount(); + float deltaTime = (float)( curTime - lastTime ) / 1000.0f; + lastTime = curTime; + + if ( gotMsg ) + { + if (msg.message==WM_QUIT) + { + done=1; + } + else + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + else + SendMessage( esContext->hWnd, WM_PAINT, 0, 0 ); + + // Call update function if registered + if ( esContext->updateFunc != NULL ) + esContext->updateFunc ( esContext, deltaTime ); + } +} diff --git a/third_party/gles_book_examples/Common/Source/esShader.c b/third_party/gles_book_examples/Common/Source/esShader.c index 47bf6af..4ea2cbc 100644 --- a/third_party/gles_book_examples/Common/Source/esShader.c +++ b/third_party/gles_book_examples/Common/Source/esShader.c @@ -40,7 +40,7 @@ /// \param shaderSrc Shader source string /// \return A new shader object on success, 0 on failure // -GLuint esLoadShader ( GLenum type, const char *shaderSrc ) +GLuint ESUTIL_API esLoadShader ( GLenum type, const char *shaderSrc ) { GLuint shader; GLint compiled; @@ -93,7 +93,7 @@ GLuint esLoadShader ( GLenum type, const char *shaderSrc ) /// \param fragShaderSrc Fragment shader source code /// \return A new program object linked with the vertex/fragment shader pair, 0 on failure // -GLuint esLoadProgram ( const char *vertShaderSrc, const char *fragShaderSrc ) +GLuint ESUTIL_API esLoadProgram ( const char *vertShaderSrc, const char *fragShaderSrc ) { GLuint vertexShader; GLuint fragmentShader; diff --git a/third_party/gles_book_examples/Common/Source/esShapes.c b/third_party/gles_book_examples/Common/Source/esShapes.c index 16e4ef0..b0f061d 100644 --- a/third_party/gles_book_examples/Common/Source/esShapes.c +++ b/third_party/gles_book_examples/Common/Source/esShapes.c @@ -19,7 +19,6 @@ #include "esUtil.h" #include #include -#include /// // Defines @@ -51,8 +50,8 @@ /// \return The number of indices required for rendering the buffers (the number of indices stored in the indices array /// if it is not NULL ) as a GL_TRIANGLE_STRIP // -int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **normals, - GLfloat **texCoords, GLuint **indices ) +int ESUTIL_API esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **normals, + GLfloat **texCoords, GLuint **indices ) { int i; int j; @@ -138,8 +137,8 @@ int esGenSphere ( int numSlices, float radius, GLfloat **vertices, GLfloat **nor /// \return The number of indices required for rendering the buffers (the number of indices stored in the indices array /// if it is not NULL ) as a GL_TRIANGLE_STRIP // -int esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, - GLfloat **texCoords, GLuint **indices ) +int ESUTIL_API esGenCube ( float scale, GLfloat **vertices, GLfloat **normals, + GLfloat **texCoords, GLuint **indices ) { int i; int numVertices = 24; diff --git a/third_party/gles_book_examples/Common/Source/esTransform.c b/third_party/gles_book_examples/Common/Source/esTransform.c index c714327..12182fd 100644 --- a/third_party/gles_book_examples/Common/Source/esTransform.c +++ b/third_party/gles_book_examples/Common/Source/esTransform.c @@ -20,11 +20,11 @@ // #include "esUtil.h" #include -#include #define PI 3.1415926535897932384626433832795f -void esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz) +void ESUTIL_API +esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz) { result->m[0][0] *= sx; result->m[0][1] *= sx; @@ -42,7 +42,8 @@ void esScale(ESMatrix *result, GLfloat sx, GLfloat sy, GLfloat sz) result->m[2][3] *= sz; } -void esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz) +void ESUTIL_API +esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz) { result->m[3][0] += (result->m[0][0] * tx + result->m[1][0] * ty + result->m[2][0] * tz); result->m[3][1] += (result->m[0][1] * tx + result->m[1][1] * ty + result->m[2][1] * tz); @@ -50,7 +51,8 @@ void esTranslate(ESMatrix *result, GLfloat tx, GLfloat ty, GLfloat tz) result->m[3][3] += (result->m[0][3] * tx + result->m[1][3] * ty + result->m[2][3] * tz); } -void esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) +void ESUTIL_API +esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) { GLfloat sinAngle, cosAngle; GLfloat mag = sqrtf(x * x + y * y + z * z); @@ -102,7 +104,8 @@ void esRotate(ESMatrix *result, GLfloat angle, GLfloat x, GLfloat y, GLfloat z) } } -void esFrustum(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ) +void ESUTIL_API +esFrustum(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ) { float deltaX = right - left; float deltaY = top - bottom; @@ -131,7 +134,8 @@ void esFrustum(ESMatrix *result, float left, float right, float bottom, float to } -void esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float farZ) +void ESUTIL_API +esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, float farZ) { GLfloat frustumW, frustumH; @@ -141,7 +145,8 @@ void esPerspective(ESMatrix *result, float fovy, float aspect, float nearZ, floa esFrustum( result, -frustumW, frustumW, -frustumH, frustumH, nearZ, farZ ); } -void esOrtho(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ) +void ESUTIL_API +esOrtho(ESMatrix *result, float left, float right, float bottom, float top, float nearZ, float farZ) { float deltaX = right - left; float deltaY = top - bottom; @@ -163,7 +168,8 @@ void esOrtho(ESMatrix *result, float left, float right, float bottom, float top, } -void esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB) +void ESUTIL_API +esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB) { ESMatrix tmp; int i; @@ -194,7 +200,8 @@ void esMatrixMultiply(ESMatrix *result, ESMatrix *srcA, ESMatrix *srcB) } -void esMatrixLoadIdentity(ESMatrix *result) +void ESUTIL_API +esMatrixLoadIdentity(ESMatrix *result) { memset(result, 0x0, sizeof(ESMatrix)); result->m[0][0] = 1.0f; @@ -202,3 +209,4 @@ void esMatrixLoadIdentity(ESMatrix *result) result->m[2][2] = 1.0f; result->m[3][3] = 1.0f; } + diff --git a/third_party/gles_book_examples/Common/Source/esUtil.c b/third_party/gles_book_examples/Common/Source/esUtil.c index 34a1efe..fe774ae 100644 --- a/third_party/gles_book_examples/Common/Source/esUtil.c +++ b/third_party/gles_book_examples/Common/Source/esUtil.c @@ -20,21 +20,96 @@ // #include #include -#include -#include - #include - +#include #include "esUtil.h" #include "esUtil_win.h" + + + +/// +// CreateEGLContext() +// +// Creates an EGL rendering context and all associated elements +// +EGLBoolean CreateEGLContext ( EGLNativeWindowType hWnd, EGLDisplay* eglDisplay, + EGLContext* eglContext, EGLSurface* eglSurface, + EGLint attribList[]) +{ + EGLint numConfigs; + EGLint majorVersion; + EGLint minorVersion; + EGLDisplay display; + EGLContext context; + EGLSurface surface; + EGLConfig config; + EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE }; + + // Get Display + display = eglGetDisplay(GetDC(hWnd)); + if ( display == EGL_NO_DISPLAY ) + { + return EGL_FALSE; + } + + // Initialize EGL + if ( !eglInitialize(display, &majorVersion, &minorVersion) ) + { + return EGL_FALSE; + } + + // Get configs + if ( !eglGetConfigs(display, NULL, 0, &numConfigs) ) + { + return EGL_FALSE; + } + + // Choose config + if ( !eglChooseConfig(display, attribList, &config, 1, &numConfigs) ) + { + return EGL_FALSE; + } + + // Create a surface + surface = eglCreateWindowSurface(display, config, (EGLNativeWindowType)hWnd, NULL); + if ( surface == EGL_NO_SURFACE ) + { + return EGL_FALSE; + } + + // Create a GL context + context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs ); + if ( context == EGL_NO_CONTEXT ) + { + return EGL_FALSE; + } + + // Make the context current + if ( !eglMakeCurrent(display, surface, surface, context) ) + { + return EGL_FALSE; + } + + *eglDisplay = display; + *eglSurface = surface; + *eglContext = context; + return EGL_TRUE; +} + +////////////////////////////////////////////////////////////////// +// +// Public Functions +// +// + /// // esInitContext() // // Initialize ES utility context. This must be called before calling any other // functions. // -void esInitContext ( ESContext *esContext ) +void ESUTIL_API esInitContext ( ESContext *esContext ) { if ( esContext != NULL ) { @@ -43,11 +118,103 @@ void esInitContext ( ESContext *esContext ) } /// +// esCreateWindow() +// +// title - name for title bar of window +// width - width of window to create +// height - height of window to create +// flags - bitwise or of window creation flags +// ES_WINDOW_ALPHA - specifies that the framebuffer should have alpha +// ES_WINDOW_DEPTH - specifies that a depth buffer should be created +// ES_WINDOW_STENCIL - specifies that a stencil buffer should be created +// ES_WINDOW_MULTISAMPLE - specifies that a multi-sample buffer should be created +// +GLboolean ESUTIL_API esCreateWindow ( ESContext *esContext, const char* title, GLint width, GLint height, GLuint flags ) +{ + EGLint attribList[] = + { + EGL_RED_SIZE, 5, + EGL_GREEN_SIZE, 6, + EGL_BLUE_SIZE, 5, + EGL_ALPHA_SIZE, (flags & ES_WINDOW_ALPHA) ? 8 : EGL_DONT_CARE, + EGL_DEPTH_SIZE, (flags & ES_WINDOW_DEPTH) ? 8 : EGL_DONT_CARE, + EGL_STENCIL_SIZE, (flags & ES_WINDOW_STENCIL) ? 8 : EGL_DONT_CARE, + EGL_SAMPLE_BUFFERS, (flags & ES_WINDOW_MULTISAMPLE) ? 1 : 0, + EGL_NONE + }; + + if ( esContext == NULL ) + { + return GL_FALSE; + } + + esContext->width = width; + esContext->height = height; + + if ( !WinCreate ( esContext, title) ) + { + return GL_FALSE; + } + + + if ( !CreateEGLContext ( esContext->hWnd, + &esContext->eglDisplay, + &esContext->eglContext, + &esContext->eglSurface, + attribList) ) + { + return GL_FALSE; + } + + + return GL_TRUE; +} + +/// +// esMainLoop() +// +// Start the main loop for the OpenGL ES application +// +void ESUTIL_API esMainLoop ( ESContext *esContext ) +{ + WinLoop ( esContext ); +} + + +/// +// esRegisterDrawFunc() +// +void ESUTIL_API esRegisterDrawFunc ( ESContext *esContext, void (ESCALLBACK *drawFunc) (ESContext* ) ) +{ + esContext->drawFunc = drawFunc; +} + + +/// +// esRegisterUpdateFunc() +// +void ESUTIL_API esRegisterUpdateFunc ( ESContext *esContext, void (ESCALLBACK *updateFunc) ( ESContext*, float ) ) +{ + esContext->updateFunc = updateFunc; +} + + +/// +// esRegisterKeyFunc() +// +void ESUTIL_API esRegisterKeyFunc ( ESContext *esContext, + void (ESCALLBACK *keyFunc) (ESContext*, unsigned char, int, int ) ) +{ + esContext->keyFunc = keyFunc; +} + + +/// // esLogMessage() // // Log an error message to the debug output for the platform // -void esLogMessage ( const char *formatStr, ... ) +void ESUTIL_API esLogMessage ( const char *formatStr, ... ) { va_list params; char buf[BUFSIZ]; @@ -60,12 +227,13 @@ void esLogMessage ( const char *formatStr, ... ) va_end ( params ); } + /// // esLoadTGA() // // Loads a 24-bit TGA image from a file // -char* esLoadTGA ( char *fileName, int *width, int *height ) +char* ESUTIL_API esLoadTGA ( char *fileName, int *width, int *height ) { char *buffer; diff --git a/third_party/gles_book_examples/gles_book_examples.gyp b/third_party/gles_book_examples/gles_book_examples.gyp deleted file mode 100644 index eeaa68d..0000000 --- a/third_party/gles_book_examples/gles_book_examples.gyp +++ /dev/null @@ -1,49 +0,0 @@ -# Copyright (c) 2009 The Chromium Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. - -{ - 'targets': [ - { - 'target_name': 'es_util', - 'type': 'static_library', - 'dependencies': [ - '../../gpu/gpu.gyp:gles2_c_lib', - ], - 'include_dirs': [ - 'Common/Include', - ], - 'all_dependent_settings': { - 'include_dirs': [ - 'Common/Include', - ], - }, - 'sources': [ - 'Common/Include/esUtil.h', - 'Common/Include/esUtil_win.h', - 'Common/Source/esShader.c', - 'Common/Source/esShapes.c', - 'Common/Source/esTransform.c', - 'Common/Source/esUtil.c', - 'Common/Source/Win32/esUtil_TGA.c', - ], - }, - { - 'target_name': 'hello_triangle', - 'type': 'static_library', - 'dependencies': [ - 'es_util', - ], - 'sources': [ - 'Chapter_2/Hello_Triangle/Hello_Triangle.c', - 'Chapter_2/Hello_Triangle/Hello_Triangle.h', - ], - }, - ] -} - -# Local Variables: -# tab-width:2 -# indent-tabs-mode:nil -# End: -# vim: set expandtab tabstop=2 shiftwidth=2: -- cgit v1.1