//================================================================
// E3D library
//
// - Interactive rendering
//
// AUTHOR:  Gabor Nagy
// DATE:    2024-Oct-08 22:29:21
//
// Copyright (C) 1996-2026 by Gabor Nagy. All rights reserved.
//================================================================
#ifndef _E3DGL_Material_h
#define _E3DGL_Material_h

#include <E3D/GL/E3GL.h>


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

#ifndef sgi
static inline void _E3dGL_GetShaderLog(const GLuint PGLObject, const char* PPrefix, const char* PName, const EBool PPrintSummary, EStrLineAndStatusListA* PLineStatus)
{
	const EIndex  LMaxLength = 16384;
	GLsizei  LLength;GLcharARB  LLog[LMaxLength];glGetShaderInfoLog(PGLObject, LMaxLength, &LLength, LLog);if(LLength > 0)
	{
		const char*  LPtr = LLog;while(1)
		{
			if(EStr_NEqual(LPtr, "0(", 2))
			{
				LPtr += 2;
				const int  LLine = EStr_StringToIntI(&LPtr);if(LLine > 0)
				{
//printf("%s() %s:%d  %s%s%s\n", __FUNCTION__, __FILE__, __LINE__, ECodeGreen, EStrDupLine(LPtr), ECodeNormal);fflush(stdout);
					if(EStr_NEqual(LPtr, " : warning ", 11))
					{
						LPtr += 11;
						if(PLineStatus)
						{
							EStrLineAndStatus*  LRec = EList_StructAddA_p(PLineStatus, 8);
							LRec->Line = LLine;
							LRec->StatusCode = ELogWARNING;
							LRec->Message = EStrDupLine(LPtr);
						}
//printf("%s() %s:%d  %sWarning%s at %d  [%s]\n", __FUNCTION__, __FILE__, __LINE__, ECodeYellow, ECodeNormal, LLine, LRec->Message);fflush(stdout);
					}
					else if(EStr_NEqual(LPtr, " : error ", 9))
					{
						LPtr += 9;
						if(PLineStatus)
						{
							EStrLineAndStatus*  LRec = EList_StructAddA_p(PLineStatus, 8);
							LRec->Line = LLine;
							LRec->StatusCode = ELogERROR;
							LRec->Message = EStrDupLine(LPtr);
						}
//printf("%s() %s:%d  %sError%s at %d  [%s]\n", __FUNCTION__, __FILE__, __LINE__, ECodeRed, ECodeNormal, LLine, LRec->Message);fflush(stdout);
					}
				}
				for(;*LPtr && *LPtr != '\n';LPtr++)
				;
				if(*LPtr == '\n') LPtr++;
			}
			else break;
		}

		if(PPrintSummary)
		{
			if(PName) printf("%s() %s:%d  %s%s%s %s\n%s%s%s", __FUNCTION__, __FILE__, __LINE__, ECodeCyan, PPrefix, ECodeNormal, PName, ECodeYellow, LLog, ECodeNormal);
			else printf("%s%s%s", ECodeYellow, LLog, ECodeNormal);
			fflush(stdout);
		}
	}
}
#endif

#define _E3dGL_GetProgramLog(mObject, mPrefix, mName) do\
{\
	GLsizei  lLength;GLcharARB  lLog[8192];glGetProgramInfoLog(mObject, 8192, &lLength, lLog);if(lLength)\
	{\
		printf("%s() %s:%d %s%s%s %s\n%s%s%s", __FUNCTION__, __FILE__, __LINE__, ECodeCyan, mPrefix, ECodeNormal, mName, ECodeYellow, lLog, ECodeNormal);fflush(stdout);\
	}\
} while(0)




#define E3dGLShaderVariantCore\
	E3dShaderVariantCore;\
	GLuint  Program;\
\
	GLint   Param_VtA_Position, Param_VtA_Normal, Param_VtA_Color, Param_VtA_TexCoord0;\
	GLint   Param_VtU_ModelViewProjectionMatrix, Param_VtU_LocalToWorldMatrix

typedef struct
{
	E3dGLShaderVariantCore;

	GLint  Param_FgU_Color,
	       Param_FgU_Light0WorldPosition, Param_FgU_Light0Color;
} E3dGLCoreShader;


typedef struct
{
	E3dGLCoreShader  Sh_Unlit;
	E3dGLCoreShader  Sh_UnlitTextured;
	E3dGLCoreShader  Sh_Lambert;
	E3dGLCoreShader  Sh_LambertTextured;
} E3dGLGlobals;


extern E3dGLGlobals*  _E3dGL_Globals;


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

#endif	// _E3DGL_Material_h
