实现任意曲线围成封闭图形的渲染

dev-VirtualTexture
wuyize 2022-08-04 20:36:44 +08:00
parent 781ec614bc
commit 38bd57c4c9
5 changed files with 662 additions and 399 deletions

View File

@ -140,13 +140,31 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
} }
GLuint bvhChildren[] = {7/*数组长度*/,0/*与显存对齐*/, GLuint bvhChildren[] = {7/*rootBVH长度*/,0/*与显存对齐*/,
//root
1,2, 1,2,
3,4, 5,6, 3,4, 5,6,
7,0, 7,30./360* 4294967296 , 8,0, 7,0}; 7,0, 7,30./360* 4294967296 /*右儿子用来表示旋转角度*/, 8,0, 7,0,
QVector4D bvhBound[] = { QVector4D(-1,-1,1,1) , //elememt0
1,2,
5+28/*contour索引由于contour不定长这里需要给到contour在elementIndex中位置*/,5+12/*style索引在elementData中位置*/, 3,4,
5+36,5+12, 5+32,5+12,
//elememt1
1+0/*line索引element中第几条*/,1 + 25
};
QVector4D bvhBound[] = {
//root
QVector4D(-1,-1,1,1),
QVector4D(-0.9,-0.9,-0.1,0.9), QVector4D(0.1, -0.9,0.9,0.9), QVector4D(-0.9,-0.9,-0.1,0.9), QVector4D(0.1, -0.9,0.9,0.9),
QVector4D(-0.8,-0.8,-0.2,-0.1), QVector4D(-0.7,0.2,-0.2,0.7), QVector4D(0.2,-0.8,0.8,-0.1), QVector4D(0.2,0.1,0.8,0.8) }; QVector4D(-0.8,-0.8,-0.2,-0.1), QVector4D(-0.7,0.2,-0.2,0.7), QVector4D(0.2,-0.8,0.8,-0.1), QVector4D(0.2,0.1,0.8,0.8),
//elememt0
QVector4D(-1,-1,1,1),
QVector4D(-1,-0.5,1,1), QVector4D(-1,-1,1,0.5),
QVector4D(-1,-1,1,-0.5), QVector4D(-1,-0.5,1,0.5),
//elememt1
QVector4D(-1,0,1,1),
};
glFunc->glGenBuffers(1, &m_mesh->bvhSSBO); glFunc->glGenBuffers(1, &m_mesh->bvhSSBO);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->bvhSSBO); glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->bvhSSBO);
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER,sizeof(bvhChildren), bvhChildren, GL_DYNAMIC_DRAW); glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER,sizeof(bvhChildren), bvhChildren, GL_DYNAMIC_DRAW);
@ -157,9 +175,41 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(bvhBound), bvhBound, GL_DYNAMIC_DRAW); glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(bvhBound), bvhBound, GL_DYNAMIC_DRAW);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
GLuint elementIndex[] = { 1/*数组长度*/, GLuint elementOffset[] = {
0,0,14,14,14,14, //element0
14,14,21,21,21,21 7, //elementBvhRoot
5, //elementBvhLength
0, //pointsOffset
0, //linesOffset
//element1
12, //elementBvhRoot
1, //elementBvhLength
19, //pointsOffset
40, //linesOffset
};
glFunc->glGenBuffers(1, &m_mesh->elementOffsetSSBO);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->elementOffsetSSBO);
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(elementOffset), elementOffset, GL_DYNAMIC_DRAW);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
GLuint elementIndex[] = {
//element0
//lines, 全部当作三阶贝塞尔, 每条线四个点索引
4,2,2,0,
0,0,1,1,
1,1,4,4,
1,1,5,5,
4,4,5,5,
1,1,3,3,
3,3,5,5,
//contours, 第一个元素指明轮廓段数后面为lines索引
3, 0,1,2,
3, 2,3,4,
3, 3,5,6,
//element2
//lines
0,1,2
}; };
glFunc->glGenBuffers(1, &m_mesh->elementIndexSSBO); glFunc->glGenBuffers(1, &m_mesh->elementIndexSSBO);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->elementIndexSSBO); glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->elementIndexSSBO);
@ -168,12 +218,34 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
GLfloat elementData[] = { GLfloat elementData[] = {
1,0, 0,1, -1,0, 1, //element0
-1,0, 0,-1, 1,0, 0, //points
1,0, 0,1, -1,0, 2, -1,0.5, -1,-0.5, 0,1, 0,-1, 1,0.5, 1,-0.5,
//fillStyle
//fill
0,
//fillType
0, //单色
//fillColorMetallicRoughness
1,1,0, 0,0.8,
//element1
//points
-1,0.5, 0,1, 1,0.5,
//strokeStyle
//stroke
1,
//strokeWidth
0.02,
//strokeEndType
0, //圆角
//strokeFillType
0, //单色
//strokeFillColorMetallicRoughness
0,1,0, 0,0.8
}; };
glFunc->glGenBuffers(1, &m_mesh->elementSSBO); glFunc->glGenBuffers(1, &m_mesh->elementDataSSBO);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->elementSSBO); glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->elementDataSSBO);
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(elementData), elementData, GL_DYNAMIC_DRAW); glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(elementData), elementData, GL_DYNAMIC_DRAW);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

