cube例子自带的说明文档
来源:互联网 发布:unity3d角色控制脚本 编辑:程序博客网 时间:2024/05/22 18:44
<?xml version="1.0" encoding="UTF-8"?>
Cube OpenGL ES 2.0 example
This example has been written for OpenGL ES 2.0 but it works also on desktop OpenGL because this example is simple enough and for the most parts desktop OpenGL API is same. It compiles also without OpenGL support but then it just shows a label stating that OpenGL support is required.
The example consist of two classes:
MainWidget
extends QGLWidget and contains OpenGL ES 2.0 initialization and drawing and mouse and timer event handlingGeometryEngine
handles polygon geometries. Transfers polygon geometry to vertex buffer objects and draws geometries from vertex buffer objects.
We'll start by initializing OpenGL ES 2.0 in MainWidget
.
Initializing OpenGL ES 2.0
Since OpenGL ES 2.0 doesn't support fixed graphics pipeline anymore it has to be implemented by ourselves. This makes graphics pipeline very flexible but in the same time it becomes more difficult because user has to implement graphics pipeline to get even the simplest example running. It also makes graphics pipeline more efficient because user can decide what kind of pipeline is needed for the application.
First we have to implement vertex shader. It gets vertex data and model-view-projection matrix (MVP) as parameters. It transforms vertex position using MVP matrix to screen space and passes texture coordinate to fragment shader. Texture coordinate will be automatically interpolated on polygon faces.
After that we need to implement second part of the graphics pipeline - fragment shader. For this exercise we need to implement fragment shader that handles texturing. It gets interpolated texture coordinate as a parameter and looks up fragment color from the given texture.
Using QGLShaderProgram
we can compile, link and bind shader code to graphics pipeline. This code uses Qt Resource files to access shader source code.
The following code enables depth buffering and back face culling.
Loading Textures from Qt Resource Files
QGLWidget
interface implements methods for loading textures from QImage to GL texture memory. We still need to use OpenGL provided functions for specifying the GL texture unit and configuring texture filtering options.
Cube Geometry
There are many ways to render polygons in OpenGL but the most efficient way is to use only triangle strip primitives and render vertices from graphics hardware memory. OpenGL has a mechanism to create buffer objects to this memory area and transfer vertex data to these buffers. In OpenGL terminology these are referred as Vertex Buffer Objects (VBO).
This is how cube faces break down to triangles. Vertices are ordered this way to get vertex ordering correct using triangle strips. OpenGL determines triangle front and back face based on vertex ordering. By default OpenGL uses counter-clockwise order for front faces. This information is used by back face culling which improves rendering performance by not rendering back faces of the triangles. This way graphics pipeline can omit rendering sides of the triangle that aren't facing towards screen.
Creating vertex buffer objects and transferring data to them is quite simple using QOpenGLBuffer. MainWidget makes sure the GeometryEngine instance is created and destroyed with the OpenGL context current. This way we can use OpenGL resources in the constructor and perform proper cleanup in the destructor.
Drawing primitives from VBOs and telling programmable graphics pipeline how to locate vertex data requires few steps. First we need to bind VBOs to be used. After that we bind shader program attribute names and configure what kind of data it has in the bound VBO. Finally we'll draw triangle strip primitives using indices from the other VBO.
Perspective Projection
Using QMatrix4x4
helper methods it's really easy to calculate perpective projection matrix. This matrix is used to project vertices to screen space.
Orientation of the 3D Object
Quaternions are handy way to represent orientation of the 3D object. Quaternions involve quite complex mathematics but fortunately all the necessary mathematics behind quaternions is implemented in QQuaternion
. That allows us to store cube orientation in quaternion and rotating cube around given axis is quite simple.
The following code calculates rotation axis and angular speed based on mouse events.
QBasicTimer
is used to animate scene and update cube orientation. Rotations can be concatenated simply by multiplying quaternions.
Model-view matrix is calculated using the quaternion and by moving world by Z axis. This matrix is multiplied with the projection matrix to get MVP matrix for shader program.
Files:
- cube/fshader.glsl
- cube/geometryengine.cpp
- cube/geometryengine.h
- cube/mainwidget.cpp
- cube/mainwidget.h
- cube/vshader.glsl
- cube/main.cpp
- cube/cube.pro
- cube/shaders.qrc
- cube/textures.qrc
from: http://blog.csdn.net/fu851523125/article/details/51169440
- cube例子自带的说明文档
- android自带例子autoComplete的学习
- C#自带Ftp下载的例子
- C#自带Ftp下载的例子
- Oracle自带的一个Oci8例子
- NotesList(Robotium自带的例子)
- android自带播放器的例子
- mahout自带例子的常用语法
- Unity3d自带例子AngryBots的分析
- mahout自带的例子 -------------------------分类
- hadoop mahout 运行自带的例子
- Unity3D自带例子AngryBots的分析
- Cocos2dx-3.2自带例子的运行
- Unity3D自带例子AngryBots的分析
- Unity3D自带例子AngryBots的分析
- 运行caffe自带的两个例子
- 浏览OSG自带的例子
- jdk自带的动态代理说明
- java集合作为参数时,未考虑地址传递遇到的坑
- 最爱的上野树里
- 第4周项目3求圆柱表面积
- Volley的完美解析(二)
- SpringMVC Custom ArgumentResolver
- cube例子自带的说明文档
- 理解Spring的隔离级别
- 2016百度大数据云计算笔试总结
- 第4周项目3 并联电阻
- 死锁的必要条件,怎么处理死锁
- ListView 焦点问题
- 对于特定目录下的所有文件中都加入特定的头文件信息
- 第4周项目1求并联电阻大小
- Linux学习(CentOS-7)---在命令行模式下执行命令