《OpenGL ES 2.0 Programming Guide》第9章 “最简单的本地纹理显示”示例代码【C语言版】

来源:互联网 发布:老王python培训 编辑:程序博客网 时间:2024/06/05 07:32

由于《OpenGL ES 2.0 Programming Guide》原书第9章并没有提供本地纹理加载的示例,都是程序生成的,遂自己实现了一份C语言版本的,希望能够帮助到同样喜欢OpenGL ES 2.0的同学。

废话不多说,直接上代码:


#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;    // Texture handle    GLuint textureId;} UserData;///// Load texture from disk//GLuint LoadTexture ( char *fileName ){    int width, height;    char *buffer = esLoadTGA ( fileName, &width, &height );    GLuint texId;    if ( buffer == NULL )    {        esLogMessage ( "Error loading (%s) image.\n", fileName );        return 0;    }    glGenTextures ( 1, &texId );    glBindTexture ( GL_TEXTURE_2D, texId );    glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer );    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );    glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );    free ( buffer );    return texId;}///// Initialize the shader and program object//int Init ( ESContext *esContext ){    char *fileName = "D:/Projects/Visual Studio 2012/OpenGL_Demo/opengles-book-samples-master/Windows/Chapter_9/Simple_Texture2D/Fieldstone.tga";    UserData *userData = esContext->userData;    GLbyte vShaderStr[] =        "attribute vec4 a_position;   \n"        "attribute vec2 a_texCoord;   \n"        "varying vec2 v_texCoord;     \n"        "void main()                  \n"        "{                            \n"        "   gl_Position = a_position; \n"        "   v_texCoord = a_texCoord;  \n"        "}                            \n";    GLbyte fShaderStr[] =        "precision mediump float;                            \n"        "varying vec2 v_texCoord;                            \n"        "uniform sampler2D s_texture;                        \n"        "void main()                                         \n"        "{                                                   \n"        "  gl_FragColor = texture2D( s_texture, v_texCoord );\n"        "}                                                   \n";    // Load the shaders and get a linked program object    userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );    // Get the attribute locations    userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );    userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" );    // Get the sampler location    userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );    // Load the texture    //userData->textureId = CreateSimpleTexture2D ();    userData->textureId = LoadTexture(fileName);    glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );    return TRUE;}///// Draw a triangle using the shader pair created in Init()//void Draw ( ESContext *esContext ){    UserData *userData = esContext->userData;    GLfloat vVertices[] = { -0.5f,  0.5f, 0.0f,  // Position 0                            0.0f,  0.0f,        // TexCoord 0                            -0.5f, -0.5f, 0.0f,  // Position 1                            0.0f,  1.0f,        // TexCoord 1                            0.5f, -0.5f, 0.0f,  // Position 2                            1.0f,  1.0f,        // TexCoord 2                            0.5f,  0.5f, 0.0f,  // Position 3                            1.0f,  0.0f         // TexCoord 3                          };    GLushort indices[] = { 0, 1, 2, 0, 2, 3 };    // Set the viewport    glViewport ( 0, 0, esContext->width, esContext->height );    // Clear the color buffer    glClear ( GL_COLOR_BUFFER_BIT );    // Use the program object    glUseProgram ( userData->programObject );    // Load the vertex position    glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,                            GL_FALSE, 5 * sizeof(GLfloat), vVertices );    // Load the texture coordinate    glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,                            GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );    glEnableVertexAttribArray ( userData->positionLoc );    glEnableVertexAttribArray ( userData->texCoordLoc );    // Bind the texture    glActiveTexture ( GL_TEXTURE0 );    glBindTexture ( GL_TEXTURE_2D, userData->textureId );    // Set the sampler texture unit to 0    glUniform1i ( userData->samplerLoc, 0 );    glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );    eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );}///// Cleanup//void ShutDown ( ESContext *esContext ){    UserData *userData = esContext->userData;    // Delete texture object    glDeleteTextures ( 1, &userData->textureId );    // Delete program object    glDeleteProgram ( userData->programObject );}int main ( int argc, char *argv[] ){    ESContext esContext;    UserData  userData;    esInitContext ( &esContext );    esContext.userData = &userData;    esCreateWindow ( &esContext, "Simple Texture 2D", 512, 512, ES_WINDOW_RGB );    if ( !Init ( &esContext ) )        return 0;    esRegisterDrawFunc ( &esContext, Draw );    esMainLoop ( &esContext );    ShutDown ( &esContext );}

效果如图



0 0
原创粉丝点击