基于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 的混合模式,并使 ...
随机推荐
- php 备份和恢复数据库
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- PHP 中的对象传递
<?php class A { public $age = 0; public $username = ""; public $obj = null; } $a = new ...
- bzoj1042硬币购物
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1042 dp预处理+容斥原理. 先预处理求出无限制的各面值的组成方案数 f (完全背包). 求s ...
- java操作Excel之POI(1)
一.新建工作簿.sheet.单元格 public static void main(String[] args) throws Exception { Workbook wb = new HSSFWo ...
- java web 程序---javabean代码,出现错误。奇怪,无法解释的运行问题
深夜吧.这个点11点半了 写了一个简单的javabean实例,发现没有任何代码书写的错误,但是问题就是程序运行会有问题,然后换一个包,重写一个,问题没了? 请问问题出现在哪里了?巧合?还是操作有误?这 ...
- 杂项:node.js
ylbtech-杂项:node.js Node.js是一个Javascript运行环境(runtime),发布于2009年5月,由Ryan Dahl开发,实质是对Chrome V8引擎进行了封装.No ...
- 1115 Counting Nodes in a BST (30 分)
1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...
- Web安全测试指南--会话管理
会话复杂度: 5.3.2.会话预测: 5.3.3.会话定置: 5.3.4.CSRF: 5.3.5.会话注销: 5.3.6.会话超时:
- linux nfs怪现象——软连接、文件属主的变更
怪现象:proxmox:/etc-asterisk# ls sip.confsip.confproxmox:/etc-asterisk# more sip.confsip.conf: No such ...
- javascript的防篡改对象之preventExtensions()方法
js在默认情况下,所有的对象都是可扩展的.这也是让很多开发人员头特疼的问题.因为在同一环境中,一不小心就会发生修改了不必要的对象,而自己却不知道. 在ECMAScript5可以解决这种问题了. pre ...