@@ -324,19 +324,17 @@ enum eTextureUnit
324324decl_enum_strings (eTextureUnit);
325325
326326// standard vertex attributes
327- enum eVertexAttributeSemantics
328- {
329- eVertexAttributeSemantics_Position, // 3 floats
330- eVertexAttributeSemantics_Normal, // 3 floats
331- eVertexAttributeSemantics_Color, // 4 unsigned bytes
332- eVertexAttributeSemantics_Texcoord, // 2 floats
333- eVertexAttributeSemantics_Position2d, // 2 floats
334- eVertexAttributeSemantics_Texcoord3d, // 3 floats
335- eVertexAttributeSemantics_PaletteIndex, // 1 unsigned short
336- eVertexAttributeSemantics_Unknown
327+ enum eVertexAttributeFormat
328+ {
329+ eVertexAttributeFormat_2F, // 2 floats
330+ eVertexAttributeFormat_3F, // 3 floats
331+ eVertexAttributeFormat_4UB, // 4 unsigned bytes
332+ eVertexAttributeFormat_1US, // 1 unsigned short
333+ eVertexAttributeFormat_2US, // 2 unsigned shorts
334+ eVertexAttributeFormat_Unknown
337335};
338336
339- decl_enum_strings (eVertexAttributeSemantics );
337+ decl_enum_strings (eVertexAttributeFormat );
340338
341339enum eVertexAttribute
342340{
@@ -354,63 +352,33 @@ enum eVertexAttribute
354352
355353decl_enum_strings (eVertexAttribute);
356354
357- // Get semantics of vertex attribute
358- // @param attribute: Attribute identifier
359- inline eVertexAttributeSemantics GetAttributeSemantics (eVertexAttribute attribute)
360- {
361- switch (attribute)
362- {
363- case eVertexAttribute_Position0:
364- case eVertexAttribute_Position1:
365- return eVertexAttributeSemantics_Position;
366-
367- case eVertexAttribute_Normal0:
368- case eVertexAttribute_Normal1:
369- return eVertexAttributeSemantics_Normal;
370-
371- case eVertexAttribute_Texcoord0:
372- case eVertexAttribute_Texcoord1:
373- return eVertexAttributeSemantics_Texcoord;
374-
375- case eVertexAttribute_Color0:
376- case eVertexAttribute_Color1:
377- return eVertexAttributeSemantics_Color;
378- }
379- debug_assert (false );
380- return eVertexAttributeSemantics_Unknown;
381- }
382-
383355// Get number of component for vertex attribute
384- // @param attributeSemantics: Attribute semantics
385- inline unsigned int GetAttributeComponentCount (eVertexAttributeSemantics attributeSemantics )
356+ // @param attributeFormat: Format identifier
357+ inline unsigned int GetAttributeComponentCount (eVertexAttributeFormat attributeFormat )
386358{
387- switch (attributeSemantics )
359+ switch (attributeFormat )
388360 {
389- case eVertexAttributeSemantics_Normal: return 3 ;
390- case eVertexAttributeSemantics_Position: return 3 ;
391- case eVertexAttributeSemantics_Color: return 4 ;
392- case eVertexAttributeSemantics_Texcoord: return 2 ;
393- case eVertexAttributeSemantics_Position2d: return 2 ;
394- case eVertexAttributeSemantics_Texcoord3d: return 3 ;
395- case eVertexAttributeSemantics_PaletteIndex: return 1 ;
361+ case eVertexAttributeFormat_2F: return 2 ;
362+ case eVertexAttributeFormat_3F: return 3 ;
363+ case eVertexAttributeFormat_4UB: return 4 ;
364+ case eVertexAttributeFormat_1US: return 1 ;
365+ case eVertexAttributeFormat_2US: return 2 ;
396366 }
397367 debug_assert (false );
398368 return 0 ;
399369}
400370
401371// Get vertex attribute size in bytes
402- // @param attributeSemantics: Attribute semantics
403- inline unsigned int GetAttributeSizeBytes (eVertexAttributeSemantics attributeSemantics )
372+ // @param attributeFormat: Format identifier
373+ inline unsigned int GetAttributeSizeBytes (eVertexAttributeFormat attributeFormat )
404374{
405- switch (attributeSemantics )
375+ switch (attributeFormat )
406376 {
407- case eVertexAttributeSemantics_Normal: return sizeof (float ) * 3 ;
408- case eVertexAttributeSemantics_Position: return sizeof (float ) * 3 ;
409- case eVertexAttributeSemantics_Color: return sizeof (unsigned int );
410- case eVertexAttributeSemantics_Texcoord: return sizeof (float ) * 2 ;
411- case eVertexAttributeSemantics_Position2d: return sizeof (float ) * 2 ;
412- case eVertexAttributeSemantics_Texcoord3d: return sizeof (float ) * 3 ;
413- case eVertexAttributeSemantics_PaletteIndex: return sizeof (unsigned short );
377+ case eVertexAttributeFormat_2F: return 2 * sizeof (float );
378+ case eVertexAttributeFormat_3F: return 3 * sizeof (float );
379+ case eVertexAttributeFormat_4UB: return 4 * sizeof (unsigned char );
380+ case eVertexAttributeFormat_1US: return 1 * sizeof (unsigned short );
381+ case eVertexAttributeFormat_2US: return 2 * sizeof (unsigned short );
414382 }
415383 debug_assert (false );
416384 return 0 ;
@@ -424,28 +392,34 @@ struct VertexFormat
424392
425393 // Enable attribute or modify data offset for enabled attribute
426394 // @param attribute: Attribute identifier
395+ // @param attributeFormat: Attribute format format
427396 // @param dataOffset: Attribute data offset in bytes within buffer
428- inline void SetAttribute (eVertexAttribute attribute, unsigned int dataOffset)
397+ inline void SetAttribute (eVertexAttribute attribute, eVertexAttributeFormat attributeFormat, unsigned int dataOffset)
429398 {
430399 debug_assert (attribute < eVertexAttribute_COUNT);
400+ debug_assert (attributeFormat != eVertexAttributeFormat_Unknown);
431401 mAttributes [attribute].mDataOffset = dataOffset;
432- mAttributes [attribute].mSemantics = GetAttributeSemantics (attribute);
402+ mAttributes [attribute].mFormat = attributeFormat;
403+ mAttributes [attribute].mNormalized = false ;
433404 }
434- // @param forceSemantics: Override default semantics for specified attribute
435- inline void SetAttribute (eVertexAttribute attribute, eVertexAttributeSemantics forceSemantics, unsigned int dataOffset)
405+ inline void SetAttributeNormalized (eVertexAttribute attribute, bool isNormalized = true )
436406 {
437407 debug_assert (attribute < eVertexAttribute_COUNT);
438- mAttributes [attribute].mDataOffset = dataOffset;
439- mAttributes [attribute].mSemantics = forceSemantics;
408+ mAttributes [attribute].mNormalized = isNormalized;
440409 }
441410public:
442411 struct SingleAttribute
443412 {
444413 public:
445414 SingleAttribute () = default ;
446415 public:
416+ eVertexAttributeFormat mFormat = eVertexAttributeFormat_Unknown;
447417 unsigned int mDataOffset = 0 ;
448- eVertexAttributeSemantics mSemantics = eVertexAttributeSemantics_Unknown;
418+
419+ // attribute normalization - opengl specific
420+ // if set to true, it indicates that values stored in an integer format are
421+ // to be mapped to the range [-1,1] (for signed values) or [0,1] (for unsigned values) when they are accessed and converted to floating point
422+ bool mNormalized = false ;
449423 };
450424 SingleAttribute mAttributes [eVertexAttribute_COUNT];
451425 unsigned int mDataStride = 0 ; // common to all attributes
@@ -471,10 +445,11 @@ struct Vertex3D_Format: public VertexFormat
471445 inline void Setup ()
472446 {
473447 this ->mDataStride = Sizeof_Vertex3D;
474- this ->SetAttribute (eVertexAttribute_Position0, offsetof (TVertexType, mPosition ));
475- this ->SetAttribute (eVertexAttribute_Normal0, offsetof (TVertexType, mNormal ));
476- this ->SetAttribute (eVertexAttribute_Texcoord0, offsetof (TVertexType, mTexcoord ));
477- this ->SetAttribute (eVertexAttribute_Color0, offsetof (TVertexType, mColor ));
448+ this ->SetAttribute (eVertexAttribute_Position0, eVertexAttributeFormat_3F, offsetof (TVertexType, mPosition ));
449+ this ->SetAttribute (eVertexAttribute_Normal0, eVertexAttributeFormat_3F, offsetof (TVertexType, mNormal ));
450+ this ->SetAttribute (eVertexAttribute_Texcoord0, eVertexAttributeFormat_2F, offsetof (TVertexType, mTexcoord ));
451+ this ->SetAttribute (eVertexAttribute_Color0, eVertexAttributeFormat_4UB, offsetof (TVertexType, mColor ));
452+ this ->SetAttributeNormalized (eVertexAttribute_Color0);
478453 }
479454};
480455
@@ -497,10 +472,11 @@ struct Vertex2D_Format: public VertexFormat
497472 inline void Setup ()
498473 {
499474 this ->mDataStride = Sizeof_Vertex2D;
500- this ->SetAttribute (eVertexAttribute_Texcoord0, offsetof (TVertexType, mTexcoord ));
501- this ->SetAttribute (eVertexAttribute_Color0, offsetof (TVertexType, mColor ));
475+ this ->SetAttribute (eVertexAttribute_Texcoord0, eVertexAttributeFormat_2F, offsetof (TVertexType, mTexcoord ));
476+ this ->SetAttribute (eVertexAttribute_Color0, eVertexAttributeFormat_4UB, offsetof (TVertexType, mColor ));
477+ this ->SetAttributeNormalized (eVertexAttribute_Color0);
502478 // force semantics for pos0 attribute - expect 2 floats per vertex
503- this ->SetAttribute (eVertexAttribute_Position0, eVertexAttributeSemantics_Position2d , offsetof (TVertexType, mPosition ));
479+ this ->SetAttribute (eVertexAttribute_Position0, eVertexAttributeFormat_2F , offsetof (TVertexType, mPosition ));
504480 }
505481};
506482
@@ -523,8 +499,9 @@ struct Vertex3D_Debug_Format: public VertexFormat
523499 inline void Setup ()
524500 {
525501 this ->mDataStride = Sizeof_Vertex3D_Debug;
526- this ->SetAttribute (eVertexAttribute_Position0, offsetof (TVertexType, mPosition ));
527- this ->SetAttribute (eVertexAttribute_Color0, offsetof (TVertexType, mColor ));
502+ this ->SetAttribute (eVertexAttribute_Position0, eVertexAttributeFormat_3F, offsetof (TVertexType, mPosition ));
503+ this ->SetAttribute (eVertexAttribute_Color0, eVertexAttributeFormat_4UB, offsetof (TVertexType, mColor ));
504+ this ->SetAttributeNormalized (eVertexAttribute_Color0);
528505 }
529506};
530507
0 commit comments