实现三种三角形的编码

dev-VirtualTexture
wuyize 2022-08-01 23:13:26 +08:00
parent 5fd42b6636
commit 781ec614bc
6 changed files with 63 additions and 15 deletions

View File

@ -143,10 +143,10 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
GLuint bvhChildren[] = {7/*Êý×鳤¶È*/,0/*ÓëÏÔ´æ¶ÔÆë*/,
1,2,
3,4, 5,6,
7,0, 7,45./360* 4294967296 , 7,0, 7,0};
7,0, 7,30./360* 4294967296 , 8,0, 7,0};
QVector4D bvhBound[] = { QVector4D(-1,-1,1,1) ,
QVector4D(0.1,0.1,0.4,0.9), QVector4D(0.6,0.1,0.9,0.9),
QVector4D(0.2,0.2,0.3,0.4), QVector4D(0.2,0.5,0.3,0.8), QVector4D(0.7,0.2,0.8,0.4), QVector4D(0.7,0.3,0.8,0.8) };
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) };
glFunc->glGenBuffers(1, &m_mesh->bvhSSBO);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->bvhSSBO);
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER,sizeof(bvhChildren), bvhChildren, GL_DYNAMIC_DRAW);
@ -157,8 +157,21 @@ Drawable* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 mod
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(bvhBound), bvhBound, GL_DYNAMIC_DRAW);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
GLuint elementIndex[] = { 1/*Êý×鳤¶È*/,
0,0,14,14,14,14,
14,14,21,21,21,21
};
glFunc->glGenBuffers(1, &m_mesh->elementIndexSSBO);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->elementIndexSSBO);
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(elementIndex), elementIndex, GL_DYNAMIC_DRAW);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
GLfloat elementData[] = { 1,2,3,4,5 };
GLfloat elementData[] = {
1,0, 0,1, -1,0, 1,
-1,0, 0,-1, 1,0, 0,
1,0, 0,1, -1,0, 2,
};
glFunc->glGenBuffers(1, &m_mesh->elementSSBO);
glFunc->glBindBuffer(GL_SHADER_STORAGE_BUFFER, m_mesh->elementSSBO);
glFunc->glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(elementData), elementData, GL_DYNAMIC_DRAW);

View File

@ -17,6 +17,7 @@ void PaintingMesh::draw()
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, bvhSSBO);
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, bvhBoundSSBO);
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, elementIndexSSBO);
glFunc->glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, elementSSBO);
glFunc->glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);

View File

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

View File

@ -155,15 +155,18 @@ void RendererWidget::paintGL()
void RendererWidget::resizeGL(int width, int height)
{
//qDebug() << devicePixelRatio() << width << height;
frameWidth = devicePixelRatioF() * width;
frameHeight = devicePixelRatioF() * height;
qDebug() << frameWidth << "x" << frameHeight;
//qDebug() << devicePixelRatioF() << width << height;
//glViewport(0, 0, (GLint)devicePixelRatio()*width, (GLint)devicePixelRatio()*height);
if (fboPtr != nullptr)
delete fboPtr;
fboPtr = new QOpenGLFramebufferObject(devicePixelRatio() * width, devicePixelRatio() * height, QOpenGLFramebufferObject::Depth, GL_TEXTURE_2D);
fboPtr = new QOpenGLFramebufferObject(frameWidth, frameHeight, QOpenGLFramebufferObject::Depth, GL_TEXTURE_2D);
fboPtr->bind();
fboPtr->addColorAttachment(devicePixelRatio() * width, devicePixelRatio() * height, GL_RGB16F);
fboPtr->addColorAttachment(devicePixelRatio() * width, devicePixelRatio() * height, GL_RGB16F);
fboPtr->addColorAttachment(devicePixelRatio() * width, devicePixelRatio() * height, GL_RG);
fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RGB16F);
fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RGB16F);
fboPtr->addColorAttachment(frameWidth, frameHeight, GL_RG);
GLenum attachments[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(4, attachments);
@ -183,6 +186,7 @@ void RendererWidget::timerEvent(QTimerEvent* event)
frameCnt++;
if (accTime > 1.)
{
std::cout << " \r";
std::cout << "FPS: " << frameCnt / accTime << "\r";
accTime = 0;
frameCnt = 0;

View File

@ -29,6 +29,7 @@ protected:
void focusOutEvent(QFocusEvent* event) override;
private:
int frameWidth, frameHeight;
QSet<int> pressedKeys;
Camera camera;
clock_t lastFrame;

View File

@ -18,10 +18,10 @@ layout(std430, binding = 2) buffer bvhBoundBuffer
{
vec4 bvhBound[];
};
layout(std430, binding = 3) buffer elementIndexBuffer
layout(std430, binding = 3) buffer elementOffsetBuffer
{
int elementCount;
int elementIndex[];
uint elementOffset[][6];
};
layout(std430, binding = 4) buffer elementBuffer
{
@ -32,7 +32,7 @@ const float PI = 3.14159265359;
////////////////////////////////////////////////////////////////////////////
float border;
float border=0;
mat2 inv(mat2 m){
return mat2(m[1][1],-m[0][1],-m[1][0],m[0][0])/(m[0][0]*m[1][1]-m[1][0]*m[0][1]);
@ -454,6 +454,7 @@ void main()
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())
@ -470,7 +471,32 @@ void main()
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));
for(uint i=elementOffset[elementIndex][1];i<elementOffset[elementIndex][2];i+=7)
{
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))
{
if(elementData[i+6]==0)
{
debugHit=true;
}
else if(elementData[i+6]==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)
debugHit=true;
}
else
{
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)
debugHit=true;
}
}
}
}
index=bvhLength;
}
else if(all(lessThan(bound.xy, uv))&&all(lessThan( uv, bound.zw)))
@ -488,8 +514,11 @@ void main()
index = bvhChildren[index].y;
}
}
gBaseColor = vec4( debugBVH,1 );
if(debugHit)
gBaseColor = vec4( vec3(1,1,0),1 );
else
gBaseColor = vec4( debugBVH,1 );
//gBaseColor = vec4( vec3(elementData[6]<0.5),1 );
//mainImage(gBaseColor, vec2(1.,1.)-TexCoords);
gPosition = WorldPos;
gNormal = normalize(Normal);