Skip to content

Commit

Permalink
Moved gltools functions to Scene, work in progress drawing code overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
fo76utils committed Jan 3, 2025
1 parent 8941f79 commit acf10ee
Show file tree
Hide file tree
Showing 14 changed files with 853 additions and 660 deletions.
7 changes: 5 additions & 2 deletions src/gl/BSMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,11 @@ void BSMesh::drawSelection() const
t.scale = 1.0f / t.scale;
sph.radius *= t.scale;
sph.center = t * sph.center;
glColor4f( 1, 1, 1, 0.33f );
drawSphereSimple( sph.center, sph.radius, 72 );
scene->setGLColor( 1.0f, 1.0f, 1.0f, 0.33f );
scene->setGLLineParams( GLView::Settings::lineWidthWireframe );
scene->setModelViewMatrix( viewTrans() );
scene->drawSphereSimple( sph.center, sph.radius, 72 );
bindShape();
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/gl/bsshape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ void BSShape::drawSelection() const
float pbvR = nif->get<float>( nif->getIndex( iBSphere, 1, 2 ) );

if ( pbvR > 0.0 ) {
glColor4f( 0, 1, 0, 0.33f );
drawSphereSimple( pbvC, pbvR, 72 );
scene->setGLColor( 0.0f, 1.0f, 0.0f, 0.33f );
scene->drawSphereSimple( pbvC, pbvR, 72 );
bindShape();
}

glPopMatrix();
Expand All @@ -455,8 +456,9 @@ void BSShape::drawSelection() const
glMultMatrix( scene->view * t );

if ( bvR > 0.0 ) {
glColor4f( 1, 1, 1, 0.33f );
drawSphereSimple( Vector3( 0, 0, 0 ), bvR, 72 );
scene->setGLColor( 1.0f, 1.0f, 1.0f, 0.33f );
scene->drawSphereSimple( Vector3( 0, 0, 0 ), bvR, 72 );
bindShape();
}

glPopMatrix();
Expand Down
76 changes: 37 additions & 39 deletions src/gl/glcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,24 @@ void NifSkopeOpenGLContext::setGlobalUniforms()
fn->glUseProgram( 0 );
}

void NifSkopeOpenGLContext::setDefaultVertexAttribs( std::uint64_t attrMask, const float * const * attrData )
{
fn->glBindVertexArray( 0 );
for ( size_t i = 0; attrMask; i++, attrMask = attrMask >> 4 ) {
size_t n = attrMask & 15;
if ( !n )
continue;
if ( n >= 4 )
vertexAttrib4fv( GLuint( i ), attrData[i] );
else if ( n == 3 )
vertexAttrib3fv( GLuint( i ), attrData[i] );
else if ( n == 2 )
vertexAttrib2fv( GLuint( i ), attrData[i] );
else
vertexAttrib1f( GLuint( i ), attrData[i][0] );
}
}

void NifSkopeOpenGLContext::bindShape(
unsigned int numVerts, std::uint64_t attrMask, size_t elementDataSize,
const float * const * attrData, const void * elementData )
Expand Down Expand Up @@ -1120,9 +1138,7 @@ inline std::uint32_t NifSkopeOpenGLContext::ShapeDataHash::hashFunction() const

