Skip to content

Commit ab7fefc

Browse files
HarrievGRobertBeckebans
authored andcommitted
- Added possibility to use Focal Length / FOV animation with GLTF Camera animations.
- Added blenderPy script which shows howto set lenscurves as a custom prop float array containing all evaluated fov values - minor cleanup to gltfparser and gltfExtras. - gltfExtra key value pairs can now contain a bracket enclosed string as value # Conflicts: # neo/idlib/gltfProperties.h
1 parent edb62c1 commit ab7fefc

File tree

9 files changed

+223
-59
lines changed

9 files changed

+223
-59
lines changed

neo/d3xp/Camera.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ If you have questions concerning this license or the applicable additional terms
3232

3333
#include "Game_local.h"
3434
#include "gltfParser.h"
35+
#include "gltfExtras.h"
3536

3637

3738
static const byte BCANIM_VERSION = 100;
@@ -771,7 +772,7 @@ void idCameraAnim::gltfLoadAnim( idStr gltfFileName, idStr animName )
771772
{
772773
gameLocal.Error( "Missing 'anim.%s' on '%s'", animName.c_str(), gltfFileName.c_str() );
773774
}
774-
775+
//check for
775776
cameraCuts.Clear();
776777
cameraCuts.SetGranularity( 1 );
777778
camera.Clear();
@@ -853,13 +854,41 @@ void idCameraAnim::gltfLoadAnim( idStr gltfFileName, idStr animName )
853854
}
854855
break;
855856
case gltfAnimation_Channel_Target::scale:
857+
{
856858
idList<idVec3*>& values = data->GetAccessorView<idVec3>( output );
857859
if( values.Num() > i )
858860
{
859861
gameLocal.Printf( "^5Frame: ^7%i ignored scale on /%s \n\n\n", i, anim->name.c_str() );
860862
}
861-
break;
863+
}
864+
break;
865+
}
866+
}
867+
}
868+
869+
//check for extra anim data
870+
if( anim->extras.json.Length() )
871+
{
872+
gltfItemArray animExtras;
873+
GLTFARRAYITEM( animExtras, CameraLensFrames, gltfExtra_CameraLensFrames );
874+
idLexer lexer( LEXFL_ALLOWPATHNAMES | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_NOSTRINGESCAPECHARS );
875+
lexer.LoadMemory( anim->extras.json, anim->extras.json.Size(), "idCameraAnim_gltfExtra", 0 );
876+
animExtras.Parse( &lexer , true );
877+
878+
if( CameraLensFrames->item )
879+
{
880+
auto* lensFrameValues = ( idList<double, TAG_IDLIB_LIST>* )CameraLensFrames->item;
881+
882+
if( lensFrameValues )
883+
{
884+
assert( lensFrameValues->Num() == camera.Num() );
885+
for( int i = 0; i < lensFrameValues->Num(); i++ )
886+
{
887+
camera[i].fov = ( float )( *lensFrameValues )[i];
888+
}
862889
}
890+
//Dont forget to free! normally a gltfPropertyArray destructor frees the itemdata
891+
delete CameraLensFrames->item;
863892
}
864893
}
865894
}

neo/idlib/Lexer.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,92 @@ const char* idLexer::ParseBracedSectionExact( idStr& out, int tabs )
19151915
return out.c_str();
19161916
}
19171917

