/* 
 * This file is automaticaly generated, Please dont edit this file! 
 */
#include <engine/sampler2D.h> 































































void sampler2D_constructor( sampler2D * this ) {

		glGenTextures( 1, &this->glTexture );

	}

void sampler2D_addTexture( sampler2D * this, texture2D *	texture ) {

		array_add( this->textures,  texture );

	}

int sampler2D_getTextureIndex( sampler2D * this ) {

		int numberOfTextures	=  array_length( this->textures );

		return numberOfTextures;

	}

void sampler2D_bind( sampler2D * this ) {

		this->binded = true;











		

		glActiveTexture( GL_TEXTURE0 + this->index );

		glBindTexture( this->target, this->glTexture );

		glTexParameteri( this->target, GL_TEXTURE_WRAP_S, this->WRAP_S );

		glTexParameteri( this->target, GL_TEXTURE_WRAP_T, this->WRAP_T );

		glTexParameteri( this->target, GL_TEXTURE_MIN_FILTER, this->MIN_FILTER );

		glTexParameteri( this->target, GL_TEXTURE_MAG_FILTER, this->MAG_FILTER );



		if( this->target == GL_TEXTURE_2D_ARRAY ) {

			

			int offsetX 			= 0;

			int offsetY 			= 0;

			int offsetZ 			= 0;

			int depth 				= 1;

			int levelOfDetail		= 0;

			int layerCount 			= 2;

			int mipLevelCount		= 1;

			int currentLayer		= 0;

			texture2D * texture1 = array_get( this->textures,  0 );

			int numberOfTextures	=  array_length( this->textures );

			
			if( this->cubeSize == NULL ) {

				this->cubeSize	= vector3_newPointer( texture1->width, texture1->height, numberOfTextures );

			}

			
			
			if( this->UNPACK_ALIGNMENT ) {

				glPixelStorei( GL_UNPACK_ALIGNMENT, 1 );

			} else {

				glPixelStorei( GL_UNPACK_ALIGNMENT, 0 );

			}

			glTexStorage3D( this->target, mipLevelCount, this->format, this->cubeSize->x, this->cubeSize->y, numberOfTextures );

			GLint data[ texture1->width * texture1->height * numberOfTextures ];

			glTexImage3D( GL_TEXTURE_2D_ARRAY, levelOfDetail, this->internalFormat, this->cubeSize->x, this->cubeSize->y, numberOfTextures, this->border, this->format, this->type, data);

			for (int i = 0; i < numberOfTextures; ++i)
			{

				texture2D * currentTexture = array_get( this->textures,  i );

				float test =  0;

				glTexSubImage3D( 	this->target, 
									0, 
									offsetX, test , i, 
									currentTexture->width, currentTexture->height, 1, 
									this->format, 
									this->type, 
									currentTexture->data );

			}



		} else {

			texture2D * texture = array_get( this->textures,  0 );

			glTexImage2D( this->target, 0, this->internalFormat, texture->width, texture->height, this->border, this->format, this->type, texture->data ); 

		}


		if( this->generateMipmap ) {

			glGenerateMipmap( this->target );

		}
		

		

	}

sampler2D sampler2D_new() {

	sampler2D instance;

	instance.textures = array_newPointer();

	instance.binded =  false;

	instance.filter =  GL_LINEAR;

	instance.MIN_FILTER =  GL_LINEAR;

	instance.MAG_FILTER =  GL_LINEAR;

	instance.WRAP_S =  GL_REPEAT;

	instance.WRAP_T =  GL_REPEAT;

	instance.datatype =  GL_RGBA;

	instance.format =  GL_RGBA;

	instance.internalFormat =  GL_RGBA;

	instance.target =  GL_TEXTURE_2D;

	instance.type =  GL_UNSIGNED_BYTE;

	instance.cubeSize =  NULL;

	instance.border =  false;

	instance.generateMipmap =  true;

	instance.UNPACK_ALIGNMENT =  false;

	instance.index =  0;

	sampler2D_constructor( &instance);

	return instance;

}

sampler2D * sampler2D_newPointer() {

	struct sampler2D * pointer = malloc( sizeof ( struct sampler2D ) );

	pointer->textures = array_newPointer();

	pointer->binded =  false;

	pointer->filter =  GL_LINEAR;

	pointer->MIN_FILTER =  GL_LINEAR;

	pointer->MAG_FILTER =  GL_LINEAR;

	pointer->WRAP_S =  GL_REPEAT;

	pointer->WRAP_T =  GL_REPEAT;

	pointer->datatype =  GL_RGBA;

	pointer->format =  GL_RGBA;

	pointer->internalFormat =  GL_RGBA;

	pointer->target =  GL_TEXTURE_2D;

	pointer->type =  GL_UNSIGNED_BYTE;

	pointer->cubeSize =  NULL;

	pointer->border =  false;

	pointer->generateMipmap =  true;

	pointer->UNPACK_ALIGNMENT =  false;

	pointer->index =  0;

	sampler2D_constructor( pointer );

	return pointer;

}

