Просмотр исходного кода

Implement basic GL texture support

Daniele Bartolini 13 лет назад
Родитель
Сommit
298fd93134
3 измененных файлов с 239 добавлено и 247 удалено
  1. 160 172
      src/renderers/gl/GLRenderer.cpp
  2. 77 74
      src/renderers/gl/GLRenderer.h
  3. 2 1
      src/renderers/gl/GLUtils.h

+ 160 - 172
src/renderers/gl/GLRenderer.cpp

@@ -23,14 +23,15 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
+#include "Config.h"
+
 #include <GL/glew.h>
 #include <cassert>
+#include <algorithm>
 #include "Types.h"
 #include "GLIndexBuffer.h"
 #include "GLOcclusionQuery.h"
 #include "GLRenderer.h"
-#include "GLTexture.h"
-//#include "GLTextureManager.h"
 #include "GLUtils.h"
 #include "GLVertexBuffer.h"
 #include "Log.h"
@@ -38,7 +39,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Rect.h"
 #include "Allocator.h"
 
-#include "Config.h"
+#include "TextureResource.h"
+
 #if defined(WINDOWS)
 	//Define the missing constants in vs' gl.h
 	#define GL_TEXTURE_3D					0x806F
@@ -63,10 +65,7 @@ GLRenderer::GLRenderer() :
 	mMaxVertexVertices(0),
 	mMaxAnisotropy(0.0f),
 
-	mOcclusionQueryList(get_default_allocator()),
-	mVertexBufferList(get_default_allocator()),
-	mIndexBufferList(get_default_allocator()),
-
+	m_texture_count(0),
 	mActiveTextureUnit(0),
 
 	mLinesCount(0)
@@ -76,13 +75,14 @@ GLRenderer::GLRenderer() :
 	mMinMaxLineWidth[0] = 0.0f;
 	mMinMaxLineWidth[1] = 0.0f;
 
+	// Initialize texture units
 	for (uint32_t i = 0; i < MAX_TEXTURE_UNITS; i++)
 	{
-//		mTextureUnit[i] = 0;
+		mTextureUnit[i] = 0;
 		mTextureUnitTarget[i] = GL_TEXTURE_2D;
 	}
 
