Skip to content

Commit

Permalink
Improvements to selection drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
fo76utils committed Dec 26, 2024
1 parent 4a9a110 commit 876a8ad
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 203 deletions.
22 changes: 21 additions & 1 deletion res/shaders/selection.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,29 @@

in vec4 C;

// bit 0 = Scene::selecting
// bit 1 = vertex mode (drawing points instead of triangles)
// bits 8 to 15 = point size * 8 (0: do not draw smooth points)
uniform int selectionFlags;

out vec4 fragColor;

void main(void)
{
fragColor = C;
float a = C.a;

if ( ( selectionFlags & 2 ) != 0 ) {
// draw points as circles
vec2 d = gl_PointCoord - vec2( 0.5 );
float r = sqrt( dot( d, d ) );
if ( r > 0.5 )
discard;

// with anti-aliasing if enabled
int p = selectionFlags & 0xFF00;
if ( p != 0 )
a = a * clamp( ( 0.5 - r ) * float( p ) / 2048.0, 0.0, 1.0 );
}

fragColor = vec4( C.rgb, a );
}
24 changes: 22 additions & 2 deletions res/shaders/selection.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

uniform vec4 vertexColorOverride; // components greater than zero replace the vertex color
uniform vec4 highlightColor;

// bit 0 = Scene::selecting
// bit 1 = vertex mode (drawing points instead of triangles)
// bits 8 to 15 = point size * 8 (0: do not draw smooth points)
uniform int selectionFlags;
// if Scene::selecting == false: vertex selected (-1: none)
// if Scene::selecting == true: value to add to color key (e.g. shapeID << 16)
uniform int selectionParam;

uniform int numBones;
uniform mat4x3 boneTransforms[100];
Expand Down Expand Up @@ -43,7 +52,18 @@ void main( void )
}

v = modelViewMatrix * v;
gl_Position = projectionMatrix * v;
v = projectionMatrix * v;

if ( ( selectionFlags & 1 ) != 0 ) {
int colorKey = selectionParam + 1;
if ( ( selectionFlags & 2 ) != 0 )
colorKey = colorKey + gl_VertexID;
C = unpackUnorm4x8( uint( colorKey ) ) + vec4( 0.0005 );
} else if ( !( gl_VertexID == selectionParam && ( selectionFlags & 2 ) != 0 ) ) {
C = mix( vertexColor, vertexColorOverride, greaterThan( vertexColorOverride, vec4( 0.0 ) ) );
} else {
C = highlightColor;
}

