1、主要 模仿代码:OpenGL的教程 第3课 "tutorial03_matrices"的代码(E:\OpenGL_something\ogl-master)

2、参考代码:Qt5中的例子源码:

  (1)C:\Qt\Qt5.3.2\Examples\Qt-5.3\opengl            (E:\Project_Qt532\Official_Examples)

  (2)C:\Qt\Qt5.3.2\Examples\Qt-5.3\opengl\cube

  (3)C:\Qt\Qt5.5.1\Examples\Qt-5.5\opengl            (E:\Project_Qt551\Official_Examples)

  (4)C:\Qt\Qt5.5.1\Examples\Qt-5.5\opengl\hellogl2

3、代码:

 (1)main.cpp

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3.  
  4. #include "test01.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. QApplication a(argc, argv);
  9. // MainWindow w;
  10. test01 w;
  11. w.show();
  12.  
  13. return a.exec();
  14. }

 (2)test01.h

  1. #ifndef __TEST_01_H__
  2. #define __TEST_01_H__
  3.  
  4. #include <iostream>
  5. #include <string>
  6. using namespace std;
  7.  
  8. //openGL窗口相关文件
  9. #include <QOpenGLWidget>
  10. #include <QOpenGLFunctions_3_3_Core>
  11.  
  12. #include <QMatrix4x4>
  13.  
  14. class test01 :public QOpenGLWidget, public QOpenGLFunctions_3_3_Core
  15. {
  16. Q_OBJECT
  17. public:
  18. explicit test01(QWidget *parent = );
  19. ~test01();
  20.  
  21. public:
  22. virtual void initializeGL();
  23. virtual void resizeGL(int w, int h);// ZC: 不处理窗口大小变化
  24. virtual void paintGL();
  25.  
  26. private:
  27. unsigned int FuiProgramID;
  28. GLuint Fvbo;
  29.  
  30. public:
  31. GLuint FuiMVP;
  32. QMatrix4x4 Fmodel;
  33. QMatrix4x4 Fview;
  34. QMatrix4x4 Fprojection;
  35. QMatrix4x4 Fmvp;
  36.  
  37. public:
  38. int ShaderLoad_Z(
  39. const QString &_strFullFileName_ShaderVertex,
  40. const QString &_strFullFileName_ShaderFregment);
  41. bool CheckCompileErrors(unsigned int _shader, const std::string &_strType);
  42. };
  43.  
  44. #endif // __TEST_01_H__

 (3)test01.cpp

  ZC:注意 资源文件的路径问题:":/test01/shaders/test01/ShaderVertex.glsl",前一部分"/test01"是前缀,后一部分"shaders/test01/ShaderVertex.glsl"是文件所在地址,两部分用"/"相连,最后再在最前面加上冒号":"

  1. #include "test01.h"
  2.  
  3. #include <QFile>
  4. #include <QTextStream>
  5.  
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. test01::test01(QWidget *parent): QOpenGLWidget(parent)
  10. {
  11. //设置OpenGL的版本信息
  12. QSurfaceFormat format;
  13. format.setRenderableType(QSurfaceFormat::OpenGL);
  14. format.setProfile(QSurfaceFormat::CoreProfile);
  15. format.setVersion(,);
  16. // format.setVersion(2,0);
  17. setFormat(format);
  18.  
  19. // *** *** ***
  20. FuiProgramID = ;
  21. }
  22.  
  23. test01::~test01()
  24. {}
  25.  
  26. void test01::initializeGL()
  27. {
  28. // Initialize OpenGL Backend
  29. initializeOpenGLFunctions();
  30.  
  31. // ZC: 刷背景色
  32. // Set global information
  33. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  34.  
  35. // ZC: 在类test01的构造函数中,如果指明使用3.x版本的OpenGL的话,下面3句代码是必须的(至少Qt中是这样的,C++的使用glfw时 我暂时不知道 如何指定使用哪个版本的OpenGL)
  36. unsigned int VAO;
  37. glGenVertexArrays(, &VAO);
  38. glBindVertexArray(VAO);
  39.  
  40. // 创建着色器对象
  41. ShaderLoad_Z(":/test01/shaders/test01/ShaderVertex.glsl", ":/test01/shaders/test01/ShaderFragment.glsl");
  42.  
  43. // *** *** ***
  44.  
  45. FuiMVP = glGetUniformLocation(FuiProgramID, "MVP");
  46.  
  47. // ZC: MVP设置:(3步骤)
  48. {
  49. // ZC: (1)MVP中的 P
  50. // ZC: 投射矩阵的设置在 resizeGL(...)中
  51. // ZC: (2)MVP中的 V
  52. Fview.lookAt(
  53. QVector3D(, , ),// Camera is at (4,3,3), in World Space
  54. QVector3D(, , ), // and looks at the origin
  55. QVector3D(, , ) // Head is up (set to 0,-1,0 to look upside-down)
  56. );
  57. // ZC: (3)MVP中的 M
  58. Fmodel.setToIdentity();
  59. // model.translate();
  60. // model.rotate();
  61. }
  62.  
  63. // *** *** ***
  64.  
  65. // static const GLfloat g_vertex_buffer_data[] = {
  66. // -0.8f, -0.8f, -10.0f,
  67. // 0.8f, -0.8f, -10.0f,
  68. // 0.0f, 0.8f, -10.0f,
  69. // };
  70. static const GLfloat g_vertex_buffer_data[] = {
  71. -1.0f, -1.0f, 0.0f,
  72. 1.0f, -1.0f, 0.0f,
  73. 0.0f, 1.0f, 0.0f,
  74. };
  75.  
  76. glGenBuffers(, &Fvbo);
  77. glBindBuffer(GL_ARRAY_BUFFER, Fvbo);
  78. glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  79.  
  80. // ***
  81. glEnable(GL_DEPTH_TEST);
  82. glPointSize();
  83. // cout << "test01::initializeGL() out" << endl;
  84. }
  85.  
  86. void test01::resizeGL(int w, int h)
  87. {
  88. qDebug() << "MainWidget::resizeGL(...)";
  89.  
  90. // Set OpenGL viewport to cover whole widget
  91. glViewport(, , w, h);
  92.  
  93. // ZC: 下面是 投射矩阵相关
  94. {
  95. // Calculate aspect ratio
  96. qreal aspect = qreal(w) / qreal(h ? h : );
  97.  
  98. // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
  99. // const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
  100. const qreal zNear = 0.1, zFar = 100.0, fov = 45.0;
  101.  
  102. // Reset projection
  103. Fprojection.setToIdentity();
  104.  
  105. // Set perspective projection
  106. Fprojection.perspective(fov, aspect, zNear, zFar);
  107. }
  108.  
  109. Fmvp = Fprojection * Fview * Fmodel;
  110. }
  111.  
  112. void test01::paintGL()
  113. {
  114. // cout << "test01::paintGL()" << endl;
  115.  
  116. //每次绘图前清理屏幕,否则会有残影
  117. //glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  118. glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
  119. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  120.  
  121. // ***
  122. glUseProgram( FuiProgramID );
  123.  
  124. glUniformMatrix4fv(FuiMVP, , GL_FALSE, Fmvp.data());
  125.  
  126. // ZC: 这下面的参数"0"应该是对应顶点着色器的layout的location序号(我记得在哪里看到有函数可以通过变量名取到这个序号的)
  127. glEnableVertexAttribArray();
  128. glBindBuffer(GL_ARRAY_BUFFER, Fvbo);
  129. glVertexAttribPointer(
  130. , // attribute. No particular reason for 0, but must match the layout in the shader.
  131. , // size
  132. GL_FLOAT, // type
  133. GL_FALSE, // normalized?
  134. , // stride
  135. (void*) // array buffer offset
  136. );
  137. glDrawArrays(GL_TRIANGLES, , ); // 3 indices starting at 0 -> 1 triangle
  138. glDisableVertexAttribArray();
  139.  
  140. }
  141.  
  142. int test01::ShaderLoad_Z(const QString &_strFullFileName_ShaderVertex, const QString &_strFullFileName_ShaderFregment)
  143. {
  144. // ZC: 着色器文件-->着色器字符串
  145. QFile file1( _strFullFileName_ShaderVertex );
  146. file1.open(QIODevice::ReadOnly);
  147. QTextStream in1(&file1);
  148. // 将文本流读取到字符串中:
  149. string strShaderVertex = in1.readAll().toStdString();
  150. file1.close();
  151.  
  152. QFile file2( _strFullFileName_ShaderFregment );
  153. file2.open(QIODevice::ReadOnly);
  154. QTextStream in2(&file2);
  155. // 将文本流读取到字符串中:
  156. string strShaderFregment = in2.readAll().toStdString();
  157. file2.close();
  158.  
  159. // cout << "strShaderVertex : " << strShaderVertex << endl;
  160. // cout << "strShaderFregment : " << strShaderFregment << endl;
  161.  
  162. // *** *** *** ***
  163.  
  164. const char* pcShaderVertex = strShaderVertex.c_str();
  165. const char* pcShaderFregment= strShaderFregment.c_str();
  166.  
  167. // 编辑编译着色器
  168. unsigned int shaderVertex;// vertex, fragment;
  169. unsigned int shaderFregment;
  170.  
  171. // 顶点着色器
  172. shaderVertex = glCreateShader(GL_VERTEX_SHADER);
  173. glShaderSource(shaderVertex, , &pcShaderVertex, NULL);
  174. glCompileShader(shaderVertex);
  175.  
  176. // 检查着色器编译错误
  177. CheckCompileErrors(shaderVertex, "VERTEX");
  178.  
  179. // ***
  180.  
  181. // 片段着色器
  182. shaderFregment = glCreateShader(GL_FRAGMENT_SHADER);
  183. glShaderSource(shaderFregment, , &pcShaderFregment, NULL);
  184. glCompileShader(shaderFregment);
  185.  
  186. // 检查着色器编译错误
  187. CheckCompileErrors(shaderFregment, "FRAGMENT");
  188.  
  189. // ***
  190.  
  191. // 链接着色器程序
  192. FuiProgramID = glCreateProgram();
  193. glAttachShader(FuiProgramID, shaderVertex);
  194. glAttachShader(FuiProgramID, shaderFregment);
  195. glLinkProgram(FuiProgramID);
  196.  
  197. // 检查链接错误
  198. CheckCompileErrors(FuiProgramID, "PROGRAM");
  199.  
  200. glDetachShader(FuiProgramID, shaderVertex);
  201. glDetachShader(FuiProgramID, shaderFregment);
  202.  
  203. // 删除着色器
  204. glDeleteShader(shaderVertex);
  205. glDeleteShader(shaderFregment);
  206.  
  207. return ;//uiProgramID;
  208. }
  209.  
  210. bool test01::CheckCompileErrors(unsigned int _shader, const string& _strType)
  211. {
  212. int success;
  213. char infoLog[];
  214. if (_strType != "PROGRAM"){
  215. glGetShaderiv(_shader, GL_COMPILE_STATUS, &success);
  216. if (!success){
  217. glGetShaderInfoLog(_shader, , NULL, infoLog);
  218. cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << _strType << "---" << infoLog << endl;
  219. return false;
  220. }
  221. }else{
  222. glGetProgramiv(_shader, GL_LINK_STATUS, &success);
  223. if (!success){
  224. glGetProgramInfoLog(_shader, , NULL, infoLog);
  225. cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << _strType << "---" << infoLog << endl;
  226. return false;
  227. }
  228. }
  229. return true;
  230. }

