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

举报
ShaderJoy 发表于 2021/12/30 00:12:59 2021/12/30
【摘要】 由于《OpenGL ES 2.0 Programming Guide》原书第9章并没有提供本地纹理加载的示例,都是程序生成的,遂自己实现了一份C语言版本的,希望能够帮助到同样喜欢OpenGL ES 2.0的同学。 废话不多说,直接上代码: #include <stdlib.h>#include "esU...

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

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



  
  1. #include <stdlib.h>
  2. #include "esUtil.h"
  3. typedef struct
  4. {
  5. // Handle to a program object
  6. GLuint programObject;
  7. // Attribute locations
  8. GLint positionLoc;
  9. GLint texCoordLoc;
  10. // Sampler location
  11. GLint samplerLoc;
  12. // Texture handle
  13. GLuint textureId;
  14. } UserData;
  15. ///
  16. // Load texture from disk
  17. //
  18. GLuint LoadTexture ( char *fileName )
  19. {
  20. int width, height;
  21. char *buffer = esLoadTGA ( fileName, &width, &height );
  22. GLuint texId;
  23. if ( buffer == NULL )
  24. {
  25. esLogMessage ( "Error loading (%s) image.\n", fileName );
  26. return 0;
  27. }
  28. glGenTextures ( 1, &texId );
  29. glBindTexture ( GL_TEXTURE_2D, texId );
  30. glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer );
  31. glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  32. glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  33. glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
  34. glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
  35. free ( buffer );
  36. return texId;
  37. }
  38. ///
  39. // Initialize the shader and program object
  40. //
  41. int Init ( ESContext *esContext )
  42. {
  43. char *fileName = "D:/Projects/Visual Studio 2012/OpenGL_Demo/opengles-book-samples-master/Windows/Chapter_9/Simple_Texture2D/Fieldstone.tga";
  44. UserData *userData = esContext->userData;
  45. GLbyte vShaderStr[] =
  46. "attribute vec4 a_position; \n"
  47. "attribute vec2 a_texCoord; \n"
  48. "varying vec2 v_texCoord; \n"
  49. "void main() \n"
  50. "{ \n"
  51. " gl_Position = a_position; \n"
  52. " v_texCoord = a_texCoord; \n"
  53. "} \n";
  54. GLbyte fShaderStr[] =
  55. "precision mediump float; \n"
  56. "varying vec2 v_texCoord; \n"
  57. "uniform sampler2D s_texture; \n"
  58. "void main() \n"
  59. "{ \n"
  60. " gl_FragColor = texture2D( s_texture, v_texCoord );\n"
  61. "} \n";
  62. // Load the shaders and get a linked program object
  63. userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
  64. // Get the attribute locations
  65. userData->positionLoc = glGetAttribLocation ( userData->programObject, "a_position" );
  66. userData->texCoordLoc = glGetAttribLocation ( userData->programObject, "a_texCoord" );
  67. // Get the sampler location
  68. userData->samplerLoc = glGetUniformLocation ( userData->programObject, "s_texture" );
  69. // Load the texture
  70. //userData->textureId = CreateSimpleTexture2D ();
  71. userData->textureId = LoadTexture(fileName);
  72. glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
  73. return TRUE;
  74. }
  75. ///
  76. // Draw a triangle using the shader pair created in Init()
  77. //
  78. void Draw ( ESContext *esContext )
  79. {
  80. UserData *userData = esContext->userData;
  81. GLfloat vVertices[] = { -0.5f, 0.5f, 0.0f, // Position 0
  82. 0.0f, 0.0f, // TexCoord 0
  83. -0.5f, -0.5f, 0.0f, // Position 1
  84. 0.0f, 1.0f, // TexCoord 1
  85. 0.5f, -0.5f, 0.0f, // Position 2
  86. 1.0f, 1.0f, // TexCoord 2
  87. 0.5f, 0.5f, 0.0f, // Position 3
  88. 1.0f, 0.0f // TexCoord 3
  89. };
  90. GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
  91. // Set the viewport
  92. glViewport ( 0, 0, esContext->width, esContext->height );
  93. // Clear the color buffer
  94. glClear ( GL_COLOR_BUFFER_BIT );
  95. // Use the program object
  96. glUseProgram ( userData->programObject );
  97. // Load the vertex position
  98. glVertexAttribPointer ( userData->positionLoc, 3, GL_FLOAT,
  99. GL_FALSE, 5 * sizeof(GLfloat), vVertices );
  100. // Load the texture coordinate
  101. glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
  102. GL_FALSE, 5 * sizeof(GLfloat), &vVertices[3] );
  103. glEnableVertexAttribArray ( userData->positionLoc );
  104. glEnableVertexAttribArray ( userData->texCoordLoc );
  105. // Bind the texture
  106. glActiveTexture ( GL_TEXTURE0 );
  107. glBindTexture ( GL_TEXTURE_2D, userData->textureId );
  108. // Set the sampler texture unit to 0
  109. glUniform1i ( userData->samplerLoc, 0 );
  110. glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
  111. eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
  112. }
  113. ///
  114. // Cleanup
  115. //
  116. void ShutDown ( ESContext *esContext )
  117. {
  118. UserData *userData = esContext->userData;
  119. // Delete texture object
  120. glDeleteTextures ( 1, &userData->textureId );
  121. // Delete program object
  122. glDeleteProgram ( userData->programObject );
  123. }
  124. int main ( int argc, char *argv[] )
  125. {
  126. ESContext esContext;
  127. UserData userData;
  128. esInitContext ( &esContext );
  129. esContext.userData = &userData;
  130. esCreateWindow ( &esContext, "Simple Texture 2D", 512, 512, ES_WINDOW_RGB );
  131. if ( !Init ( &esContext ) )
  132. return 0;
  133. esRegisterDrawFunc ( &esContext, Draw );
  134. esMainLoop ( &esContext );
  135. ShutDown ( &esContext );
  136. }



文章来源: panda1234lee.blog.csdn.net,作者:panda1234lee,版权归原作者所有,如需转载,请联系作者。

原文链接:panda1234lee.blog.csdn.net/article/details/52406190

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。