<commit_before>#include "allocore/graphics/al_Graphics.hpp"
#include "allocore/graphics/al_Light.hpp"

namespace al{

Material::Material(Graphics::Face f)
:	mAmbient(0.2),		// Do not change these initial values. (Yes, that means
	mDiffuse(0.8),		// you!) They are the default OpenGL values.
	mEmission(0),		// See http://www.khronos.org/opengles/sdk/1.1/docs/man/glMaterial.xml
	mSpecular(0),		// Specular is 0 to ensure linear lighting (no popping).
	mShine(5.),
	mFace(f),
	mUseColorMaterial(true)
{}

void Material::operator()() const {

	GLenum glface = face();

	if (useColorMaterial()) {
		glEnable(GL_COLOR_MATERIAL);	// need to enable for glColor* to work
		glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	} else {
		glDisable(GL_COLOR_MATERIAL);	// need to disable for glMaterial* to work
	}
	glMaterialfv(glface, GL_AMBIENT,	mAmbient.components);
	glMaterialfv(glface, GL_DIFFUSE,	mDiffuse.components);
	glMaterialfv(glface, GL_EMISSION,	mEmission.components);
	glMaterialfv(glface, GL_SPECULAR,	mSpecular.components);
	glMaterialf (glface, GL_SHININESS,	mShine);
}
Material& Material::ambientAndDiffuse(const Color& v){ ambient(v); return diffuse(v); }
Material& Material::ambient(const Color& v){ mAmbient=v; return *this; }
Material& Material::diffuse(const Color& v){ mDiffuse=v; return *this; }
Material& Material::emission(const Color& v){ mEmission=v; return *this; }
Material& Material::specular(const Color& v){ mSpecular=v; return *this; }
Material& Material::shininess(float v){ mShine=v; return *this; }
Material& Material::face(Graphics::Face f){ mFace=f; return *this; }

static bool * lightPool(){
	static bool x[8] = {0};
	return x;
}

static int lightID(int i){
	static int x[]={GL_LIGHT0,GL_LIGHT1,GL_LIGHT2,GL_LIGHT3,GL_LIGHT4,GL_LIGHT5,GL_LIGHT6,GL_LIGHT7};
	return x[i];
}

static int nextID(){
	for(int i=0; i<8; ++i){
		if(!lightPool()[i]){
			lightPool()[i]=true;
			return i;
		}
	}
	return 7;
}

static void freeID(int i){ lightPool()[i]=false; }


// NOTE: all defaults match the OpenGL defaults for LIGHT0
Light::Light(float x, float y, float z)
:	mID(nextID()), mAmbient(0), mDiffuse(1), mSpecular(1)
{
	pos(x,y,z);
	attenuation(1,0,0);
}

Light::~Light(){
	freeID(mID);
}

void Light::operator()() const {
	glEnable(GL_LIGHTING);
	int glID = lightID(mID);
	glLightfv(glID, GL_AMBIENT,		mAmbient.components);
	glLightfv(glID, GL_DIFFUSE,		mDiffuse.components);
	glLightfv(glID, GL_SPECULAR,	mSpecular.components);
	glLightfv(glID, GL_POSITION,	mPos);
    glLightf (glID, GL_CONSTANT_ATTENUATION,	mAtten[0]);
    glLightf (glID, GL_LINEAR_ATTENUATION,		mAtten[1]);
    glLightf (glID, GL_QUADRATIC_ATTENUATION,	mAtten[2]);

	glEnable(glID); // MUST enable each light source after configuration
//	glShadeModel(GL_SMOOTH); // enabled by default and don't want to force...
}

Light& Light::attenuation(float c0, float c1, float c2){
	mAtten[0]=c0; mAtten[1]=c1; mAtten[2]=c2; return *this;
}

Light& Light::ambient(const Color& v){ mAmbient=v; return *this; }
Light& Light::diffuse(const Color& v){ mDiffuse=v; return *this; }
Light& Light::specular(const Color& v){ mSpecular=v; return *this; }

Light& Light::spot(float xDir, float yDir, float zDir, float cutoff, float expo){
	int glID = lightID(mID);
	float direction[] = {xDir, yDir, yDir};<commit_msg>Light enables color material once<commit_after>#include "allocore/graphics/al_Graphics.hpp"
#include "allocore/graphics/al_Light.hpp"

namespace al{

Material::Material(Graphics::Face f)
:	mAmbient(0.2),		// These are the default OpenGL values. Do not change!
	mDiffuse(0.8),		// See http://www.khronos.org/opengles/sdk/1.1/docs/man/glMaterial.xml
	mEmission(0),
	mSpecular(0),		// Specular is 0 to ensure linear lighting (no popping).
	mShine(5.),
	mFace(f),
	mUseColorMaterial(true)
{}

void Material::operator()() const {

	GLenum glface = face();

	if (useColorMaterial()) {
		glEnable(GL_COLOR_MATERIAL);	// need to enable for glColor* to work
		glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
	} else {
		glDisable(GL_COLOR_MATERIAL);	// need to disable for glMaterial* to work
	}
	glMaterialfv(glface, GL_AMBIENT,	mAmbient.components);
	glMaterialfv(glface, GL_DIFFUSE,	mDiffuse.components);
	glMaterialfv(glface, GL_EMISSION,	mEmission.components);
	glMaterialfv(glface, GL_SPECULAR,	mSpecular.components);
	glMaterialf (glface, GL_SHININESS,	mShine);
}
Material& Material::ambientAndDiffuse(const Color& v){ ambient(v); return diffuse(v); }
Material& Material::ambient(const Color& v){ mAmbient=v; return *this; }
Material& Material::diffuse(const Color& v){ mDiffuse=v; return *this; }
Material& Material::emission(const Color& v){ mEmission=v; return *this; }
Material& Material::specular(const Color& v){ mSpecular=v; return *this; }
Material& Material::shininess(float v){ mShine=v; return *this; }
Material& Material::face(Graphics::Face f){ mFace=f; return *this; }



// This is used to manage GL light IDs
struct LightPool{
	static const int Nlights = 8;
	bool mIDs[Nlights];

	LightPool(){
		for(int i=0; i<Nlights; ++i) mIDs[i] = false;
	}

	static int glLightID(int i){

		// To simplify cases where materials are not used explicitly (e.g., only
		// mesh colors are used), we enable color material once.
		static bool calledColorMaterial = false;
		if(!calledColorMaterial){
			calledColorMaterial = true;
			glEnable(GL_COLOR_MATERIAL);
			glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
		}
		static int x[Nlights] =
			{GL_LIGHT0,GL_LIGHT1,GL_LIGHT2,GL_LIGHT3,GL_LIGHT4,GL_LIGHT5,GL_LIGHT6,GL_LIGHT7};
		return x[i];
	}

	int nextID(){
		for(int i=0; i<Nlights; ++i){
			if(!mIDs[i]){
				mIDs[i] = true;
				//printf("LightPool::nextID returning %d\n", i);
				return i;
			}
		}
		return Nlights-1;
	}

	void freeID(int i){
		mIDs[i] = false;
	}
};

static LightPool lightPool;


// NOTE: all defaults match the OpenGL defaults for LIGHT0
Light::Light(float x, float y, float z)
:	mID(lightPool.nextID()), mAmbient(0), mDiffuse(1), mSpecular(1)
{
	pos(x,y,z);
	attenuation(1,0,0);
}

Light::~Light(){
	lightPool.freeID(mID);
}

void Light::operator()() const {
	glEnable(GL_LIGHTING);
	int glID = lightPool.glLightID(mID);
	glLightfv(glID, GL_AMBIENT,		mAmbient.components);
	glLightfv(glID, GL_DIFFUSE,		mDiffuse.components);
	glLightfv(glID, GL_SPECULAR,	mSpecular.components);
	glLightfv(glID, GL_POSITION,	mPos);
    glLightf (glID, GL_CONSTANT_ATTENUATION,	mAtten[0]);
    glLightf (glID, GL_LINEAR_ATTENUATION,		mAtten[1]);
    glLightf (glID, GL_QUADRATIC_ATTENUATION,	mAtten[2]);

	glEnable(glID); // MUST enable each light source after configuration
//	glShadeModel(GL_SMOOTH); // enabled by default and don't want to force...
}

Light& Light::attenuation(float c0, float c1, float c2){
	mAtten[0]=c0; mAtten[1]=c1; mAtten[2]=c2; return *this;
}

Light& Light::ambient(const Color& v){ mAmbient=v; return *this; }
Light& Light::diffuse(const Color& v){ mDiffuse=v; return *this; }
Light& Light::specular(const Color& v){ mSpecular=v; return *this; }

Light& Light::spot(float xDir, float yDir, float zDir, float cutoff, float expo){
	int glID = lightPool.glLightID(mID);
	float direction[] = {xDir, yDir, yDir};
{'fraction_non_alphanumeric': 0.14673913043478262, 'fraction_numerical': 0.011566332218506132, 'mean_word_length': 5.639222941720629, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3378354', 'n_tokens_mistral': 2875, 'n_tokens_neox': 2751, 'n_words': 631}