#include "OglTex.h"

OglTex::OglTex()
{
	intFormat = GL_RGB;
	format = GL_RGB;
	type = GL_UNSIGNED_BYTE;
	minFilter = GL_NEAREST;
	magFilter = GL_NEAREST;
	clampBorder = false;
	compMode = GL_NONE;
	color[0] = color[1] = color[2] = color[3] = 0;
}

void OglTex::init()
{
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, minFilter );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, magFilter );
	if ( compMode != GL_NONE )
		glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, compMode );

	if ( clampBorder )
	{
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
		glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color );
	}
}

void OglTexTarget::init(uint wi, uint he, uint depth)
{
	GLenum texType = depth ? GL_TEXTURE_2D_ARRAY : GL_TEXTURE_2D;

	glGenTextures( 1, &id );
	//glActiveTexture(GL_TEXTURE0);
	glBindTexture(texType, id );
	if (depth) {
		glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, intFormat, wi, he, depth, 0, format, type, 0);
	}
	else
		glTexImage2D( GL_TEXTURE_2D, 0, intFormat, wi, he, 0, format, type, 0 );
	
	glTexParameteri(texType, GL_TEXTURE_MAG_FILTER, minFilter);
	glTexParameteri(texType, GL_TEXTURE_MIN_FILTER, magFilter);
	if (compMode != GL_NONE)
		glTexParameteri(texType, GL_TEXTURE_COMPARE_MODE, compMode);

	if (clampBorder)
	{
		glTexParameteri(texType, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
		glTexParameteri(texType, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
		//glTexParameterfv(texType, GL_TEXTURE_BORDER, color);
	}
}

void OglTexImage::init(uint wi, uint he, uint i, uint depth)
{
	OglTexTarget::init(wi, he, depth);
	bind(i);
}
void OglTexImage::bind(uint i) {
	glBindImageTexture(i, id, 0, GL_FALSE, 0, GL_WRITE_ONLY, intFormat);
}

void OglTex::init( GLvoid* data, uint wi, uint he )
{
	glGenTextures( 1, &id );
	glBindTexture( GL_TEXTURE_2D, id );
	glTexImage2D( GL_TEXTURE_2D, 0, intFormat, wi, he, 0, format, type, data );

	OglTex::init( );
}

void OglTex::bind() {
	glBindTexture(GL_TEXTURE_2D, id);
}