基于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 的混合模式,并使 ...
随机推荐
- restheart 基本使用
restheart 是一个方便基于mongodb的restapi 开发框架 参考项目 https://github.com/rongfengliang/restheart-docker-compose ...
- spring4 知识点
1 bean的 创建 1,直接在配置文件里面写一个带有@Bean注解的方法(返回值就是那个bena对象),(name等于 方法名) 2,使用 FactoryBean 接口(三个方法分别是创建,类型,单 ...
- JVM内存模型(二)
JVM为什么要区分为栈和堆? 栈代表的操作逻辑存储,堆代表的是数据逻辑存储,这样来划分更加清晰: JVM的内存在宏观上面来讲分为私有内存和共享内存:所谓共享内存(堆)寓意就是各个私有的栈(每个线程私有 ...
- jQuery如何取得HiddenField值(转)
<f:HiddenField runat="server" ID="cat_id" Text="ssss"/> var strD ...
- C# List的深复制(转)
C# List的深复制 1.关于深拷贝和浅拷贝 C#支持两种类型:值类型和引用类型 值类型(Value Type):如 char, int, float,枚举类型和结构类型 引用类型(Referenc ...
- 大快搜索DKhadoop集群管理平台添加节点的步骤说明
Hadoop作为搭建大数据处理平台的重要“基石”,关于它的分析和讲解的文章已经有很多了.Hadoop本身是一分布式的系统,因此在安装的时候,需要多每一个节点进行组建的安装.并且由于是开源软件,其安装过 ...
- 一个spring boot集成dubbo的小例子
请移步github,介绍和代码均在上面了:https://github.com/wuxun1997/voicebox 这里再多说两句.github上的这个小例子默认使用组播作为注册中心,你也可以把组播 ...
- 导入wordpress数据库到mysql报错
mysql字符集编码错误的导入数据会提示错误了,这个和插入数据一样如果保存的数据与mysql编码不一样那么肯定会出现导入乱码或插入数据丢失的问题,下面我们一起来看一个例子. 恢复数据库报错:由于字符集 ...
- Oracle学习操作(6)函数与存储过程
一.oracle自定义函数 1.不带参数的函数: 返回t_book表的总条数: SQL> create function getBookCount return number as begin ...
- HDU 2159 FATE (dp)
FATE Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissi ...