基于OpenGL编写一个简易的2D渲染框架-03 渲染基本几何图形
阅读文章前需要了解的知识,你好,三角形:https://learnopengl-cn.github.io/01%20Getting%20started/04%20Hello%20Triangle/
要渲染出几何图形来,首先需要变换矩阵,那么自然就需要一个数学库了。本来想用 glm 库的,但用不惯这个库,只好编写一个简单的数据库了。这个库暂不需要复杂的功能,就几个向量类和 4x4 的矩阵类,矩阵类有一个重要的函数,用于创建正交矩阵(由于这是一个简单的项目,就不需要 LookAt(视图) 矩阵了)。
Matrix4 Matrix4::ortho(GLfloat fLeft, GLfloat fRight, GLfloat fBottom, GLfloat fTop, GLfloat fNear, GLfloat fFar)
{
Matrix4 mat4 = Matrix4::ZERO; mat4.m[][] = / (fRight - fLeft);
mat4.m[][] = / (fTop - fBottom);
mat4.m[][] = / (fNear - fFar);
mat4.m[][] = ; mat4.m[][] = -(fRight + fLeft) / (fRight - fLeft);
mat4.m[][] = -(fTop + fBottom) / (fTop - fBottom);
mat4.m[][] = (fNear + fFar) / (fNear - fFar); return mat4;
}
对于这个矩阵的推导感兴趣的话,这里推荐一篇文章:http://blog.csdn.net/popy007/article/details/4126809
接着需要着色器程序,可以渲染几何图形就行了
const GLchar *shader_vs = "#version 330 core\n"
"layout (location = 0) in vec3 position;\n"
"layout (location = 1) in vec4 color;\n"
"uniform mat4 projection;\n"
"out vec4 Vcolor;\n"
"void main(){\n"
"gl_Position = vec4(position, 1.0f);\n"
"Vcolor = color;\n"
"}"; const GLchar *shader_frag = "#version 330 core\n"
"out vec4 color;\n"
"in vec4 Vcolor;\n"
"void main(){\n"
"color = Vcolor;\n"
"}";
创建着色程序并绑定
void GraphicsContext::createShaderProgram()
{
/* 创建顶点作色器 */
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, , &shader_vs, NULL);
glCompileShader(vertexShader); GLint success;
GLchar infoLog[];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
if ( !success ) {
glGetShaderInfoLog(vertexShader, , NULL, infoLog);
return;
} /* 创建片段着色器 */
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, , &shader_frag, NULL);
glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
if ( !success ) {
glGetShaderInfoLog(fragmentShader, , NULL, infoLog);
return;
} /* 创建着色程序 */
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if ( !success ) {
glGetProgramInfoLog(shaderProgram, , NULL, infoLog);
return;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader); /* 使用着色程序 */
glUseProgram(shaderProgram);
}
准备阶段已经完成,接下来开始渲染几何图形,新建一个渲染器类 Renderer,渲染顶点数据。此外,还有一个储存顶点数据的类
class VertexData
{
public:
std::vector<Vec3> positions;
std::vector<Vec2> texcoords;
std::vector<Color> colors;
std::vector<GLuint> indices; int nPositionCount;
int nIndexCount;
bool bHasTexcoord; RenderType renderType; void clear()
{
nPositionCount = ;
nIndexCount = ;
} void resize(int positionCount, int indexCount)
{
if ( positions.size() - nPositionCount < positionCount ) {
positions.resize(positions.size() + positionCount);
colors.resize(colors.size() + positionCount); if ( bHasTexcoord ) texcoords.resize(texcoords.size() + positionCount);
}
if ( indices.size() - nIndexCount < indexCount ) {
indices.resize(indices.size() + indexCount);
}
} void pushData(const Vec3& pos, const Color& color)
{
positions[nPositionCount] = pos;
colors[nPositionCount++] = color;
} void pushData(const Vec3& pos, const Vec2& texcoord, const Color& color)
{
positions[nPositionCount] = pos;
texcoords[nPositionCount] = texcoord;
colors[nPositionCount++] = color;
} void pushIndex(GLuint index)
{
indices[nIndexCount++] = index;
}
};
主要储存顶点位置,顶点颜色,纹理坐标(现在先不用)和顶点的索引。
渲染类型
enum RenderType
{
RENDER_TYPE_LINES,
RENDER_TYPE_TRIANGLES,
RENDER_TYPE_TEXTURE
};
渲染单元的结构
struct RenderUnit
{
Vec3* pPositions;
int nPositionCount; Color* pColors;
bool bSameColor; GLuint* pIndices;
int nIndexCount; RenderType renderType;
};
渲染器头文件
#pragma once
#include "Common.h"
#include "Math.h" #include <vector> namespace Simple2D
{
enum RenderType
{
RENDER_TYPE_LINES,
RENDER_TYPE_TRIANGLES,
RENDER_TYPE_TEXTURE
}; struct RenderUnit
{
Vec3* pPositions;
int nPositionCount; Color* pColors;
bool bSameColor; GLuint* pIndices;
int nIndexCount; RenderType renderType;
}; class DLL_export Renderer
{
class VertexData
{
public:
std::vector<Vec3> positions;
std::vector<Vec2> texcoords;
std::vector<Color> colors;
std::vector<GLuint> indices; int nPositionCount;
int nIndexCount;
bool bHasTexcoord; RenderType renderType; void clear()
{
nPositionCount = ;
nIndexCount = ;
} void resize(int positionCount, int indexCount)
{
if ( positions.size() - nPositionCount < positionCount ) {
positions.resize(positions.size() + positionCount);
colors.resize(colors.size() + positionCount); if ( bHasTexcoord ) texcoords.resize(texcoords.size() + positionCount);
}
if ( indices.size() - nIndexCount < indexCount ) {
indices.resize(indices.size() + indexCount);
}
} void pushData(const Vec3& pos, const Color& color)
{
positions[nPositionCount] = pos;
colors[nPositionCount++] = color;
} void pushData(const Vec3& pos, const Vec2& texcoord, const Color& color)
{
positions[nPositionCount] = pos;
texcoords[nPositionCount] = texcoord;
colors[nPositionCount++] = color;
} void pushIndex(GLuint index)
{
indices[nIndexCount++] = index;
}
}; public:
Renderer();
~Renderer(); void render();
void renderVertexData(VertexData& vertexData); void pushRenderUnit(const RenderUnit& unit); private:
void initBuffers();
Vec3 tranformPosition(Vec3& pos); private:
VertexData triangleData;
VertexData lineData; GLuint positionBuffer;
GLuint colorBuffer;
GLuint indexBuffer;
GLuint VAO; Matrix4 mTransformMatrix;
};
}
triangleData,储存绘制三角形图元的顶点数据
lineData,储存绘制线段的顶点数据
RenderUnit类,通过 pushRenderUnit 函数, 将顶点数据传到渲染器,然后渲染器将相同渲染类型的 RenderUnit 数据储存到同一个顶点数据缓冲区(VertexData)。
void Renderer::pushRenderUnit(const RenderUnit& unit)
{
VertexData* vertexData = nullptr;
if ( unit.renderType == RENDER_TYPE_TRIANGLES ) {
vertexData = &triangleData;
}
else if ( unit.renderType == RENDER_TYPE_LINES ) {
vertexData = &lineData;
} /* 填充数据 */
vertexData->resize(unit.nPositionCount, unit.nIndexCount); int baseIndex = vertexData->nPositionCount;
for ( int i = ; i < unit.nPositionCount; i++ ) {
if ( unit.bSameColor ) {
vertexData->pushData(tranformPosition(unit.pPositions[i]), unit.pColors[]);
}
else {
vertexData->pushData(tranformPosition(unit.pPositions[i]), unit.pColors[i]);
}
}
for ( int i = ; i < unit.nIndexCount; i++ ) {
vertexData->pushIndex(baseIndex + unit.pIndices[i]);
}
}
在函数中,对顶点位置向量进行了矩阵变换
Vec3 Renderer::tranformPosition(Vec3& pos)
{
return mTransformMatrix * pos;
}
这个变换矩阵主要一个正交矩阵和一个变换矩阵组成
Matrix4 ortho = Matrix4::ortho(, , , , -, );
Matrix4 tranform = Matrix4::makeTransform(Vec3(, , ), Vec3(, -, ));
mTransformMatrix = ortho * tranform;
因为坐标原点在左上角,并且 Y 轴朝下为正。所以通过一个变换矩阵 transform 变换到左下角,Y 轴朝上为正。
最后在函数 renderVertexData 中渲染出来
void Renderer::renderVertexData(VertexData& vertexData)
{
/* 填充顶点数据 */
glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof( Vec3 ) * vertexData.nPositionCount, &vertexData.positions[], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof( Color ) * vertexData.nPositionCount, &vertexData.colors[], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof( GLuint ) * vertexData.nIndexCount, &vertexData.indices[], GL_STATIC_DRAW);
glBindVertexArray(); glBindVertexArray(VAO);
switch ( vertexData.renderType ) {
case RENDER_TYPE_TRIANGLES:
glDrawElements(GL_TRIANGLES, vertexData.nIndexCount, GL_UNSIGNED_INT, );
break;
case RENDER_TYPE_LINES:
glDrawElements(GL_LINES, vertexData.nIndexCount, GL_UNSIGNED_INT, );
break;
}
glBindVertexArray(); vertexData.clear();
}
为了方便绘制几何图形,创建一个画布类 Canvas2D
#pragma once
#include "Common.h"
#include "Math.h" #include <vector> namespace Simple2D
{
class Renderer; class DLL_export Canvas2D
{
public:
Canvas2D(Renderer* renderer);
~Canvas2D(); void drawLine(int x1, int y1, int z1, int x2, int y2, int z2, Color& color); void drawCircle(const Vec3& center, int radius, Color& color, int nSlice = );
void fillCircle(const Vec3& center, int radius, int degrees, Color& color);
void fillCircle(const Vec3& center, int in_radius, int out_radius, int beginAngle, int endAngle, Color& color); void drawRect(float x, float y, float w, float h, Color color);
void fillRect(float x, float y, float w, float h, Color color); void drawTriangles(Vec3* positions, Color* colors, int positionCount, GLuint* indices, int indexCount, bool sameColor = true);
void drawLines(Vec3* positions, Color* colors, int positionCount, bool sameColor = true); private:
void resizeVector(int positionCount, int indexCount); Renderer* pRenderer; std::vector<Vec3> vPositions;
int nPositionCount; std::vector<GLuint> vIndices;
int nIndexCount;
};
}
如果要绘制一个矩形,需要准备顶点数据
void Canvas2D::fillRect(float x, float y, float w, float h, Color color)
{
this->resizeVector(, ); vPositions[].set(x + , y + , );
vPositions[].set(x + , y + h, );
vPositions[].set(x + w, y + h, );
vPositions[].set(x + w, y + , ); vIndices[] = ;
vIndices[] = ;
vIndices[] = ;
vIndices[] = ;
vIndices[] = ;
vIndices[] = ; this->drawTriangles(&vPositions[], &color, , &vIndices[], );
}
然后在函数 drawTriangle 中将数据传递到 渲染器
void Canvas2D::drawTriangles(Vec3* positions, Color* colors, int positionCount, GLuint* indices, int indexCount, bool sameColor)
{
static RenderUnit unit;
unit.pPositions = positions;
unit.nPositionCount = positionCount;
unit.pIndices = indices;
unit.nIndexCount = indexCount;
unit.pColors = colors;
unit.bSameColor = sameColor;
unit.renderType = RENDER_TYPE_TRIANGLES; pRenderer->pushRenderUnit(unit);
}
最后在主函数中绘制几个图形
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
RenderWindow window(DEFAULT_WIN_W, DEFAULT_WIN_H);
GraphicsContext graphicsContext(&window); Canvas2D canvas(graphicsContext.getRenderer()); MSG msg = { };
while ( msg.message != WM_QUIT ) {
if ( PeekMessage(&msg, , , , PM_REMOVE) ) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
float n = ;
for ( int i = ; i < n; i++ ) {
for ( int j = ; j < n; j++ ) {
if ( i % == && j % == ) {
canvas.fillRect(
+ i * , + j * , , ,
Color(i / n, i / n, , ));
}
else {
canvas.drawRect(
+ i * , + j * , , ,
Color(i / n, i / n, , ));
}
}
} canvas.drawLine(, , , , , , Color(, , , ));
canvas.drawCircle(Vec3(, , ), , Color(, , , )); canvas.fillCircle(Vec3(, , ), , , , , Color(, , , ));
canvas.fillCircle(Vec3(, , ), , , , , Color(, , , ));
canvas.fillCircle(Vec3(, , ), , , , , Color(, , , )); graphicsContext.flip();
}
}
return ;
}
运行程序的结果

