当前位置:   article > 正文

Qt+OpenGL-part4

Qt+OpenGL-part4

1-5练习_哔哩哔哩_bilibili

添加更多顶点到数据中,使用glDrawElements,尝试绘制两个彼此相连的三角形 

  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.4f, 0.0f, // 左上角
  30. -0.5f, 0.8f, 0.0f, //另一个左上角
  31. };
  32. unsigned int indices[] = {
  33. // 注意索引从0开始!
  34. // 此例的索引(0,1,2,3)就是顶点数组vertices的下标,
  35. // 这样可以由下标代表顶点组合成矩形
  36. 0, 1, 4, // 第一个三角形
  37. 1, 2, 3 // 第二个三角形
  38. };
  39. //顶点数组
  40. glGenVertexArrays(1,&VAO);
  41. //顶点缓冲对象
  42. glGenBuffers(1,&VBO);
  43. glGenBuffers(1,&EBO);
  44. //绑定顶点数组
  45. glBindVertexArray(VAO);
  46. //绑定顶点缓冲对象
  47. glBindBuffer(GL_ARRAY_BUFFER,VBO);
  48. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
  49. //把相关数据复制到缓冲内存中
  50. glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
  51. //第二个参数:数据的大小(字节为单位)
  52. //第三个参数:数据指针
  53. glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
  54. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
  55. glEnableVertexAttribArray(0);
  56. glBindBuffer(GL_ARRAY_BUFFER,0);//解除绑定
  57. glBindVertexArray(0);
  58. unsigned int vertexShader=glCreateShader(GL_VERTEX_SHADER);
  59. glShaderSource(vertexShader,1,&vertexShaderSource,NULL);
  60. glCompileShader(vertexShader);
  61. int success;
  62. char infoLog[512];
  63. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  64. if(!success)
  65. {
  66. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  67. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  68. }
  69. unsigned int fragmentShader=glCreateShader(GL_FRAGMENT_SHADER);
  70. glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
  71. glCompileShader(fragmentShader);
  72. shaderProgram=glCreateProgram();
  73. glAttachShader(shaderProgram,vertexShader);
  74. glAttachShader(shaderProgram,fragmentShader);
  75. glLinkProgram(shaderProgram);
  76. glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  77. if(!success) {
  78. glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  79. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  80. }
  81. glDeleteShader(vertexShader);
  82. glDeleteShader(fragmentShader);
  83. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//使用线条填充
  84. }
  85. void OpenGLWidget::resizeGL(int w, int h)
  86. {
  87. }
  88. void OpenGLWidget::paintGL()
  89. {
  90. glClearColor(0.2f,0.3f,0.3f,1.0f);
  91. glClear(GL_COLOR_BUFFER_BIT);
  92. glUseProgram(shaderProgram);
  93. glBindVertexArray(VAO);
  94. //VAO知道VBO和EBO的情况
  95. glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
  96. }

添加更多顶点到数据中,使用glDrawArrays,尝试绘制两个彼此相连的三角形 

  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.8f, 0.0f,
  29. -0.5f, 0.5f, 0.0f,
  30. 0.3f, -0.5f, 0.0f,
  31. -0.5f, 0.8f, 0.0f,
  32. };
  33. //顶点数组
  34. glGenVertexArrays(1,&VAO);
  35. //顶点缓冲对象
  36. glGenBuffers(1,&VBO);
  37. glGenBuffers(1,&EBO);
  38. //绑定顶点数组
  39. glBindVertexArray(VAO);
  40. //绑定顶点缓冲对象
  41. glBindBuffer(GL_ARRAY_BUFFER,VBO);
  42. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,EBO);
  43. //把相关数据复制到缓冲内存中
  44. glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);
  45. //第二个参数:数据的大小(字节为单位)
  46. //第三个参数:数据指针
  47. glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(indices),indices,GL_STATIC_DRAW);
  48. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
  49. glEnableVertexAttribArray(0);
  50. glBindBuffer(GL_ARRAY_BUFFER,0);//解除绑定
  51. glBindVertexArray(0);
  52. unsigned int vertexShader=glCreateShader(GL_VERTEX_SHADER);
  53. glShaderSource(vertexShader,1,&vertexShaderSource,NULL);
  54. glCompileShader(vertexShader);
  55. int success;
  56. char infoLog[512];
  57. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  58. if(!success)
  59. {
  60. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  61. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  62. }
  63. unsigned int fragmentShader=glCreateShader(GL_FRAGMENT_SHADER);
  64. glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
  65. glCompileShader(fragmentShader);
  66. shaderProgram=glCreateProgram();
  67. glAttachShader(shaderProgram,vertexShader);
  68. glAttachShader(shaderProgram,fragmentShader);
  69. glLinkProgram(shaderProgram);
  70. glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  71. if(!success) {
  72. glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  73. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  74. }
  75. glDeleteShader(vertexShader);
  76. glDeleteShader(fragmentShader);
  77. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//使用线条填充
  78. }
  79. void OpenGLWidget::resizeGL(int w, int h)
  80. {
  81. }
  82. void OpenGLWidget::paintGL()
  83. {
  84. glClearColor(0.2f,0.3f,0.3f,1.0f);
  85. glClear(GL_COLOR_BUFFER_BIT);
  86. glUseProgram(shaderProgram);
  87. glBindVertexArray(VAO);
  88. //VAO知道VBO和EBO的情况
  89. glDrawArrays(GL_TRIANGLES,0,6);
  90. }