4、

  

5、上面代码中的 关于 "glEnableVertexAttribArray(0); glDisableVertexAttribArray(0);" 的我的注释,

 5.1、现在 找到貌似是 在 "C:\Qt\Qt5.3.2\Examples\Qt-5.3\opengl\cube"里面的,暂时不知道 是不是准确

  drawCubeGeometry() --> int vertexLocation = program->attributeLocation("a_position");  program->enableAttributeArray(vertexLocation);  --> 看源码是 调用的 glGetAttribLocation(...) 和 glEnableVertexAttribArray(...)

  ZC:这个 貌似是 适用于 版本"version 330 core"之前的GLSL(可能是 2.x的版本)。

  ZC:个人理解:版本"version 330 core"之前的GLSL 使用 "attribute vec4 a_position;"的方式 传递顶点数组数据;3.3之后的 使用类似 "layout(location = 0) in vec3 vertexPosition_modelspace;" 的方式 传递 顶点数组数据

6、

7、

8、

9、

10、

Qt551.OpenGL.ZC简单例子的更多相关文章

  1. Hibernate4.2.4入门(一)——环境搭建和简单例子

    一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...

  2. AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答

    一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...

  3. spring mvc(注解)上传文件的简单例子

    spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...

  4. ko 简单例子

    Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...

  5. mysql定时任务简单例子

    mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9     如果要每30秒执行以下语句:   [sql] update userinfo set endtime = now() WHE ...

  6. java socket编程开发简单例子 与 nio非阻塞通道

    基本socket编程 1.以下只是简单例子,没有用多线程处理,只能一发一收(由于scan.nextLine()线程会进入等待状态),使用时可以根据具体项目功能进行优化处理 2.以下代码使用了1.8新特 ...

  7. 一个简单例子:贫血模型or领域模型

    转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...

  8. [转] 3个学习Socket编程的简单例子:TCP Server/Client, Select

    以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计 ...

  9. jsonp的简单例子

    jsonp的简单例子 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...