C = mix( vertexColor, vertexColorOverride, greaterThan( vertexColorOverride, vec4( 0.0 ) ) );
gl_Position = v;
}
111 changes: 41 additions & 70 deletions src/gl/BSMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,19 @@ void BSMesh::drawShapes( NodeList * secondPass )
} else {
glDisable( GL_FRAMEBUFFER_SRGB );

FloatVector4 c( 0.0f, 0.0f, 0.0f, 1.0f );
if ( scene->isSelModeObject() )
c = getColorKeyFromID( nodeId );
if ( drawInSecondPass && scene->isSelModeVertex() ) {
glDisable( GL_POLYGON_OFFSET_FILL );

if ( !( drawInSecondPass && scene->isSelModeVertex() ) ) {
auto prog = context->useProgram( "selection.prog" );
drawVerts();

return;
}

auto prog = context->useProgram( "selection.prog" );
if ( prog ) {
setUniforms( prog );
setGLColor( c );
prog->uni1i( "selectionFlags", 0x0001 );
prog->uni1i( "selectionParam", ( scene->isSelModeObject() ? nodeId : -1 ) );
}
}

Expand Down Expand Up @@ -111,6 +116,7 @@ void BSMesh::drawSelection() const
glPolygonOffset(-1.0f, -2.0f);

setUniforms( prog );
prog->uni1i( "selectionFlags", 0 );

glPointSize( GLView::Settings::vertexPointSize );
glLineWidth( GLView::Settings::lineWidthWireframe );
Expand All @@ -124,22 +130,11 @@ void BSMesh::drawSelection() const
float normalScale = std::max< float >( boundSphere.radius / 20.0f, 1.0f / 512.0f );
qsizetype numTriangles = sortedTriangles.size();

// Draw All Verts lambda
auto allv = [this]( float size ) {
glPointSize( size );
glBegin( GL_POINTS );

for ( int j = 0; j < verts.size(); j++ )
glVertex( verts.value( j ) );

glEnd();
};

// Draw Lines lambda
auto lines = [this, &normalScale, &allv]( const QVector<Vector3> & v, int s, bool isBitangent = false ) {
auto lines = [this, &normalScale]( const QVector<Vector3> & v, int s, bool isBitangent = false ) {
glNormalColor();
if ( !isBitangent ) {
allv( GLView::Settings::tbnPointSize );
Shape::drawVerts( GLView::Settings::tbnPointSize, -1 );

glLineWidth( GLView::Settings::lineWidthWireframe * 0.78125f );
glBegin( GL_LINES );
Expand Down Expand Up @@ -191,23 +186,15 @@ void BSMesh::drawSelection() const
drawBox( boundsCenter - boundsDims, boundsCenter + boundsDims );
}
} else if ( n == "Vertices" || n == "UVs" || n == "UVs 2" || n == "Vertex Colors" || n == "Weights" ) {
f->glPointSize( GLView::Settings::vertexPointSize );
f->glDrawArrays( GL_POINTS, 0, GLsizei( verts.size() ) );

int s;
int s = -1;
if ( n == p && ( s = idx.row() ) >= 0 ) {
if ( n == "Weights" ) {
int weightsPerVertex = int( nif->get<quint32>(idx.parent().parent(), "Weights Per Vertex") );
if ( weightsPerVertex > 1 )
s /= weightsPerVertex;
}
glPointSize( GLView::Settings::vertexPointSizeSelected );
glDepthFunc( GL_ALWAYS );
glHighlightColor();
glBegin( GL_POINTS );
glVertex( verts.value( s ) );
glEnd();
}
Shape::drawVerts( GLView::Settings::vertexPointSize, s );
} else if ( n == "Normals" ) {
int s = -1;
if ( n == p )
Expand Down Expand Up @@ -348,53 +335,36 @@ int BSMesh::meshCount()

void BSMesh::drawVerts() const
{
glNormalColor();

glPointSize( GLView::Settings::vertexSelectPointSize );
glBegin( GL_POINTS );
for ( int i = 0; i < verts.size(); i++ ) {
if ( scene->selecting ) {
getColorKeyFromID( ( shapeNumber << 16 ) + i );
}
glVertex( verts.value(i) );
}
glEnd();

if ( scene->selecting || !( scene->currentBlock == iBlock ) )
return;

int vertexSelected = -1;

for ( const auto & idx = scene->currentIndex; idx.isValid(); ) {
// Name of this index
auto n = idx.data( NifSkopeDisplayRole ).toString();
if ( !( n == "Vertices" || n == "UVs" || n == "UVs 2" || n == "Vertex Colors"
|| n == "Normals" || n == "Tangents" || n == "Weights" ) ) {
break;
}
// Name of this index's parent
auto p = idx.parent().data( NifSkopeDisplayRole ).toString();
if ( n == p ) {
vertexSelected = idx.row();
if ( n == "Weights" ) {
auto nif = NifModel::fromValidIndex( idx );
int weightsPerVertex;
if ( nif && ( weightsPerVertex = nif->get<int>( idx.parent().parent(), "Weights Per Vertex" ) ) > 0 )
vertexSelected /= weightsPerVertex;
else
vertexSelected = -1;
if ( !scene->selecting && scene->currentBlock == iBlock ) {
for ( const auto & idx = scene->currentIndex; idx.isValid(); ) {
// Name of this index
auto n = idx.data( NifSkopeDisplayRole ).toString();
if ( !( n == "Vertices" || n == "UVs" || n == "UVs 2" || n == "Vertex Colors"
|| n == "Normals" || n == "Tangents" || n == "Weights" ) ) {
break;
}
// Name of this index's parent
auto p = idx.parent().data( NifSkopeDisplayRole ).toString();
if ( n == p ) {
vertexSelected = idx.row();
if ( n == "Weights" ) {
auto nif = NifModel::fromValidIndex( idx );
int weightsPerVertex = 0;
if ( nif )
weightsPerVertex = nif->get<int>( idx.parent().parent(), "Weights Per Vertex" );
if ( weightsPerVertex > 0 )
vertexSelected /= weightsPerVertex;
else
vertexSelected = -1;
}
}
break;
}
break;
}

if ( vertexSelected >= 0 && vertexSelected < verts.size() ) {
glHighlightColor();
glPointSize( GLView::Settings::vertexPointSizeSelected );
glBegin( GL_POINTS );
glVertex( verts.value(vertexSelected) );
glEnd();
}
Shape::drawVerts( GLView::Settings::vertexSelectPointSize, vertexSelected );
}

QModelIndex BSMesh::vertexAt( int c ) const
Expand Down Expand Up @@ -506,6 +476,7 @@ void BSMesh::updateData(const NifModel* nif)
sortedTriangles = mesh->triangles;
}
verts = mesh->positions;
removeInvalidIndices();
coords.resize( mesh->coords2.isEmpty() ? 1 : 2 );
coords.first() = mesh->coords1;
if ( !mesh->coords2.isEmpty() )
Expand Down
2 changes: 1 addition & 1 deletion src/gl/BSMesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BSMesh : public Shape

// Shape

void drawVerts() const override;
void drawVerts() const;
QModelIndex vertexAt(int) const override;

QVector<std::shared_ptr<MeshFile>> meshes;
Expand Down
Loading

0 comments on commit 876a8ad

Please sign in to comment.