《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
- 《OpenGL ES 2.0 Programming Guide》第9章 “最简单的本地纹理显示”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第9章 “最简单的本地纹理+VBO”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第12章 “最简单的本地纹理+FBO+VBO”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第9章“最简单的MipMap”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第11章“最简单的MSAA”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第8章 “最简单的Vertex Buffer Object”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第12章“最简单的FBO Depth Texture”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第12章“最简单的ReadPixels并保存为BMP”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第12章 “最简单的Multi-Pass+VBO”示例代码【C语言版】
- 《OpenGL ES 2.0 Programming Guide》第12章“Framebuffer Objects”示例代码【C语言版】
- 西蒙OpenGL ES教程第五章的示例代码显示不了纹理
- OpenGL(R) ES 2.0 Programming Guide
- 《OpenGL ES 2.0 Programming Guide》摘录
- opengl es 纹理无法显示的问题
- Opengl ES 2.0 简单的纹理范例 iOS
- OpenGL ES Programming Guide for iOS 第二章
- OpenGL ES Programming Guide for iOS 第二章
- OpenGL ES Programming Guide for iOS 第一章
- PAT 1011. World Cup Betting (20)(浮点数乘法)
- Java-TCP/IP编程-02 * 客户端: 主线程:从控制台录入消息,发送到服务器 子线程:接收服务器转发来的消息
- iOS开发系列--打造自己的“美图秀秀”
- Java-TCP/IP编程-03 消息群发功能
- 【类库】java时间操作类库:Joda-Time
- 《OpenGL ES 2.0 Programming Guide》第9章 “最简单的本地纹理显示”示例代码【C语言版】
- Java-TCP/IP编程-04 带窗口的群聊天功能
- 文件结构图
- CodeForces 148BEscape
- linux(CVE-2010-3847)漏洞和利用方法
- u盘安装centos6
- 对象转换为JSON数据格式&使用JQuery获取数据
- 深度解析CentOS通过日志反查入侵
- 120. Triangle