size_t NifSkopeOpenGLContext::ShapeDataHash::getBufferDataSize() const
{
std::uint64_t tmp = ( ~attrMask >> 3 ) & 0x1111111111111111ULL;
tmp = ( tmp * 7U ) & attrMask;
tmp = ( tmp + ( tmp >> 4 ) ) & 0x0F0F0F0F0F0F0F0FULL;
std::uint64_t tmp = ( attrMask & 0x0F0F0F0F0F0F0F0FULL ) + ( ( attrMask >> 4 ) & 0x0F0F0F0F0F0F0F0FULL );
tmp = tmp + ( tmp >> 8 );
tmp = tmp + ( tmp >> 16 );
tmp = tmp + ( tmp >> 32 );
Expand All @@ -1149,27 +1165,14 @@ NifSkopeOpenGLContext::ShapeData::ShapeData(
f.glBindBuffer( GL_ARRAY_BUFFER, vbo );
f.glBufferData( GL_ARRAY_BUFFER, GLsizeiptr( attrDataSize ), (void *) 0, GL_STATIC_DRAW );
for ( size_t i = 0; attrMask; i++, attrMask = attrMask >> 4 ) {
size_t nBytes = attrMask & 7;
if ( !nBytes )
size_t n = attrMask & 15;
if ( !n )
continue;
nBytes = nBytes * sizeof( float );
if ( attrMask & 8 ) {
f.glDisableVertexAttribArray( GLuint( i ) );
if ( ( attrMask & 7 ) >= 4 )
context.vertexAttrib4fv( GLuint( i ), attrData[i] );
else if ( ( attrMask & 7 ) == 3 )
context.vertexAttrib3fv( GLuint( i ), attrData[i] );
else if ( ( attrMask & 7 ) == 2 )
context.vertexAttrib2fv( GLuint( i ), attrData[i] );
else
context.vertexAttrib1f( GLuint( i ), attrData[i][0] );
} else {
nBytes = nBytes * vertCnt;
f.glBufferSubData( GL_ARRAY_BUFFER, GLintptr( attrOffset ), GLsizeiptr( nBytes ), attrData[i] );
f.glVertexAttribPointer( GLuint( i ), GLint( attrMask & 7 ), GL_FLOAT, GL_FALSE, 0, (void *) attrOffset );
f.glEnableVertexAttribArray( GLuint( i ) );
attrOffset = attrOffset + nBytes;
}
size_t nBytes = n * sizeof( float ) * vertCnt;
f.glBufferSubData( GL_ARRAY_BUFFER, GLintptr( attrOffset ), GLsizeiptr( nBytes ), attrData[i] );
f.glVertexAttribPointer( GLuint( i ), GLint( n ), GL_FLOAT, GL_FALSE, 0, (void *) attrOffset );
f.glEnableVertexAttribArray( GLuint( i ) );
attrOffset = attrOffset + nBytes;
}

if ( elementDataSize ) [[likely]] {
Expand Down Expand Up @@ -1234,23 +1237,18 @@ NifSkopeOpenGLContext::ShapeDataHash::ShapeDataHash(
xxhState = &xxhStateBuf;
#endif
XXH3_128bits_reset_withSecret( xxhState, shapeDataHashSecret.buf, sizeof( shapeDataHashSecret.buf ) );
for ( std::uint32_t i = 0; true; i++, attrModeMask = attrModeMask >> 4 ) {
const void * p;
size_t nBytes;
if ( !attrModeMask ) {
nBytes = elementDataSize;
p = elementData;
} else if ( ( nBytes = attrModeMask & 7 ) != 0 ) {
nBytes = nBytes * sizeof( float );
if ( !( attrModeMask & 8 ) )
nBytes = nBytes * vertCnt;
p = attrData[i];
}
if ( nBytes > 0 )
XXH3_128bits_update( xxhState, p, nBytes );
if ( !attrModeMask )
break;
if ( !vertCnt ) [[unlikely]]
attrModeMask = 0;
for ( std::uint32_t i = 0; attrModeMask; i++, attrModeMask = attrModeMask >> 4 ) {
size_t nBytes = attrModeMask & 15;
if ( !nBytes )
continue;
nBytes = nBytes * sizeof( float ) * vertCnt;
const void * p = attrData[i];
XXH3_128bits_update( xxhState, p, nBytes );
}
if ( elementDataSize > 0 ) [[likely]]
XXH3_128bits_update( xxhState, elementData, elementDataSize );
XXH128_hash_t xxhResult = XXH3_128bits_digest( xxhState );
h[0] = xxhResult.low64;
h[1] = xxhResult.high64;
Expand Down
2 changes: 1 addition & 1 deletion src/gl/glcontext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ class NifSkopeOpenGLContext
// M = (attrMask >> (N * 4)) & 15 = vertex attribute mode for attribute N:
// 0: unused attribute, attrData[N] can be nullptr or invalid
// 1 to 4: array of float, vec2, vec3 or vec4, attrData[N] is expected to contain vertCnt * M floats
// 9 to 12: static attribute that does not use a VBO, M & 7 floats are read from attrData[N]
std::uint64_t attrMask;
std::uint32_t numVerts;
std::uint32_t elementBytes;
Expand Down Expand Up @@ -323,6 +322,7 @@ class NifSkopeOpenGLContext
}

void setGlobalUniforms();
void setDefaultVertexAttribs( std::uint64_t attrMask, const float * const * attrData );

//! Load and bind geometry data without drawing the shape
void bindShape( unsigned int numVerts, std::uint64_t attrMask, size_t elementDataSize,
Expand Down
Loading

0 comments on commit acf10ee

Please sign in to comment.