3D Computer Grapihcs Using OpenGL - 11 Model View Projection Matrices
本节我们将绘制一个3维物体,立方体。
如果要渲染3D物体,我们需要了解MVP(Model View Projection),它表示三个转换矩阵。实际上这个名字不够明确,更加确切的释义如下:
- Model - Model to World 模型空间到世界空间
- View - World to View 世界空间到视图空间
- Projection - View to Projection 视图空间到投影空间
要实现这三个转换矩阵,我们需要借助glm数学库提供的一些方便的结构体和函数。
重构
我们先对程序结构进行修改,对工程右键>Add > New Filter, 创建一个Primitives 文件夹,在其中创建两个文件,一个Vertex.h,一个ShapeData.h
Vertex.h中定义了一个Vertex结构体,它包含两个glm::vec3成员,分别表示位置和颜色。
#pragma once
#include <glm\glm.hpp> struct Vertex
{
glm::vec3 position;
glm::vec3 color;
};
ShapeData.h中定义了一个ShapeData结构体,包含四个成员变量,分别是
- Vertex* 类型:顶点数组指针
- Gluint类型:顶点数量
- GLushort* 类型:索引数组指针
- GLuint 类型:索引数组长度
另外还提供了构造函数,清理函数
#pragma once
#include <GL\glew.h>
#include "Vertex.h" struct ShapeData
{
ShapeData() :
vertices(), numVertices(), indices(), numIndices() {} Vertex* vertices;
GLuint numVertices;
GLushort* indices;
GLuint numIndices; GLsizeiptr vertexBufferSize() const
{
return numVertices * sizeof(Vertex);
}
GLsizeiptr indexBufferSize() const
{
return numIndices * sizeof(GLushort);
} void cleanUp()
{
delete[] vertices;
delete[] indices;
numVertices = numIndices = ;
}
};
此外还加入了一个新的类,ShapeGenerator
ShapeGenerator.h
#pragma once
#include <ShapeData.h> class ShapeGenerator
{
public:
static ShapeData makeCube();
};
ShapeGenerator.cpp
#include "ShapeGenerator.h"
#include "Vertex.h" #define NUM_ARRAY_ELEMENTS(a) sizeof(a) / sizeof(*a) ShapeData ShapeGenerator::makeCube()
{
ShapeData ret;
Vertex stackVerts[]=
{
glm::vec3(-1.0f, +1.0f, +1.0f), //
glm::vec3(+1.0f, 0.0f, 0.0f), //Color
glm::vec3(+1.0f, +1.0f, +1.0f), //
glm::vec3(0.0f, +1.0f, 0.0f), //Color
glm::vec3(+1.0f, +1.0f, -1.0f), //
glm::vec3(0.0f, 0.0f, +1.0f), //Color
glm::vec3(-1.0f, +1.0f, -1.0f), //
glm::vec3(+1.0f, +1.0f, +1.0f), //Color glm::vec3(-1.0f, +1.0f, -1.0f), //
glm::vec3(+1.0f, 0.0f, +1.0f), //Color
glm::vec3(+1.0f, +1.0f, -1.0f), //
glm::vec3(0.0f, 0.5f, 0.2f), //Color
glm::vec3(+1.0f, -1.0f, -1.0f), //
glm::vec3(0.8f, 0.6f, 0.4f), //Color
glm::vec3(-1.0f, -1.0f, -1.0f), //
glm::vec3(0.3f, +1.0f, +0.5f), //Color glm::vec3(+1.0f, +1.0f, -1.0f), //
glm::vec3(0.2f, 0.5f, 0.2f), //Color
glm::vec3(+1.0f, +1.0f, +1.0f), //
glm::vec3(0.9f, 0.3f, 0.7f), //Color
glm::vec3(+1.0f, -1.0f, +1.0f), //
glm::vec3(0.3f, 0.7f, 0.5f), //Color
glm::vec3(+1.0f, -1.0f, -1.0f), //
glm::vec3(0.5f, 0.7f, 0.5f), //Color glm::vec3(-1.0f, +1.0f, +1.0f), //
glm::vec3(0.7f, 0.8f, 0.2f), //Color
glm::vec3(-1.0f, +1.0f, -1.0f), //
glm::vec3(0.5f, 0.7f, 0.3f), //Color
glm::vec3(-1.0f, -1.0f, -1.0f), //
glm::vec3(0.8f, 0.6f, 0.4f), //Color
glm::vec3(-1.0f, -1.0f, +1.0f), //
glm::vec3(0.3f, +1.0f, +0.5f), //Color glm::vec3(+1.0f, +1.0f, +1.0f), //
glm::vec3(0.7f, 0.8f, 0.2f), //Color
glm::vec3(-1.0f, +1.0f, +1.0f), //
glm::vec3(0.5f, 0.7f, 0.3f), //Color
glm::vec3(-1.0f, -1.0f, +1.0f), //
glm::vec3(0.8f, 0.6f, 0.4f), //Color
glm::vec3(+1.0f, -1.0f, +1.0f), //
glm::vec3(0.3f, +1.0f, +0.5f), //Color glm::vec3(+1.0f, -1.0f, -1.0f), //
glm::vec3(0.7f, 0.8f, 0.2f), //Color
glm::vec3(-1.0f, -1.0f, -1.0f), //
glm::vec3(0.5f, 0.7f, 0.3f), //Color
glm::vec3(-1.0f, -1.0f, +1.0f), //
glm::vec3(0.8f, 0.6f, 0.4f), //Color
glm::vec3(+1.0f, -1.0f, +1.0f), //
glm::vec3(0.3f, +1.0f, +0.5f), //Color
}; ret.numVertices = NUM_ARRAY_ELEMENTS(stackVerts);
ret.vertices = new Vertex[ret.numVertices];
memcpy(ret.vertices, stackVerts, sizeof(stackVerts)); unsigned short stackIndices[] =
{
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
,,,,,,
}; ret.numIndices = NUM_ARRAY_ELEMENTS(stackIndices);
ret.indices = new GLushort[ret.numIndices];
memcpy(ret.indices, stackIndices, sizeof(stackIndices));
return ret;
}
主要作用是提供了一个静态方法 makeCube,返回一个立方体的数据。
修改MyGlWindow类
#include <gl\glew.h>
#include "MyGlWindow.h"
#include <iostream>
#include <fstream>
#include <glm\gtc\matrix_transform.hpp>
#include <ShapeGenerator.h> GLuint programID;
GLuint numIndices; void MyGlWindow::sendDataToOpenGL()
{ ShapeData shape = ShapeGenerator::makeCube(); GLuint vertexBufferID;
glGenBuffers(, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, shape.vertexBufferSize(), shape.vertices, GL_STATIC_DRAW); GLuint indexBufferID;
glGenBuffers(, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, shape.indexBufferSize(), shape.indices, GL_STATIC_DRAW); glEnableVertexAttribArray();
glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, sizeof(GLfloat) * , ); glEnableVertexAttribArray();
glVertexAttribPointer(, , GL_FLOAT, GL_FALSE, sizeof(GLfloat) * , (char*)(sizeof(GLfloat) * )); numIndices = shape.numIndices;
shape.cleanUp(); } void MyGlWindow::installShaders()
{
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER); std::string tmp = ReadShaderCode("VertexShaderCode2.glsl");
const char* vertexShaderCode = tmp.c_str();
glShaderSource(vertexShaderID, , &vertexShaderCode, ); tmp = ReadShaderCode("FragmentShaderCode2.glsl");
const char* fragmentShaderCode = tmp.c_str();
glShaderSource(fragmentShaderID, , &fragmentShaderCode, ); glCompileShader(vertexShaderID);
glCompileShader(fragmentShaderID); programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID); glLinkProgram(programID); glUseProgram(programID);
} void MyGlWindow::initializeGL()
{
glewInit();
glEnable(GL_DEPTH_TEST);
sendDataToOpenGL();
installShaders();
} void MyGlWindow::paintGL()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glViewport(, , width(), height()); //更新:最新版本的glm中,glm::mat4()生成的是不是单位矩阵,而是零矩阵,这里要使用glm::mat4(1.0f)才可以
glm::mat4 modelTransformMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f,-3.0f));
glm::mat4 projectionMatrix = glm::perspective(30.0f, ((float)width()) / height(), 0.1f, 10.0f); GLint modelTransformUniformLocation = glGetUniformLocation(programID, "modelMatrix");
GLint projectionMatrixUniformLocation = glGetUniformLocation(programID, "projectionMatrix"); glUniformMatrix4fv(modelTransformUniformLocation, , GL_FALSE, &modelTransformMatrix[][]);
glUniformMatrix4fv(projectionMatrixUniformLocation, , GL_FALSE, &projectionMatrix[][]); glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, ); } std::string MyGlWindow::ReadShaderCode(const char* fileName)
{
std::ifstream myInput(fileName);
if (!myInput.good())
{
std::cout << "File failed to load..." << fileName;
exit();
}
return std::string(
std::istreambuf_iterator<char>(myInput),
std::istreambuf_iterator<char>());
}
Vertex Shader :
#version in layout(location=) vec3 position;
in layout(location=) vec3 vertexColor; uniform mat4 modelMatrix;
uniform mat4 projectionMatrix; out vec3 passingColor; void main()
{
vec4 v = vec4(position,1.0);
vec4 newPosition = modelMatrix * v;
gl_Position = projectionMatrix * newPosition;
passingColor= vertexColor;
}
Fragment Shader:
#version in vec3 passingColor;
out vec4 finalColor; void main()
{
finalColor = vec4(passingColor,1.0);
}
注意MyGlWindow的78-85行,是使用Uniform 变量的通用方法,使用的是Vertex Shader中第6-7行的两个uniform。
使用Uniform变量的步骤总结:
- 使用glGetUniformLocation获取Uniform变量的ID,并储存在一个GLint 变量中
- 使用glUnifomxxxx()类的函数和刚才得到的ID给Uniform赋值。
另外要注意85-86行,函数的最后一参数需要一个const GLfloat * 类型的变量,所以我们使用[0][0]获取矩阵的第一个元素,它是个GLfloat类型的,再对他使用取地址符&得到它的地址。
编译运行以后得到一个平面(实际上是立方体的一个面):
我们在最开始提到了3个矩阵,但是这里只用到了两个,实际上少了第二个矩阵,World to View矩阵,这也正是为什么我们现在无法移动观察视角的原因,我们的相机被假设在世界原点,朝向-z的方向看去,这是默认的设置。后面我们会学习world to view的转换矩阵。
3D Computer Grapihcs Using OpenGL - 11 Model View Projection Matrices的更多相关文章
- 3D Computer Grapihcs Using OpenGL - 17 添加相机(旋转)
在11节我们说过,MVP矩阵中目前只应用了两个矩阵,World to View 矩阵被省略了,这就导致我们的画面没有办法转换视角. 本节我们将添加这一环节,让相机可以旋转. 为了实现这一目的,我们添加 ...
- 3D Computer Grapihcs Using OpenGL - 15 Draw Element Instanced
友情提示:继续本节之前,需要保存此前的代码,本节为了试验,会对代码做一些修改,但后续的修改需要我们把代码返回之前的进度. OpenGL内置支持Instancing,有专门的函数来处理这件事情. 为了方 ...
- 3D Computer Grapihcs Using OpenGL - 09 Enable Depth Test
启用Depth Test OpenGL是个3D绘图API,也就是说不只有xy坐标轴,还有第三个坐标轴z,z轴的方向是垂直于屏幕,指向屏幕内. 靠近人眼的方向是负方向,标准化设备坐标的最小值是-1, 最 ...
- 3D Computer Grapihcs Using OpenGL - 06 Vertex and Fragment Shaders
从这里就接触到了可编程图形渲染管线. 下面介绍使用Vertex Shader (顶点着色器)和 Fragment Shader(像素着色器)的方法. 我们的目标是使用这两个着色器给三角形填充绿色. 添 ...
- 3D Computer Grapihcs Using OpenGL - 19 Vertex Array Object(顶点数组对象)
大部分OpenGL教程都会在一开始就讲解VAO,但是该教程的作者认为这是很不合理的,因为要理解它的作用需要建立在我们此前学过的知识基础上.因此直到教程已经进行了一大半,作者才引入VAO这个概念.在我看 ...
- 3D Computer Grapihcs Using OpenGL - 16 使用DrawElementsInstanced绘制立方体
我们使用15节学到的知识来绘制14节的立方体. 在第14节我们使用了两次glDrawElements实现了OpenGL实例化,发现这样仍然不太方便,如果需要绘制成千上万的立方体,就需要手写成千上万次的 ...
- 3D Computer Grapihcs Using OpenGL - 14 OpenGL Instancing
如果我们需要绘制两个(或者多个)一样的立方体(或者物体),只是位置.缩放.旋转不一样,那么我们可以不需要多次将这个物体的顶点信息.颜色信息等发送到显卡,而是发送一次,绘制多次,仅仅是每次绘制之前应用不 ...
- 3D Computer Grapihcs Using OpenGL - 12 Rotation Matrix
为了证明我们上节渲染出来的是一个立方体而不是一个平面,我们决定将它旋转一定角度,这样我们就需要一个旋转矩阵(也属于ModelTransformMatrix的一部分) 上一节我们的ModelTransf ...
- 3D Computer Grapihcs Using OpenGL - 10 Color Buffer
本节我们将尝试利用三角形制作一个“走马灯”效果. 一个三角形如图示方式,从左向右依次移动. 先看一下代码: MyGlWindow.cpp #include <gl\glew.h> #inc ...
随机推荐
- 【Linux开发】【Qt开发】交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别
一. 什么是ABI和EABI1) ABI: 二进制应用程序接口(Application Binary Interface (ABI) for the ARM Architecture)在计算机中,应用 ...
- Mac021--编辑软件
一.思维导图MindNode 知乎Mac常用的思维导图:https://zhuanlan.zhihu.com/p/37768277 MindNode下载地址:https://macblcom.ctfi ...
- rename批量修改文件名
批量改名: 如文件,批量修改,把hello去掉[root@localhost wang]# ll-rw-r--r-- 1 root root 0 5月 14 02:36 a.hello.txt-rw- ...
- Linux命令学习(0)
作为一名前端,可能接触到linux的机会并不多,但这不代表就不需要学.对我而言,学习linux主要是为了方便部署我的项目到服务器,我并没有花时间去学这些,只是上网查怎么部署项目,然后按教程一步一步来, ...
- tensorflow学习笔记七----------卷积神经网络
卷积神经网络比神经网络稍微复杂一些,因为其多了一个卷积层(convolutional layer)和池化层(pooling layer). 使用mnist数据集,n个数据,每个数据的像素为28*28* ...
- redis持久化机制与过期策略
RDB的持久化策略 (快照方式,默认持久化方式): 按照规则定时将内存中的数据同步到磁盘,它有以下4个触发场景. 1. 自己配置的快照规则 vim /redis/bin/ redis.conf:按照 ...
- 分布式之redis(转发)
为什么写这篇文章? 博主的<分布式之消息队列复习精讲>得到了大家的好评,内心诚惶诚恐,想着再出一篇关于复习精讲的文章.但是还是要说明一下,复习精讲的文章偏面试准备,真正在开发过程中,还是脚 ...
- 08-Django加载静态文件
1.css文件以及js文件要放在static目录下,static和templates属于同级目录 2.在Django项目的同名项目文件的setting.py中,最后添加静态文件夹static目录路径 ...
- DEV控件的分页控件,实现勾选复选框
/// <summary> /// 单元格的点击事件 /// </summary> /// <param name="sender"></ ...
- 202-基于TI DSP TMS320C6678、Xilinx K7 FPGA XC7K325T的高速数据处理核心板
该DSP+FPGA高速信号采集处理板由我公司自主研发,包含一片TI DSP TMS320C6678和一片Xilinx FPGA K7 XC72K325T-1ffg900.包含1个千兆网口,1个FMC ...