随机推荐

  1. 使用genism训练词向量【转载】

    转自:https://blog.csdn.net/qq_16912257/article/details/79099581 https://blog.csdn.net/thriving_fcl/art ...

  2. GAITC 2019全球人工智能技术大会(南京)

    2019年5月25日至26日,由中国人工智能学会主办,以“交叉.融合.相生.共赢”为主题的2019GAITC将在南京全新亮相. 2019 全球人工智能技术大会(2019 GAITC)以“前端引领.深度 ...

  3. Complex类的实现

    #include<iostream> #include<cmath> using namespace std; class complex{ public: complex() ...

  4. Chrome Inspect调试微信出现空白页面的解决方法

    首先,需要打开手机的USB调试和微信的TBS 调试开关. 如果不打开TBS开关,Inspect时会检测不到任何微信的H5页面 使用微信扫码下方二维码,打开TBS调试开关: 普通网页: 小程序: 微信扫 ...

  5. 《linux就该这么学》第十六节课:第16,17章,Squid服务和iscsi网络存储

    第十六章 squid总结: 正向代理:yum  安装后清空防火墙即可正常使用,客户端设置浏览器 透明正向代理:vim  /etc/squid/squid.conf 59行:http_port  312 ...

  6. jenkins+Gitlab+maven+tomcat实现自动集成、打包、部署

    一.前言 首先出于提高自己技术水平和琢磨能做点什么能提高工作效率,上线工作实在无聊.重复(手动编译.打包,传包,重启),于是就想到了jenkins,jenkins持续集成已经不是什么新鲜的话题,网上文 ...

  7. MVC 使用缓存

    public AController() { ViewBag[); } private List<BlogsClass> GetClass(int parentId) { List< ...

  8. sql中批量插入begin的使用

    private static String ADD_ATTR_EXT_ITEM="insert into attr_ext_item(attr_ext_main_key,attr_name_ ...

  9. document.wrtie()用法

    推荐看这篇文章,非常的好 http://www.softwhy.com/article-8326-1.html

  10. spring源码解析2--容器的基本实现

    spring的主要特性是IOC,实现IOC的关键是bean,而更关键的是如何bean的管理容器,也就是BeanFactory,本文的目标是弄清楚BeanFactory具体是怎么样的存在. 先看下最简单 ...