#include "Wumbo-RenderTarget.h"
#if PLATFORM_WINDOWS
#include <GL/glew.h>
#include <GL/wglew.h>
#elif PLATFORM_IOS
/*#define GL_FRAMEBUFFER_EXT GL_FRAMEBUFFER
#define glGenFramebuffersEXT glGenFramebuffers
#define glBindFramebufferEXT glBindFramebuffer
#define GL_COLOR_ATTACHMENT0_EXT GL_COLOR_ATTACHMENT0
#define glFramebufferTexture2DEXT glFramebufferTexture2D*/
#endif




namespace Wumbo
{

#if PLATFORM_IOS
	void removeBind(GLint zero)
	{
		glBindFramebuffer(GL_FRAMEBUFFER,zero);
	}
#else
	void removeBind()
	{
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	}
#endif

	void RenderTarget::bind()
	{
#if PLATFORM_IOS
		glBindFramebuffer(GL_FRAMEBUFFER, frameID);
#else
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameID);
#endif
	}

	void RenderTarget::initialize(unsigned int ww, unsigned int hh)
	{

		//glGenFramebuffers = wglGetProcAddress("glGenFramebuffers");;

		width = ww;
		height = hh;
		initialized = true;
		
		glGenTextures(1, &textureID);
		glBindTexture(GL_TEXTURE_2D, textureID);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glBindTexture(GL_TEXTURE_2D, 0);
		
#if PLATFORM_IOS
		glGenFramebuffers(1, &frameID); // Generate one frame buffer and store the ID in fbo
		glBindFramebuffer(GL_FRAMEBUFFER, frameID); // Bind our frame buffer
		
			/*glGenRenderbuffers(1, &renderID);
			glBindRenderbuffer(GL_RENDERBUFFER, renderID);
				glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, width, height);
				glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderID);
			glBindRenderbuffer(GL_RENDERBUFFER, 0);*/
		
		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
				
		GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
		if(status != GL_FRAMEBUFFER_COMPLETE) {
			NSLog(@"failed to make complete framebuffer object %x", status);
		}
		
		glBindFramebuffer(GL_FRAMEBUFFER, 0); // Unbind our frame buffer

		

		
#else
		//glGenRenderbuffers(1, &ID);
		//glBindRenderbuffer(GL_RENDERBUFFER, ID);

		//glBindRenderbuffer(GL_RENDERBUFFER, 0);
		
		glGenFramebuffersEXT(1, &frameID); // Generate one frame buffer and store the ID in fbo
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameID); // Bind our frame buffer  
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureID,0);
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind our frame buffer
#endif

		printf("renderbuff %i tex %i\n",frameID,textureID);

		texture = new Texture(textureID,width,height);
	}

	RenderTarget::RenderTarget(unsigned int ww, unsigned int hh)
	{
		frameID = 0;
		texture = 0;
		initialized = false;
		initialize(ww,hh);
	}

	RenderTarget::RenderTarget()
	{
		frameID = 0;
		texture = 0;
		initialized = false;
		width = 1;
		height = 1;
	}

	unsigned int RenderTarget::getWidth()
	{
		return width;
	}
	unsigned int RenderTarget::getHeight()
	{
		//printf("height = %u\n",height);
		return height;
	}

	/*GLuint RenderTarget::getID()
	{
		return frameID;
	}*/
	Texture* RenderTarget::getTexture()
	{
		return texture;
	}
}