#define NO_SDL_GLEXT

#include <GL/glew.h>
#include <SDL/SDL_OpenGL.h>
#include <GL/gl.h>
#include <GL/glu.h>

#include <math.h>

#include "demo_script.h"
#include "demo_shaders.h"
#include "demo_screen.h"
#include "demo_textures.h"

#include "math_vector3D.h"

#include "part_landscape.h"

bool landscape_init = false;

GLint vl_timer;

GLuint heightTexture;

int programm7 = PR_LANDSCAPE;

void landscape_precalcs()
{
	glClearColor(0.0f, 0.0f, 0.5f, 0.0f);

	heightTexture = GenTexture(1024, 1024, GEN_LANDSCAPE, 2);

	vl_timer = MyGetUniformLocation(programm7, "ptimer");

    landscape_init = true;
}

void DrawQuad7(float l)
{
	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f); glVertex2f(-l, -l);
		glTexCoord2f(1.0f, 0.0f); glVertex2f( l, -l);
		glTexCoord2f(1.0f, 1.0f); glVertex2f( l, l);
		glTexCoord2f(0.0f, 1.0f); glVertex2f(-l, l);
	glEnd();
}

#define shite -2.415f

void DrawLandscape(int ptime)
{
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	glEnable(GL_TEXTURE_2D);

	glActiveTextureARB(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, heightTexture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);


	MyUseProgram(programm7);
	MyUniform1f(vl_timer, (float)ptime);

	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, shite * 16.0f);
	DrawQuad7(16.0f);

	NotUseProgram();
}

void landscape_run(int ptime)
{
	if (!landscape_init) landscape_precalcs();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	DrawLandscape(ptime);
}
