ZBLOG

使用glfw缩放纹理并显示的例子

下面是一个使用GLFW缩放纹理并显示的简单示例代码:

  1. #include <GL/glew.h>
  2. #include <GLFW/glfw3.h>
  3. #include <iostream>
  4. // 回调函数:窗口大小变化时触发,用于调整视口
  5. void framebuffer_size_callback(GLFWwindow* window, int width, int height)
  6. {
  7. glViewport(0, 0, width, height);
  8. }
  9. int main()
  10. {
  11. // 初始化GLFW
  12. if (!glfwInit())
  13. {
  14. std::cout << "Failed to initialize GLFW" << std::endl;
  15. return -1;
  16. }
  17. // 创建窗口和OpenGL上下文
  18. GLFWwindow* window = glfwCreateWindow(800, 600, "Texture Scaling Example", nullptr, nullptr);
  19. if (window == nullptr)
  20. {
  21. std::cout << "Failed to create GLFW window" << std::endl;
  22. glfwTerminate();
  23. return -1;
  24. }
  25. // 设置当前窗口为当前线程的主上下文
  26. glfwMakeContextCurrent(window);
  27. // 设置视口大小回调函数
  28. glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  29. // 初始化GLEW
  30. if (glewInit() != GLEW_OK)
  31. {
  32. std::cout << "Failed to initialize GLEW" << std::endl;
  33. glfwTerminate();
  34. return -1;
  35. }
  36. // 纹理坐标数组
  37. float textureCoordinates[] = {
  38. 0.0f, 0.0f,
  39. 1.0f, 0.0f,
  40. 1.0f, 1.0f,
  41. 0.0f, 1.0f
  42. };
  43. // 顶点数组
  44. float vertices[] = {
  45. // 位置 // 纹理坐标
  46. -0.5f, -0.5f, 0.0f, 0.0f,
  47. 0.5f, -0.5f, 1.0f, 0.0f,
  48. 0.5f, 0.5f, 1.0f, 1.0f,
  49. -0.5f, 0.5f, 0.0f, 1.0f
  50. };
  51. // 索引数组
  52. unsigned int indices[] = {
  53. 0, 1, 2,
  54. 2, 3 ,0
  55. };
  56. // 创建并绑定顶点数组对象(VAO)
  57. unsigned int VAO;
  58. glGenVertexArrays(1, &VAO);
  59. glBindVertexArray(VAO);
  60. // 创建并绑定顶点缓冲对象(VBO)
  61. unsigned int VBO;
  62. glGenBuffers(1, &VBO);
  63. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  64. glBufferData(GL_ARRAY_BUFFER,sizeof(vertices), vertices,GL_STATIC_DRAW);
  65. // 设置顶点属性指针
  66. glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,sizeof(float)*4,(void*)nullptr);
  67. glEnableVertexAttribArray(0);
  68. glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,sizeof(float)*4,(void*)(sizeof(float)*2));
  69. glEnableVertexAttribArray(1);
  70. // 创建并绑定索引缓冲对象(EBO)
  71. unsigned int EBO;
  72. glGenBuffers(1,&EBO);
  73. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
  74. glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
  75. // 解绑VAO、VBO和EBO
  76. glBindVertexArray(0);
  77. glBindBuffer(GL_ARRAY_BUFFER, 0);
  78. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  79. // 加载纹理
  80. unsigned int texture;
  81. glGenTextures(1, &texture);
  82. glBindTexture(GL_TEXTURE_2D, texture);
  83. // 设置纹理参数
  84. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  85. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  86. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  87. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  88. // 加载图片并生成纹理
  89. int width, height, nrChannels;
  90. stbi_set_flip_vertically_on_load(true); // 翻转加载图片的Y轴,因为OpenGL坐标系的Y轴方向与大部分图片格式相反
  91. unsigned char* data = stbi_load("texture.jpg", &width,&height,&nrChannels, 0);
  92. if (data)
  93. {
  94. glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB,width,height ,0,GL_RGB,GL_UNSIGNED_BYTE,data);
  95. glGenerateMipmap(GL_TEXTURE_2D);
  96. }
  97. else
  98. {
  99. std::cout << "Failed to load texture" << std::endl;
  100. glfwTerminate();
  101. return -1;
  102. }
  103. stbi_image_free(data);
  104. while(!glfwWindowShouldClose(window))
  105. {
  106. // 清除颜色缓冲区
  107. glClear(GL_COLOR_BUFFER_BIT);
  108. // 绑定纹理
  109. glBindTexture(GL_TEXTURE_2D, texture);
  110. // 使用着色器程序、VAO和EBO绘制矩形
  111. glUseProgram(shaderProgram);
  112. glBindVertexArray(VAO);
  113. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  114. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  115. // 交换前后缓冲区
  116. glfwSwapBuffers(window);
  117. // 处理事件
  118. glfwPollEvents();
  119. }
  120. // 清理资源
  121. glDeleteVertexArrays(1, &VAO);
  122. glDeleteBuffers(1,&VBO);
  123. glDeleteBuffers(1,&EBO);
  124. glDeleteTextures(1,&texture);
  125. // 终止GLFW
  126. glfwTerminate();
  127. return 0;
  128. }

这个示例使用了glfw库来创建窗口和OpenGL上下文,glew库来初始化GLEW,stb_image库来加载纹理图像。你需要在代码中替换掉"texture.jpg"为你自己的纹理图像路径。

以上是一个简单的纹理缩放并显示的例子,但是没有包括完整的着色器程序等内容,你可以根据实际需求进行扩展和修改。


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

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?