/*
    QGLView
    Ivan Pelipenko peri4ko@yandex.ru

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GLRENDERERBASE_H
#define GLRENDERERBASE_H

#include "glcamera.h"
#include "glshaders.h"

class GLRendererBase
	: public QObject
	, protected QOpenGLExtraFunctions {
	friend class QGLView;
	Q_OBJECT

public:
	GLRendererBase(QGLView * view_);
	virtual void prepareScene() { ; }
	virtual void renderScene() = 0;

	struct RenderingParameters {
		RenderingParameters();
		void prepare();
		void setUniform(QOpenGLShaderProgram * prog);
		int pass;
		int light_pass;
		bool light;
		bool fog;
		bool textures;
		bool prev_light;
		bool prev_fog;
		GLuint prev_tex[32];
		void * shaders;
		QMatrix4x4 view_matrix, view_matrix_i, prev_view_matrix;
		QMatrix4x4 proj_matrix, proj_matrix_i, prev_proj_matrix;
		QMatrix4x4 viewproj_matrix, viewproj_matrix_i;
		QMatrix3x3 normal_matrix;
		QMatrix4x4 cam_offset_matrix;
		QOpenGLShaderProgram * cur_shader;
	};

	RenderingParameters rp;

protected:
	virtual void setupLight(const Light & l, int inpass_index, int gl_index);
	virtual void setupAmbientLight(const QColor & a, bool first_pass);
	virtual void setupShadersLights(int lights_count);
	virtual void setupTextures(GLObjectBase & object, GLRendererBase::RenderingParameters & rp, bool first_object = false);
	virtual void setupShadersTextures(GLObjectBase & object, GLRendererBase::RenderingParameters & rp) {}
	virtual void reloadShaders() {}
	virtual void init(int width, int height) {}
	virtual void resize(int width, int height) {}

	void setupLights(int pass, int lights_per_pass);
	inline void applyFilteringParameters();
	void renderObjects(int pass, int light_pass, void * shaders = 0, bool textures = true, bool light = true, bool fog = true);
	void renderSingleObject(GLObjectBase & o, RenderingParameters & rpl);
	void renderShadow(Light * l, QOpenGLShaderProgram * prog = 0, QMatrix4x4 mat = QMatrix4x4());
	void renderSingleShadow(GLObjectBase & o, RenderingParameters & rpl);

	QGLView & view;
	QImage white_image, violent_image;
	GLuint white_image_id, violent_image_id;
};

#endif // GLRENDERERBASE_H
