#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_textures.h"
#include "demo_meshes.h"
#include "demo_shaders.h"
#include "demo_screen.h"

#include "math_vector3D.h"

#include "part_shading.h"

mesh3D_gen *object3;

bool shading_init = false;

int programm10 = PR_SHADING;

void shading_precalcs()
{
	//object3 = GenMesh(32, 32, GEN_FLOWER_MESH, 8.0f, 65536.0f, 65536.0f);
	object3 = GenMesh(128, 128, GEN_FLOWER_MESH, 8.0f, 6.0f, 6.0f);

	glClearColor(0.5f, 0.5f, 0.5f, 0.0f);

    shading_init = true;
}

void DrawTest10()
{
	quad2D *q = object3->quads;
	point3D *v = object3->points;
	vector3D *n = object3->vx_normals;

	int isize = object3->npls_x * object3->npls_y;


	glBegin(GL_QUADS);

	for (int i=0; i<isize; i++)
	{
		int p1 = q->p1; int p2 = q->p2; int p3 = q->p3;	int p4 = q->p4;
		glNormal3f(n[p1].x, n[p1].y, n[p1].z);	glVertex3f(v[p1].x, v[p1].y, v[p1].z);
		glNormal3f(n[p2].x, n[p2].y, n[p2].z);	glVertex3f(v[p2].x, v[p2].y, v[p2].z);
		glNormal3f(n[p3].x, n[p3].y, n[p3].z);	glVertex3f(v[p3].x, v[p3].y, v[p3].z);
		glNormal3f(n[p4].x, n[p4].y, n[p4].z);	glVertex3f(v[p4].x, v[p4].y, v[p4].z);
		q++;
	}

	glEnd();
}

void DrawShading(int ptime)
{
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	MyUseProgram(programm10);

	float px = sin((float)ptime / 768.0f) * 12.0f;
	float py = sin((float)ptime / 512.0f) * 10.0f;
	float pz = sin((float)ptime / 1280.0f) * 16.0f;

	glLoadIdentity();
	glTranslatef(px, py, -48.0f + pz);
	glRotatef(1.5f*px*py, 1.0f, 0.0f, 0.0f);
	glRotatef(1.0f*px*py, 0.0f, 1.0f, 0.0f);
	glRotatef(0.5f*px*py, 0.0f, 0.0f, 1.0f);

	DrawTest10();
}

void shading_run(int ptime)
{
	if (!shading_init) shading_precalcs();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	DrawShading(ptime);
}
