ZBLOG

Ubuntu OpenGL ES Shader中v_texCoord赋值的完整代码例子

下面是一个完整的代码示例,展示了如何在Ubuntu上使用OpenGL ES进行纹理映射。这个示例包括创建窗口、设置着色器、定义顶点数据并绘制带有纹理的矩形。

完整代码示例

以下示例使用EGLGLES2库来创建上下文和绘制图形。请确保你已经安装了相关的OpenGL ES库。

1. main.cpp

  1. #include <EGL/egl.h>
  2. #include <GLES2/gl2.h>
  3. #include <GLES2/gl2ext.h>
  4. #include <iostream>
  5. #include <vector>
  6. const char* vertexShaderSource = R"(
  7. attribute vec4 a_position;
  8. attribute vec2 a_texCoord;
  9. varying vec2 v_texCoord;
  10. void main() {
  11. gl_Position = a_position;
  12. v_texCoord = a_texCoord;
  13. }
  14. )";
  15. const char* fragmentShaderSource = R"(
  16. precision mediump float;
  17. varying vec2 v_texCoord;
  18. uniform sampler2D u_texture;
  19. void main() {
  20. gl_FragColor = texture2D(u_texture, v_texCoord);
  21. }
  22. )";
  23. // Function to compile shaders
  24. GLuint compileShader(GLenum type, const char* source) {
  25. GLuint shader = glCreateShader(type);
  26. glShaderSource(shader, 1, &source, nullptr);
  27. glCompileShader(shader);
  28. // Check for compilation errors
  29. GLint success;
  30. glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
  31. if (!success) {
  32. GLchar infoLog[512];
  33. glGetShaderInfoLog(shader, sizeof(infoLog), nullptr, infoLog);
  34. std::cerr << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl;
  35. return 0; // Return 0 on error
  36. }
  37. return shader;
  38. }
  39. // Function to create shader program
  40. GLuint createProgram() {
  41. GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
  42. GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
  43. GLuint program = glCreateProgram();
  44. glAttachShader(program, vertexShader);
  45. glAttachShader(program, fragmentShader);
  46. glLinkProgram(program);
  47. // Cleanup shaders as they're linked into the program now and no longer needed
  48. glDeleteShader(vertexShader);
  49. glDeleteShader(fragmentShader);
  50. return program;
  51. }
  52. // Main function
  53. int main() {
  54. // Setup EGL context (similar setup code would go here)
  55. // Vertex data including position and texture coordinates
  56. GLfloat vertices[] = {
  57. // Positions // Texture Coords (u,v)
  58. 0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
  59. 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
  60. -0.5f,-0.5f , 0.0f , 0.0f , 0.0f ,
  61. -0.5f , 0.5f , 0.0f , 0.0f , 1.0f,
  62. };
  63. GLuint indices[] = {
  64. 0, 1, 3,
  65. 1, 2, 3
  66. };
  67. GLuint VBOs[2], VAO;
  68. // Generate buffers and arrays.
  69. glGenVertexArrays(1,&VAO);
  70. glGenBuffers(2,VBOs);
  71. // Bind the Vertex Array Object first before binding buffers and setting attributes.
  72. glBindVertexArray(VAO);
  73. // Position Buffer
  74. glBindBuffer(GL_ARRAY_BUFFER,VBOs[0]);
  75. glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices.GL_STATIC_DRAW );
  76. // Index buffer.
  77. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,VBOs[1]);
  78. glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices.GL_STATIC_DRAW );
  79. // Specify position attribute.
  80. GLint posAttrib=glGetAttribLocation(createProgram(),"a_position");
  81. GLint texAttrib=glGetAttribLocation(createProgram(),"a_texCoord");
  82. if(posAttrib>=o && texAttrib >=o){
  83. std::cerr<< "ERROR::Attribute location failed."<<std::endl ;
  84. return -1 ;
  85. }
  86. /* specify the layout of the vertex data */
  87. const int stride=sizeof(float)*5;
  88. /* Position */
  89. size_t offset=offsetof(VertexData,y)
  90. /* position*/
  91. gGlVertexAttribPointer(posAttributionSTRIDE,(3+offset));
  92. /* Enable the position attribute */
  93. gGlEnableVertexAttribArray(posAttributionSTRIDE);
  94. }

编译和运行

为了编译这个程序,你需要确保系统中安装了 OpenGL ES 和 EGL 的开发包。在 Ubuntu 中,可以使用以下命令安装它们:

  1. sudo apt-get install libgles2-mesa-dev libegl1-mesa-dev

然后,使用以下命令编译你的程序:

  1. g++ main.cpp -o opengles_example -lGLESv2 -lEGL -lpthread -ldl

注意事项

  • 上面的代码提供了一个基本框架,用于加载着色器、生成顶点缓冲区对象(VBO)以及定义顶点数据和纹理坐标。
  • 实际的窗口管理和 EGL 初始化部分并没有完全实现。你需要根据你的需求选择适当的窗口管理库(如 GLFW 或 SDL)。
  • 请确保添加纹理加载逻辑,并绑定适当的纹理到 u_texture uniform。

希望这个示例能帮助你开始使用 OpenGL ES!如果还有其他问题,请随时问我!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://sdn.0voice.com/?id=6595

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?