创建两个三角形,对它们的数据使用不同的VAO和VBO 

  1. #include "openglwidget.h"
  2. #include <QDebug>
  3. unsigned int VBOs[2],VAOs[2];
  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 firstTriangle[]={
  26. -0.9f,-0.5f,0.0f,
  27. -0.0f,-0.5f,0.0f,
  28. -0.45f,0.5f,0.0f
  29. };
  30. float secondTriangle[]={
  31. -0.0f,-0.5f,0.0f,
  32. 0.9f,-0.5f,0.0f,
  33. 0.45f,0.5f,0.0f
  34. };
  35. unsigned int indices[] = {
  36. // 注意索引从0开始!
  37. // 此例的索引(0,1,2,3)就是顶点数组vertices的下标,
  38. // 这样可以由下标代表顶点组合成矩形
  39. 0, 1, 4, // 第一个三角形
  40. 1, 2, 3 // 第二个三角形
  41. };
  42. //顶点数组
  43. glGenVertexArrays(2,VAOs);
  44. //顶点缓冲对象
  45. glGenBuffers(2,VBOs);
  46. //绑定顶点数组
  47. glBindVertexArray(VAOs[0]);
  48. //绑定顶点缓冲对象
  49. glBindBuffer(GL_ARRAY_BUFFER,VBOs[0]);
  50. //把相关数据复制到缓冲内存中
  51. glBufferData(GL_ARRAY_BUFFER,sizeof(firstTriangle),firstTriangle,GL_STATIC_DRAW);
  52. //第二个参数:数据的大小(字节为单位)
  53. //第三个参数:数据指针
  54. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
  55. glEnableVertexAttribArray(0);
  56. //绑定顶点数组
  57. glBindVertexArray(VAOs[1]);
  58. //绑定顶点缓冲对象
  59. glBindBuffer(GL_ARRAY_BUFFER,VBOs[1]);
  60. //把相关数据复制到缓冲内存中
  61. glBufferData(GL_ARRAY_BUFFER,sizeof(secondTriangle),secondTriangle,GL_STATIC_DRAW);
  62. //第二个参数:数据的大小(字节为单位)
  63. //第三个参数:数据指针
  64. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
  65. glEnableVertexAttribArray(0);
  66. glBindBuffer(GL_ARRAY_BUFFER,0);//解除绑定
  67. glBindVertexArray(0);
  68. unsigned int vertexShader=glCreateShader(GL_VERTEX_SHADER);
  69. glShaderSource(vertexShader,1,&vertexShaderSource,NULL);
  70. glCompileShader(vertexShader);
  71. int success;
  72. char infoLog[512];
  73. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  74. if(!success)
  75. {
  76. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  77. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  78. }
  79. unsigned int fragmentShader=glCreateShader(GL_FRAGMENT_SHADER);
  80. glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
  81. glCompileShader(fragmentShader);
  82. shaderProgram=glCreateProgram();
  83. glAttachShader(shaderProgram,vertexShader);
  84. glAttachShader(shaderProgram,fragmentShader);
  85. glLinkProgram(shaderProgram);
  86. glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  87. if(!success) {
  88. glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  89. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  90. }
  91. glDeleteShader(vertexShader);
  92. glDeleteShader(fragmentShader);
  93. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//使用线条填充
  94. }
  95. void OpenGLWidget::resizeGL(int w, int h)
  96. {
  97. }
  98. void OpenGLWidget::paintGL()
  99. {
  100. glClearColor(0.2f,0.3f,0.3f,1.0f);
  101. glClear(GL_COLOR_BUFFER_BIT);
  102. glUseProgram(shaderProgram);
  103. glBindVertexArray(VAOs[0]);
  104. glDrawArrays(GL_TRIANGLES,0,3);
  105. glBindVertexArray(VAOs[1]);
  106. glDrawArrays(GL_TRIANGLES,0,3);
  107. }

