diff options
-rw-r--r-- | gpu/demos/demos.gyp | 11 | ||||
-rw-r--r-- | gpu/demos/mip_map_2d/main.cc | 64 | ||||
-rw-r--r-- | third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.c | 118 | ||||
-rw-r--r-- | third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.h | 52 | ||||
-rw-r--r-- | third_party/gles2_book/gles2_book.gyp | 11 |
5 files changed, 185 insertions, 71 deletions
diff --git a/gpu/demos/demos.gyp b/gpu/demos/demos.gyp index 43ecb50..a7596c1 100644 --- a/gpu/demos/demos.gyp +++ b/gpu/demos/demos.gyp @@ -35,6 +35,17 @@ ], }, { + 'target_name': 'mip_map_2d', + 'type': 'executable', + 'dependencies': [ + 'app_framework', + '../../third_party/gles2_book/gles2_book.gyp:mip_map_2d', + ], + 'sources': [ + 'mip_map_2d/main.cc', + ], + }, + { 'target_name': 'simple_vertex_shader', 'type': 'executable', 'dependencies': [ diff --git a/gpu/demos/mip_map_2d/main.cc b/gpu/demos/mip_map_2d/main.cc new file mode 100644 index 0000000..6fa9bed --- /dev/null +++ b/gpu/demos/mip_map_2d/main.cc @@ -0,0 +1,64 @@ +// Copyright (c) 2010 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. + +// This is a simple example that demonstrates generating a mipmap chain +// and rendering with it. + +#include "gpu/demos/app_framework/application.h" +#include "third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.h" + +namespace gpu_demos { +class MipMap2D : public Application { + public: + MipMap2D(); + ~MipMap2D(); + + bool Init(); + + protected: + virtual void Draw(float); + + private: + ESContext context_; + MMUserData user_data_; + + DISALLOW_COPY_AND_ASSIGN(MipMap2D); +}; + +MipMap2D::MipMap2D() { + esInitContext(&context_); + + memset(&user_data_, 0, sizeof(MMUserData)); + context_.userData = &user_data_; +} + +MipMap2D::~MipMap2D() { + mmShutDown(&context_); +} + +bool MipMap2D::Init() { + if (!Application::InitRenderContext()) return false; + + context_.width = width(); + context_.height = height(); + if (!mmInit(&context_)) return false; + + return true; +} + +void MipMap2D::Draw(float) { + mmDraw(&context_); +} +} // namespace gpu_demos + +int main(int argc, char *argv[]) { + gpu_demos::MipMap2D app; + if (!app.Init()) { + printf("Could not init.\n"); + return EXIT_FAILURE; + } + + app.MainLoop(); + return EXIT_SUCCESS; +} diff --git a/third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.c b/third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.c index 47c2a563..f45d214 100644 --- a/third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.c +++ b/third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.c @@ -14,33 +14,12 @@ // and rendering with it // #include <stdlib.h> -#include "esUtil.h" - -typedef struct -{ - // Handle to a program object - GLuint programObject; - - // Attribute locations - GLint positionLoc; - GLint texCoordLoc; - - // Sampler location - GLint samplerLoc; - - // Offset location - GLint offsetLoc; - - // Texture handle - GLuint textureId; - -} UserData; - +#include "MipMap2D.h" /// // From an RGB8 source image, generate the next level mipmap // -GLboolean GenMipMap2D( GLubyte *src, GLubyte **dst, int srcWidth, int srcHeight, int *dstWidth, int *dstHeight ) +static GLboolean GenMipMap2D( GLubyte *src, GLubyte **dst, int srcWidth, int srcHeight, int *dstWidth, int *dstHeight ) { int x, y; @@ -105,7 +84,7 @@ GLboolean GenMipMap2D( GLubyte *src, GLubyte **dst, int srcWidth, int srcHeight, /// // Generate an RGB8 checkerboard image // -GLubyte* GenCheckImage( int width, int height, int checkSize ) +static GLubyte* GenCheckImage( int width, int height, int checkSize ) { int x, y; @@ -142,7 +121,7 @@ GLubyte* GenCheckImage( int width, int height, int checkSize ) /// // Create a mipmapped 2D texture image // -GLuint CreateMipMappedTexture2D( ) +static GLuint CreateMipMappedTexture2D( ) { // Texture object handle GLuint textureId; @@ -210,9 +189,9 @@ GLuint CreateMipMappedTexture2D( ) /// // Initialize the shader and program object // -int Init ( ESContext *esContext ) +int mmInit ( ESContext *esContext ) { - UserData *userData = esContext->userData; + MMUserData *userData = esContext->userData; GLbyte vShaderStr[] = "uniform float u_offset; \n" "attribute vec4 a_position; \n" @@ -225,8 +204,9 @@ int Init ( ESContext *esContext ) " v_texCoord = a_texCoord; \n" "} \n"; + // TODO(alokp): Shaders containing "precision" do not compile. GLbyte fShaderStr[] = - "precision mediump float; \n" + "//precision mediump float; \n" "varying vec2 v_texCoord; \n" "uniform sampler2D s_texture; \n" "void main() \n" @@ -234,8 +214,20 @@ int Init ( ESContext *esContext ) " gl_FragColor = texture2D( s_texture, v_texCoord );\n" "} \n"; + GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, 1.5f, // Position 0 + 0.0f, 0.0f, // TexCoord 0 + -0.5f, -0.5f, 0.0f, 0.75f, // Position 1 + 0.0f, 1.0f, // TexCoord 1 + 0.5f, -0.5f, 0.0f, 0.75f, // Position 2 + 1.0f, 1.0f, // TexCoord 2 + 0.5f, 0.5f, 0.0f, 1.5f, // Position 3 + 1.0f, 0.0f // TexCoord 3 + }; + GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; + // Load the shaders and get a linked program object userData->programObject = esLoadProgram ( vShaderStr, fShaderStr ); + if (userData->programObject == 0) return FALSE; // Get the attribute locations userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" ); @@ -250,6 +242,15 @@ int Init ( ESContext *esContext ) // Load the texture userData->textureId = CreateMipMappedTexture2D (); + // Load vertex data + glGenBuffers ( 2, userData->vboIds ); + glBindBuffer ( GL_ARRAY_BUFFER, userData->vboIds[0] ); + glBufferData ( GL_ARRAY_BUFFER, sizeof(vVertices), + vVertices, GL_STATIC_DRAW); + glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, userData->vboIds[1] ); + glBufferData ( GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), + indices, GL_STATIC_DRAW ); + glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); return TRUE; } @@ -257,19 +258,13 @@ int Init ( ESContext *esContext ) /// // Draw a triangle using the shader pair created in Init() // -void Draw ( ESContext *esContext ) +#define VTX_POS_SIZE 4 +#define VTX_TEX_SIZE 2 +#define VTX_STRIDE (6 * sizeof(GLfloat)) +void mmDraw ( ESContext *esContext ) { - UserData *userData = esContext->userData; - GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, 1.5f, // Position 0 - 0.0f, 0.0f, // TexCoord 0 - -0.5f, -0.5f, 0.0f, 0.75f, // Position 1 - 0.0f, 1.0f, // TexCoord 1 - 0.5f, -0.5f, 0.0f, 0.75f, // Position 2 - 1.0f, 1.0f, // TexCoord 2 - 0.5f, 0.5f, 0.0f, 1.5f, // Position 3 - 1.0f, 0.0f // TexCoord 3 - }; - GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; + MMUserData *userData = esContext->userData; + GLuint offset = 0; // Set the viewport glViewport ( 0, 0, esContext->width, esContext->height ); @@ -281,11 +276,12 @@ void Draw ( ESContext *esContext ) glUseProgram ( userData->programObject ); // Load the vertex position - glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT, - GL_FALSE, 6 * sizeof(GLfloat), vVertices ); + glVertexAttribPointer ( userData->positionLoc, VTX_POS_SIZE, GL_FLOAT, + GL_FALSE, VTX_STRIDE, (GLvoid*) offset ); + offset += VTX_POS_SIZE * sizeof(GLfloat); // Load the texture coordinate - glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT, - GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] ); + glVertexAttribPointer ( userData->texCoordLoc, VTX_TEX_SIZE, GL_FLOAT, + GL_FALSE, VTX_STRIDE, (GLvoid*) offset ); glEnableVertexAttribArray ( userData->positionLoc ); glEnableVertexAttribArray ( userData->texCoordLoc ); @@ -300,47 +296,27 @@ void Draw ( ESContext *esContext ) // Draw quad with nearest sampling glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glUniform1f ( userData->offsetLoc, -0.6f ); - glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); + glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); // Draw quad with trilinear filtering glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); glUniform1f ( userData->offsetLoc, 0.6f ); - glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices ); - - eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface ); + glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0 ); } /// // Cleanup // -void ShutDown ( ESContext *esContext ) +void mmShutDown ( ESContext *esContext ) { - UserData *userData = esContext->userData; + MMUserData *userData = esContext->userData; // Delete texture object glDeleteTextures ( 1, &userData->textureId ); + // Delete VBOs + glDeleteBuffers ( 2, userData->vboIds ); + // Delete program object glDeleteProgram ( userData->programObject ); } - - -int main ( int argc, char *argv[] ) -{ - ESContext esContext; - UserData userData; - - esInitContext ( &esContext ); - esContext.userData = &userData; - - esCreateWindow ( &esContext, "MipMap 2D", 320, 240, ES_WINDOW_RGB ); - - if ( !Init ( &esContext ) ) - return 0; - - esRegisterDrawFunc ( &esContext, Draw ); - - esMainLoop ( &esContext ); - - ShutDown ( &esContext ); -} diff --git a/third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.h b/third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.h new file mode 100644 index 0000000..9dbe834 --- /dev/null +++ b/third_party/gles2_book/Chapter_9/MipMap2D/MipMap2D.h @@ -0,0 +1,52 @@ +// +// 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 +// + +#ifndef MIP_MAP_2D_H +#define MIP_MAP_2D_H + +#include "esUtil.h" + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +typedef struct +{ + // Handle to a program object + GLuint programObject; + + // Attribute locations + GLint positionLoc; + GLint texCoordLoc; + + // Sampler location + GLint samplerLoc; + + // Offset location + GLint offsetLoc; + + // Texture handle + GLuint textureId; + + // Vertex buffer object handle + GLuint vboIds[2]; + +} MMUserData; + +extern int mmInit ( ESContext *esContext ); + +extern void mmDraw ( ESContext *esContext ); + +extern void mmShutDown ( ESContext *esContext ); + +#ifdef __cplusplus +} +#endif // __cplusplus +#endif // MIP_MAP_2D_H diff --git a/third_party/gles2_book/gles2_book.gyp b/third_party/gles2_book/gles2_book.gyp index 96a6673..d94fcc3 100644 --- a/third_party/gles2_book/gles2_book.gyp +++ b/third_party/gles2_book/gles2_book.gyp @@ -40,6 +40,17 @@ ], }, { + 'target_name': 'mip_map_2d', + 'type': 'static_library', + 'dependencies': [ + 'es_util', + ], + 'sources': [ + 'Chapter_9/MipMap2D/MipMap2D.c', + 'Chapter_9/MipMap2D/MipMap2D.h', + ], + }, + { 'target_name': 'simple_vertex_shader', 'type': 'static_library', 'dependencies': [ |