mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 09:19:18 +02:00
recognition of basic texture traits
This commit is contained in:
85
Texture.cpp
85
Texture.cpp
@@ -359,13 +359,9 @@ opengl_texture::load_TGA() {
|
|||||||
|
|
||||||
file.read( (char *)&chunkheader, sizeof( unsigned char ) );
|
file.read( (char *)&chunkheader, sizeof( unsigned char ) );
|
||||||
|
|
||||||
if( chunkheader < 128 ) {
|
if( (chunkheader & 0x80 ) == 0 ) {
|
||||||
// if the header is < 128, it means it is the number of RAW color packets minus 1
|
// if the high bit is not set, it means it is the number of RAW color packets, plus 1
|
||||||
// that follow the header
|
for( int i = 0; i <= chunkheader; ++i ) {
|
||||||
// add 1 to get number of following color values
|
|
||||||
++chunkheader;
|
|
||||||
// read RAW color values
|
|
||||||
for( int i = 0; i < (int)chunkheader; ++i ) {
|
|
||||||
|
|
||||||
file.read( (char *)&buffer[ 0 ], bytesperpixel );
|
file.read( (char *)&buffer[ 0 ], bytesperpixel );
|
||||||
|
|
||||||
@@ -382,8 +378,8 @@ opengl_texture::load_TGA() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// chunkheader > 128 RLE data, next color reapeated (chunkheader - 127) times
|
// rle chunk, the color supplied afterwards is reapeated header + 1 times (not including the highest bit)
|
||||||
chunkheader -= 127; // Subteact 127 to get rid of the ID bit
|
chunkheader &= ~0x80;
|
||||||
// read the current color
|
// read the current color
|
||||||
file.read( (char *)&buffer[ 0 ], bytesperpixel );
|
file.read( (char *)&buffer[ 0 ], bytesperpixel );
|
||||||
|
|
||||||
@@ -393,9 +389,9 @@ opengl_texture::load_TGA() {
|
|||||||
buffer[ 2 ] = buffer[ 0 ];
|
buffer[ 2 ] = buffer[ 0 ];
|
||||||
}
|
}
|
||||||
// copy the color into the image data as many times as dictated
|
// copy the color into the image data as many times as dictated
|
||||||
for( int i = 0; i < (int)chunkheader; ++i ) {
|
for( int i = 0; i <= chunkheader; ++i ) {
|
||||||
|
|
||||||
( *datapointer ) = ( *buffer );
|
( *datapointer ) = ( *bufferpointer );
|
||||||
++datapointer;
|
++datapointer;
|
||||||
++currentpixel;
|
++currentpixel;
|
||||||
}
|
}
|
||||||
@@ -432,9 +428,21 @@ opengl_texture::create() {
|
|||||||
glGenTextures( 1, &id );
|
glGenTextures( 1, &id );
|
||||||
glBindTexture( GL_TEXTURE_2D, id );
|
glBindTexture( GL_TEXTURE_2D, id );
|
||||||
|
|
||||||
|
// analyze specified texture traits
|
||||||
|
bool wraps{ true };
|
||||||
|
bool wrapt{ true };
|
||||||
|
for( auto const &trait : traits ) {
|
||||||
|
|
||||||
|
switch( trait ) {
|
||||||
|
|
||||||
|
case 's': { wraps = false; break; }
|
||||||
|
case 't': { wrapt = false; break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TODO: set wrapping according to supplied parameters
|
// TODO: set wrapping according to supplied parameters
|
||||||
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, ( wraps == true ? GL_REPEAT : GL_CLAMP_TO_EDGE ) );
|
||||||
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, ( wrapt == true ? GL_REPEAT : GL_CLAMP_TO_EDGE ) );
|
||||||
|
|
||||||
set_filtering();
|
set_filtering();
|
||||||
|
|
||||||
@@ -457,8 +465,8 @@ opengl_texture::create() {
|
|||||||
// compressed dds formats
|
// compressed dds formats
|
||||||
int const datablocksize =
|
int const datablocksize =
|
||||||
( data_format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ?
|
( data_format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT ?
|
||||||
8 :
|
8 :
|
||||||
16 );
|
16 );
|
||||||
|
|
||||||
datasize = ( ( std::max(datawidth, 4) + 3 ) / 4 ) * ( ( std::max(dataheight, 4) + 3 ) / 4 ) * datablocksize;
|
datasize = ( ( std::max(datawidth, 4) + 3 ) / 4 ) * ( ( std::max(dataheight, 4) + 3 ) / 4 ) * datablocksize;
|
||||||
|
|
||||||
@@ -484,8 +492,8 @@ opengl_texture::create() {
|
|||||||
is_ready = true;
|
is_ready = true;
|
||||||
has_alpha = (
|
has_alpha = (
|
||||||
data_components == GL_RGBA ?
|
data_components == GL_RGBA ?
|
||||||
true :
|
true :
|
||||||
false );
|
false );
|
||||||
|
|
||||||
data.resize( 0 ); // TBD, TODO: keep the texture data if we start doing some gpu data cleaning down the road
|
data.resize( 0 ); // TBD, TODO: keep the texture data if we start doing some gpu data cleaning down the road
|
||||||
data_state = resource_state::none;
|
data_state = resource_state::none;
|
||||||
@@ -494,14 +502,21 @@ opengl_texture::create() {
|
|||||||
void
|
void
|
||||||
opengl_texture::set_filtering() {
|
opengl_texture::set_filtering() {
|
||||||
|
|
||||||
bool hash = ( name.find( '#' ) != std::string::npos );
|
bool sharpen{ false };
|
||||||
|
for( auto const &trait : traits ) {
|
||||||
|
|
||||||
|
switch( trait ) {
|
||||||
|
|
||||||
|
case '#': { sharpen = true; break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if( GLEW_VERSION_1_4 ) {
|
if( GLEW_VERSION_1_4 ) {
|
||||||
|
|
||||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
|
||||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
|
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
|
||||||
|
|
||||||
if( true == hash ) {
|
if( true == sharpen ) {
|
||||||
// #: sharpen more
|
// #: sharpen more
|
||||||
glTexEnvf( GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, -2.0 );
|
glTexEnvf( GL_TEXTURE_FILTER_CONTROL, GL_TEXTURE_LOD_BIAS, -2.0 );
|
||||||
}
|
}
|
||||||
@@ -520,12 +535,21 @@ texture_manager::Init() {
|
|||||||
texture_manager::size_type
|
texture_manager::size_type
|
||||||
texture_manager::GetTextureId( std::string Filename, std::string const &Dir, int const Filter, bool const Loadnow ) {
|
texture_manager::GetTextureId( std::string Filename, std::string const &Dir, int const Filter, bool const Loadnow ) {
|
||||||
|
|
||||||
if( Filename.find( ':' ) != std::string::npos )
|
|
||||||
Filename.erase( Filename.find( ':' ) ); // po dwukropku mogą być podane dodatkowe informacje niebędące nazwą tekstury
|
|
||||||
if( Filename.find( '|' ) != std::string::npos )
|
if( Filename.find( '|' ) != std::string::npos )
|
||||||
Filename.erase( Filename.find( '|' ) ); // po | może być nazwa kolejnej tekstury
|
Filename.erase( Filename.find( '|' ) ); // po | może być nazwa kolejnej tekstury
|
||||||
|
|
||||||
|
std::string traits;
|
||||||
|
auto const traitpos = Filename.find( ':' );
|
||||||
|
if( traitpos != std::string::npos ) {
|
||||||
|
// po dwukropku mogą być podane dodatkowe informacje niebędące nazwą tekstury
|
||||||
|
if( Filename.size() > traitpos + 1 )
|
||||||
|
traits = Filename.substr( traitpos + 1 );
|
||||||
|
Filename.erase( traitpos );
|
||||||
|
}
|
||||||
|
|
||||||
if( Filename.rfind( '.' ) != std::string::npos )
|
if( Filename.rfind( '.' ) != std::string::npos )
|
||||||
Filename.erase( Filename.rfind( '.' ) ); // trim extension if there's one
|
Filename.erase( Filename.rfind( '.' ) ); // trim extension if there's one
|
||||||
|
|
||||||
for( char &c : Filename ) {
|
for( char &c : Filename ) {
|
||||||
// change forward slashes to windows ones. NOTE: probably not strictly necessary, but eh
|
// change forward slashes to windows ones. NOTE: probably not strictly necessary, but eh
|
||||||
c = ( c == '/' ? '\\' : c );
|
c = ( c == '/' ? '\\' : c );
|
||||||
@@ -589,7 +613,15 @@ texture_manager::GetTextureId( std::string Filename, std::string const &Dir, int
|
|||||||
|
|
||||||
opengl_texture texture;
|
opengl_texture texture;
|
||||||
texture.name = filename;
|
texture.name = filename;
|
||||||
texture.attributes = std::to_string( Filter ); // temporary. TODO, TBD: check how it's used and possibly get rid of it
|
if( ( Filter > 0 ) && ( Filter < 10 ) ) {
|
||||||
|
// temporary. TODO, TBD: check how it's used and possibly get rid of it
|
||||||
|
traits += std::to_string( Filter );
|
||||||
|
}
|
||||||
|
if( Filename.find('#') !=std::string::npos ) {
|
||||||
|
// temporary code for legacy assets -- textures with names beginning with # are to be sharpened
|
||||||
|
traits += '#';
|
||||||
|
}
|
||||||
|
texture.traits = traits;
|
||||||
auto const textureindex = m_textures.size();
|
auto const textureindex = m_textures.size();
|
||||||
m_textures.emplace_back( texture );
|
m_textures.emplace_back( texture );
|
||||||
m_texturemappings.emplace( filename, textureindex );
|
m_texturemappings.emplace( filename, textureindex );
|
||||||
@@ -608,18 +640,25 @@ texture_manager::GetTextureId( std::string Filename, std::string const &Dir, int
|
|||||||
void
|
void
|
||||||
texture_manager::Bind( texture_manager::size_type const Id ) {
|
texture_manager::Bind( texture_manager::size_type const Id ) {
|
||||||
|
|
||||||
// TODO: keep track of what's currently bound and don't do it twice
|
if( Id == m_activetexture ) {
|
||||||
|
// don't bind again what's already active
|
||||||
|
return;
|
||||||
|
}
|
||||||
// TODO: do binding in texture object, add support for other types
|
// TODO: do binding in texture object, add support for other types
|
||||||
if( Id != 0 ) {
|
if( Id != 0 ) {
|
||||||
|
|
||||||
auto const &texture = Texture( Id );
|
auto const &texture = Texture( Id );
|
||||||
if( true == texture.is_ready ) {
|
if( true == texture.is_ready ) {
|
||||||
glBindTexture( GL_TEXTURE_2D, texture.id );
|
glBindTexture( GL_TEXTURE_2D, texture.id );
|
||||||
|
m_activetexture = Id;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glBindTexture( GL_TEXTURE_2D, 0 );
|
glBindTexture( GL_TEXTURE_2D, 0 );
|
||||||
|
m_activetexture = 0;
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
// checks whether specified texture is in the texture bank. returns texture id, or npos.
|
// checks whether specified texture is in the texture bank. returns texture id, or npos.
|
||||||
texture_manager::size_type
|
texture_manager::size_type
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ struct opengl_texture {
|
|||||||
GLuint id{ -1 }; // associated GL resource
|
GLuint id{ -1 }; // associated GL resource
|
||||||
bool has_alpha{ false }; // indicates the texture has alpha channel
|
bool has_alpha{ false }; // indicates the texture has alpha channel
|
||||||
bool is_ready{ false }; // indicates the texture was processed and is ready for use
|
bool is_ready{ false }; // indicates the texture was processed and is ready for use
|
||||||
std::string attributes; // requested texture attributes: wrapping modes etc
|
std::string traits; // requested texture attributes: wrapping modes etc
|
||||||
std::string name; // name of the texture source file
|
std::string name; // name of the texture source file
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -95,6 +95,7 @@ private:
|
|||||||
static const size_type npos{ 0 }; // should be -1, but the rest of the code uses -1 for something else
|
static const size_type npos{ 0 }; // should be -1, but the rest of the code uses -1 for something else
|
||||||
opengltexture_array m_textures;
|
opengltexture_array m_textures;
|
||||||
index_map m_texturemappings;
|
index_map m_texturemappings;
|
||||||
|
size_type m_activetexture{ 0 }; // last i.e. currently bound texture
|
||||||
};
|
};
|
||||||
|
|
||||||
extern texture_manager TextureManager;
|
extern texture_manager TextureManager;
|
||||||
Reference in New Issue
Block a user