View File

@ -17,8 +17,9 @@ void PaintingMesh::draw()
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, bvhSSBO); glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, bvhSSBO);
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, bvhBoundSSBO); glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, bvhBoundSSBO);
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, elementIndexSSBO); glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, elementOffsetSSBO);
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, elementSSBO); glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, elementIndexSSBO);
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, elementDataSSBO);
glFunc->glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0); glFunc->glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);

View File

@ -42,7 +42,7 @@ public:
QOpenGLFunctions_4_5_Compatibility* glFunc; //opengl函数入口 QOpenGLFunctions_4_5_Compatibility* glFunc; //opengl函数入口
QOpenGLShaderProgram* shaderProgram; //着色器程序 QOpenGLShaderProgram* shaderProgram; //着色器程序
GLuint bvhSSBO, bvhBoundSSBO, elementIndexSSBO, elementSSBO; GLuint bvhSSBO, bvhBoundSSBO, elementOffsetSSBO, elementIndexSSBO, elementDataSSBO;
/* 函数 */ /* 函数 */
PaintingMesh(QOpenGLFunctions_4_5_Compatibility* glFunc, QOpenGLShaderProgram* shaderProgram, aiMatrix4x4 model); PaintingMesh(QOpenGLFunctions_4_5_Compatibility* glFunc, QOpenGLShaderProgram* shaderProgram, aiMatrix4x4 model);

View File

@ -3,11 +3,13 @@
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <QScreen>
#include <QGuiApplication>
RendererWidget::RendererWidget(QWidget* parent) RendererWidget::RendererWidget(QWidget* parent)
: QOpenGLWidget(parent), camera(QVector3D(0.0f, 100.0f, 0.0f)) : QOpenGLWidget(parent), camera(QVector3D(0.0f, 100.0f, 0.0f))
{ {
startTimer(1000 / 120); startTimer(1000 / QGuiApplication::primaryScreen()->refreshRate());
lastFrame = std::clock(); lastFrame = std::clock();
setFocusPolicy(Qt::StrongFocus); setFocusPolicy(Qt::StrongFocus);
} }
@ -177,7 +179,7 @@ void RendererWidget::resizeGL(int width, int height)
void RendererWidget::timerEvent(QTimerEvent* event) void RendererWidget::timerEvent(QTimerEvent* event)
{ {
clock_t currentFrame = std::clock(); clock_t currentFrame = std::clock();
deltaTime = (float)(std::clock() - lastFrame) / CLOCKS_PER_SEC; deltaTime = (float)(currentFrame - lastFrame) / CLOCKS_PER_SEC;
lastFrame = currentFrame; lastFrame = currentFrame;

File diff suppressed because it is too large Load Diff