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 ...
随机推荐
- python+selenium操作cookie
WebDriver提供了操作Cookie的相关方法,可以读取.添加和删除cookie信息. WebDriver操作cookie的方法: get_cookies(): 获得所有cookie信息. get ...
- 前端 CSS的选择器
什么是选择器.在一个HTML页面中会有很多很多的元素,不同的元素可能会有不同的样式,某些元素又需要设置相同的样式,选择器就是用来从HTML页面中查找特定元素的,找到元素之后就可以为它们设置样式了. 选 ...
- [Git] 023 Re:从零开始的 rebase 命令
1. 开门见山 我新建了一个本地仓库,并进行了一些操作 当前情况 查看(直观但不明了) 上图的第二条 "log" 命令详见 [Git] 024 log 命令的补充 的 " ...
- SpringBoot自定义Starter实现
自定义Starter: Starter会把所有用到的依赖都给包含进来,避免了开发者自己去引入依赖所带来的麻烦.Starter 提供了一种开箱即用的理念,其中核心就是springboot的自动配置原理相 ...
- CentOS7 开启路由转发
1.临时开启,(写入内存,在内存中开启) echo "1" > /proc/sys/net/ipv4/ip_forward 2.永久开启,(写入内核) 在 vim /etc/ ...
- 《剑指offer》面试题23 从上往下打印二叉树 Java版
注意层序遍历的时候对每一层的处理方式可能不同,这里把每一层的元素保存进一个List中了,那么就需要记录每一层的数量. public List<List<Integer>> se ...
- 源码看React 事件机制
对React熟悉的同学都知道,React中的事件机制并不是原生的那一套,事件没有绑定在原生DOM上,发出的事件也是对原生事件的包装.那么这一切是怎么实现的呢? 事件注册 首先还是看我们熟悉的代码 &l ...
- spring controller 方法测试
controller 测试 不使用其他api接口测试工具 一般而言,我们写好一个模块后,会对其进行单元测试,再集成到现有的系统中. 但是呢~针对Controller.Service.Dao三层来说,我 ...
- 卷积神经网络基础(CNN)【转载】
作者: Sanjay Chan [ http://blog.csdn.net/chenzomi ] 背景 之前在网上搜索了好多好多关于CNN的文章,由于网络上的文章很多断章取义或者描述不清晰,看了很多 ...
- 06java进阶——集合框架(list和泛型)
1.ArrayList ArrayList集合是程序中最常见的一种集合,它属于引用数据类型(类).在ArrayList内部封装了一个长度可变的数组,当存入的元素超过数组长度时,ArrayList会在内 ...