优化了着色器

dev-VirtualTexture
wuyize 2022-10-28 15:41:51 +08:00
parent 341a03be81
commit 4446ef73dd
6 changed files with 172 additions and 153 deletions

View File

@ -147,13 +147,13 @@
<None Include="Shaders\ssgi.comp" /> <None Include="Shaders\ssgi.comp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<QtMoc Include="src\MainWindow.h" />
<ClInclude Include="src\SvgParser.h" />
<QtMoc Include="src\TitleWidget.h" /> <QtMoc Include="src\TitleWidget.h" />
<QtMoc Include="src\IconWidget.h" /> <QtMoc Include="src\IconWidget.h" />
<QtMoc Include="src\CaptionButton.h" /> <QtMoc Include="src\CaptionButton.h" />
<QtMoc Include="src\NavigationBarWidget.h" /> <QtMoc Include="src\NavigationBarWidget.h" />
<QtMoc Include="src\Renderer\RendererWidget.h" /> <QtMoc Include="src\Renderer\RendererWidget.h" />
<ClInclude Include="src\SvgParser.h" />
<QtMoc Include="src\MainWindow.h" />
<QtMoc Include="src\Editor\EditorWidget.h" /> <QtMoc Include="src\Editor\EditorWidget.h" />
<ClInclude Include="src\Renderer\BvhTree.h" /> <ClInclude Include="src\Renderer\BvhTree.h" />
<ClInclude Include="src\Renderer\Camera.h" /> <ClInclude Include="src\Renderer\Camera.h" />

View File

