//========================================================================
// Drawing macros etc.
//
// AUTHOR:  Gabor Nagy
// DATE:    2025-Apr-11 18:42:25
//
// Copyright (C) 1996-2026 by Gabor Nagy. All rights reserved.
//========================================================================
#ifndef _E3D_GL_MeshDraw_h
#define _E3D_GL_MeshDraw_h


#ifndef _E3D_Mesh_h
#include <E3D/Mesh.h>
#endif

#ifndef _E3D_Rendering_h
#include <E3D/Rendering.h>
#endif

#ifndef _E3D_PolygroupDraw_h
#include <E3D/GL/PolyGroupDraw.h>
#endif


static inline void E3dPolygon_GLDraw(const E3dPolygon* PPolygon, const E3dVertexNodeSize PVertexNodeSize,  const E3dVertex* PMeshVertices, const E3dVertexSize PMeshVertexSize)
{
#ifdef EGLES
if(E3d_Verbosity & E3dVbOPENGL) { printf("%s() %s:%d  %sGLES IMPLEMENT ME%s\n", __FUNCTION__, __FILE__, __LINE__, ECodeYellow, ECodeNormal);fflush(stdout); }
#else
	const E3dITriangle*  LITriangle = PPolygon->ITriangles;if(LITriangle == NULL)
	{
//if(PPolygon->NumOfVertices > 3) { printf("NO ITRIANGLES\n");fflush(stdout); }
		const E3dVertexNode*  LVertexNode = PPolygon->VertexNodes;if(LVertexNode)
		{
			EpglBegin(GL_POLYGON);
			EIndex  LVI = 0;const EIndex  LVN = PPolygon->NumOfVertexNodes;for(;LVI < LVN;LVI++)
			{
				const E3dVertex*  LVertex = EStruct_ByIndex(PMeshVertices, PMeshVertexSize, LVertexNode->VertexID);
				const E3dCoord3*  LVertexPosition = &LVertex->Position;

#if E3dGL_FLOAT_BITS == E3dFLOAT_BITS
				E3dGL_Vertex3v((E3dCoordinate*)LVertexPosition);
#else
				E3dGL_Vertex3(LVertexPosition->X, LVertexPosition->Y, LVertexPosition->Z);
#endif
				EStruct_Inc(LVertexNode, PVertexNodeSize);
			}
			EpglEnd();
		}
	}
	else
	{
		const E3dVertexNode*  LVertexNodes = PPolygon->VertexNodes;if(LVertexNodes)
		{
			EpglBegin(GL_TRIANGLES);
			EIndex  LPI = 0;const EIndex  LPN = PPolygon->ITriangles_Count;for(;LPI < LPN;LPI++)
			{
				const E3dVertexNodeIndex*  LVertexNodeIndices = LITriangle->VertexNodes;

				int  LI = 0;for(;LI < 3;LI++)
				{
					const E3dVertexNode*  LVertexNode = EStruct_ByIndex(LVertexNodes, PVertexNodeSize, LVertexNodeIndices[LI]);
					const E3dCoord3*      LPosition = &(EStruct_ByIndex(PMeshVertices, PMeshVertexSize,  LVertexNode->VertexID)->Position);

#if E3dGL_FLOAT_BITS == E3dFLOAT_BITS
					E3dGL_Vertex3v((E3dCoordinate*)LPosition);
#else
					E3dGL_Vertex3(LPosition->X, LPosition->Y, LPosition->Z);
#endif
				}				

				LITriangle++;
			}
			EpglEnd();
		}
	}
#endif
}



#if defined (__cplusplus) && (!(defined (__CPLUSLIBS)))
extern "C" {
#endif


//----------------------------------------
// DrawMesh.c
//----------------------------------------
extern void          E3dVertexGeometry_GLCreateVertexArray(E3dVertexGeometry* PVertexGeometry);

extern void          E3dMeshPolyGroups_GLInvalidateVertexArrays(E3dMesh* PMesh);

extern void          E3dMesh_DrawEdges(E3dGPUContext* PGPUContext, E3dMesh* PMesh,  const EcRGBAf32Color* PColor, const EcRGBAf32Color* PHiddenEdgeColor);

extern E3dDisplayFlags  E3dMesh_DrawCommon1(E3dModel* PModel, E3dMesh* PMesh,  E3dRenderer* PRenderer, const E3dDrawContext* PDrawContext, E3dGPUContext* PGPUContext, const EBool PDrawSelectedPolygonsInSolidMode);
extern void             E3dMesh_DrawComponents(E3dModel* PModel, E3dMesh* PMesh,  const E3dDrawContext* PDrawContext, E3dGPUContext* PGPUContext, const EBool PNormalGeometry, const EBool PDrawSelectedPolygonsInSolidMode);

extern void          E3dMesh_Draw(E3dModel* PModel, E3dMesh* PMesh,  E3dScene* PScene, E3dRenderer* PRenderer, E3dDrawContext* PDrawContext, E3dGPUContext* PGPUContext,  const EBool PNormalGeometry, EBool LDrawSelectedPolygonsInShadedMode);
extern void          E3dMesh_GLDrawForPick(E3dGPUContext* PGPUContext, E3dMesh* PMesh,  const E3dDrawContext* PDrawContext, const E3dDisplayFlags PDisplayFlags,  const unsigned int PPickTargets);

extern void          E3dMesh_GLVertexAttributeDraw(E3dMesh* PMesh, const E3dDisplayFlags PDisplayFlags,  const void* PColors,  const uint16_t PType, const uint16_t PComponentType);

extern void          E3dMesh_DrawConstSolid(E3dMesh* PMesh, E3dGPUContext* PGPUContext);
extern void          E3dMesh_DrawColorSolid(const E3dModel* PModel, E3dMesh* PMesh,  E3dGPUContext* PGPUContext, const uint16_t POpaqueTransparent, const EBool PTransparencyOn);
extern void          E3dMesh_DrawColorSolidTextured(E3dScene* PScene, const E3dModel* PModel, E3dMesh* PMesh,  E3dGPUContext* PGPUContext, const uint16_t POpaqueTransparent, const EBool PTransparencyOn);

extern void          E3dMesh_DrawShadedSolid(E3dModel* PModel, E3dMesh* PMesh,  E3dScene* PScene, E3dRenderer* PRenderer, const E3dDrawContext* PDrawContext, E3dGPUContext* PGPUContext, const EBool PDrawSelectedPolygonsInSolidMode);

extern void          E3dModel_DrawTextureMappers(const E3dDrawContext* PDrawContext, E3dGPUContext* PGPUContext,  E3dModel* PModel);


#if defined (__cplusplus) && (!(defined (__CPLUSLIBS)))
}	// Close scope of 'extern "C"'
#endif

#endif	// _E3D_GL_MeshDraw_h
