Qt551.OpenGL.ZC简单例子
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
- #include "mainwindow.h"
- #include <QApplication>
- #include "test01.h"
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- // MainWindow w;
- test01 w;
- w.show();
- return a.exec();
- }
(2)test01.h
- #ifndef __TEST_01_H__
- #define __TEST_01_H__
- #include <iostream>
- #include <string>
- using namespace std;
- //openGL窗口相关文件
- #include <QOpenGLWidget>
- #include <QOpenGLFunctions_3_3_Core>
- #include <QMatrix4x4>
- class test01 :public QOpenGLWidget, public QOpenGLFunctions_3_3_Core
- {
- Q_OBJECT
- public:
- explicit test01(QWidget *parent = );
- ~test01();
- public:
- virtual void initializeGL();
- virtual void resizeGL(int w, int h);// ZC: 不处理窗口大小变化
- virtual void paintGL();
- private:
- unsigned int FuiProgramID;
- GLuint Fvbo;
- public:
- GLuint FuiMVP;
- QMatrix4x4 Fmodel;
- QMatrix4x4 Fview;
- QMatrix4x4 Fprojection;
- QMatrix4x4 Fmvp;
- public:
- int ShaderLoad_Z(
- const QString &_strFullFileName_ShaderVertex,
- const QString &_strFullFileName_ShaderFregment);
- bool CheckCompileErrors(unsigned int _shader, const std::string &_strType);
- };
- #endif // __TEST_01_H__
(3)test01.cpp
ZC:注意 资源文件的路径问题:":/test01/shaders/test01/ShaderVertex.glsl",前一部分"/test01"是前缀,后一部分"shaders/test01/ShaderVertex.glsl"是文件所在地址,两部分用"/"相连,最后再在最前面加上冒号":"
- #include "test01.h"
- #include <QFile>
- #include <QTextStream>
- #include <iostream>
- using namespace std;
- test01::test01(QWidget *parent): QOpenGLWidget(parent)
- {
- //设置OpenGL的版本信息
- QSurfaceFormat format;
- format.setRenderableType(QSurfaceFormat::OpenGL);
- format.setProfile(QSurfaceFormat::CoreProfile);
- format.setVersion(,);
- // format.setVersion(2,0);
- setFormat(format);
- // *** *** ***
- FuiProgramID = ;
- }
- test01::~test01()
- {}
- void test01::initializeGL()
- {
- // Initialize OpenGL Backend
- initializeOpenGLFunctions();
- // ZC: 刷背景色
- // Set global information
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- // ZC: 在类test01的构造函数中,如果指明使用3.x版本的OpenGL的话,下面3句代码是必须的(至少Qt中是这样的,C++的使用glfw时 我暂时不知道 如何指定使用哪个版本的OpenGL)
- unsigned int VAO;
- glGenVertexArrays(, &VAO);
- glBindVertexArray(VAO);
- // 创建着色器对象
- ShaderLoad_Z(":/test01/shaders/test01/ShaderVertex.glsl", ":/test01/shaders/test01/ShaderFragment.glsl");
- // *** *** ***
- FuiMVP = glGetUniformLocation(FuiProgramID, "MVP");
- // ZC: MVP设置:(3步骤)
- {
- // ZC: (1)MVP中的 P
- // ZC: 投射矩阵的设置在 resizeGL(...)中
- // ZC: (2)MVP中的 V
- Fview.lookAt(
- QVector3D(, , ),// Camera is at (4,3,3), in World Space
- QVector3D(, , ), // and looks at the origin
- QVector3D(, , ) // Head is up (set to 0,-1,0 to look upside-down)
- );
- // ZC: (3)MVP中的 M
- Fmodel.setToIdentity();
- // model.translate();
- // model.rotate();
- }
- // *** *** ***
- // static const GLfloat g_vertex_buffer_data[] = {
- // -0.8f, -0.8f, -10.0f,
- // 0.8f, -0.8f, -10.0f,
- // 0.0f, 0.8f, -10.0f,
- // };
- static const GLfloat g_vertex_buffer_data[] = {
- -1.0f, -1.0f, 0.0f,
- 1.0f, -1.0f, 0.0f,
- 0.0f, 1.0f, 0.0f,
- };
- glGenBuffers(, &Fvbo);
- glBindBuffer(GL_ARRAY_BUFFER, Fvbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
- // ***
- glEnable(GL_DEPTH_TEST);
- glPointSize();
- // cout << "test01::initializeGL() out" << endl;
- }
- void test01::resizeGL(int w, int h)
- {
- qDebug() << "MainWidget::resizeGL(...)";
- // Set OpenGL viewport to cover whole widget
- glViewport(, , w, h);
- // ZC: 下面是 投射矩阵相关
- {
- // Calculate aspect ratio
- qreal aspect = qreal(w) / qreal(h ? h : );
- // Set near plane to 3.0, far plane to 7.0, field of view 45 degrees
- // const qreal zNear = 3.0, zFar = 7.0, fov = 45.0;
- const qreal zNear = 0.1, zFar = 100.0, fov = 45.0;
- // Reset projection
- Fprojection.setToIdentity();
- // Set perspective projection
- Fprojection.perspective(fov, aspect, zNear, zFar);
- }
- Fmvp = Fprojection * Fview * Fmodel;
- }
- void test01::paintGL()
- {
- // cout << "test01::paintGL()" << endl;
- //每次绘图前清理屏幕,否则会有残影
- //glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // ***
- glUseProgram( FuiProgramID );
- glUniformMatrix4fv(FuiMVP, , GL_FALSE, Fmvp.data());
- // ZC: 这下面的参数"0"应该是对应顶点着色器的layout的location序号(我记得在哪里看到有函数可以通过变量名取到这个序号的)
- glEnableVertexAttribArray();
- glBindBuffer(GL_ARRAY_BUFFER, Fvbo);
- glVertexAttribPointer(
- , // attribute. No particular reason for 0, but must match the layout in the shader.
- , // size
- GL_FLOAT, // type
- GL_FALSE, // normalized?
- , // stride
- (void*) // array buffer offset
- );
- glDrawArrays(GL_TRIANGLES, , ); // 3 indices starting at 0 -> 1 triangle
- glDisableVertexAttribArray();
- }
- int test01::ShaderLoad_Z(const QString &_strFullFileName_ShaderVertex, const QString &_strFullFileName_ShaderFregment)
- {
- // ZC: 着色器文件-->着色器字符串
- QFile file1( _strFullFileName_ShaderVertex );
- file1.open(QIODevice::ReadOnly);
- QTextStream in1(&file1);
- // 将文本流读取到字符串中:
- string strShaderVertex = in1.readAll().toStdString();
- file1.close();
- QFile file2( _strFullFileName_ShaderFregment );
- file2.open(QIODevice::ReadOnly);
- QTextStream in2(&file2);
- // 将文本流读取到字符串中:
- string strShaderFregment = in2.readAll().toStdString();
- file2.close();
- // cout << "strShaderVertex : " << strShaderVertex << endl;
- // cout << "strShaderFregment : " << strShaderFregment << endl;
- // *** *** *** ***
- const char* pcShaderVertex = strShaderVertex.c_str();
- const char* pcShaderFregment= strShaderFregment.c_str();
- // 编辑编译着色器
- unsigned int shaderVertex;// vertex, fragment;
- unsigned int shaderFregment;
- // 顶点着色器
- shaderVertex = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(shaderVertex, , &pcShaderVertex, NULL);
- glCompileShader(shaderVertex);
- // 检查着色器编译错误
- CheckCompileErrors(shaderVertex, "VERTEX");
- // ***
- // 片段着色器
- shaderFregment = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(shaderFregment, , &pcShaderFregment, NULL);
- glCompileShader(shaderFregment);
- // 检查着色器编译错误
- CheckCompileErrors(shaderFregment, "FRAGMENT");
- // ***
- // 链接着色器程序
- FuiProgramID = glCreateProgram();
- glAttachShader(FuiProgramID, shaderVertex);
- glAttachShader(FuiProgramID, shaderFregment);
- glLinkProgram(FuiProgramID);
- // 检查链接错误
- CheckCompileErrors(FuiProgramID, "PROGRAM");
- glDetachShader(FuiProgramID, shaderVertex);
- glDetachShader(FuiProgramID, shaderFregment);
- // 删除着色器
- glDeleteShader(shaderVertex);
- glDeleteShader(shaderFregment);
- return ;//uiProgramID;
- }
- bool test01::CheckCompileErrors(unsigned int _shader, const string& _strType)
- {
- int success;
- char infoLog[];
- if (_strType != "PROGRAM"){
- glGetShaderiv(_shader, GL_COMPILE_STATUS, &success);
- if (!success){
- glGetShaderInfoLog(_shader, , NULL, infoLog);
- cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << _strType << "---" << infoLog << endl;
- return false;
- }
- }else{
- glGetProgramiv(_shader, GL_LINK_STATUS, &success);
- if (!success){
- glGetProgramInfoLog(_shader, , NULL, infoLog);
- cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << _strType << "---" << infoLog << endl;
- return false;
- }
- }
- return true;
- }
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简单例子的更多相关文章
- Hibernate4.2.4入门(一)——环境搭建和简单例子
一.前言 发下牢骚,这段时间要做项目,又要学框架,搞得都没时间写笔记,但是觉得这知识学过还是要记录下.进入主题了 1.1.Hibernate简介 什么是Hibernate?Hibernate有什么用? ...
- AgileEAS.NET SOA 中间件平台.Net Socket通信框架-简单例子-实现简单的服务端客户端消息应答
一.AgileEAS.NET SOA中间件Socket/Tcp框架介绍 在文章AgileEAS.NET SOA 中间件平台Socket/Tcp通信框架介绍一文之中我们对AgileEAS.NET SOA ...
- spring mvc(注解)上传文件的简单例子
spring mvc(注解)上传文件的简单例子,这有几个需要注意的地方1.form的enctype=”multipart/form-data” 这个是上传文件必须的2.applicationConte ...
- ko 简单例子
Knockout是在下面三个核心功能是建立起来的: 监控属性(Observables)和依赖跟踪(Dependency tracking) 声明式绑定(Declarative bindings) 模板 ...
- mysql定时任务简单例子
mysql定时任务简单例子 ? 1 2 3 4 5 6 7 8 9 如果要每30秒执行以下语句: [sql] update userinfo set endtime = now() WHE ...
- java socket编程开发简单例子 与 nio非阻塞通道
基本socket编程 1.以下只是简单例子,没有用多线程处理,只能一发一收(由于scan.nextLine()线程会进入等待状态),使用时可以根据具体项目功能进行优化处理 2.以下代码使用了1.8新特 ...
- 一个简单例子:贫血模型or领域模型
转:一个简单例子:贫血模型or领域模型 贫血模型 我们首先用贫血模型来实现.所谓贫血模型就是模型对象之间存在完整的关联(可能存在多余的关联),但是对象除了get和set方外外几乎就没有其它的方法,整个 ...
- [转] 3个学习Socket编程的简单例子:TCP Server/Client, Select
以前都是采用ACE的编写网络应用,最近由于工作需要,需要直接只用socket接口编写CS的代码,重新学习这方面的知识,给出自己所用到的3个简单例子,都是拷贝别人的程序.如果你能完全理解这3个例子,估计 ...
- jsonp的简单例子
jsonp的简单例子 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
随机推荐
- 使用genism训练词向量【转载】
转自:https://blog.csdn.net/qq_16912257/article/details/79099581 https://blog.csdn.net/thriving_fcl/art ...
- GAITC 2019全球人工智能技术大会(南京)
2019年5月25日至26日,由中国人工智能学会主办,以“交叉.融合.相生.共赢”为主题的2019GAITC将在南京全新亮相. 2019 全球人工智能技术大会(2019 GAITC)以“前端引领.深度 ...
- Complex类的实现
#include<iostream> #include<cmath> using namespace std; class complex{ public: complex() ...
- Chrome Inspect调试微信出现空白页面的解决方法
首先,需要打开手机的USB调试和微信的TBS 调试开关. 如果不打开TBS开关,Inspect时会检测不到任何微信的H5页面 使用微信扫码下方二维码,打开TBS调试开关: 普通网页: 小程序: 微信扫 ...
- 《linux就该这么学》第十六节课:第16,17章,Squid服务和iscsi网络存储
第十六章 squid总结: 正向代理:yum 安装后清空防火墙即可正常使用,客户端设置浏览器 透明正向代理:vim /etc/squid/squid.conf 59行:http_port 312 ...
- jenkins+Gitlab+maven+tomcat实现自动集成、打包、部署
一.前言 首先出于提高自己技术水平和琢磨能做点什么能提高工作效率,上线工作实在无聊.重复(手动编译.打包,传包,重启),于是就想到了jenkins,jenkins持续集成已经不是什么新鲜的话题,网上文 ...
- MVC 使用缓存
public AController() { ViewBag[); } private List<BlogsClass> GetClass(int parentId) { List< ...
- sql中批量插入begin的使用
private static String ADD_ATTR_EXT_ITEM="insert into attr_ext_item(attr_ext_main_key,attr_name_ ...
- document.wrtie()用法
推荐看这篇文章,非常的好 http://www.softwhy.com/article-8326-1.html
- spring源码解析2--容器的基本实现
spring的主要特性是IOC,实现IOC的关键是bean,而更关键的是如何bean的管理容器,也就是BeanFactory,本文的目标是弄清楚BeanFactory具体是怎么样的存在. 先看下最简单 ...