@ -122,9 +122,6 @@
<QtMoc Include="src\Editor\EditorWidget.h"> <QtMoc Include="src\Editor\EditorWidget.h">
<Filter>Header Files\Editor</Filter> <Filter>Header Files\Editor</Filter>
</QtMoc> </QtMoc>
<QtMoc Include="src\MainWindow.h">
<Filter>Source Files</Filter>
</QtMoc>
<QtMoc Include="src\Renderer\RendererWidget.h"> <QtMoc Include="src\Renderer\RendererWidget.h">
<Filter>Header Files\Renderer</Filter> <Filter>Header Files\Renderer</Filter>
</QtMoc> </QtMoc>
@ -140,6 +137,9 @@
<QtMoc Include="src\TitleWidget.h"> <QtMoc Include="src\TitleWidget.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</QtMoc> </QtMoc>
<QtMoc Include="src\MainWindow.h">
<Filter>Header Files</Filter>
</QtMoc>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="Shaders\shader.frag"> <None Include="Shaders\shader.frag">
@ -246,7 +246,7 @@
<Filter>Header Files\Renderer</Filter> <Filter>Header Files\Renderer</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\SvgParser.h"> <ClInclude Include="src\SvgParser.h">
<Filter>Source Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -32,7 +32,7 @@ layout(std430, binding = 4) buffer elementOffsetBuffer
** @[2] pointsOffset ** @[2] pointsOffset
** @[3] linesOffset ** @[3] linesOffset
**********************/ **********************/
uint elementOffset[][4]; uvec4 elementOffset[];
}; };
layout(std430, binding = 5) buffer elementIndexBuffer layout(std430, binding = 5) buffer elementIndexBuffer
{ {
@ -45,7 +45,7 @@ layout(std430, binding = 6) buffer elementDataBuffer
const float PI = 3.14159265358979; const float PI = 3.14159265358979;
const uint STACK_SIZE = 16; const uint STACK_SIZE = 10;
struct Stack struct Stack
{ {
@ -716,7 +716,7 @@ bool drawElement(uint elementIndex, vec2 localUV, out vec3 color, out vec2 metal
bool hitElement = false; bool hitElement = false;
vec4 elementColor = vec4(-1); vec4 elementColor = vec4(-1);
uint currentOffset[4] = elementOffset[elementIndex]; uvec4 currentOffset = elementOffset[elementIndex];
uint elementBvhRoot = currentOffset[0]; uint elementBvhRoot = currentOffset[0];
uint elementBvhLength = currentOffset[1]; uint elementBvhLength = currentOffset[1];
uint pointsOffset = currentOffset[2]; uint pointsOffset = currentOffset[2];
@ -752,32 +752,38 @@ bool drawElement(uint elementIndex, vec2 localUV, out vec3 color, out vec2 metal
for ( uint contourIterator = contourIndex + 1;contourIterator < contourIndex + 1 + lineCount; contourIterator++) for ( uint contourIterator = contourIndex + 1;contourIterator < contourIndex + 1 + lineCount; contourIterator++)
{ {
uint lineIndex = elementIndexs[contourIterator]; uint lineIndex = elementIndexs[contourIterator];
uint p0Index = elementIndexs[linesOffset + 4 * lineIndex]; uint pLocation = linesOffset + 4 * lineIndex;
uint p1Index = elementIndexs[linesOffset + 4 * lineIndex + 1]; uvec4 pxIndex = uvec4(pointsOffset)+2*uvec4(elementIndexs[pLocation], elementIndexs[pLocation+1], elementIndexs[pLocation+2], elementIndexs[pLocation+3]);
uint p2Index = elementIndexs[linesOffset + 4 * lineIndex + 2]; uvec4 pyIndex = uvec4(1)+pxIndex;
uint p3Index = elementIndexs[linesOffset + 4 * lineIndex + 3]; // vec2 p0 = vec2(elementData[pxIndex[0]],
// elementData[pyIndex[0]]);
vec2 p0 = vec2(elementData[pointsOffset + 2 * p0Index], // vec2 p1 = vec2(elementData[pxIndex[1]],
elementData[pointsOffset + 2 * p0Index + 1]); // elementData[pyIndex[1]]);
vec2 p1 = vec2(elementData[pointsOffset + 2 * p1Index], // vec2 p2 = vec2(elementData[pxIndex[2]],
elementData[pointsOffset + 2 * p1Index + 1]); // elementData[pyIndex[2]]);
vec2 p2 = vec2(elementData[pointsOffset + 2 * p2Index], // vec2 p3 = vec2(elementData[pxIndex[3]],
elementData[pointsOffset + 2 * p2Index + 1]); // elementData[pyIndex[3]]);
vec2 p3 = vec2(elementData[pointsOffset + 2 * p3Index], mat4x2 p = mat4x2(elementData[pxIndex[0]], elementData[pyIndex[0]],
elementData[pointsOffset + 2 * p3Index + 1]); elementData[pxIndex[1]], elementData[pyIndex[1]],
if( bound.z==p0.x && distance(localUV, p3)<0.01) elementData[pxIndex[2]],elementData[pyIndex[2]],
elementData[pxIndex[3]], elementData[pyIndex[3]]);
// vec2 p[4] = {vec2(elementData[pxIndex[0]], elementData[pyIndex[0]]),
// vec2(elementData[pxIndex[1]], elementData[pyIndex[1]]),
// vec2(elementData[pxIndex[2]],elementData[pyIndex[2]]),
// vec2(elementData[pxIndex[3]], elementData[pyIndex[3]])};
if( bound.z==p[0].x && distance(localUV, p[3])<0.01)
{ {
debugBVH = vec3(0,0,1); debugBVH = vec3(0,0,1);
} }
else if(distance(localUV, p0)<0.01) else if(distance(localUV, p[0])<0.01)
debugBVH = vec3(1,1,1); debugBVH = vec3(1,1,1);
if (p0 == p1 && p2 == p3) // if (p0 == p1 && p2 == p3)
{ // {
num_its += segment_int_test(localUV, p0, p3); // num_its += segment_int_test(localUV, p0, p3);
} // }
//
else // else
num_its += cubic_bezier_int_test(localUV, p0, p1, p2, p3); num_its += cubic_bezier_int_test(localUV, p[0], p[1], p[2], p[3]);
} }
if (num_its % 2 == 1 && elementColor.a<1) if (num_its % 2 == 1 && elementColor.a<1)

View File

@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include "captionbutton.h" #include "CaptionButton.h"
CaptionButton::CaptionButton(QWidget *parent) : QWidget(parent) CaptionButton::CaptionButton(QWidget *parent) : QWidget(parent)
{ {

View File

@ -20,6 +20,8 @@ struct BvhTreeData {
bound.x(), bound.y(), bound.z(), bound.w(), bound.x(), bound.y(), bound.z(), bound.w(),
leftSon, rightSon); leftSon, rightSon);
} }
BvhTreeData(QVector4D bound, GLuint leftSon, GLuint rightSon)
: bound(bound), leftSon(leftSon), rightSon(rightSon) {}
BvhTreeData() BvhTreeData()
: leftSon(0), rightSon(0) {} : leftSon(0), rightSon(0) {}
~BvhTreeData() {} ~BvhTreeData() {}

View File

@ -121,7 +121,10 @@ GLuint encodeChild(GLuint index)
{ {
return 0x80000000 + index; return 0x80000000 + index;
} }
GLuint encodeZIndexAngle(GLuint zIndex, float angle)
{
return GLuint(angle / 360 * 65536 + zIndex * 65536);
}
Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 model) Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 model)
{ {
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex]; aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
@ -173,21 +176,23 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
} }
} }
BvhTree bvhTree; BvhTree rootBvhTree;
std::vector<QVector4D> initBound; vector<BvhTreeData> rootBvhTreeData;
for (int i = 0; i < 30000; i++) for (int i = 0; i < 1000; i++)
{ {
float x = (float)rand() / RAND_MAX * 2 - 1; float x = (float)rand() / RAND_MAX * 2 - 1;
float y = (float)rand() / RAND_MAX * 2 - 1; float y = (float)rand() / RAND_MAX * 2 - 1;
float z = 0.01 + x;//(float)rand() / RAND_MAX * (0.1) + x; float z = 0.05 + x;//(float)rand() / RAND_MAX * (0.1) + x;
float w = 0.01 + y;//(float)rand() / RAND_MAX * (0.1) + y; float w = 0.05 + y;//(float)rand() / RAND_MAX * (0.1) + y;
initBound.push_back(QVector4D(x, y, z, w)); rootBvhTreeData.push_back(BvhTreeData( QVector4D(x, y, z, w), 0, encodeZIndexAngle(1, (float)rand() / RAND_MAX * 360)));
} }
//rootBvhTreeData.push_back(BvhTreeData(QVector4D(-0.8, -0.8, -0.2, -0.1), 0, 0));
//rootBvhTreeData.push_back(BvhTreeData(QVector4D(-0.7, -0.8, -0.2, -0.1), 0, 0));
/* initBound.push_back(QVector4D(-0.8, -0.8, -0.7, -0.7)); /* initBound.push_back(QVector4D(-0.8, -0.8, -0.7, -0.7));
initBound.push_back(QVector4D(-0.8, 0.7, -0.7, 0.8)); initBound.push_back(QVector4D(-0.8, 0.7, -0.7, 0.8));
initBound.push_back(QVector4D(0.7, -0.8, 0.8, -0.7)); initBound.push_back(QVector4D(0.7, -0.8, 0.8, -0.7));
initBound.push_back(QVector4D(0.7, 0.7, 0.8, 0.8));*/ initBound.push_back(QVector4D(0.7, 0.7, 0.8, 0.8));*/
//bvhTree.buildBvhTree(initBound.data(), initBound.size()); rootBvhTree.buildBvhTree(rootBvhTreeData.data(), rootBvhTreeData.size());
@ -212,45 +217,51 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
//vector<vector<Point>> lineSet = { {Point{-1,-1}, Point{1,-1}},{Point{1,-1}, Point{1,1}}, {Point{1,1}, Point{-1,1}},{Point{-1,1}, Point{-1,-1}} }; //vector<vector<Point>> lineSet = { {Point{-1,-1}, Point{1,-1}},{Point{1,-1}, Point{1,1}}, {Point{1,1}, Point{-1,1}},{Point{-1,1}, Point{-1,-1}} };
//vector<vector<Point>> lineSet = { {Point{-1,-1}, Point{1,-1}},{Point{1,-1}, Point{1,1}}, {Point{1,1}, Point{-1,1}},{Point{-1,1}, Point{-1,-1}} }; //vector<vector<Point>> lineSet = { {Point{-1,-1}, Point{1,-1}},{Point{1,-1}, Point{1,1}}, {Point{1,1}, Point{-1,1}},{Point{-1,1}, Point{-1,-1}} };
ShortCutTree shortCutTree(100); ShortCutTree shortCutTree(20);
shortCutTree.buildShortCutTree(lineSet); shortCutTree.buildShortCutTree(lineSet);
vector<float> pointVector; vector<float> pointVector;
vector<GLuint> lineVector; vector<GLuint> lineVector;
vector<BvhTreeData> bvhTreeData = shortCutTree.getPointLineAndBvhTree(pointVector, lineVector); vector<BvhTreeData> bvhTreeData = shortCutTree.getPointLineAndBvhTree(pointVector, lineVector);
qDebug() << pointVector; qDebug() << "----------------------------------------------";
qDebug() << lineVector; qDebug() << "element0CellNum: " << bvhTreeData.size();
for (BvhTreeData data : bvhTreeData) //qDebug() << pointVector;
//qDebug() << lineVector;
/*for (BvhTreeData data : bvhTreeData)
{ {
data.show(); data.show();
} }*/
BvhTree element0Bvh;
bvhTree.buildBvhTree(bvhTreeData.data(), bvhTreeData.size()); element0Bvh.buildBvhTree(bvhTreeData.data(), bvhTreeData.size());
std::vector<GLuint> children; std::vector<GLuint> element0Children;
std::vector<QVector4D> bounds; std::vector<QVector4D> element0Bounds;
bvhTree.getBvhArray(children, bounds); element0Bvh.getBvhArray(element0Children, element0Bounds);
qDebug() << children; //qDebug() << element0Children;
qDebug() << bounds; //qDebug() << element0Bounds;
std::vector<GLuint> bvhChildren = {
//root
1,2,
3,4, 5,6,
encodeChild(0),0, encodeChild(0),GLuint(30. / 360 * 65536 + 1 * 65536) /*右儿子用来表示旋转角度和zIndex*/, encodeChild(0),0, encodeChild(0),0
};
std::vector<QVector4D> bvhBounds = {
//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.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),
};
bvhChildren.insert(bvhChildren.end(), children.begin(), children.end());
bvhBounds.insert(bvhBounds.end(), bounds.begin(), bounds.end());
//std::vector<GLuint> bvhChildren = {
// //root
// 1,2,
// 3,4, 5,6,
// encodeChild(0),0, encodeChild(0),GLuint(30. / 360 * 65536 + 1 * 65536) /*右儿子用来表示旋转角度和zIndex*/, encodeChild(0),0, encodeChild(0),0
//};
//std::vector<QVector4D> bvhBounds = {
// //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.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),
//};
std::vector<GLuint> bvhChildren;
std::vector<QVector4D> bvhBounds;
rootBvhTree.getBvhArray(bvhChildren, bvhBounds);
//qDebug() << bvhChildren;
bvhChildren.insert(bvhChildren.end(), element0Children.begin(), element0Children.end());
bvhBounds.insert(bvhBounds.end(), element0Bounds.begin(), element0Bounds.end());
std::vector<GLuint> elementOffset = { std::vector<GLuint> elementOffset = {
//element0 //element0
7, //elementBvhRoot rootBvhTree.getBvhNodeNum(), //elementBvhRoot
(GLuint)bounds.size(), //elementBvhLength (GLuint)element0Bounds.size(), //elementBvhLength
0, //pointsOffset 0, //pointsOffset
0, //linesOffset 0, //linesOffset
}; };
@ -265,99 +276,99 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
240/255.,220/255.,160/255., 0.996,0.18, 240/255.,220/255.,160/255., 0.996,0.18,
}; };
elementData.insert(elementData.begin(), pointVector.begin(), pointVector.end()); elementData.insert(elementData.begin(), pointVector.begin(), pointVector.end());
qDebug() << elementIndex; //qDebug() << elementIndex;
qDebug() << elementData; //qDebug() << elementData;
std::vector<GLuint> bvhChildren0 = { //std::vector<GLuint> bvhChildren0 = {
//root // //root
1,2, // 1,2,
3,4, 5,6, // 3,4, 5,6,
encodeChild(0),0, encodeChild(0),GLuint(30. / 360 * 65536 + 1 * 65536) /*右儿子用来表示旋转角度和zIndex*/, encodeChild(1),0, encodeChild(0),0, // encodeChild(0),0, encodeChild(0),GLuint(30. / 360 * 65536 + 1 * 65536) /*右儿子用来表示旋转角度和zIndex*/, encodeChild(1),0, encodeChild(0),0,
//elememt0 // //elememt0
1,2, // 1,2,
encodeChild(20)/*contour索引由于contour不定长这里需要给到contour在elementIndex中位置*/,14/*style索引在elementData中位置*/, encodeChild(24), 14, // encodeChild(20)/*contour索引由于contour不定长这里需要给到contour在elementIndex中位置*/,14/*style索引在elementData中位置*/, encodeChild(24), 14,
//elememt1 // //elememt1
encodeChild(0)/*line索引element中第几条*/, 27 // encodeChild(0)/*line索引element中第几条*/, 27
}; //};
std::vector<QVector4D> bvhBounds0 = { //std::vector<QVector4D> bvhBounds0 = {
//root // //root
QVector4D(-1,-1,1,1), // 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 // //elememt0
QVector4D(-1,-1,1,1), // QVector4D(-1,-1,1,1),
QVector4D(-1,-1,-0.2,1), QVector4D(-0.2,-1,1,1), // QVector4D(-1,-1,-0.2,1), QVector4D(-0.2,-1,1,1),
//elememt1 // //elememt1
QVector4D(-1,-1,1,1) // QVector4D(-1,-1,1,1)
}; //};
std::vector<GLuint> elementOffset0 = { //std::vector<GLuint> elementOffset0 = {
//element0 // //element0
7, //elementBvhRoot // 7, //elementBvhRoot
3, //elementBvhLength // 3, //elementBvhLength
0, //pointsOffset // 0, //pointsOffset
0, //linesOffset // 0, //linesOffset
//element1 // //element1
10, //elementBvhRoot // 10, //elementBvhRoot
1, //elementBvhLength // 1, //elementBvhLength
21, //pointsOffset // 21, //pointsOffset
28, //linesOffset // 28, //linesOffset
}; //};
std::vector<GLuint> elementIndex0 = { //std::vector<GLuint> elementIndex0 = {
//element0 // //element0
//lines, 全部当作三阶贝塞尔, 每条线四个点索引 // //lines, 全部当作三阶贝塞尔, 每条线四个点索引
0,1,1,2, // 0,1,1,2,
0,0,3,3, // 0,0,3,3,
3,4,4,5, // 3,4,4,5,
2,2,5,5, // 2,2,5,5,
5,5,6,6, // 5,5,6,6,
//contours, 第一个元素为线数,后面为轮廓线索引 // //contours, 第一个元素为线数,后面为轮廓线索引
3, 1,2,4, // 3, 1,2,4,
3, 0,2,3, // 3, 0,2,3,
//element1 // //element1
//lines // //lines
0,1,1,2 // 0,1,1,2
}; //};
std::vector<GLfloat> elementData0 = { //std::vector<GLfloat> elementData0 = {
//element0 // //element0
//points // //points
-0.2,1, -0.2,-0.2, 1,-0.2, -1,1, -1,-1, 1,-1, 1,1, // -0.2,1, -0.2,-0.2, 1,-0.2, -1,1, -1,-1, 1,-1, 1,1,
//fillStyle // //fillStyle
//fill // //fill
0, // 0,
//fillType // //fillType
0, //单色 // 0, //单色
//fillColorMetallicRoughness // //fillColorMetallicRoughness
1,1,0, 0,0.8, // 1,1,0, 0,0.8,
//element1 // //element1
//points // //points
0,0.8, 1,0, 0,-0.8, // 0,0.8, 1,0, 0,-0.8,
//strokeStyle // //strokeStyle
//stroke // //stroke
1, // 1,
//strokeWidth // //strokeWidth
0.02, // 0.02,
//strokeEndType // //strokeEndType
0, //圆角 // 0, //圆角
//strokeFillType // //strokeFillType
0, //单色 // 0, //单色
//线类型 // //线类型
1, //左侧 // 1, //左侧
//线外描边宽度 // //线外描边宽度
0, // 0,
//线外描边方式 // //线外描边方式
0, //单色 // 0, //单色
//strokeFillColorMetallicRoughness // //strokeFillColorMetallicRoughness
1,0,1, 0,0.8 // 1,0,1, 0,0.8
}; //};
//m_mesh->paintingIndex = paintingHelper->addPainting(bounds.size(), std::vector<GLuint>(children.begin()+2, children.end()), bounds, //m_mesh->paintingIndex = paintingHelper->addPainting(bounds.size(), std::vector<GLuint>(children.begin()+2, children.end()), bounds,
// elementOffset, elementIndex, elementData); // elementOffset, elementIndex, elementData);
m_mesh->paintingIndex = paintingHelper->addPainting(7, bvhChildren, bvhBounds, m_mesh->paintingIndex = paintingHelper->addPainting(rootBvhTree.getBvhNodeNum(), bvhChildren, bvhBounds,
elementOffset, elementIndex, elementData); elementOffset, elementIndex, elementData);
m_mesh->setupMesh(); m_mesh->setupMesh();