Compare commits

..

No commits in common. "b37e5a0ee7bdb580e5bfcf617ff6944c18a107a8" and "5e4a893af11fa3aa34fae737d070a5eeb53f7f64" have entirely different histories.

11 changed files with 562 additions and 785 deletions

View File

@ -105,7 +105,6 @@
<None Include="Shaders\final.vert" /> <None Include="Shaders\final.vert" />
<None Include="Shaders\model.frag" /> <None Include="Shaders\model.frag" />
<None Include="Shaders\model.vert" /> <None Include="Shaders\model.vert" />
<None Include="Shaders\painting.comp" />
<None Include="Shaders\painting.frag" /> <None Include="Shaders\painting.frag" />
<None Include="Shaders\painting.vert" /> <None Include="Shaders\painting.vert" />
<None Include="Shaders\shader.frag" /> <None Include="Shaders\shader.frag" />

View File

@ -92,9 +92,6 @@
<None Include="Shaders\painting.vert"> <None Include="Shaders\painting.vert">
<Filter>Resource Files\Shaders</Filter> <Filter>Resource Files\Shaders</Filter>
</None> </None>
<None Include="Shaders\painting.comp">
<Filter>Resource Files\Shaders</Filter>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Camera.h"> <ClInclude Include="Camera.h">

View File

@ -9,6 +9,5 @@
<file>container.jpg</file> <file>container.jpg</file>
<file>Shaders/painting.frag</file> <file>Shaders/painting.frag</file>
<file>Shaders/painting.vert</file> <file>Shaders/painting.vert</file>
<file>Shaders/painting.comp</file>
</qresource> </qresource>
</RCC> </RCC>

View File

