Model loading and texturing

This commit is contained in:
Dane Johnson
2021-01-18 18:25:47 -06:00
parent 66bf7776c7
commit 155b572aca
1283 changed files with 533814 additions and 42 deletions

314
thirdparty/assimp/code/MD3/MD3FileData.h vendored Normal file
View File

@@ -0,0 +1,314 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2019, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
/** @file Md3FileData.h
*
* @brief Defines helper data structures for importing MD3 files.
* http://linux.ucla.edu/~phaethon/q3/formats/md3format.html
*/
#ifndef AI_MD3FILEHELPER_H_INC
#define AI_MD3FILEHELPER_H_INC
#include <string>
#include <vector>
#include <sstream>
#include <stdint.h>
#include <assimp/types.h>
#include <assimp/mesh.h>
#include <assimp/anim.h>
#include <assimp/Compiler/pushpack1.h>
namespace Assimp {
namespace MD3 {
// to make it easier for us, we test the magic word against both "endianesses"
#define AI_MD3_MAGIC_NUMBER_BE AI_MAKE_MAGIC("IDP3")
#define AI_MD3_MAGIC_NUMBER_LE AI_MAKE_MAGIC("3PDI")
// common limitations
#define AI_MD3_VERSION 15
#define AI_MD3_MAXQPATH 64
#define AI_MD3_MAXFRAME 16
#define AI_MD3_MAX_FRAMES 1024
#define AI_MD3_MAX_TAGS 16
#define AI_MD3_MAX_SURFACES 32
#define AI_MD3_MAX_SHADERS 256
#define AI_MD3_MAX_VERTS 4096
#define AI_MD3_MAX_TRIANGLES 8192
// master scale factor for all vertices in a MD3 model
#define AI_MD3_XYZ_SCALE (1.0f/64.0f)
// -------------------------------------------------------------------------------
/** @brief Data structure for the MD3 main header
*/
struct Header
{
//! magic number
uint32_t IDENT;
//! file format version
uint32_t VERSION;
//! original name in .pak archive
char NAME[ AI_MD3_MAXQPATH ];
//! unknown
int32_t FLAGS;
//! number of frames in the file
uint32_t NUM_FRAMES;
//! number of tags in the file
uint32_t NUM_TAGS;
//! number of surfaces in the file
uint32_t NUM_SURFACES;
//! number of skins in the file
uint32_t NUM_SKINS;
//! offset of the first frame
uint32_t OFS_FRAMES;
//! offset of the first tag
uint32_t OFS_TAGS;
//! offset of the first surface
uint32_t OFS_SURFACES;
//! end of file
uint32_t OFS_EOF;
} PACK_STRUCT;
// -------------------------------------------------------------------------------
/** @brief Data structure for the frame header
*/
struct Frame
{
//! minimum bounds
aiVector3D min;
//! maximum bounds
aiVector3D max;
//! local origin for this frame
aiVector3D origin;
//! radius of bounding sphere
ai_real radius;
//! name of frame
char name[ AI_MD3_MAXFRAME ];
} /* PACK_STRUCT */;
// -------------------------------------------------------------------------------
/**
* @brief Data structure for the tag header
*/
struct Tag {
//! name of the tag
char NAME[ AI_MD3_MAXQPATH ];
//! Local tag origin and orientation
aiVector3D origin;
ai_real orientation[3][3];
} /* PACK_STRUCT */;
// -------------------------------------------------------------------------------
/** @brief Data structure for the surface header
*/
struct Surface {
//! magic number
int32_t IDENT;
//! original name of the surface
char NAME[ AI_MD3_MAXQPATH ];
//! unknown
int32_t FLAGS;
//! number of frames in the surface
uint32_t NUM_FRAMES;
//! number of shaders in the surface
uint32_t NUM_SHADER;
//! number of vertices in the surface
uint32_t NUM_VERTICES;
//! number of triangles in the surface
uint32_t NUM_TRIANGLES;
//! offset to the triangle data
uint32_t OFS_TRIANGLES;
//! offset to the shader data
uint32_t OFS_SHADERS;
//! offset to the texture coordinate data
uint32_t OFS_ST;
//! offset to the vertex/normal data
uint32_t OFS_XYZNORMAL;
//! offset to the end of the Surface object
int32_t OFS_END;
} /*PACK_STRUCT*/;
// -------------------------------------------------------------------------------
/** @brief Data structure for a shader defined in there
*/
struct Shader {
//! filename of the shader
char NAME[ AI_MD3_MAXQPATH ];
//! index of the shader
uint32_t SHADER_INDEX;
} /*PACK_STRUCT*/;
// -------------------------------------------------------------------------------
/** @brief Data structure for a triangle
*/
struct Triangle
{
//! triangle indices
uint32_t INDEXES[3];
} /*PACK_STRUCT*/;
// -------------------------------------------------------------------------------
/** @brief Data structure for an UV coord
*/
struct TexCoord
{
//! UV coordinates
ai_real U,V;
} /*PACK_STRUCT*/;
// -------------------------------------------------------------------------------
/** @brief Data structure for a vertex
*/
struct Vertex
{
//! X/Y/Z coordinates
int16_t X,Y,Z;
//! encoded normal vector
uint16_t NORMAL;
} /*PACK_STRUCT*/;
#include <assimp/Compiler/poppack1.h>
// -------------------------------------------------------------------------------
/** @brief Unpack a Q3 16 bit vector to its full float3 representation
*
* @param p_iNormal Input normal vector in latitude/longitude form
* @param p_afOut Pointer to an array of three floats to receive the result
*
* @note This has been taken from q3 source (misc_model.c)
*/
inline void LatLngNormalToVec3(uint16_t p_iNormal, ai_real* p_afOut)
{
ai_real lat = (ai_real)(( p_iNormal >> 8u ) & 0xff);
ai_real lng = (ai_real)(( p_iNormal & 0xff ));
const ai_real invVal( ai_real( 1.0 ) / ai_real( 128.0 ) );
lat *= ai_real( 3.141926 ) * invVal;
lng *= ai_real( 3.141926 ) * invVal;
p_afOut[ 0 ] = std::cos(lat) * std::sin(lng);
p_afOut[ 1 ] = std::sin(lat) * std::sin(lng);
p_afOut[ 2 ] = std::cos(lng);
}
// -------------------------------------------------------------------------------
/** @brief Pack a Q3 normal into 16bit latitute/longitude representation
* @param p_vIn Input vector
* @param p_iOut Output normal
*
* @note This has been taken from q3 source (mathlib.c)
*/
inline void Vec3NormalToLatLng( const aiVector3D& p_vIn, uint16_t& p_iOut )
{
// check for singularities
if ( 0.0f == p_vIn[0] && 0.0f == p_vIn[1] )
{
if ( p_vIn[2] > 0.0f )
{
((unsigned char*)&p_iOut)[0] = 0;
((unsigned char*)&p_iOut)[1] = 0; // lat = 0, long = 0
}
else
{
((unsigned char*)&p_iOut)[0] = 128;
((unsigned char*)&p_iOut)[1] = 0; // lat = 0, long = 128
}
}
else
{
int a, b;
a = int(57.2957795f * ( std::atan2( p_vIn[1], p_vIn[0] ) ) * (255.0f / 360.0f ));
a &= 0xff;
b = int(57.2957795f * ( std::acos( p_vIn[2] ) ) * ( 255.0f / 360.0f ));
b &= 0xff;
((unsigned char*)&p_iOut)[0] = (unsigned char) b; // longitude
((unsigned char*)&p_iOut)[1] = (unsigned char) a; // latitude
}
}
} // Namespace MD3
} // Namespace Assimp
#endif // !! AI_MD3FILEHELPER_H_INC