源码地址:http://files.cnblogs.com/files/ForEmail5/Simple2D-03.rar
基于OpenGL编写一个简易的2D渲染框架-03 渲染基本几何图形的更多相关文章
- 基于OpenGL编写一个简易的2D渲染框架-05 渲染文本
		
阅读文章前需要了解的知识:文本渲染 https://learnopengl-cn.github.io/06%20In%20Practice/02%20Text%20Rendering/ 简要步骤: 获 ...
 - 基于OpenGL编写一个简易的2D渲染框架-06 编写一个粒子系统
		
在这篇文章中,我将详细说明如何编写一个简易的粒子系统. 粒子系统可以模拟许多效果,下图便是这次的粒子系统的显示效果.为了方便演示,就弄成了一个动图. 图中,同时显示了 7 种不同粒子效果,看上去效果挺 ...
 - 基于OpenGL编写一个简易的2D渲染框架-01 创建窗口
		
最近正在学习OpenGL,我认为学习的最快方法就是做一个小项目了. 如果对OpenGL感兴趣的话,这里推荐一个很好的学习网站 https://learnopengl-cn.github.io/ 我用的 ...
 - 基于OpenGL编写一个简易的2D渲染框架-04 绘制图片
		
阅读文章前需要了解的知识,纹理:https://learnopengl-cn.github.io/01%20Getting%20started/06%20Textures/ 过程简述:利用 FreeI ...
 - 基于OpenGL编写一个简易的2D渲染框架-02 搭建OpenGL环境
		
