当前位置:   article > 正文

Qt+OpenGL-part3

Qt+OpenGL-part3

1-4EBO画矩形_哔哩哔哩_bilibili

可以绘制两个三角形来组成一个矩形(OpenGL主要处理三角形)

直接画两个三角形:

  1. #include "openglwidget.h"
  2. #include <QDebug>
  3. unsigned int VBO,VAO;
  4. unsigned int shaderProgram;
  5. //顶点着色器
  6. const char* vertexShaderSource="#version 330 core\n"
  7. "layout(location=0) in vec3 aPos;\n"
  8. "void main()\n"
  9. "{\n"
  10. "gl_Position=vec4(aPos.x,aPos.y,aPos.z,1.0);\n"
  11. "}\0";
  12. //片段着色器
  13. const char* fragmentShaderSource="#version 330 core\n"
  14. "out vec4 FragColor;\n"
  15. "void main()\n"
  16. "{\n"
  17. "FragColor=vec4(0.3f,0.5f,0.2f,0.2f);\n"
  18. "}\0";
  19. OpenGLWidget::OpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
  20. {
  21. }
  22. void OpenGLWidget::initializeGL()
  23. {
  24. initializeOpenGLFunctions();
  25. float vertices[]={
  26. 0.5f, 0.5f, 0.0f, // 右上角
  27. 0.5f, -0.5f, 0.0f, // 右下角
  28. -0.5f, 0.5f, 0.0f, // 左上角
  29. // 第二个三角形
  30. 0.5f, -0.5f, 0.0f, // 右下角
  31. -0.5f, -0.5f, 0.0f, // 左下角
  32. -0.5f, 0.5f, 0.0f // 左上角
  33. };
  34. //顶点数组
  35. glGenVertexArrays(1,&VAO);
  36. //顶点缓冲对象
  37. glGenBuffers(1,&VBO);
  38. //绑定顶点数组
  39. glBindVertexArray(VAO);
  40. //绑定顶点缓冲对象
  41. glBindBuffer(GL_ARRAY_BUFFER,VBO);
  42. //把三角形相关数据复制到缓冲内存中
  43. glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
  44. //第二个参数:数据的大小(字节为单位)
  45. //第三个参数:数据指针
  46. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
  47. glEnableVertexAttribArray(0);
  48. glBindBuffer(GL_ARRAY_BUFFER,0);
  49. glBindVertexArray(0);
  50. unsigned int vertexShader=glCreateShader(GL_VERTEX_SHADER);
  51. glShaderSource(vertexShader,1,&vertexShaderSource,NULL);
  52. glCompileShader(vertexShader);
  53. int success;
  54. char infoLog[512];
  55. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  56. if(!success)
  57. {
  58. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  59. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  60. }
  61. unsigned int fragmentShader=glCreateShader(GL_FRAGMENT_SHADER);
  62. glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
  63. glCompileShader(fragmentShader);
  64. shaderProgram=glCreateProgram();
  65. glAttachShader(shaderProgram,vertexShader);
  66. glAttachShader(shaderProgram,fragmentShader);
  67. glLinkProgram(shaderProgram);
  68. glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  69. if(!success) {
  70. glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  71. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  72. }
  73. glDeleteShader(vertexShader);
  74. glDeleteShader(fragmentShader);
  75. }
  76. void OpenGLWidget::resizeGL(int w, int h)
  77. {
  78. }
  79. void OpenGLWidget::paintGL()
  80. {
  81. glClearColor(0.2f,0.3f,0.3f,1.0f);
  82. glClear(GL_COLOR_BUFFER_BIT);
  83. glUseProgram(shaderProgram);
  84. glBindVertexArray(VAO);
  85. glDrawArrays(GL_TRIANGLES,0,6);
  86. }

使用线条填充:

void OpenGLWidget::initializeGL()

{

glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//使用线条填充

}