@ -6,7 +6,6 @@
#include <iostream> #include <iostream>
#include "PaintingMesh.h" #include "PaintingMesh.h"
#include <QtMath> #include <QtMath>
#include "BvhTree.h"
Model::Model(QString path, QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram) Model::Model(QString path, QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram)
: context(context) : context(context)
@ -105,7 +104,7 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
if (paintingProgram != nullptr && std::strcmp(str.C_Str(), "17876391417123941155.jpg") == 0) if (paintingProgram != nullptr && std::strcmp(str.C_Str(), "17876391417123941155.jpg") == 0)
{ {
qDebug() << str.C_Str() << "Replaced"; qDebug() << str.C_Str();
// 初始化网格 // 初始化网格
PaintingMesh* m_mesh = new PaintingMesh(glFunc, paintingProgram, model); PaintingMesh* m_mesh = new PaintingMesh(glFunc, paintingProgram, model);
// 遍历网格的每个顶点 // 遍历网格的每个顶点
@ -140,17 +139,17 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
} }
GLuint bvhChildren[] = { 7/*rootBVH长度*/,0/*与显存对齐*/, GLuint bvhChildren[] = {7/*rootBVH长度*/,0/*与显存对齐*/,
//root //root
1,2, 1,2,
3,4, 5,6, 3,4, 5,6,
7,0, 7,30. / 360 * 65536 + 1 * 65536 /*右儿子用来表示旋转角度和层数*/, 8,0, 7,0, 7,0, 7,30./360* 4294967296 /*右儿子用来表示旋转角度*/, 8,0, 7,0,
//elememt0 //elememt0
1,2, 1,2,
5 + 28/*contour索引由于contour不定长这里需要给到contour在elementIndex中位置*/,5 + 12/*style索引在elementData中位置*/, 3,4, 5+28/*contour索引由于contour不定长这里需要给到contour在elementIndex中位置*/,5+12/*style索引在elementData中位置*/, 3,4,
5 + 36,5 + 12, 5 + 32,5 + 12, 5+36,5+12, 5+32,5+12,
//elememt1 //elememt1
1 + 0/*line索引element中第几条*/,1 + 25 1+0/*line索引element中第几条*/,1 + 25
}; };
QVector4D bvhBound[] = { QVector4D bvhBound[] = {
@ -165,36 +164,14 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
//elememt1 //elememt1
QVector4D(-1,0,1,1), QVector4D(-1,0,1,1),
}; };
BvhTree bvhTree;
std::vector<QVector4D> initBound;
for (int i = 0; i < 2000; i++)
{
float x = (float)rand() / RAND_MAX * 2 - 1;
float y = (float)rand() / RAND_MAX * 2 - 1;
float z = 0.1+x;//(float)rand() / RAND_MAX * (0.1) + x;
float w = 0.1+y;//(float)rand() / RAND_MAX * (0.1) + y;
initBound.push_back(QVector4D(x,y,z,w));
}
/* 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.7, -0.8, 0.8, -0.7));
initBound.push_back(QVector4D(0.7, 0.7, 0.8, 0.8));*/
bvhTree.buildBvhTree(initBound.data(), initBound.size());
std::vector<GLuint> children;
std::vector<QVector4D> bounds;
bvhTree.getBvhArray(children, bounds);
qDebug() << children;
qDebug() << bounds;
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, children.size()*sizeof(GLuint), children.data(), GL_DYNAMIC_DRAW); glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER,sizeof(bvhChildren), bvhChildren, GL_DYNAMIC_DRAW);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glFunc->glGenBuffers(1, &m_mesh->bvhBoundSSBO); glFunc->glGenBuffers(1, &m_mesh->bvhBoundSSBO);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->bvhBoundSSBO); glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->bvhBoundSSBO);
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, bounds.size()*sizeof(QVector4D), bounds.data(), 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 elementOffset[] = { GLuint elementOffset[] = {

View File

@ -64,12 +64,6 @@ void RendererWidget::initializeGL()
if (!paintingProgramPtr->link()) if (!paintingProgramPtr->link())
qDebug() << "ERROR:" << paintingProgramPtr->log(); qDebug() << "ERROR:" << paintingProgramPtr->log();
paintingCompProgramPtr = new QOpenGLShaderProgram;
if (!paintingCompProgramPtr->addShaderFromSourceFile(QOpenGLShader::Compute, ":/Shaders/painting.comp"))
qDebug() << "ERROR:" << paintingCompProgramPtr->log();
if (!paintingCompProgramPtr->link())
qDebug() << "ERROR:" << paintingCompProgramPtr->log();
finalProgramPtr = new QOpenGLShaderProgram; finalProgramPtr = new QOpenGLShaderProgram;
if (!finalProgramPtr->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/Shaders/final.vert")) if (!finalProgramPtr->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/Shaders/final.vert"))
qDebug() << "ERROR:" << finalProgramPtr->log(); qDebug() << "ERROR:" << finalProgramPtr->log();
@ -78,13 +72,11 @@ void RendererWidget::initializeGL()
if (!finalProgramPtr->link()) if (!finalProgramPtr->link())
qDebug() << "ERROR:" << finalProgramPtr->log(); qDebug() << "ERROR:" << finalProgramPtr->log();
finalProgramPtr->bind(); finalProgramPtr->bind();
finalProgramPtr->setUniformValue("gBaseColor", 0); finalProgramPtr->setUniformValue("gBaseColor", 0);
finalProgramPtr->setUniformValue("gNormal", 1); finalProgramPtr->setUniformValue("gNormal", 1);
finalProgramPtr->setUniformValue("gPosition", 2); finalProgramPtr->setUniformValue("gPosition", 2);
finalProgramPtr->setUniformValue("gMetallicRoughness", 3); finalProgramPtr->setUniformValue("gMetallicRoughness", 3);
finalProgramPtr->setUniformValue("gPainting", 4);
finalProgramPtr->release(); finalProgramPtr->release();
model = new Model("Models/Sponza/Sponza.gltf", context(), modelProgramPtr, paintingProgramPtr); model = new Model("Models/Sponza/Sponza.gltf", context(), modelProgramPtr, paintingProgramPtr);
@ -139,14 +131,6 @@ void RendererWidget::paintGL()
fboPtr->release(); fboPtr->release();
} }
paintingCompProgramPtr->bind();
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, gbuffers[4]);
glBindImageTexture(0, gbuffers[0], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA8);
glBindImageTexture(1, gbuffers[3], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RG16);
glBindImageTexture(2, gbuffers[4], 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
glDispatchCompute(ceil(frameWidth / 8.), ceil(frameHeight / 8.), 1);
//QOpenGLFramebufferObject::blitFramebuffer(nullptr, QRect(0, 0, 2*width(), 2*height()), fboPtr, QRect(0, 0, 1156, 756)); //QOpenGLFramebufferObject::blitFramebuffer(nullptr, QRect(0, 0, 2*width(), 2*height()), fboPtr, QRect(0, 0, 1156, 756));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -158,7 +142,7 @@ void RendererWidget::paintGL()
finalProgramPtr->setUniformValueArray("lightPositions", lightPositions, 4); finalProgramPtr->setUniformValueArray("lightPositions", lightPositions, 4);
finalProgramPtr->setUniformValueArray("lightColors", lightColors, 4); finalProgramPtr->setUniformValueArray("lightColors", lightColors, 4);
QVector<GLuint> gbuffers = fboPtr->textures();
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, gbuffers[0]); glBindTexture(GL_TEXTURE_2D, gbuffers[0]);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
@ -167,8 +151,6 @@ void RendererWidget::paintGL()
glBindTexture(GL_TEXTURE_2D, gbuffers[2]); glBindTexture(GL_TEXTURE_2D, gbuffers[2]);
glActiveTexture(GL_TEXTURE3); glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, gbuffers[3]); glBindTexture(GL_TEXTURE_2D, gbuffers[3]);
glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, gbuffers[4]);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
finalProgramPtr->release(); finalProgramPtr->release();
} }
@ -183,18 +165,12 @@ void RendererWidget::resizeGL(int width, int height)
if (fboPtr != nullptr) if (fboPtr != nullptr)
delete fboPtr; delete fboPtr;
fboPtr = new QOpenGLFramebufferObject(frameWidth, frameHeight, QOpenGLFramebufferObject::Depth, GL_TEXTURE_2D); fboPtr = new QOpenGLFramebufferObject(frameWidth, frameHeight, QOpenGLFramebufferObject::Depth, GL_TEXTURE_2D);
if (fboPtr->bind()) fboPtr->bind();
{
fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RGB16F); fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RGB16F);
fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RGB16F); fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RGB16F);
fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RG16); fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RG);
fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RGBA32F); GLenum attachments[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
GLenum attachments[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3, GL_COLOR_ATTACHMENT4 }; glDrawBuffers(4, attachments);
glDrawBuffers(5, attachments);
gbuffers = fboPtr->textures();
fboPtr->release();
}
std::cout << "\033[?25l"; std::cout << "\033[?25l";
@ -207,7 +183,7 @@ void RendererWidget::timerEvent(QTimerEvent* event)
lastFrame = currentFrame; lastFrame = currentFrame;
static float accTime = 0, frameCnt = 0; static float accTime = 0,frameCnt = 0;
accTime += deltaTime; accTime += deltaTime;
frameCnt++; frameCnt++;
if (accTime > 1.) if (accTime > 1.)
@ -267,7 +243,7 @@ void RendererWidget::keyReleaseEvent(QKeyEvent* event)
void RendererWidget::wheelEvent(QWheelEvent* event) void RendererWidget::wheelEvent(QWheelEvent* event)
{ {
camera.ProcessMouseScroll(event->delta() / 15.); camera.ProcessMouseScroll(event->delta()/15.);
} }
void RendererWidget::focusInEvent(QFocusEvent* event) void RendererWidget::focusInEvent(QFocusEvent* event)