由于没有使用GLFW库,接下来得费一番功夫. 阅读这篇文章前请看一下这个网页:https://learnopengl-cn.github.io/01%20Getting%20started/02%20 ...
 - 基于OpenGL编写一个简易的2D渲染框架-11 重构渲染器-Renderer
		
假如要渲染一个纯色矩形在窗口上,应该怎么做? 先确定顶点的格式,一个顶点应该包含位置信息 vec3 以及颜色信息 vec4,所以顶点的结构体定义可以这样: struct Vertex { Vec3 p ...
 - 基于OpenGL编写一个简易的2D渲染框架-08 重构渲染器-整体架构
		
事实上,前面编写的渲染器 Renderer 非常简陋,虽然能够进行一些简单的渲染,但是它并不能满足我们的要求. 当渲染粒子系统时,需要开启混合模式,但渲染其他顶点时却不需要开启混合模式.所以同时渲染粒 ...
 - 基于OpenGL编写一个简易的2D渲染框架-09 重构渲染器-Shader
		
Shader 只是进行一些简单的封装,主要功能: 1.编译着色程序 2.绑定 Uniform 数据 3.根据着色程序的顶点属性传递顶点数据到 GPU 着色程序的编译 GLuint Shader::cr ...
 - 基于OpenGL编写一个简易的2D渲染框架-10 重构渲染器-Pass
		
Pass,渲染通路,一个渲染通路指的是一次像素处理和一次顶点处理,也就是指的是一次绘制.简单来说就是顶点数据在渲染管线中走一遍最后绘制. 渲染粒子系统的粒子时,需要开启 OpenGL 的混合模式,并使 ...
 
随机推荐
- ZH奶酪:Python使用ElementTree解析XML【译】
			
19.7. xml.etree.ElementTree — The ElementTree XML API 源代码: Lib/xml/etree/ElementTree.py Element类型是一种 ...
 - vault 集群搭建(active standby 模式)
			
参考架构图: consul server cluster 搭建 consul 基本配置格式 { "server": true, "node_name": ...
 - windows 版 nginx 运行错误的一些解决方法
			
1. 关于文件夹的中文的问题. 错误的截图如下: 看得到这个 failed (1113: No mapping for the Unicode character exists in the targ ...
 - 走进windows编程的世界-----消息处理函数(1)
			
Win32消息机制 过程驱动:程序是依照我们预先定义好的顺序运行.每运行一步,下一步都已经依照预定的顺序 继续运行,直至程序结束. 事件驱动:程序的运行顺序是无序的.某个时间点所运行的 ...
 - webpack的3个路径配置项: assetsRoot、assetsSubDirectory、assetsPublicPath
			
在 vue-cli 构建模版的配置文件config.js中有assetsRoot,assetsSubDirectory和assetsPublicPath这三个路径配置项 assetsRoot:构建输出 ...
 - Angular 4.0 使用第三方类库
			
使用第三方类库分为以下几步 1. 将第三方类库安装到本地 1) Jquery的命令 npm install jquery --save 2) 安装bootstrap 安装成功后,将文件下载到node_ ...
 - RK3399 Android7.1 try 'jack-diagnose' or see Jack server log
			
CPU:RK3399 系统:Android 7.1 Android 7.1系统使用 jack-server 作为 Java 代码编译器 jack-server 由两个配置文件来决定用户使用的端口 /h ...
 - bzoj4419 发微博
			
Description 刚开通的SH微博共有n个用户(1..n标号),在短短一个月的时间内,用户们活动频繁,共有m条按时间顺序的记录: ! x 表示用户x发了一条微博: + x y 表示用户x和用 ...
 - R语言学习——向量,矩阵
			
在R中,基本的数据结构有:向量,矩阵,数组,数据框,列表,因子,函数等. 向量:一系列同类型的有序元素构成. 向量是一维结构. 向量是R最简单的数据结构,在R中没有标量. 标量被看成1个元素的向量. ...
 - airtest IDE问题汇总
			
FAQ 1.同一个脚本,使用IDE可以运行,使用命令行运行报错 原因:曾经开启过anyproxy代理,添加过HTTP_PROXY环境变量,将其取消即可 unset HTTP_PROXY https:/ ...