diff --git a/ArchitectureColoredPainting/Model.cpp b/ArchitectureColoredPainting/Model.cpp index e333fbb..ea283e8 100644 --- a/ArchitectureColoredPainting/Model.cpp +++ b/ArchitectureColoredPainting/Model.cpp @@ -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); diff --git a/ArchitectureColoredPainting/PaintingMesh.cpp b/ArchitectureColoredPainting/PaintingMesh.cpp index 7ab1d7d..786ce7a 100644 --- a/ArchitectureColoredPainting/PaintingMesh.cpp +++ b/ArchitectureColoredPainting/PaintingMesh.cpp @@ -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); diff --git a/ArchitectureColoredPainting/PaintingMesh.h b/ArchitectureColoredPainting/PaintingMesh.h index 8d6fd40..a6d801e 100644 --- a/ArchitectureColoredPainting/PaintingMesh.h +++ b/ArchitectureColoredPainting/PaintingMesh.h @@ -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); diff --git a/ArchitectureColoredPainting/RendererWidget.cpp b/ArchitectureColoredPainting/RendererWidget.cpp index befee22..41be7b5 100644 --- a/ArchitectureColoredPainting/RendererWidget.cpp +++ b/ArchitectureColoredPainting/RendererWidget.cpp @@ -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; diff --git a/ArchitectureColoredPainting/RendererWidget.h b/ArchitectureColoredPainting/RendererWidget.h index c227506..68bb2d0 100644 --- a/ArchitectureColoredPainting/RendererWidget.h +++ b/ArchitectureColoredPainting/RendererWidget.h @@ -29,6 +29,7 @@ protected: void focusOutEvent(QFocusEvent* event) override; private: + int frameWidth, frameHeight; QSet pressedKeys; Camera camera; clock_t lastFrame; diff --git a/ArchitectureColoredPainting/Shaders/painting.frag b/ArchitectureColoredPainting/Shaders/painting.frag index 81285ae..676a8df 100644 --- a/ArchitectureColoredPainting/Shaders/painting.frag +++ b/ArchitectureColoredPainting/Shaders/painting.frag @@ -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(index0) + 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);