创建两个着色器程序,第二个程序使用一个不同的片段着色器。

  1. #include "openglwidget.h"
  2. #include <QDebug>
  3. unsigned int VBOs[2],VAOs[2];
  4. unsigned int shaderProgram,shaderProgram2;
  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. const char* fragmentShader2Source="#version 330 core\n"
  20. "out vec4 FragColor;\n"
  21. "void main()\n"
  22. "{\n"
  23. "FragColor=vec4(0.8f,0.8f,1.0f,0.2f);\n"
  24. "}\0";
  25. OpenGLWidget::OpenGLWidget(QWidget *parent) : QOpenGLWidget(parent)
  26. {
  27. }
  28. void OpenGLWidget::initializeGL()
  29. {
  30. initializeOpenGLFunctions();
  31. float firstTriangle[]={
  32. -0.9f,-0.5f,0.0f,
  33. -0.0f,-0.5f,0.0f,
  34. -0.45f,0.5f,0.0f
  35. };
  36. float secondTriangle[]={
  37. -0.0f,-0.5f,0.0f,
  38. 0.9f,-0.5f,0.0f,
  39. 0.45f,0.5f,0.0f
  40. };
  41. //顶点数组
  42. glGenVertexArrays(2,VAOs);
  43. //顶点缓冲对象
  44. glGenBuffers(2,VBOs);
  45. //绑定顶点数组
  46. glBindVertexArray(VAOs[0]);
  47. //绑定顶点缓冲对象
  48. glBindBuffer(GL_ARRAY_BUFFER,VBOs[0]);
  49. //把相关数据复制到缓冲内存中
  50. glBufferData(GL_ARRAY_BUFFER,sizeof(firstTriangle),firstTriangle,GL_STATIC_DRAW);
  51. //第二个参数:数据的大小(字节为单位)
  52. //第三个参数:数据指针
  53. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
  54. glEnableVertexAttribArray(0);
  55. //绑定顶点数组
  56. glBindVertexArray(VAOs[1]);
  57. //绑定顶点缓冲对象
  58. glBindBuffer(GL_ARRAY_BUFFER,VBOs[1]);
  59. //把相关数据复制到缓冲内存中
  60. glBufferData(GL_ARRAY_BUFFER,sizeof(secondTriangle),secondTriangle,GL_STATIC_DRAW);
  61. //第二个参数:数据的大小(字节为单位)
  62. //第三个参数:数据指针
  63. glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3*sizeof(float),(void *)0);
  64. glEnableVertexAttribArray(0);
  65. glBindBuffer(GL_ARRAY_BUFFER,0);//解除绑定
  66. glBindVertexArray(0);
  67. unsigned int vertexShader=glCreateShader(GL_VERTEX_SHADER);
  68. glShaderSource(vertexShader,1,&vertexShaderSource,NULL);
  69. glCompileShader(vertexShader);
  70. int success;
  71. char infoLog[512];
  72. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
  73. if(!success)
  74. {
  75. glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
  76. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  77. }
  78. unsigned int fragmentShader=glCreateShader(GL_FRAGMENT_SHADER);
  79. glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
  80. glCompileShader(fragmentShader);
  81. unsigned int fragmentShader2=glCreateShader(GL_FRAGMENT_SHADER);
  82. glShaderSource(fragmentShader2,1,&fragmentShader2Source,NULL);
  83. glCompileShader(fragmentShader2);
  84. shaderProgram=glCreateProgram();
  85. glAttachShader(shaderProgram,vertexShader);
  86. glAttachShader(shaderProgram,fragmentShader);
  87. glLinkProgram(shaderProgram);
  88. glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  89. if(!success) {
  90. glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
  91. qDebug() << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog;
  92. }
  93. shaderProgram2=glCreateProgram();
  94. glAttachShader(shaderProgram2,vertexShader);
  95. glAttachShader(shaderProgram2,fragmentShader2);
  96. glLinkProgram(shaderProgram2);
  97. glDeleteShader(vertexShader);
  98. glDeleteShader(fragmentShader);
  99. glDeleteShader(fragmentShader2);
  100. glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);//使用线条填充
  101. }
  102. void OpenGLWidget::resizeGL(int w, int h)
  103. {
  104. }
  105. void OpenGLWidget::paintGL()
  106. {
  107. glClearColor(0.2f,0.3f,0.3f,1.0f);
  108. glClear(GL_COLOR_BUFFER_BIT);
  109. glUseProgram(shaderProgram);
  110. glBindVertexArray(VAOs[0]);
  111. glDrawArrays(GL_TRIANGLES,0,3);
  112. glUseProgram(shaderProgram2);
  113. glBindVertexArray(VAOs[1]);
  114. glDrawArrays(GL_TRIANGLES,0,3);
  115. }

 

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

闽ICP备14008679号