View File

@ -36,10 +36,8 @@ private:
float deltaTime; float deltaTime;
QOpenGLShaderProgram* modelProgramPtr = nullptr; QOpenGLShaderProgram* modelProgramPtr = nullptr;
QOpenGLShaderProgram* paintingProgramPtr = nullptr; QOpenGLShaderProgram* paintingProgramPtr = nullptr;
QOpenGLShaderProgram* paintingCompProgramPtr = nullptr;
QOpenGLShaderProgram* finalProgramPtr = nullptr; QOpenGLShaderProgram* finalProgramPtr = nullptr;
QOpenGLFramebufferObject* fboPtr = nullptr; QOpenGLFramebufferObject* fboPtr = nullptr;
QVector<GLuint> gbuffers;
QOpenGLBuffer quadVBO; QOpenGLBuffer quadVBO;
QOpenGLVertexArrayObject quadVAO; QOpenGLVertexArrayObject quadVAO;
Model* model; Model* model;

View File

@ -8,7 +8,7 @@ uniform sampler2D gBaseColor;
uniform sampler2D gNormal; uniform sampler2D gNormal;
uniform sampler2D gPosition; uniform sampler2D gPosition;
uniform sampler2D gMetallicRoughness; uniform sampler2D gMetallicRoughness;
uniform sampler2D gPainting;
// lights // lights
uniform vec3 lightPositions[4]; uniform vec3 lightPositions[4];
uniform vec3 lightColors[4]; uniform vec3 lightColors[4];
@ -108,6 +108,4 @@ void main()
color = pow(color, vec3(1.0/2.2)); color = pow(color, vec3(1.0/2.2));
FragColor = vec4(color, 1.0); FragColor = vec4(color, 1.0);
//FragColor = vec4(texture(gPainting, TexCoords).rgb, 1.0);
} }