1096
thirdparty/assimp/code/MD3/MD3Loader.cpp vendored Normal file

File diff suppressed because it is too large Load Diff

333
thirdparty/assimp/code/MD3/MD3Loader.h vendored Normal file
View File

@@ -0,0 +1,333 @@
/*
Open Asset Import Library (assimp)
----------------------------------------------------------------------
Copyright (c) 2006-2019, assimp team
All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the
following conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of the assimp team, nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of the assimp team.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------
*/
/** @file Md3Loader.h
* @brief Declaration of the .MD3 importer class.
*/
#ifndef AI_MD3LOADER_H_INCLUDED
#define AI_MD3LOADER_H_INCLUDED
#include <assimp/BaseImporter.h>
#include <assimp/ByteSwapper.h>
#include "MD3FileData.h"
#include <assimp/StringComparison.h>
#include <assimp/types.h>
#include <list>
struct aiMaterial;
namespace Assimp {
using namespace MD3;
namespace Q3Shader {
// ---------------------------------------------------------------------------
/** @brief Tiny utility data structure to hold the data of a .skin file
*/
struct SkinData
{
//! A single entryin texture list
struct TextureEntry : public std::pair<std::string,std::string>
{
// did we resolve this texture entry?
bool resolved;
// for std::find()
bool operator == (const std::string& f) const {
return f == first;
}
};
//! List of textures
std::list<TextureEntry> textures;
// rest is ignored for the moment
};
// ---------------------------------------------------------------------------
/** @brief Specifies cull modi for Quake shader files.
*/
enum ShaderCullMode
{
CULL_NONE,
CULL_CW,
CULL_CCW
};
// ---------------------------------------------------------------------------
/** @brief Specifies alpha blend modi (src + dest) for Quake shader files
*/
enum BlendFunc
{
BLEND_NONE,
BLEND_GL_ONE,
BLEND_GL_ZERO,
BLEND_GL_DST_COLOR,
BLEND_GL_ONE_MINUS_DST_COLOR,
BLEND_GL_SRC_ALPHA,
BLEND_GL_ONE_MINUS_SRC_ALPHA
};
// ---------------------------------------------------------------------------
/** @brief Specifies alpha test modi for Quake texture maps
*/
enum AlphaTestFunc
{
AT_NONE,
AT_GT0,
AT_LT128,
AT_GE128
};
// ---------------------------------------------------------------------------
/** @brief Tiny utility data structure to hold a .shader map data block
*/
struct ShaderMapBlock
{
ShaderMapBlock() AI_NO_EXCEPT
: blend_src (BLEND_NONE)
, blend_dest (BLEND_NONE)
, alpha_test (AT_NONE)
{}
//! Name of referenced map
std::string name;
//! Blend and alpha test settings for texture
BlendFunc blend_src,blend_dest;
AlphaTestFunc alpha_test;
//! For std::find()
bool operator== (const std::string& o) const {
return !ASSIMP_stricmp(o,name);
}
};
// ---------------------------------------------------------------------------
/** @brief Tiny utility data structure to hold a .shader data block
*/
struct ShaderDataBlock
{
ShaderDataBlock() AI_NO_EXCEPT
: cull (CULL_CW)
{}
//! Name of referenced data element
std::string name;
//! Cull mode for the element
ShaderCullMode cull;
//! Maps defined in the shader
std::list<ShaderMapBlock> maps;
//! For std::find()
bool operator== (const std::string& o) const {
return !ASSIMP_stricmp(o,name);
}
};
// ---------------------------------------------------------------------------
/** @brief Tiny utility data structure to hold the data of a .shader file
*/
struct ShaderData
{
//! Shader data blocks
std::list<ShaderDataBlock> blocks;
};
// ---------------------------------------------------------------------------
/** @brief Load a shader file
*
* Generally, parsing is error tolerant. There's no failure.
* @param fill Receives output data
* @param file File to be read.
* @param io IOSystem to be used for reading
* @return false if file is not accessible
*/
bool LoadShader(ShaderData& fill, const std::string& file,IOSystem* io);
// ---------------------------------------------------------------------------
/** @brief Convert a Q3Shader to an aiMaterial
*
* @param[out] out Material structure to be filled.
* @param[in] shader Input shader
*/
void ConvertShaderToMaterial(aiMaterial* out, const ShaderDataBlock& shader);
// ---------------------------------------------------------------------------
/** @brief Load a skin file
*
* Generally, parsing is error tolerant. There's no failure.
* @param fill Receives output data
* @param file File to be read.
* @param io IOSystem to be used for reading
* @return false if file is not accessible
*/
bool LoadSkin(SkinData& fill, const std::string& file,IOSystem* io);
} // ! namespace Q3SHader
// ---------------------------------------------------------------------------
/** @brief Importer class to load MD3 files
*/
class MD3Importer : public BaseImporter
{
public:
MD3Importer();
~MD3Importer();
public:
// -------------------------------------------------------------------
/** Returns whether the class can handle the format of the given file.
* See BaseImporter::CanRead() for details. */
bool CanRead( const std::string& pFile, IOSystem* pIOHandler,
bool checkSig) const;
// -------------------------------------------------------------------
/** Called prior to ReadFile().
* The function is a request to the importer to update its configuration
* basing on the Importer's configuration property list.
*/
void SetupProperties(const Importer* pImp);
protected:
// -------------------------------------------------------------------
/** Return importer meta information.
* See #BaseImporter::GetInfo for the details
*/
const aiImporterDesc* GetInfo () const;
// -------------------------------------------------------------------
/** Imports the given file into the given scene structure.
* See BaseImporter::InternReadFile() for details
*/
void InternReadFile( const std::string& pFile, aiScene* pScene,
IOSystem* pIOHandler);
// -------------------------------------------------------------------
/** Validate offsets in the header
*/
void ValidateHeaderOffsets();
void ValidateSurfaceHeaderOffsets(const MD3::Surface* pcSurfHeader);
// -------------------------------------------------------------------
/** Read a Q3 multipart file
* @return true if multi part has been processed
*/
bool ReadMultipartFile();
// -------------------------------------------------------------------
/** Try to read the skin for a MD3 file
* @param fill Receives output information
*/
void ReadSkin(Q3Shader::SkinData& fill) const;
// -------------------------------------------------------------------
/** Try to read the shader for a MD3 file
* @param fill Receives output information
*/
void ReadShader(Q3Shader::ShaderData& fill) const;
// -------------------------------------------------------------------
/** Convert a texture path in a MD3 file to a proper value
* @param[in] texture_name Path to be converted
* @param[in] header_path Base path specified in MD3 header
* @param[out] out Receives the converted output string
*/
void ConvertPath(const char* texture_name, const char* header_path,
std::string& out) const;
protected:
/** Configuration option: frame to be loaded */
unsigned int configFrameID;
/** Configuration option: process multi-part files */
bool configHandleMP;
/** Configuration option: name of skin file to be read */
std::string configSkinFile;
/** Configuration option: name or path of shader */
std::string configShaderFile;
/** Configuration option: speed flag was set? */
bool configSpeedFlag;
/** Header of the MD3 file */
BE_NCONST MD3::Header* pcHeader;
/** File buffer */
BE_NCONST unsigned char* mBuffer;
/** Size of the file, in bytes */
unsigned int fileSize;
/** Current file name */
std::string mFile;
/** Current base directory */
std::string path;
/** Pure file we're currently reading */
std::string filename;
/** Output scene to be filled */
aiScene* mScene;
/** IO system to be used to access the data*/
IOSystem* mIOHandler;
};
} // end of namespace Assimp
#endif // AI_3DSIMPORTER_H_INC