-	// This code snippet initializes the matrices
+	// Initialize the matrices
 	for (uint32_t i = 0; i < MT_COUNT; i++)
 	{
 		mMatrix[i].load_identity();
@@ -155,7 +155,7 @@ GLRenderer::GLRenderer() :
 	float amb[] = { 0.0f, 0.0f, 0.0f, 1.0f };
 	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
 
-	// Some hint32_ts
+	// Some hints
 	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
 
 	// Set the framebuffer clear color
@@ -178,37 +178,23 @@ GLRenderer::GLRenderer() :
 //-----------------------------------------------------------------------------
 GLRenderer::~GLRenderer()
 {
-	for (uint32_t i = 0; i < mOcclusionQueryList.size(); i++)
-	{
-		delete mOcclusionQueryList[i];
-	}
-
-	for (uint32_t i = 0; i < mVertexBufferList.size(); i++)
-	{
-		delete mVertexBufferList[i];
-	}
-
-	for (uint32_t i = 0; i < mIndexBufferList.size(); i++)
-	{
-		delete mIndexBufferList[i];
-	}
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetViewport(const Rect& absArea)
+void GLRenderer::set_viewport(const Rect& absArea)
 {
 	glViewport((int32_t)absArea.min.x, (int32_t)absArea.min.y, (int32_t)absArea.max.x, (int32_t)absArea.max.y);
 	glScissor((int32_t)absArea.min.x, (int32_t)absArea.min.y, (int32_t)absArea.max.x, (int32_t)absArea.max.y);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::SetClearColor(const Color4& color)
+void GLRenderer::set_clear_color(const Color4& color)
 {
 	glClearColor(color.r, color.g, color.b, color.a);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetMaterialParams(const Color4& ambient, const Color4& diffuse, const Color4& specular,
+void GLRenderer::set_material_params(const Color4& ambient, const Color4& diffuse, const Color4& specular,
 				const Color4& emission, int32_t shininess)
 {
 	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, &ambient.r);
@@ -219,13 +205,13 @@ void GLRenderer::_SetMaterialParams(const Color4& ambient, const Color4& diffuse
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetAmbientLight(const Color4& color)
+void GLRenderer::set_ambient_light(const Color4& color)
 {
 	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color.to_float_ptr());
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetLighting(bool lighting)
+void GLRenderer::set_lighting(bool lighting)
 {
 	if (lighting)
 	{
@@ -238,9 +224,9 @@ void GLRenderer::_SetLighting(bool lighting)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetTexturing(uint32_t unit, bool texturing)
+void GLRenderer::set_texturing(uint32_t unit, bool texturing)
 {
-	if (!ActivateTextureUnit(unit))
+	if (!activate_texture_unit(unit))
 		return;
 
 	if (texturing)
@@ -253,65 +239,64 @@ void GLRenderer::_SetTexturing(uint32_t unit, bool texturing)
 	}
 }
 
-////-----------------------------------------------------------------------------
-//void GLRenderer::_SetTexture(uint32_t unit, Texture* texture)
-//{
-//	if (!ActivateTextureUnit(unit))
-//		return;
+//-----------------------------------------------------------------------------
+void GLRenderer::set_texture(uint32_t unit, TextureId texture)
+{
+	if (!activate_texture_unit(unit))
+	{
+		return;
+	}
 
-//	if (texture != NULL)
-//	{
-//		mTextureUnit[unit] = texture;
-//		mTextureUnitTarget[unit] = static_cast<const GLTexture*>(texture)->GetGLTarget();
+	mTextureUnitTarget[unit] = GL_TEXTURE_2D;
+	mTextureUnit[unit] = m_textures[texture.index].texture_object;
 
-//		glEnable(mTextureUnitTarget[unit]);
-//		glBindTexture(mTextureUnitTarget[unit], static_cast<const GLTexture*>(texture)->GetGLObject());
-//	}
-//}
+	glEnable(mTextureUnitTarget[unit]);
+	glBindTexture(mTextureUnitTarget[unit], mTextureUnit[unit]);
+}
 
-////-----------------------------------------------------------------------------
-//void GLRenderer::_SetTextureMode(uint32_t unit, TextureMode mode, const Color4& blendColor)
-//{
-//	if (!ActivateTextureUnit(unit))
-//		return;
+//-----------------------------------------------------------------------------
+void GLRenderer::set_texture_mode(uint32_t unit, TextureMode mode, const Color4& blendColor)
+{
+	if (!activate_texture_unit(unit))
+		return;
 
-//	GLint envMode = GL::GetTextureMode(mode);
+	GLint envMode = GL::GetTextureMode(mode);
 
-//	if (envMode == GL_BLEND)
-//	{
-//		glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &blendColor.r);
-//	}
+	if (envMode == GL_BLEND)
+	{
+		glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, &blendColor.r);
+	}
 
-//	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode);
-//}
+	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, envMode);
+}
 
-////-----------------------------------------------------------------------------
-//void GLRenderer::_SetTextureWrap(uint32_t unit, TextureWrap wrap)
-//{
-//	GLenum glWrap = GL::GetTextureWrap(wrap);
+//-----------------------------------------------------------------------------
+void GLRenderer::set_texture_wrap(uint32_t unit, TextureWrap wrap)
+{
+	GLenum glWrap = GL::GetTextureWrap(wrap);
 
-//	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_WRAP_S, glWrap);
-//	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_WRAP_T, glWrap);
-//	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_WRAP_R, glWrap);
-//}
+	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_WRAP_S, glWrap);
+	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_WRAP_T, glWrap);
+	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_WRAP_R, glWrap);
+}
 
-////-----------------------------------------------------------------------------
-//void GLRenderer::_SetTextureFilter(uint32_t unit, TextureFilter filter)
-//{
-//	if (!ActivateTextureUnit(unit))
-//		return;
+//-----------------------------------------------------------------------------
+void GLRenderer::set_texture_filter(uint32_t unit, TextureFilter filter)
+{
+	if (!activate_texture_unit(unit))
+		return;
 
-//	GLint minFilter;
-//	GLint magFilter;
+	GLint minFilter;
+	GLint magFilter;
 
-//	GL::GetTextureFilter(filter, minFilter, magFilter);
+	GL::GetTextureFilter(filter, minFilter, magFilter);
 
-//	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_MIN_FILTER, minFilter);
-//	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_MAG_FILTER, magFilter);
-//}
+	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_MIN_FILTER, minFilter);
+	glTexParameteri(mTextureUnitTarget[unit], GL_TEXTURE_MAG_FILTER, magFilter);
+}
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetBackfaceCulling(bool culling)
+void GLRenderer::set_backface_culling(bool culling)
 {
 	if (culling)
 	{
@@ -324,7 +309,7 @@ void GLRenderer::_SetBackfaceCulling(bool culling)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetSeparateSpecularColor(bool separate)
+void GLRenderer::set_separate_specular_color(bool separate)
 {
 	if (separate)
 	{
@@ -337,7 +322,7 @@ void GLRenderer::_SetSeparateSpecularColor(bool separate)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetDepthTest(bool test)
+void GLRenderer::set_depth_test(bool test)
 {
 	if (test)
 	{
@@ -350,13 +335,13 @@ void GLRenderer::_SetDepthTest(bool test)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetDepthWrite(bool write)
+void GLRenderer::set_depth_write(bool write)
 {
 	glDepthMask((GLboolean) write);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetDepthFunc(CompareFunction func)
+void GLRenderer::set_depth_func(CompareFunction func)
 {
 	GLenum glFunc = GL::GetCompareFunction(func);
 
@@ -364,7 +349,7 @@ void GLRenderer::_SetDepthFunc(CompareFunction func)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetRescaleNormals(bool rescale)
+void GLRenderer::set_rescale_normals(bool rescale)
 {
 	if (rescale)
 	{
@@ -377,7 +362,7 @@ void GLRenderer::_SetRescaleNormals(bool rescale)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetBlending(bool blending)
+void GLRenderer::set_blending(bool blending)
 {
 	if (blending)
 	{
@@ -390,7 +375,7 @@ void GLRenderer::_SetBlending(bool blending)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetBlendingParams(BlendEquation equation, BlendFunction src, BlendFunction dst, const Color4& color)
+void GLRenderer::set_blending_params(BlendEquation equation, BlendFunction src, BlendFunction dst, const Color4& color)
 {
 	GLenum glEquation = GL::GetBlendEquation(equation);
 
@@ -405,7 +390,7 @@ void GLRenderer::_SetBlendingParams(BlendEquation equation, BlendFunction src, B
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetColorWrite(bool write)
+void GLRenderer::set_color_write(bool write)
 {
 	if (write)
 	{
@@ -418,7 +403,7 @@ void GLRenderer::_SetColorWrite(bool write)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetFog(bool fog)
+void GLRenderer::set_fog(bool fog)
 {
 	if (fog)
 	{
@@ -431,7 +416,7 @@ void GLRenderer::_SetFog(bool fog)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetFogParams(FogMode mode, float density, float start, float end, const Color4& color)
+void GLRenderer::set_fog_params(FogMode mode, float density, float start, float end, const Color4& color)
 {
 	GLenum glMode = GL::GetFogMode(mode);
 
@@ -443,7 +428,7 @@ void GLRenderer::_SetFogParams(FogMode mode, float density, float start, float e
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetAlphaTest(bool test)
+void GLRenderer::set_alpha_test(bool test)
 {
 	if (test)
 	{
@@ -456,7 +441,7 @@ void GLRenderer::_SetAlphaTest(bool test)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetAlphaParams(CompareFunction func, float ref)
+void GLRenderer::set_alpha_params(CompareFunction func, float ref)
 {
 	GLenum glFunc = GL::GetCompareFunction(func);
 
@@ -464,7 +449,7 @@ void GLRenderer::_SetAlphaParams(CompareFunction func, float ref)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetShadingType(ShadingType type)
+void GLRenderer::set_shading_type(ShadingType type)
 {
 	GLenum glMode = GL_SMOOTH;
 
@@ -477,7 +462,7 @@ void GLRenderer::_SetShadingType(ShadingType type)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetPolygonMode(PolygonMode mode)
+void GLRenderer::set_polygon_mode(PolygonMode mode)
 {
 	GLenum glMode = GL::GetPolygonMode(mode);
 
@@ -485,7 +470,7 @@ void GLRenderer::_SetPolygonMode(PolygonMode mode)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetFrontFace(FrontFace face)
+void GLRenderer::set_front_face(FrontFace face)
 {
 	GLenum glFace = GL_CCW;
 
@@ -498,13 +483,13 @@ void GLRenderer::_SetFrontFace(FrontFace face)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetViewportParams(int32_t x, int32_t y, int32_t width, int32_t height)
+void GLRenderer::set_viewport_params(int32_t x, int32_t y, int32_t width, int32_t height)
 {
 	glViewport(x, y, width, height);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetScissor(bool scissor)
+void GLRenderer::set_scissor(bool scissor)
 {
 	if (scissor)
 	{
@@ -517,13 +502,13 @@ void GLRenderer::_SetScissor(bool scissor)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetScissorParams(int32_t x, int32_t y, int32_t width, int32_t height)
+void GLRenderer::set_scissor_params(int32_t x, int32_t y, int32_t width, int32_t height)
 {
 	glScissor(x, y, width, height);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetPointSprite(bool sprite)
+void GLRenderer::set_point_sprite(bool sprite)
 {
 	if (sprite)
 	{
@@ -538,41 +523,41 @@ void GLRenderer::_SetPointSprite(bool sprite)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetPointSize(float size)
+void GLRenderer::set_point_size(float size)
 {
 	glPointSize(size);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetPointParams(float min, float max)
+void GLRenderer::set_point_params(float min, float max)
 {
 	glPointParameterf(GL_POINT_SIZE_MIN, min);
 	glPointParameterf(GL_POINT_SIZE_MAX, max);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_BeginFrame()
+void GLRenderer::begin_frame()
 {
 	// Clear frame/depth buffer
 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_EndFrame()
+void GLRenderer::end_frame()
 {
 	glFinish();
 
-	CheckGLErrors();
+	check_gl_errors();
 }
 
 //-----------------------------------------------------------------------------
-Mat4 GLRenderer::GetMatrix(MatrixType type) const
+Mat4 GLRenderer::get_matrix(MatrixType type) const
 {
 	return mMatrix[type];
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::SetMatrix(MatrixType type, const Mat4& matrix)
+void GLRenderer::set_matrix(MatrixType type, const Mat4& matrix)
 {
 	mMatrix[type] = matrix;
 
@@ -602,28 +587,28 @@ void GLRenderer::SetMatrix(MatrixType type, const Mat4& matrix)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::PushMatrix()
+void GLRenderer::push_matrix()
 {
 	assert(mModelMatrixStackIndex != MAX_MODEL_MATRIX_STACK_DEPTH);
 
-	//Copy the current matrix int32_to the stack, and move to the next location
+	//Copy the current matrix into the stack, and move to the next location
 	glGetFloatv(GL_MODELVIEW_MATRIX, mModelMatrixStack[mModelMatrixStackIndex].to_float_ptr());
 	mModelMatrixStackIndex++;
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::PopMatrix()
+void GLRenderer::pop_matrix()
 {
 	// Note: Is checking for push-pop count necessary? Maybe it should signal matrix stack underflow.
 	//glPopMatrix();
 	assert(mModelMatrixStackIndex > 0);
 
 	mModelMatrixStackIndex--;
-	SetMatrix(MT_MODEL, mModelMatrixStack[mModelMatrixStackIndex]);
+	set_matrix(MT_MODEL, mModelMatrixStack[mModelMatrixStackIndex]);
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::SelectMatrix(MatrixType type)
+void GLRenderer::select_matrix(MatrixType type)
 {
 	switch (type)
 	{
@@ -646,7 +631,7 @@ void GLRenderer::SelectMatrix(MatrixType type)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::RenderVertexIndexBuffer(const VertexBuffer* vertices, const IndexBuffer* indices)
+void GLRenderer::render_vertex_index_buffer(const VertexBuffer* vertices, const IndexBuffer* indices)
 {
 	assert(vertices != NULL);
 	assert(indices != NULL);
@@ -668,7 +653,7 @@ void GLRenderer::RenderVertexIndexBuffer(const VertexBuffer* vertices, const Ind
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::RenderPointBuffer(const VertexBuffer* buffer)
+void GLRenderer::render_point_buffer(const VertexBuffer* buffer)
 {
 	if (buffer == NULL)
 		return;
@@ -687,58 +672,7 @@ void GLRenderer::RenderPointBuffer(const VertexBuffer* buffer)
 }
 
 //-----------------------------------------------------------------------------
-OcclusionQuery* GLRenderer::CreateOcclusionQuery()
-{
-	OcclusionQuery* oc = new GLOcclusionQuery();
-
-	if (!oc)
-		return 0;
-
-	mOcclusionQueryList.push_back(oc);
-	return oc;
-}
-
-//-----------------------------------------------------------------------------
-VertexBuffer*  GLRenderer::CreateVertexBuffer()
-{
-	VertexBuffer* vb = new GLVertexBuffer();
-
-	if (!vb)
-		return 0;
-
-	mVertexBufferList.push_back(vb);
-	return vb;
-}
-
-//-----------------------------------------------------------------------------
-IndexBuffer*  GLRenderer::CreateIndexBuffer()
-{
-	IndexBuffer* ib = new GLIndexBuffer();
-
-	if (!ib)
-		return 0;
-
-	mIndexBufferList.push_back(ib);
-	return ib;
-}
-
-////-----------------------------------------------------------------------------
-//void GLRenderer::SetTexture(uint32_t layer, Texture* texture)
-//{
-//	if (texture == NULL)
-//	{
-//		return;
-//	}
-
-//	_SetTexturing(layer, true);
-//	_SetTexture(layer, texture);
-//	_SetTextureMode(layer, texture->GetMode(), texture->GetBlendColor());
-//	_SetTextureWrap(layer, texture->GetWrap());
-//	_SetTextureFilter(layer, texture->GetFilter());
-//}
-
-//-----------------------------------------------------------------------------
-void GLRenderer::SetScissorBox(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
+void GLRenderer::set_scissor_box(uint32_t x, uint32_t y, uint32_t width, uint32_t height)
 {
 	int32_t vals[4];
 	glGetIntegerv(GL_VIEWPORT, vals);
@@ -746,7 +680,7 @@ void GLRenderer::SetScissorBox(uint32_t x, uint32_t y, uint32_t width, uint32_t
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::GetScissorBox(uint32_t& x, uint32_t& y, uint32_t& width, uint32_t& height)
+void GLRenderer::get_scissor_box(uint32_t& x, uint32_t& y, uint32_t& width, uint32_t& height)
 {
 	int32_t vals[4];
 	glGetIntegerv(GL_SCISSOR_BOX, vals);
@@ -759,7 +693,7 @@ void GLRenderer::GetScissorBox(uint32_t& x, uint32_t& y, uint32_t& width, uint32
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::DrawRectangle(const Point2& position, const Point2& dimensions, int32_t drawMode,
+void GLRenderer::draw_rectangle(const Point2& position, const Point2& dimensions, int32_t drawMode,
 															 const Color4& borderColor, const Color4& fillColor)
 {
 	if (drawMode & DM_FILL)
@@ -792,7 +726,7 @@ void GLRenderer::DrawRectangle(const Point2& position, const Point2& dimensions,
 }
 
 //-----------------------------------------------------------------------------
-bool GLRenderer::ActivateTextureUnit(uint32_t unit)
+bool GLRenderer::activate_texture_unit(uint32_t unit)
 {
 	if (unit >= (uint32_t) mMaxTextureUnits)
 	{
@@ -806,7 +740,7 @@ bool GLRenderer::ActivateTextureUnit(uint32_t unit)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetLight(uint32_t light, bool active)
+void GLRenderer::set_light(uint32_t light, bool active)
 {
 	if (light >= (uint32_t) mMaxLights)
 	{
@@ -824,7 +758,7 @@ void GLRenderer::_SetLight(uint32_t light, bool active)
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetLightParams(uint32_t light, LightType type, const Vec3& position)
+void GLRenderer::set_light_params(uint32_t light, LightType type, const Vec3& position)
 {
 	static float pos[4] =
 	{
@@ -843,7 +777,7 @@ void GLRenderer::_SetLightParams(uint32_t light, LightType type, const Vec3& pos
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetLightColor(uint32_t light, const Color4& ambient, const Color4& diffuse, const Color4& specular)
+void GLRenderer::set_light_color(uint32_t light, const Color4& ambient, const Color4& diffuse, const Color4& specular)
 {
 	glLightfv(GL_LIGHT0 + light, GL_AMBIENT, ambient.to_float_ptr());
 	glLightfv(GL_LIGHT0 + light, GL_DIFFUSE, diffuse.to_float_ptr());
@@ -851,7 +785,7 @@ void GLRenderer::_SetLightColor(uint32_t light, const Color4& ambient, const Col
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::_SetLightAttenuation(uint32_t light, float constant, float linear, float quadratic)
+void GLRenderer::set_light_attenuation(uint32_t light, float constant, float linear, float quadratic)
 {
 	glLightf(GL_LIGHT0 + light, GL_CONSTANT_ATTENUATION, constant);
 	glLightf(GL_LIGHT0 + light, GL_LINEAR_ATTENUATION, linear);
@@ -859,7 +793,62 @@ void GLRenderer::_SetLightAttenuation(uint32_t light, float constant, float line
 }
 
 //-----------------------------------------------------------------------------
-void GLRenderer::CheckGLErrors()
+TextureId GLRenderer::load_texture(TextureResource* texture)
+{
+	// Search for an already existent texture
+	for (uint32_t i = 0; i < MAX_TEXTURES; i++)
+	{
+		if (m_textures[i].texture_resource == texture)
+		{
+			return m_textures[i].id;
+		}
+	}
+
+	// If texture not found, create a new one
+	GLuint gl_texture_object;
+
+	glGenTextures(1, &gl_texture_object);
+
+	glBindTexture(GL_TEXTURE_2D, gl_texture_object);
+
+	GLint gl_texture_format = GL::GetPixelFormat(texture->format());
+
+	// FIXME FIXME FIXME
+	//if (mGenerateMipMaps)
+	//{
+	//	glTexParameteri(target, GL_GENERATE_MIPMAP, GL_TRUE);
+	//}
+
+	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->width(), texture->height(), 0,
+				 gl_texture_format, GL_UNSIGNED_BYTE, texture->data());
+
+	TextureId id;
+	id.index = m_texture_count;
+	id.id = 0;
+
+	m_textures[id.index].texture_object = gl_texture_object;
+	m_textures[id.index].texture_resource = texture;
+	m_textures[id.index].id = id;
+
+	m_texture_count++;
+
+	return id;
+}
+
+//-----------------------------------------------------------------------------
+void GLRenderer::unload_texture(TextureResource* texture)
+{
+
+}
+
+//-----------------------------------------------------------------------------
+TextureId GLRenderer::reload_texture(TextureResource* old_texture, TextureResource* new_texture)
+{
+
+}
+
+//-----------------------------------------------------------------------------
+void GLRenderer::check_gl_errors()
 {
 	GLenum error;
 
@@ -892,7 +881,7 @@ void GLRenderer::CheckGLErrors()
 	}
 }
 
-void GLRenderer::AddDebugLine(const Vec3& start, const Vec3& end, const Color4& color)
+void GLRenderer::add_debug_line(const Vec3& start, const Vec3& end, const Color4& color)
 {
 	if (mLinesCount < 256)
 	{
@@ -904,7 +893,7 @@ void GLRenderer::AddDebugLine(const Vec3& start, const Vec3& end, const Color4&
 	}
 }
 
-void GLRenderer::DrawDebugLines()
+void GLRenderer::draw_debug_lines()
 {
 	if (mLinesCount == 0)
 	{
@@ -925,4 +914,3 @@ void GLRenderer::DrawDebugLines()
 }
 
 } // namespace crown
-

+ 77 - 74
src/renderers/gl/GLRenderer.h

@@ -26,122 +26,123 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include <GL/glew.h>
-#include "List.h"
 #include "Renderer.h"
 
+#define MAX_TEXTURES 1024
 #define MAX_TEXTURE_UNITS 8
 #define MAX_MODEL_MATRIX_STACK_DEPTH 100
 
 namespace crown
 {
 
-class GLSupport;
+class TextureResource;
 
-class GLRenderer : public Renderer
+/// OpenGL texture
+struct GLTexture
 {
+	TextureId			id;
+	GLuint				texture_object;
+	TextureResource*	texture_resource;
+};
 
-	typedef List<OcclusionQuery*> OcclusionQueryList;
-	typedef List<VertexBuffer*> VertexBufferList;
-	typedef List<IndexBuffer*> IndexBufferList;
-
+/// OpenGL renderer
+class GLRenderer : public Renderer
+{
 public:
 
 						GLRenderer();
 						~GLRenderer();
 
-	void				_BeginFrame();
-	void				_EndFrame();
-
-	void				SetClearColor(const Color4& color);
-	void				_SetViewport(const Rect& absArea);
-	void				_SetMaterialParams(const Color4& ambient, const Color4& diffuse, const Color4& specular, const Color4& emission, int32_t shininess);
-	void				_SetLighting(bool lighting);
-	void				_SetAmbientLight(const Color4& color);
+	void				begin_frame();
+	void				end_frame();
 
-	void				_SetTexturing(uint32_t unit, bool texturing);
-//	void				_SetTexture(uint32_t unit, Texture* texture);
-//	void				_SetTextureMode(uint32_t unit, TextureMode mode, const Color4& blendColor);
-//	void				_SetTextureWrap(uint32_t unit, TextureWrap wrap);
-//	void				_SetTextureFilter(uint32_t unit, TextureFilter filter);
+	void				set_clear_color(const Color4& color);
+	void				set_viewport(const Rect& absArea);
+	void				set_material_params(const Color4& ambient, const Color4& diffuse, const Color4& specular, const Color4& emission, int32_t shininess);
+	void				set_lighting(bool lighting);
+	void				set_ambient_light(const Color4& color);
 
-	void				_SetLight(uint32_t light, bool active);
-	void				_SetLightParams(uint32_t light, LightType type, const Vec3& position);
-	void				_SetLightColor(uint32_t light, const Color4& ambient, const Color4& diffuse, const Color4& specular);
-	void				_SetLightAttenuation(uint32_t light, float constant, float linear, float quadratic);
+	void				set_texturing(uint32_t unit, bool texturing);
+	void				set_texture(uint32_t unit, TextureId texture);
+	void				set_texture_mode(uint32_t unit, TextureMode mode, const Color4& blendColor);
+	void				set_texture_wrap(uint32_t unit, TextureWrap wrap);
+	void				set_texture_filter(uint32_t unit, TextureFilter filter);
 
-	void				_SetBackfaceCulling(bool culling);
+	void				set_light(uint32_t light, bool active);
+	void				set_light_params(uint32_t light, LightType type, const Vec3& position);
+	void				set_light_color(uint32_t light, const Color4& ambient, const Color4& diffuse, const Color4& specular);
+	void				set_light_attenuation(uint32_t light, float constant, float linear, float quadratic);
 
-	void				_SetSeparateSpecularColor(bool separate);
+	void				set_backface_culling(bool culling);
 
-	void				_SetDepthTest(bool test);
-	void				_SetDepthWrite(bool write);
-	void				_SetDepthFunc(CompareFunction func);
+	void				set_separate_specular_color(bool separate);
 
-	void				_SetRescaleNormals(bool rescale);
+	void				set_depth_test(bool test);
+	void				set_depth_write(bool write);
+	void				set_depth_func(CompareFunction func);
 
-	void				_SetBlending(bool blending);
-	void				_SetBlendingParams(BlendEquation equation, BlendFunction src, BlendFunction dst, const Color4& color);
-	void				_SetColorWrite(bool write);
+	void				set_rescale_normals(bool rescale);
 
-	void				_SetFog(bool fog);
-	void				_SetFogParams(FogMode mode, float density, float start, float end, const Color4& color);
+	void				set_blending(bool blending);
+	void				set_blending_params(BlendEquation equation, BlendFunction src, BlendFunction dst, const Color4& color);
+	void				set_color_write(bool write);
 
-	void				_SetAlphaTest(bool test);
-	void				_SetAlphaParams(CompareFunction func, float ref);
+	void				set_fog(bool fog);
+	void				set_fog_params(FogMode mode, float density, float start, float end, const Color4& color);
 
-	void				_SetShadingType(ShadingType type);
-	void				_SetPolygonMode(PolygonMode mode);
-	void				_SetFrontFace(FrontFace face);
+	void				set_alpha_test(bool test);
+	void				set_alpha_params(CompareFunction func, float ref);
 
-	void				_SetViewportParams(int32_t x, int32_t y, int32_t width, int32_t height);
+	void				set_shading_type(ShadingType type);
+	void				set_polygon_mode(PolygonMode mode);
+	void				set_front_face(FrontFace face);
 
-	void				_SetScissor(bool scissor);
-	void				_SetScissorParams(int32_t x, int32_t y, int32_t width, int32_t height);
+	void				set_viewport_params(int32_t x, int32_t y, int32_t width, int32_t height);
 
-	void				_SetPointSprite(bool sprite);
-	void				_SetPointSize(float size);
-	void				_SetPointParams(float min, float max);
+	void				set_scissor(bool scissor);
+	void				set_scissor_params(int32_t x, int32_t y, int32_t width, int32_t height);
 
-	VertexBuffer*		CreateVertexBuffer();
-	IndexBuffer*		CreateIndexBuffer();
+	void				set_point_sprite(bool sprite);
+	void				set_point_size(float size);
+	void				set_point_params(float min, float max);
 
-	OcclusionQuery*		CreateOcclusionQuery();
+	Mat4				get_matrix(MatrixType type) const;
+	void				set_matrix(MatrixType type, const Mat4& matrix);
 
-//	void				SetTexture(uint32_t layer, Texture* texture);
+	void				push_matrix();
+	void				pop_matrix();
 
-	Mat4				GetMatrix(MatrixType type) const;
-	void				SetMatrix(MatrixType type, const Mat4& matrix);
+	void				select_matrix(MatrixType type);
 
-	void				PushMatrix();
-	void				PopMatrix();
+	void				render_vertex_index_buffer(const VertexBuffer* vertices, const IndexBuffer* indices);
+	void				render_point_buffer(const VertexBuffer* buffer);
 
-	void				SelectMatrix(MatrixType type);
-
-	void				RenderVertexIndexBuffer(const VertexBuffer* vertices, const IndexBuffer* indices);
-	void				RenderPointBuffer(const VertexBuffer* buffer);
-
-	void				SetScissorBox(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
-	void				GetScissorBox(uint32_t& x, uint32_t& y, uint32_t& width, uint32_t& height);
-	void				DrawRectangle(const Point2& position, const Point2& dimensions, int32_t drawMode,
+	void				set_scissor_box(uint32_t x, uint32_t y, uint32_t width, uint32_t height);
+	void				get_scissor_box(uint32_t& x, uint32_t& y, uint32_t& width, uint32_t& height);
+	void				draw_rectangle(const Point2& position, const Point2& dimensions, int32_t drawMode,
 														 const Color4& borderColor, const Color4& fillColor);
 
-	void				AddDebugLine(const Vec3& start, const Vec3& end, const Color4& color);
-	void				DrawDebugLines();
+	void				add_debug_line(const Vec3& start, const Vec3& end, const Color4& color);
+	void				draw_debug_lines();
 
-private:
+	TextureId			load_texture(TextureResource* texture);
+	void				unload_texture(TextureResource* texture);
+	TextureId			reload_texture(TextureResource* old_texture, TextureResource* new_texture);
 
-	bool				ActivateTextureUnit(uint32_t unit);		//!< Activates a texture unit and returns true if succes
-	bool				ActivateLight(uint32_t light);
+	bool				activate_texture_unit(uint32_t unit);		//!< Activates a texture unit and returns true if succes
+	bool				activate_light(uint32_t light);
 
-	void				CheckGLErrors();
+	void				check_gl_errors();
 
-	GLSupport*			mGLSupport;
+private:
 
+	// Matrices
 	Mat4				mMatrix[MT_COUNT];
 
 	Mat4				mModelMatrixStack[MAX_MODEL_MATRIX_STACK_DEPTH];
 	uint32_t			mModelMatrixStackIndex;
 
+	// Limits
 	int32_t				mMaxLights;
 	int32_t				mMaxTextureSize;
 	int32_t				mMaxTextureUnits;
@@ -152,12 +153,12 @@ private:
 	float				mMinMaxPointSize[2];
 	float				mMinMaxLineWidth[2];
 
-	OcclusionQueryList	mOcclusionQueryList;
-	VertexBufferList	mVertexBufferList;
-	IndexBufferList		mIndexBufferList;
+	// Texture management
+	uint32_t			m_texture_count;
+	GLTexture			m_textures[MAX_TEXTURES];
 
-	int32_t				mActiveTextureUnit;
-//	Texture*			mTextureUnit[MAX_TEXTURE_UNITS];
+	uint32_t			mActiveTextureUnit;
+	GLuint				mTextureUnit[MAX_TEXTURE_UNITS];
 	GLenum				mTextureUnitTarget[MAX_TEXTURE_UNITS];
 
 	// Debug lines
@@ -171,6 +172,8 @@ private:
 
 	uint32_t				mLinesCount;
 	DebugLinesData		mLinesData[1024];
+
+	friend class TextureResource;
 };
 
 } // namespace crown

+ 2 - 1
src/renderers/gl/GLUtils.h

@@ -26,7 +26,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include <GL/glew.h>
-#include "TextureResource.h"
+#include <cassert>
+#include "Texture.h"
 #include "Material.h"
 #include "Pixel.h"