2020-05-30 22:01:00 +00:00
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Copyright(C) 2018 Kevin Caccamo
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
2022-10-23 11:58:36 +00:00
|
|
|
// the Free Software Foundation, either version 2 of the License, or
|
2020-05-30 22:01:00 +00:00
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with this program. If not, see http://www.gnu.org/licenses/
|
|
|
|
//
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef __GL_MODELS_OBJ_H__
|
|
|
|
#define __GL_MODELS_OBJ_H__
|
|
|
|
|
|
|
|
#include "model.h"
|
|
|
|
#include "sc_man.h"
|
|
|
|
#include "tarray.h"
|
|
|
|
#include "vectors.h"
|
|
|
|
|
|
|
|
class FOBJModel : public FModel
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
const char *newSideSep = "$"; // OBJ side separator is /, which is parsed as a line comment by FScanner if two of them are next to each other.
|
|
|
|
bool hasMissingNormals;
|
|
|
|
bool hasSmoothGroups;
|
|
|
|
|
|
|
|
enum class FaceElement
|
|
|
|
{
|
|
|
|
VertexIndex,
|
|
|
|
UVIndex,
|
|
|
|
VNormalIndex
|
|
|
|
};
|
|
|
|
|
|
|
|
struct OBJTriRef
|
|
|
|
{
|
|
|
|
unsigned int surf;
|
|
|
|
unsigned int tri;
|
|
|
|
OBJTriRef(): surf(0), tri(0) {}
|
|
|
|
OBJTriRef(unsigned int surf, unsigned int tri): surf(surf), tri(tri) {}
|
|
|
|
bool operator== (OBJTriRef other) { return surf == other.surf && tri == other.tri; }
|
|
|
|
};
|
|
|
|
struct OBJFaceSide
|
|
|
|
{
|
|
|
|
int vertref;
|
|
|
|
int normref;
|
|
|
|
int uvref;
|
|
|
|
};
|
|
|
|
struct OBJFace
|
|
|
|
{
|
|
|
|
unsigned int sideCount;
|
|
|
|
unsigned int smoothGroup;
|
|
|
|
OBJFaceSide sides[4];
|
|
|
|
OBJFace(): sideCount(0), smoothGroup(0) {}
|
|
|
|
};
|
|
|
|
struct OBJSurface // 1 surface per 'usemtl'
|
|
|
|
{
|
|
|
|
unsigned int numTris; // Number of triangulated faces
|
|
|
|
unsigned int numFaces; // Number of faces
|
|
|
|
unsigned int vbStart; // First index in vertex buffer
|
|
|
|
unsigned int faceStart; // Index of first face in faces array
|
|
|
|
OBJFace* tris; // Triangles
|
|
|
|
FTextureID skin;
|
|
|
|
OBJSurface(FTextureID skin): numTris(0), numFaces(0), vbStart(0), faceStart(0), tris(nullptr), skin(skin) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
TArray<FVector3> verts;
|
|
|
|
TArray<FVector3> norms;
|
|
|
|
TArray<FVector2> uvs;
|
|
|
|
TArray<OBJFace> faces;
|
|
|
|
TArray<OBJSurface> surfaces;
|
|
|
|
FScanner sc;
|
|
|
|
TArray<OBJTriRef>* vertFaces;
|
|
|
|
|
|
|
|
int ResolveIndex(int origIndex, FaceElement el);
|
|
|
|
template<typename T, size_t L> void ParseVector(TArray<T> &array);
|
|
|
|
bool ParseFaceSide(const FString &side, OBJFace &face, int sidx);
|
|
|
|
void ConstructSurfaceTris(OBJSurface &surf);
|
|
|
|
void AddVertFaces();
|
|
|
|
void TriangulateQuad(const OBJFace &quad, OBJFace *tris);
|
|
|
|
FVector3 RealignVector(FVector3 vecToRealign);
|
|
|
|
FVector2 FixUV(FVector2 vecToRealign);
|
|
|
|
FVector3 CalculateNormalFlat(unsigned int surfIdx, unsigned int triIdx);
|
|
|
|
FVector3 CalculateNormalFlat(OBJTriRef otr);
|
|
|
|
FVector3 CalculateNormalSmooth(unsigned int vidx, unsigned int smoothGroup);
|
|
|
|
public:
|
|
|
|
FOBJModel(): hasMissingNormals(false), hasSmoothGroups(false), vertFaces(nullptr) {}
|
|
|
|
~FOBJModel();
|
|
|
|
bool Load(const char* fn, int lumpnum, const char* buffer, int length) override;
|
2021-12-29 08:24:13 +00:00
|
|
|
int FindFrame(const char* name, bool nodefault) override;
|
2022-10-20 20:24:25 +00:00
|
|
|
void RenderFrame(FModelRenderer* renderer, FGameTexture* skin, int frame, int frame2, double inter, int translation, const FTextureID* surfaceskinids, const TArray<VSMatrix>& boneData, int boneStartPosition) override;
|
2020-05-30 22:01:00 +00:00
|
|
|
void BuildVertexBuffer(FModelRenderer* renderer) override;
|
2022-07-23 10:05:27 +00:00
|
|
|
void AddSkins(uint8_t* hitlist, const FTextureID* surfaceskinids) override;
|
2020-05-30 22:01:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|