indexed geometry tangent calculation, explicit optional material parameters, uint8 serialization fix

This commit is contained in:
tmj-fstate
2020-10-20 17:23:58 +02:00
parent ccfdf5e003
commit f9a8c1fbb3
7 changed files with 35 additions and 25 deletions

View File

@@ -457,8 +457,9 @@ std::pair<int, int> TSubModel::Load( cParser &parser, bool dynamic )
0x10 ); // 0x10-nieprzezroczysta, 0x20-przezroczysta
}
// and same thing with selfillum
if (!std::isnan(mat.selfillum))
fLight = mat.selfillum;
if( mat.selfillum ) {
fLight = mat.selfillum.value();
}
}
// visibility range
@@ -677,8 +678,8 @@ std::pair<int, int> TSubModel::Load( cParser &parser, bool dynamic )
}
}
Vertices.resize( m_geometry.vertex_count ); // in case we had some degenerate triangles along the way
gfx::calculate_tangents( Vertices, GL_TRIANGLES );
gfx::calculate_indices( Indices, Vertices );
gfx::calculate_tangents( Vertices, Indices, GL_TRIANGLES );
// update values potentially changed by indexing
m_geometry.index_count = Indices.size();
m_geometry.vertex_count = Vertices.size();
@@ -2040,8 +2041,9 @@ void TSubModel::BinInit(TSubModel *s, float4x4 *m, std::vector<std::string> *t,
opengl_material const &mat = GfxRenderer->Material(m_material);
// replace submodel selfillum with material one
if (!std::isnan(mat.selfillum))
fLight = mat.selfillum;
if( mat.selfillum ) {
fLight = mat.selfillum.value();
}
}
}
else {

View File

@@ -69,7 +69,7 @@ basic_vertex::deserialize_packed( std::istream &s, bool const Tangent ) {
// based on
// Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”.
// Terathon Software, 2001. http://terathon.com/code/tangent.html
void calculate_tangents(vertex_array &vertices, int type)
void calculate_tangents(vertex_array &vertices, index_array const &indices, int const type)
{
size_t vertex_count = vertices.size();
@@ -77,12 +77,14 @@ void calculate_tangents(vertex_array &vertices, int type)
return;
size_t triangle_count;
size_t tri_count_base = indices.empty() ? vertex_count : indices.size();
if (type == GL_TRIANGLES)
triangle_count = vertex_count / 3;
triangle_count = tri_count_base / 3;
else if (type == GL_TRIANGLE_STRIP)
triangle_count = vertex_count - 2;
triangle_count = tri_count_base - 2;
else if (type == GL_TRIANGLE_FAN)
triangle_count = vertex_count - 2;
triangle_count = tri_count_base - 2;
else
return;
@@ -118,6 +120,12 @@ void calculate_tangents(vertex_array &vertices, int type)
i3 = a + 2;
}
if (!indices.empty()) {
i1 = indices[i1];
i2 = indices[i2];
i3 = indices[i3];
}
const glm::vec3 &v1 = vertices[i1].position;
const glm::vec3 &v2 = vertices[i2].position;
const glm::vec3 &v3 = vertices[i3].position;
@@ -181,7 +189,7 @@ void calculate_indices( index_array &Indices, vertex_array &Vertices ) {
Indices.resize( Vertices.size() );
std::iota( std::begin( Indices ), std::end( Indices ), 0 );
// gather instances of used verices, replace the original vertex bank with it after you're done
// gather instances of used vertices, replace the original vertex bank with it after you're done
vertex_array indexedvertices;
indexedvertices.reserve( std::max<size_t>( 100, Vertices.size() / 3 ) ); // optimistic guesstimate, but should reduce re-allocation somewhat
auto const matchtolerance { 1e-5f };

View File

@@ -52,7 +52,7 @@ using basic_index = std::uint32_t;
using vertex_array = std::vector<basic_vertex>;
using index_array = std::vector<basic_index>;
void calculate_tangents( vertex_array &vertices, int type );
void calculate_tangents( vertex_array &vertices, index_array const &indices, int const type );
void calculate_indices( index_array &Indices, vertex_array &Vertices );
// generic geometry bank class, allows storage, update and drawing of geometry chunks

View File

@@ -425,9 +425,9 @@ opengl_material::deserialize_mapping( cParser &Input, int const Priority, bool c
float opengl_material::get_or_guess_opacity() const {
if (!std::isnan(opacity))
return opacity;
if( opacity ) {
return opacity.value();
}
if (textures[0] != null_handle)
{
auto const &tex = GfxRenderer->Texture(textures[0]);

View File

@@ -24,8 +24,8 @@ struct opengl_material {
std::vector<gl::shader::param_entry> params_state;
std::shared_ptr<gl::program> shader;
float opacity = std::numeric_limits<float>::quiet_NaN();
float selfillum = std::numeric_limits<float>::quiet_NaN();
std::optional<float> opacity;
std::optional<float> selfillum;
float glossiness { 10.f };
std::string name;

View File

@@ -1736,7 +1736,7 @@ gfx::geometry_handle opengl33_renderer::Insert( gfx::index_array &Indices, gfx::
// creates a new geometry chunk of specified type from supplied data, in specified bank. returns: handle to the chunk or NULL
gfx::geometry_handle opengl33_renderer::Insert(gfx::vertex_array &Vertices, gfx::geometrybank_handle const &Geometry, int const Type)
{
gfx::calculate_tangents(Vertices, Type);
gfx::calculate_tangents(Vertices, gfx::index_array(), Type);
return m_geometry.create_chunk(Vertices, Geometry, Type);
}
@@ -1744,7 +1744,7 @@ gfx::geometry_handle opengl33_renderer::Insert(gfx::vertex_array &Vertices, gfx:
// replaces data of specified chunk with the supplied vertex data, starting from specified offset
bool opengl33_renderer::Replace(gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type, std::size_t const Offset)
{
gfx::calculate_tangents(Vertices, Type);
gfx::calculate_tangents(Vertices, gfx::index_array(), Type);
return m_geometry.replace(Vertices, Geometry, Offset);
}
@@ -1752,7 +1752,7 @@ bool opengl33_renderer::Replace(gfx::vertex_array &Vertices, gfx::geometry_handl
// adds supplied vertex data at the end of specified chunk
bool opengl33_renderer::Append(gfx::vertex_array &Vertices, gfx::geometry_handle const &Geometry, int const Type)
{
gfx::calculate_tangents(Vertices, Type);
gfx::calculate_tangents(Vertices, gfx::index_array(), Type);
return m_geometry.append(Vertices, Geometry);
}
@@ -1863,17 +1863,17 @@ void opengl33_renderer::Bind_Material( material_handle const Material, TSubModel
if( entry.size == 1 ) {
// HACK: convert color to luminosity, if it's passed as single value
src == glm::vec4 { colors::RGBtoHSV( glm::vec3 { src } ).z };
src = glm::vec4 { colors::RGBtoHSV( glm::vec3 { src } ).z };
}
for (size_t j = 0; j < entry.size; j++)
model_ubs.param[entry.location][entry.offset + j] = src[j];
}
if( !std::isnan( material.opacity ) ) {
if( material.opacity ) {
model_ubs.opacity = (
m_blendingenabled ?
-material.opacity :
material.opacity );
-material.opacity.value() :
material.opacity.value() );
}
else {
model_ubs.opacity = (

View File

@@ -132,7 +132,7 @@ glm::vec4 sn_utils::d_vec4( std::istream& s )
uint8_t sn_utils::d_uint8( std::istream& s ) {
uint8_t buf;
s.read((char*)buf, 1);
s.read((char*)&buf, 1);
return buf;
}
@@ -221,7 +221,7 @@ void sn_utils::ls_float64(std::ostream &s, double t)
void sn_utils::s_uint8(std::ostream &s, uint8_t v)
{
s.write((char*)v, 1);
s.write((char*)&v, 1);
}
void sn_utils::s_str(std::ostream &s, std::string v)