View File

@ -9,7 +9,6 @@ layout (location = 0) out vec4 gBaseColor;
layout (location = 1) out vec3 gNormal; layout (location = 1) out vec3 gNormal;
layout (location = 2) out vec3 gPosition; layout (location = 2) out vec3 gPosition;
layout (location = 3) out vec2 gMetallicRoughness; layout (location = 3) out vec2 gMetallicRoughness;
layout(location = 4) out vec3 gPainting;
in vec2 TexCoords; in vec2 TexCoords;
in vec3 WorldPos; in vec3 WorldPos;
@ -43,6 +42,6 @@ void main()
gPosition = WorldPos; gPosition = WorldPos;
gNormal = getNormalFromMap(); gNormal = getNormalFromMap();
gMetallicRoughness = texture(texture_metallic_roughness, TexCoords).bg; gMetallicRoughness = texture(texture_metallic_roughness, TexCoords).bg;
gPainting = vec3(0);
} }

View File

@ -1,149 +0,0 @@
#version 450 core
layout (local_size_x = 8, local_size_y = 8) in;
layout(rgba8, binding = 0) uniform image2D gBaseColor;
layout(rg16, binding = 1) uniform image2D gMetallicRoughness;
layout(rgba32f, binding = 2) uniform image2D gPainting;
layout(std430, binding = 1) buffer bvhBuffer
{
uint bvhLength;
uvec2 bvhChildren[];
};
layout(std430, binding = 2) buffer bvhBoundBuffer
{
vec4 bvhBound[];
};
layout(std430, binding = 3) buffer elementOffsetBuffer
{
/**********************
** @x elementBvhRoot
** @y elementBvhLength
** @z pointsOffset
** @w linesOffset
**********************/
uvec4 elementOffset[];
};
layout(std430, binding = 4) buffer elementIndexBuffer
{
uint elementIndexs[]; //线和面
};
layout(std430, binding = 5) buffer elementDataBuffer
{
float elementData[]; //点和Style
};
const float PI = 3.14159265358979;
const uint STACK_SIZE = 30;
struct Stack
{
uint top;
uint data[STACK_SIZE];
bool empty()
{
return top == 0;
}
bool full()
{
return top == STACK_SIZE;
}
bool getTop(out uint x)
{
if (empty())
return false;
x = data[top - 1];
return true;
}
bool pop()
{
if (empty())
return false;
top--;
return true;
}
bool push(in uint x)
{
if (full())
return false;
data[top] = x;
top++;
return true;
}
} stack, elementStack;
void main()
{
ivec2 pixelLocation = ivec2(gl_GlobalInvocationID.xy);
if(imageLoad(gPainting, pixelLocation).r==0)
return;
imageStore(gMetallicRoughness, pixelLocation, vec4(0 /*金属度*/, 0.8 /*粗糙度*/, 0, 1));
vec2 uv = imageLoad(gPainting, pixelLocation).gb;
vec3 debugBVH = vec3(0);
bool debugHit = false;
stack.top = 0;
uint index = 0, visitTime = 0;
while (index < bvhLength || !stack.empty())
{
while (index < bvhLength)
{
visitTime++;
vec4 bound = bvhBound[index];
uint leftChild = bvhChildren[index].x;
if (leftChild >= bvhLength)
{
uint zIndex = bvhChildren[index].y / 65535;
float angle = (bvhChildren[index].y / 65535. - zIndex) * 2 * PI;
mat2 rotation = {{cos(angle), -sin(angle)}, {sin(angle), cos(angle)}};
vec2 localUV = uv - (bound.xy + bound.zw) / 2;
localUV = rotation * localUV;
localUV /= (bound.zw - bound.xy) / 2;
if (all(lessThan(vec2(-1), localUV)) && all(lessThan(localUV, vec2(1))))
{
if (zIndex == 1)
debugBVH = vec3(1, 0, 1);
uint elementIndex = leftChild - bvhLength;
debugBVH.bg += 0.5 * (localUV + vec2(1));
// debugBVH = vec3(0);
//debugHit = drawElement(leftChild - bvhLength, localUV, debugBVH) == vec3(1);
}
index = bvhLength;
}
else if (all(lessThan(bound.xy, uv)) && all(lessThan(uv, bound.zw)))
{
debugBVH.r += 0.2;
stack.push(index);
index = leftChild;
}
else
index = bvhLength;
}
if (!stack.empty())
{
stack.getTop(index);
stack.pop();
index = bvhChildren[index].y;
}
}
if (debugHit)
imageStore(gBaseColor, pixelLocation, vec4(vec3(1, 1, 0),1));
else
imageStore(gBaseColor, pixelLocation, vec4(debugBVH,1));
}