使用VBO,EBO,VAO来画两个三角形

  1. #include "openglwidget.h"
  2. #include <QDebug>
  3. unsigned int VBO,VAO,EBO;
  4. unsigned int shaderProgram;
  5. //顶点着色器
  6. const char* vertexShaderSource="#version 330 core\n"
  7. "layout(location=0) in vec3 aPos;\n"
  8. "void main()\n"
  9. "{\n"
  10. "gl_Position=vec4(aPos.x,aPos.y,aPos.z,1.0);\n"
  11. "}\0";
  12. //片段着色器
  13. const char* fragmentShaderSource="#version 330 core\n"
  14. "out vec4 FragColor;\n"
  15. "void main()\n"
  16. "{\n"
  17. "FragColor=vec4(0.3f,0.5f,0.2f,0.2f);\n"
  18. "}\0";
  19. OpenGLWidget::OpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
  20. {
  21. }
  22. void OpenGLWidget::initializeGL()
  23. {
  24. initializeOpenGLFunctions();
  25. float vertices[] = {
  26. 0.5f, 0.5f, 0.0f, // 右上角
  27. 0.5f, -0.5f, 0.0f, // 右下角
  28. -0.5f, -0.5f, 0.0f, // 左下角
  29. -0.5f, 0.5f, 0.0f // 左上角
  30. };
  31. unsigned int indices[] = {
  32. // 注意索引从0开始!
  33. // 此例的索引(0,1,2,3)就是顶点数组vertices的下标,
  34. // 这样可以由下标代表顶点组合成矩形
  35. 0, 1, 3, // 第一个三角形
  36. 1, 2, 3 // 第二个三角形
  37. };
  38. //顶点数组
  39. glGenVertexArrays(1,&VAO);
  40. //顶点缓冲对象
  41. glGenBuffers(1,&VBO);
  42. glGenBuffers(1,&EBO);
  43. //绑定顶点数组
  44. glBindVertexArray(VAO);
  45. //绑定顶点缓冲对象
  46. glBindBuffer(GL_ARRAY_BUFFER,VBO);
  47. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
  48. //把相关数据复制到缓冲内存中
  49. glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
  50. //第二个参数:数据的大小(字节为单位)
  51. //第三个参数:数据指针
  52. glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
  53. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
  54. glEnableVertexAttribArray(0);
  55. glBindBuffer(GL_ARRAY_BUFFER,0);
  56. glBindVertexArray(0);
  57. unsigned int vertexShader=glCreateShader(GL_VERTEX_SHADER);
  58. glShaderSource(vertexShader,1,&vertexShaderSource,NULL);
  59. glCompileShader(vertexShader);
  60. int success;
  61. char infoLog[512];
  62. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  63. if(!success)
  64. {
  65. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  66. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  67. }
  68. unsigned int fragmentShader=glCreateShader(GL_FRAGMENT_SHADER);
  69. glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
  70. glCompileShader(fragmentShader);
  71. shaderProgram=glCreateProgram();
  72. glAttachShader(shaderProgram,vertexShader);
  73. glAttachShader(shaderProgram,fragmentShader);
  74. glLinkProgram(shaderProgram);
  75. glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  76. if(!success) {
  77. glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  78. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  79. }
  80. glDeleteShader(vertexShader);
  81. glDeleteShader(fragmentShader);
  82. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//使用线条填充
  83. }
  84. void OpenGLWidget::resizeGL(int w, int h)
  85. {
  86. }
  87. void OpenGLWidget::paintGL()
  88. {
  89. glClearColor(0.2f,0.3f,0.3f,1.0f);
  90. glClear(GL_COLOR_BUFFER_BIT);
  91. glUseProgram(shaderProgram);
  92. glBindVertexArray(VAO);
  93. //VAO知道VBO和EBO的情况
  94. glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
  95. }

VAO会记录EBO的绑定和解绑,不会记录VBO的绑定和解绑。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/413226
推荐阅读
相关标签
  

闽ICP备14008679号