1918+
/*
1919+
=================
1920+
idParser::ParseBracedSection
1921+
1922+
The next token should be an open brace.
1923+
Parses until a matching close brace is found.
1924+
Maintains exact characters between braces.
1925+
1926+
FIXME: this should use ReadToken and replace the token white space with correct indents and newlines
1927+
=================
1928+
*/
1929+
const char* idLexer::ParseBracketSectionExact( idStr& out, int tabs )
1930+
{
1931+
int depth;
1932+
bool doTabs;
1933+
bool skipWhite;
1934+
1935+
out.Empty();
1936+
1937+
if( !idLexer::ExpectTokenString( "[" ) )
1938+
{
1939+
return out.c_str();
1940+
}
1941+
1942+
out = "[";
1943+
depth = 1;
1944+
skipWhite = false;
1945+
doTabs = tabs >= 0;
1946+
1947+
while( depth && *idLexer::script_p )
1948+
{
1949+
char c = *( idLexer::script_p++ );
1950+
1951+
switch( c )
1952+
{
1953+
case '\t':
1954+
case ' ':
1955+
{
1956+
if( skipWhite )
1957+
{
1958+
continue;
1959+
}
1960+
break;
1961+
}
1962+
case '\n':
1963+
{
1964+
if( doTabs )
1965+
{
1966+
skipWhite = true;
1967+
out += c;
1968+
continue;
1969+
}
1970+
break;
1971+
}
1972+
case '[':
1973+
{
1974+
depth++;
1975+
tabs++;
1976+
break;
1977+
}
1978+
case ']':
1979+
{
1980+
depth--;
1981+
tabs--;
1982+
break;
1983+
}
1984+
}
1985+
1986+
if( skipWhite )
1987+
{
1988+
int i = tabs;
1989+
if( c == '[' )
1990+
{
1991+
i--;
1992+
}
1993+
skipWhite = false;
1994+
for( ; i > 0; i-- )
1995+
{
1996+
out += '\t';
1997+
}
1998+
}
1999+
out += c;
2000+
}
2001+
return out.c_str();
2002+
}
2003+
19182004
/*
19192005
=================
19202006
idLexer::ParseBracedSection

neo/idlib/Lexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ class idLexer
218218
const char* ParseBracedSection( idStr& out );
219219
// parse a braced section into a string, maintaining indents and newlines
220220
const char* ParseBracedSectionExact( idStr& out, int tabs = -1 );
221+
const char* ParseBracketSectionExact( idStr& out, int tabs = -1 );
221222
// parse the rest of the line
222223
const char* ParseRestOfLine( idStr& out );
223224
// pulls the entire line, including the \n at the end

neo/idlib/gltfExtras.cpp

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,10 @@ void gltfExtra_Scatter::parse( idToken& token, idLexer* parser )
4141
scatterInfo.Parse( parser, true );
4242
}
4343

44-
void gltfExtra_cvar::parse( idToken& token, idLexer* parser )
44+
void gltfExtra_CameraLensFrames::parse( idToken& token, idLexer* parser )
4545
{
46-
parser->UnreadToken( &token );
47-
gltfItemArray cvarInfo;
48-
idStr n, t, v, d;
49-
GLTFARRAYITEMREF( cvarInfo, name, gltfItem , n );
50-
GLTFARRAYITEMREF( cvarInfo, type, gltfItem , t );
51-
GLTFARRAYITEMREF( cvarInfo, value, gltfItem, v );
52-
GLTFARRAYITEMREF( cvarInfo, desc, gltfItem , d );
53-
int total = cvarInfo.Parse( parser );
54-
assert( total == 3 );
55-
idCVar* gltExtra_cvar = new idCVar(
56-
n.c_str(),
57-
v.c_str(),
58-
CVAR_SYSTEM | CVAR_BOOL,
59-
d.c_str()
60-
);
61-
62-
cvarSystem->Register( gltExtra_cvar );
46+
item = new idList<double>();
47+
auto* numbers = new gltfItem_number_array( "" );
48+
numbers->Set( item, parser );
49+
numbers->parse( token );
6350
}

neo/idlib/gltfExtras.h

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
===========================================================================
33
44
Doom 3 BFG Edition GPL Source Code
5-
Copyright (C) 2022 Harrie van Ginneken
5+
Copyright (C) 2022 - 2023 Harrie van Ginneken
66
77
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
88
@@ -29,36 +29,34 @@ If you have questions concerning this license or the applicable additional terms
2929
#include "gltfParser.h"
3030

3131
#ifndef gltfExtraParser
32-
#define gltfExtraParser(className,ptype) \
33-
class gltfExtra_##className : public parsable, public parseType<ptype> \
34-
{public: \
35-
gltfExtra_##className( idStr Name ) : name( Name ){ item = nullptr; } \
36-
virtual void parse( idToken &token ){parse(token,nullptr);} \
37-
virtual void parse( idToken &token , idLexer * parser ); \
38-
virtual idStr &Name() { return name; } \
39-
private: \
32+
#define gltfExtraParser(className,ptype) \
33+
class gltfExtra_##className : public parsable, public parseType<ptype> \
34+
{public: \
35+
gltfExtra_##className( idStr Name ) : name( Name ){ item = nullptr; } \
36+
virtual void parse( idToken &token ) {parse(token,nullptr); } \
37+
virtual void parse( idToken &token , idLexer * parser ); \
38+
virtual idStr &Name() { return name; } \
39+
private: \
4040
idStr name;}
41-
#pragma endregion
41+
4242
#endif
4343

4444
//Helper macros for gltf data deserialize
4545
#define GLTFARRAYITEM(target,name,type) auto * name = new type (#name); target.AddItemDef((parsable*)name)
4646
#define GLTFARRAYITEMREF(target,name,type,ref) auto * name = new type (#name); target.AddItemDef((parsable*)name); name->Set(&ref)
47+
4748
#ifndef GLTF_EXTRAS_H
4849
#define GLTF_EXTRAS_H
4950

50-
class test
51+
class gltfExtraStub
5152
{
5253
public:
53-
test() { }
54+
gltfExtraStub() { }
5455
};
5556

57+
gltfExtraParser( Scatter, gltfExtraStub );
5658

57-
gltfExtraParser( Scatter, test );
59+
gltfExtraParser( CameraLensFrames, idList<double> );
5860

59-
gltfExtraParser( cvar, idCVar );
6061
#endif // GLTF_EXTRAS_H
61-
62-
#ifndef gltfExternalParser
63-
#undef gltfExtraParser
64-
#endif
62+
#undef gltfExtraParser

neo/idlib/gltfParser.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ gltf_accessor_component::Type GetComponentTypeEnum( int id , uint* sizeInBytes
163163

164164
idList<gltfData*> gltfData::dataList;
165165
idHashIndex gltfData::fileDataHash;
166-
gltfItemArray* gltfItem_Extra::items = new gltfItemArray();
167166

168167
//Helper macros for gltf data deserialize
169168
//NOTE: gltfItems that deviate from the default SET(T*) function cannot be handled with itemref macro.
@@ -300,14 +299,20 @@ int gltfItemArray::Fill( idLexer* lexer, idDict* strPairs )
300299
idToken token;
301300
bool parsing = true;
302301
int parseCount = 0;
303-
lexer->ExpectTokenString( "{" );
302+
lexer->ReadToken( &token );
304303
while( parsing && !lexer->PeekTokenString( "}" ) && lexer->ExpectAnyToken( &token ) )
305304
{
306305
lexer->ExpectTokenString( ":" );
307306
idStr key = token;
308307
idStr value;
309308
key.StripTrailingWhitespace();
310-
if( lexer->PeekTokenString( "{" ) )
309+
if( lexer->PeekTokenString( "[" ) )
310+
{
311+
lexer->ParseBracketSectionExact( value );
312+
value.StripTrailingWhitespace();
313+
strPairs->Set( key, value );
314+
}
315+
else if( lexer->PeekTokenString( "{" ) )
311316
{
312317
lexer->ParseBracedSectionExact( value );
313318
value.StripTrailingWhitespace();
@@ -338,6 +343,7 @@ int gltfItemArray::Parse( idLexer* lexer, bool forwardLexer/* = false*/ )
338343
idToken token;
339344
bool parsing = true;
340345
int parseCount = 0;
346+
341347
lexer->ExpectTokenString( "{" );
342348
while( parsing && !lexer->PeekTokenString( "}" ) && lexer->ExpectAnyToken( &token ) )
343349
{
@@ -362,7 +368,7 @@ int gltfItemArray::Parse( idLexer* lexer, bool forwardLexer/* = false*/ )
362368
}
363369
if( !parsed )
364370
{
365-
lexer->SkipBracedSection();
371+
lexer->SkipBracedSection( true, lexer->PeekTokenString( "{" ) ? BRSKIP_BRACES : BRSKIP_BRACKET );
366372
}
367373
else
368374
{
@@ -452,25 +458,18 @@ void gltfItem_Extra::parse( idToken& token )
452458
{
453459
parser->UnreadToken( &token );
454460
parser->ParseBracedSectionExact( item->json );
455-
461+
gltfItemArray items;
456462
idLexer lexer( LEXFL_ALLOWPATHNAMES | LEXFL_ALLOWMULTICHARLITERALS | LEXFL_NOSTRINGESCAPECHARS );
457463
lexer.LoadMemory( item->json, item->json.Size(), "gltfItem_Extra", 0 );
458-
items->Fill( &lexer, &item->strPairs );
464+
items.Fill( &lexer, &item->strPairs );
459465
lexer.Reset();
460-
items->Parse( &lexer , true );
461466

462467
if( gltf_parseVerbose.GetBool() )
463468
{
464469
common->Printf( "%s", item->json.c_str() );
465470
}
466471
}
467472

468-
void gltfItem_Extra::Register( parsable* extra )
469-
{
470-
common->DPrintf( "...Registering gltf Extra \"%s\" total(%i)\n", extra->Name().c_str(), items->Num() );
471-
items->AddItemDef( extra );
472-
}
473-
474473
void gltfItem_animation_sampler::parse( idToken& token )
475474
{
476475
gltfItemArray animSampler;
@@ -692,7 +691,6 @@ void gltfItem_number_array::parse( idToken& token )
692691

693692
void gltfItem_vec4::parse( idToken& token )
694693
{
695-
696694
auto* numbers = new gltfItem_number_array( "" );
697695
idList<double> numberarray;
698696
numbers->Set( &numberarray, parser );
@@ -704,6 +702,7 @@ void gltfItem_vec4::parse( idToken& token )
704702

705703
double* val = numbers->item->Ptr();
706704
*item = idVec4( val[0], val[1], val[2], val[3] );
705+
delete numbers;
707706
}
708707

709708
void gltfItem_vec3::parse( idToken& token )
@@ -719,6 +718,7 @@ void gltfItem_vec3::parse( idToken& token )
719718

720719
double* val = numbers->item->Ptr();
721720
*item = idVec3( val[0], val[1], val[2] );
721+
delete numbers;
722722
}
723723

724724
void gltfItem_vec2::parse( idToken& token )
@@ -734,6 +734,7 @@ void gltfItem_vec2::parse( idToken& token )
734734

735735
double* val = numbers->item->Ptr();
736736
*item = idVec2( val[0], val[1] );
737+
delete numbers;
737738
}
738739

739740
void gltfItem_quat::parse( idToken& token )
@@ -749,6 +750,7 @@ void gltfItem_quat::parse( idToken& token )
749750

750751
double* val = numbers->item->Ptr();
751752
*item = idQuat( val[0] , val[1] , val[2] , val[3] );
753+
delete numbers;
752754
}
753755

754756
void gltfItem_mat4::parse( idToken& token )
@@ -769,6 +771,7 @@ void gltfItem_mat4::parse( idToken& token )
769771
val[8], val[9], val[10], val[11],
770772
val[12], val[13], val[14], val[15]
771773
);
774+
delete numbers;
772775
}
773776

774777
void gltfItem_accessor_sparse::parse( idToken& token )

0 commit comments

Comments
 (0)