View File

@ -4,7 +4,6 @@ layout(location = 0) out vec4 gBaseColor;
layout(location = 1) out vec3 gNormal; layout(location = 1) out vec3 gNormal;
layout(location = 2) out vec3 gPosition; layout(location = 2) out vec3 gPosition;
layout(location = 3) out vec2 gMetallicRoughness; layout(location = 3) out vec2 gMetallicRoughness;
layout(location = 4) out vec3 gPainting;
in vec2 TexCoords; in vec2 TexCoords;
in vec3 WorldPos; in vec3 WorldPos;
@ -494,7 +493,7 @@ void mainImage(out vec3 fragColor, in vec2 fragCoord)
} }
/////////////////////////////// ///////////////////////////////
const uint STACK_SIZE = 40; const uint STACK_SIZE = 10;
struct Stack struct Stack
{ {
@ -533,9 +532,36 @@ struct Stack
} }
} stack, elementStack; } stack, elementStack;
vec3 drawElement(uint elementIndex, vec2 localUV, inout vec3 debugBVH = vec3(0)) void main()
{ {
vec4 elementColor = vec4(0); // gBaseColor = vec4(TexCoords,1,1);
// gBaseColor = vec4(240./255, 220./255,157./255,1);
// gBaseColor = vec4(bvh[0].bound==vec4(0,0,1,1));
vec2 uv = vec2(1., 1.) - TexCoords * 2;
vec3 debugBVH = vec3(0);
bool debugHit = false;
stack.top = 0;
uint index = 0;
while (index < bvhLength || !stack.empty())
{
while (index < bvhLength)
{
vec4 bound = bvhBound[index];
uint leftChild = bvhChildren[index].x;
if (leftChild >= bvhLength)
{
float angle = float(bvhChildren[index].y) / 4294967296. * 2 * PI;
mat2 rotation = {{cos(angle), -sin(angle)}, {sin(angle), cos(angle)}};
vec2 localUV = uv - (bound.xy + bound.zw) / 2;
localUV = rotation * localUV;
localUV /= (bound.zw - bound.xy) / 2;
if (all(lessThan(vec2(-1), localUV)) && all(lessThan(localUV, vec2(1))))
{
uint elementIndex = leftChild - bvhLength;
//debugBVH.bg += 0.5 * (localUV + vec2(1));
debugBVH = vec3(0);
////////////////////////////ͼԪÄÚBVH/////////////////////////////////
uvec4 currentOffset = elementOffset[elementIndex]; uvec4 currentOffset = elementOffset[elementIndex];
uint elementBvhRoot = currentOffset.x; uint elementBvhRoot = currentOffset.x;
@ -555,37 +581,34 @@ vec3 drawElement(uint elementIndex, vec2 localUV, inout vec3 debugBVH = vec3(0))
if (all(lessThan(bound.xy, localUV)) && all(lessThan(localUV, bound.zw))) if (all(lessThan(bound.xy, localUV)) && all(lessThan(localUV, bound.zw)))
{ {
if (leftChild >= elementBvhLength) if (leftChild >= elementBvhLength )
{ {
debugBVH.g += 0.5; debugBVH.g += 0.5;
uint styleIndex = bvhChildren[elementBvhRoot + elementBvhIndex].y - elementBvhLength;
// for(int i = 0; i<200;i++) uint styleIndex = bvhChildren[elementBvhRoot + elementBvhIndex].y-elementBvhLength;
if (elementData[styleIndex] == 0.) //Ãæ
if(elementData[styleIndex]==0. )//Ãæ
{ {
uint lineCount = elementIndexs[leftChild - elementBvhLength]; uint lineCount = elementIndexs[leftChild-elementBvhLength];
uint num_its = 0; uint num_its = 0;
for(uint contourIterator = leftChild-elementBvhLength+1; contourIterator<=leftChild-elementBvhLength+lineCount; contourIterator++ )
for (uint contourIterator = leftChild - elementBvhLength + 1;
contourIterator <= leftChild - elementBvhLength + lineCount; contourIterator++)
{ {
uint lineIndex = elementIndexs[contourIterator]; uint lineIndex = elementIndexs[contourIterator];
uint p0Index = elementIndexs[linesOffset + 4 * lineIndex]; uint p0Index = elementIndexs[linesOffset+4*lineIndex];
uint p1Index = elementIndexs[linesOffset + 4 * lineIndex + 1]; uint p1Index = elementIndexs[linesOffset+4*lineIndex+1];
uint p2Index = elementIndexs[linesOffset + 4 * lineIndex + 2]; uint p2Index = elementIndexs[linesOffset+4*lineIndex+2];
uint p3Index = elementIndexs[linesOffset + 4 * lineIndex + 3]; uint p3Index = elementIndexs[linesOffset+4*lineIndex+3];
vec2 p0 = vec2(elementData[pointsOffset + 2 * p0Index], vec2 p0 = vec2(elementData[pointsOffset+2*p0Index], elementData[pointsOffset+2*p0Index+1]);
elementData[pointsOffset + 2 * p0Index + 1]); vec2 p1 = vec2(elementData[pointsOffset+2*p1Index], elementData[pointsOffset+2*p1Index+1]);
vec2 p1 = vec2(elementData[pointsOffset + 2 * p1Index], vec2 p2 = vec2(elementData[pointsOffset+2*p2Index], elementData[pointsOffset+2*p2Index+1]);
elementData[pointsOffset + 2 * p1Index + 1]); vec2 p3 = vec2(elementData[pointsOffset+2*p3Index], elementData[pointsOffset+2*p3Index+1]);
vec2 p2 = vec2(elementData[pointsOffset + 2 * p2Index],
elementData[pointsOffset + 2 * p2Index + 1]);
vec2 p3 = vec2(elementData[pointsOffset + 2 * p3Index],
elementData[pointsOffset + 2 * p3Index + 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);
} }
@ -594,24 +617,31 @@ vec3 drawElement(uint elementIndex, vec2 localUV, inout vec3 debugBVH = vec3(0))
num_its += cubic_bezier_int_test(localUV, p0, p1, p2, p3); num_its += cubic_bezier_int_test(localUV, p0, p1, p2, p3);
} }
if (num_its % 2 == 1) if (num_its % 2 == 1)
{ {
elementColor = vec4(1); debugHit = true;
//debugHit = true;
} }
} }
else if (elementData[styleIndex] == 1) //Ïß else if(elementData[styleIndex]==1)//Ïß
{ {
} }
elementBvhIndex = elementBvhLength; elementBvhIndex = elementBvhLength;
} }
else else
{ {
// debugBVH.b += 0.2; //debugBVH.b += 0.2;
elementStack.push(elementBvhIndex); elementStack.push(elementBvhIndex);
elementBvhIndex = leftChild; elementBvhIndex = leftChild;
} }
} }
else else
elementBvhIndex = elementBvhLength; elementBvhIndex = elementBvhLength;
@ -623,57 +653,7 @@ vec3 drawElement(uint elementIndex, vec2 localUV, inout vec3 debugBVH = vec3(0))
elementBvhIndex = bvhChildren[elementBvhRoot + elementBvhIndex].y; elementBvhIndex = bvhChildren[elementBvhRoot + elementBvhIndex].y;
} }
} }
return elementColor.xyz; //////////////////////////////////////////////
}
void main()
{
gBaseColor = vec4( vec3(1,0,0),1 );
// mainImage(gBaseColor, vec2(1.,1.)-TexCoords);
gPosition = WorldPos;
gNormal = normalize(Normal);
// gMetallicRoughness = vec2(1, 46./255);
gMetallicRoughness = vec2(0 /*½ðÊô¶È*/, 0.8 /*´Ö²Ú¶È*/);
gPainting = vec3(1, vec2(1., 1.) - TexCoords * 2);
return;
// gBaseColor = vec4(TexCoords,1,1);
// gBaseColor = vec4(240./255, 220./255,157./255,1);
// gBaseColor = vec4(bvh[0].bound==vec4(0,0,1,1));
//vec2 uv = vec2(1., 1.) - TexCoords * 2;
vec2 uv = vec2(0., -0.7) ;
vec3 debugBVH = vec3(0);
bool debugHit = false;
stack.top = 0;
uint index = 0, visitTime = 0;
while (index < bvhLength || !stack.empty())
{
while (index < bvhLength)
{
visitTime++;
vec4 bound = bvhBound[index];
uint leftChild = bvhChildren[index].x;
if (leftChild >= bvhLength)
{
uint zIndex = bvhChildren[index].y / 65535;
float angle = (bvhChildren[index].y / 65535. - zIndex) * 2 * PI;
mat2 rotation = {{cos(angle), -sin(angle)}, {sin(angle), cos(angle)}};
vec2 localUV = uv - (bound.xy + bound.zw) / 2;
localUV = rotation * localUV;
localUV /= (bound.zw - bound.xy) / 2;
if (all(lessThan(vec2(-1), localUV)) && all(lessThan(localUV, vec2(1))))
{
if (zIndex == 1)
debugBVH = vec3(1, 0, 1);
uint elementIndex = leftChild - bvhLength;
debugBVH.bg += 0.5 * (localUV + vec2(1));
// debugBVH = vec3(0);
//debugHit = drawElement(leftChild - bvhLength, localUV, debugBVH) == vec3(1);
// mainImage(debugBVH,localUV); // mainImage(debugBVH,localUV);
@ -681,7 +661,7 @@ void main()
// for(uint i=elementOffset[elementIndex][1];i<elementOffset[elementIndex][2];i+=7) // for(uint i=elementOffset[elementIndex][1];i<elementOffset[elementIndex][2];i+=7)
// { // {
// if(tri_test(localUV, vec2(elementData[i],elementData[i+1]), // if(tri_test(localUV, vec2(elementData[i],elementData[i+1]),
// vec2(elementData[i+2],elementData[i+3]), vec2(elementData[i+4],elementData[i+5]), true)) //vec2(elementData[i+2],elementData[i+3]), vec2(elementData[i+4],elementData[i+5]), true))
// { // {
// if(elementData[i+6]==0) // if(elementData[i+6]==0)
// { // {
@ -690,13 +670,13 @@ void main()
// else if(elementData[i+6]==1) // else if(elementData[i+6]==1)
// { // {
// if(-bezier_sd(localUV, vec2(elementData[i],elementData[i+1]), // if(-bezier_sd(localUV, vec2(elementData[i],elementData[i+1]),
// vec2(elementData[i+2],elementData[i+3]), vec2(elementData[i+4],elementData[i+5]))>0) //vec2(elementData[i+2],elementData[i+3]), vec2(elementData[i+4],elementData[i+5]))>0)
// debugHit=true; // debugHit=true;
// } // }
// else // else
// { // {
// if(bezier_sd(localUV, vec2(elementData[i],elementData[i+1]), // if(bezier_sd(localUV, vec2(elementData[i],elementData[i+1]),
// vec2(elementData[i+2],elementData[i+3]), vec2(elementData[i+4],elementData[i+5]))>0) //vec2(elementData[i+2],elementData[i+3]), vec2(elementData[i+4],elementData[i+5]))>0)
// debugHit=true; // debugHit=true;
// } // }
// //
@ -722,10 +702,14 @@ void main()
index = bvhChildren[index].y; index = bvhChildren[index].y;
} }
} }
if (debugHit) if (debugHit)
gBaseColor = vec4(vec3(1, 1, 0), 1); gBaseColor = vec4(vec3(1, 1, 0), 1);
else else
gBaseColor = vec4(vec3(int(visitTime)-9), 1); gBaseColor = vec4(debugBVH, 1);
// gBaseColor = vec4( vec3(elementData[6]<0.5),1 );
// mainImage(gBaseColor, vec2(1.,1.)-TexCoords);
gPosition = WorldPos;
gNormal = normalize(Normal);
// gMetallicRoughness = vec2(1, 46./255);
gMetallicRoughness = vec2(0 /*½ðÊô¶È*/, 0.8 /*´Ö²Ú¶È*/);
} }

View File

@ -1,6 +1,5 @@
#include "MainWindow.h" #include "MainWindow.h"
#include <QtWidgets/QApplication> #include <QtWidgets/QApplication>
#include <QGuiApplication>
extern "C" { extern "C" {
_declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001; _declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;