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

Towards a more OpenGL 3.1 compliant renderer

Daniele Bartolini 12 лет назад
Родитель
Сommit
911edbb196

+ 0 - 3
src/renderers/DebugRenderer.cpp

@@ -131,9 +131,6 @@ void DebugRenderer::draw_all()
 {
 	if (m_lines_count > 0)
 	{
-		m_renderer.set_lighting(false);
-		m_renderer.set_texturing(0, false);
-
 		m_renderer.draw_lines(m_lines[0].to_float_ptr(), m_colors[0].to_float_ptr(), m_lines_count * 2);
 	}
 

+ 0 - 34
src/renderers/Renderer.h

@@ -170,13 +170,6 @@ public:
 	/// Sets the clearing color of the framebuffer.
 	virtual void			set_clear_color(const Color4& color) = 0;
 
-	virtual void			set_material_params(const Color4& ambient, const Color4& diffuse, const Color4& specular,
-												const Color4& emission, int32_t shininess) = 0;
-
-	
-	/// Sets whether lighting is enabled.
-	virtual void			set_lighting(bool lighting) = 0;
-
 	/// Sets the global ambient light @color.
 	virtual void			set_ambient_light(const Color4& color) = 0;
 
@@ -195,17 +188,9 @@ public:
 	/// Sets the @filter for the given texture @unit.
 	virtual void			set_texture_filter(uint32_t unit, TextureFilter filter) = 0;
 
-	virtual void			set_light(uint32_t light, bool active) = 0;
-	virtual void			set_light_params(uint32_t light, LightType type, const Vec3& position) = 0;
-	virtual void			set_light_color(uint32_t light, const Color4& ambient, const Color4& diffuse, const Color4& specular) = 0;
-	virtual void			set_light_attenuation(uint32_t light, float constant, float linear, float quadratic) = 0;
-
 	/// Sets whether backface-culling is enabled.
 	virtual void			set_backface_culling(bool culling) = 0;
 
-	/// Sets whether separate specular color is enabled.	
-	virtual void			set_separate_specular_color(bool separate) = 0;
-
 	/// Sets whether depth test is enabled.
 	virtual void			set_depth_test(bool test) = 0;
 
@@ -215,13 +200,6 @@ public:
 	/// Sets the depth function to use when testing depth values.
 	virtual void			set_depth_func(CompareFunction func) = 0;
 
-	///	Sets whether normal vectors are scaled after transformations
-	///	and before lighting.
-	/// @note
-	///	If the modelview matrix scales space uniformly, this
-	///	has the effect of restoring the trasformed normal to unit length.
-	virtual void			set_rescale_normals(bool rescale) = 0;
-
 	/// Sets whether blending is enabled.
 	virtual void			set_blending(bool blending) = 0;
 
@@ -234,19 +212,7 @@ public:
 	/// Sets whether writing to color buffer is enabled.
 	virtual void			set_color_write(bool write) = 0;
 
-	/// Sets whether fog is enabled.
-	virtual void			set_fog(bool fog) = 0;
-
-	/// Specifies fog parameters.
-	virtual void			set_fog_params(FogMode mode, float density, float start, float end, const Color4& color) = 0;
-
-	virtual void			set_alpha_test(bool test) = 0;
-
-	virtual void			set_alpha_params(CompareFunction func, float ref) = 0;
-
 	virtual void			set_shading_type(ShadingType type) = 0;
-
-	virtual void 			set_polygon_mode(PolygonMode mode) = 0;
 				
 	virtual void 			set_front_face(FrontFace face) = 0;
 				

+ 43 - 225
src/renderers/gl/GLRenderer.cpp

@@ -59,13 +59,14 @@ namespace crown
 
 //-----------------------------------------------------------------------------
 GLRenderer::GLRenderer() :
-	m_max_lights(0),
 	m_max_texture_size(0),
 	m_max_texture_units(0),
 	m_max_vertex_indices(0),
 	m_max_vertex_vertices(0),
 	m_max_anisotropy(0.0f),
 
+	m_ambient_light_color(Color4::GRAY),
+
 	m_textures_id_table(m_allocator, MAX_TEXTURES),
 	m_active_texture_unit(0),
 
@@ -105,13 +106,22 @@ GLRenderer::GLRenderer() :
 		m_matrix[i].load_identity();
 	}
 
+	m_model_view_matrix.load_identity();
+	m_model_view_projection_matrix.load_identity();
+}
+
+//-----------------------------------------------------------------------------
+GLRenderer::~GLRenderer()
+{
+}
+
+//-----------------------------------------------------------------------------
+void GLRenderer::init()
+{
 	GLenum err = glewInit();
 
 	assert(err == GLEW_OK);
 
-	Log::i("GLEW initialized.");
-
-	glGetIntegerv(GL_MAX_LIGHTS, &m_max_lights);
 	glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_max_texture_size);
 	glGetIntegerv(GL_MAX_TEXTURE_UNITS, &m_max_texture_units);
 	glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &m_max_vertex_indices);
@@ -126,56 +136,31 @@ GLRenderer::GLRenderer() :
 	glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, &m_min_max_point_size[0]);
 	glGetFloatv(GL_LINE_WIDTH_RANGE, &m_min_max_line_width[0]);
 
-	const unsigned char* gl_vendor = glGetString(GL_VENDOR);
-	const unsigned char* gl_renderer = glGetString(GL_RENDERER);
-	const unsigned char* gl_version = glGetString(GL_VERSION);
-
-	Log::i("OpenGL Vendor\t: %s", gl_vendor);
-	Log::i("OpenGL Renderer\t: %s", gl_renderer);
-	Log::i("OpenGL Version\t: %s", gl_version);
-	Log::i("Min Point Size\t: %f", m_min_max_point_size[0]);
-	Log::i("Max Point Size\t: %f", m_min_max_point_size[1]);
-	Log::i("Min Line Width\t: %f", m_min_max_line_width[0]);
-	Log::i("Max Line Width\t: %f", m_min_max_line_width[1]);
-	Log::i("Max Texture Size\t: %dx%d", m_max_texture_size, m_max_texture_size);
-	Log::i("Max Texture Units\t: %d", m_max_texture_units);
-	Log::i("Max Lights\t\t: %d", m_max_lights);
-	Log::i("Max Vertex Indices\t: %d", m_max_vertex_indices);
-	Log::i("Max Vertex Vertices\t: %d", m_max_vertex_vertices);
-	Log::i("Max Anisotropy\t: %f", m_max_anisotropy);
-
-	glMatrixMode(GL_PROJECTION);
-	glLoadIdentity();
-	glMatrixMode(GL_MODELVIEW);
-	glLoadIdentity();
+	Log::i("OpenGL Vendor\t: %s", glGetString(GL_VENDOR));
+	Log::i("OpenGL Renderer\t: %s", glGetString(GL_RENDERER));
+	Log::i("OpenGL Version\t: %s", glGetString(GL_VERSION));
 
-	glDisable(GL_TEXTURE_2D);
+	Log::d("Min Point Size\t: %f", m_min_max_point_size[0]);
+	Log::d("Max Point Size\t: %f", m_min_max_point_size[1]);
+	Log::d("Min Line Width\t: %f", m_min_max_line_width[0]);
+	Log::d("Max Line Width\t: %f", m_min_max_line_width[1]);
+	Log::d("Max Texture Size\t: %dx%d", m_max_texture_size, m_max_texture_size);
+	Log::d("Max Texture Units\t: %d", m_max_texture_units);
+	Log::d("Max Vertex Indices\t: %d", m_max_vertex_indices);
+	Log::d("Max Vertex Vertices\t: %d", m_max_vertex_vertices);
+	Log::d("Max Anisotropy\t: %f", m_max_anisotropy);
 
-	glEnable(GL_LIGHTING);
+	glDisable(GL_TEXTURE_2D);
 
 	glDisable(GL_BLEND);
-	//TODO: Use Premultiplied alpha
 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 	glBlendEquation(GL_FUNC_ADD);
 
 	glFrontFace(GL_CCW);
-
 	glEnable(GL_CULL_FACE);
-
-	glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
-
 	glShadeModel(GL_SMOOTH);
 
-	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
-
-	// Set the global ambient light
-	float amb[] = { 0.0f, 0.0f, 0.0f, 1.0f };
-	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
-
-	// Some hints
-	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
-
-	// Set the framebuffer clear color
+	// Set the default framebuffer clear color
 	glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
 
 	// Enable depth test
@@ -190,16 +175,7 @@ GLRenderer::GLRenderer() :
 	glDisable(GL_DITHER);
 
 	Log::i("OpenGL Renderer initialized.");
-}
-
-//-----------------------------------------------------------------------------
-GLRenderer::~GLRenderer()
-{
-}
 
-//-----------------------------------------------------------------------------
-void GLRenderer::init()
-{
 	load_default_shaders();
 }
 
@@ -529,7 +505,7 @@ void GLRenderer::set_gpu_porgram_mat3_uniform(GPUProgramId id, const char* name,
 
 	const GLint uniform = find_gpu_program_uniform(m_gpu_programs[id.index].gl_object, name);
 
-	glUniformMatrix3fv(uniform, 1, GL_TRUE, value.to_float_ptr());
+	glUniformMatrix3fv(uniform, 1, GL_FALSE, value.to_float_ptr());
 }
 
 //-----------------------------------------------------------------------------
@@ -539,7 +515,7 @@ void GLRenderer::set_gpu_program_mat4_uniform(GPUProgramId id, const char* name,
 
 	const GLint uniform = find_gpu_program_uniform(m_gpu_programs[id.index].gl_object, name);
 
-	glUniformMatrix4fv(uniform, 1, GL_TRUE, value.to_float_ptr());
+	glUniformMatrix4fv(uniform, 1, GL_FALSE, value.to_float_ptr());
 }
 
 //-----------------------------------------------------------------------------
@@ -607,34 +583,10 @@ void GLRenderer::set_clear_color(const Color4& color)
 	glClearColor(color.r, color.g, color.b, color.a);
 }
 
-//-----------------------------------------------------------------------------
-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);
-	glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, &diffuse.r);
-	glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, &specular.r);
-	glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, &emission.r);
-	glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
-}
-
 //-----------------------------------------------------------------------------
 void GLRenderer::set_ambient_light(const Color4& color)
 {
-	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, color.to_float_ptr());
-}
-
-//-----------------------------------------------------------------------------
-void GLRenderer::set_lighting(bool lighting)
-{
-	if (lighting)
-	{
-		glEnable(GL_LIGHTING);
-	}
-	else
-	{
-		glDisable(GL_LIGHTING);
-	}
+	m_ambient_light_color = color;
 }
 
 //-----------------------------------------------------------------------------
@@ -724,19 +676,6 @@ void GLRenderer::set_backface_culling(bool culling)
 	}
 }
 
-//-----------------------------------------------------------------------------
-void GLRenderer::set_separate_specular_color(bool separate)
-{
-	if (separate)
-	{
-		glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
-	}
-	else
-	{
-		glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR);
-	}
-}
-
 //-----------------------------------------------------------------------------
 void GLRenderer::set_depth_test(bool test)
 {
@@ -764,19 +703,6 @@ void GLRenderer::set_depth_func(CompareFunction func)
 	glDepthFunc(glFunc);
 }
 
-//-----------------------------------------------------------------------------
-void GLRenderer::set_rescale_normals(bool rescale)
-{
-	if (rescale)
-	{
-		glEnable(GL_RESCALE_NORMAL);
-	}
-	else
-	{
-		glDisable(GL_RESCALE_NORMAL);
-	}
-}
-
 //-----------------------------------------------------------------------------
 void GLRenderer::set_blending(bool blending)
 {
@@ -818,52 +744,6 @@ void GLRenderer::set_color_write(bool write)
 	}
 }
 
-//-----------------------------------------------------------------------------
-void GLRenderer::set_fog(bool fog)
-{
-	if (fog)
-	{
-		glEnable(GL_FOG);
-	}
-	else
-	{
-		glDisable(GL_FOG);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void GLRenderer::set_fog_params(FogMode mode, float density, float start, float end, const Color4& color)
-{
-	GLenum glMode = GL::fog_mode(mode);
-
-	glFogi(GL_FOG_MODE, glMode);
-	glFogf(GL_FOG_DENSITY, density);
-	glFogf(GL_FOG_START, start);
-	glFogf(GL_FOG_END, end);
-	glFogfv(GL_FOG_COLOR, &color.r);
-}
-
-//-----------------------------------------------------------------------------
-void GLRenderer::set_alpha_test(bool test)
-{
-	if (test)
-	{
-		glEnable(GL_ALPHA_TEST);
-	}
-	else
-	{
-		glDisable(GL_ALPHA_TEST);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void GLRenderer::set_alpha_params(CompareFunction func, float ref)
-{
-	GLenum glFunc = GL::compare_function(func);
-
-	glAlphaFunc(glFunc, ref);
-}
-
 //-----------------------------------------------------------------------------
 void GLRenderer::set_shading_type(ShadingType type)
 {
@@ -877,14 +757,6 @@ void GLRenderer::set_shading_type(ShadingType type)
 	glShadeModel(glMode);
 }
 
-//-----------------------------------------------------------------------------
-void GLRenderer::set_polygon_mode(PolygonMode mode)
-{
-	GLenum glMode = GL::polygon_mode(mode);
-
-	glPolygonMode(GL_FRONT_AND_BACK, glMode);
-}
-
 //-----------------------------------------------------------------------------
 void GLRenderer::set_front_face(FrontFace face)
 {
@@ -987,6 +859,9 @@ void GLRenderer::begin_frame()
 
 	// Bind the default gpu program
 	bind_gpu_program(m_default_gpu_program);
+
+	set_gpu_program_mat4_uniform(m_default_gpu_program, "mvp_matrix", m_model_view_projection_matrix);
+	set_gpu_program_vec3_uniform(m_default_gpu_program, "color", Vec3(0, 1, 0));
 }
 
 //-----------------------------------------------------------------------------
@@ -1012,24 +887,20 @@ void GLRenderer::set_matrix(MatrixType type, const Mat4& matrix)
 	{
 		case MT_VIEW:
 		case MT_MODEL:
-			glMatrixMode(GL_MODELVIEW);
-			// Transformations must be listed in reverse order
-			glLoadMatrixf((m_matrix[MT_VIEW] * m_matrix[MT_MODEL]).to_float_ptr());
+		{
+			m_model_view_matrix = m_matrix[MT_VIEW] * m_matrix[MT_MODEL];
 			break;
+		}
 		case MT_PROJECTION:
-			glMatrixMode(GL_PROJECTION);
-			glLoadMatrixf(m_matrix[MT_PROJECTION].to_float_ptr());
-			break;
-		case MT_TEXTURE:
-			glMatrixMode(GL_TEXTURE);
-			glLoadMatrixf(m_matrix[MT_TEXTURE].to_float_ptr());
-			break;
-		case MT_COLOR:
-			//glMatrixMode(GL_COLOR);
-			//glLoadMatrixf(m_matrix[MT_COLOR].to_float_ptr());
+		{
+			m_model_view_projection_matrix =  m_matrix[MT_PROJECTION] * m_model_view_matrix;
 			break;
+		}
 		default:
+		{
 			break;
+			assert(0);
+		}
 	}
 }
 
@@ -1106,59 +977,6 @@ void GLRenderer::draw_triangles(IndexBufferId id) const
 // 	const GLRenderBuffer& render_buffer = m_render_buffers[id.index];
 // }
 
-//-----------------------------------------------------------------------------
-void GLRenderer::set_light(uint32_t light, bool active)
-{
-	if (light >= (uint32_t) m_max_lights)
-	{
-		return;
-	}
-
-	if (active)
-	{
-		glEnable(GL_LIGHT0 + light);
-	}
-	else
-	{
-		glDisable(GL_LIGHT0 + light);
-	}
-}
-
-//-----------------------------------------------------------------------------
-void GLRenderer::set_light_params(uint32_t light, LightType type, const Vec3& position)
-{
-	static float pos[4] =
-	{
-		position.x,
-		position.y,
-		position.z,
-		1.0f
-	};
-
-	if (type == LT_DIRECTION)
-	{
-		pos[3] = 0.0f;
-	}
-
-	glLightfv(GL_LIGHT0 + light, GL_POSITION, pos);
-}
-
-//-----------------------------------------------------------------------------
-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());
-	glLightfv(GL_LIGHT0 + light, GL_SPECULAR, specular.to_float_ptr());
-}
-
-//-----------------------------------------------------------------------------
-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);
-	glLightf(GL_LIGHT0 + light, GL_QUADRATIC_ATTENUATION, quadratic);
-}
-
 //-----------------------------------------------------------------------------
 void GLRenderer::draw_lines(const float* vertices, const float* colors, uint32_t count)
 {

+ 9 - 19
src/renderers/gl/GLRenderer.h

@@ -153,42 +153,27 @@ public:
 	// RenderBufferId	create_render_buffer(uint32_t width, uint32_t height, PixelFormat format);
 	// void				destroy_render_buffer(RenderBufferId id);
 
+	// Called once a frame
 	void				begin_frame();
 	void				end_frame();
 
 	void				set_clear_color(const Color4& color);
 
-	void				set_material_params(const Color4& ambient, const Color4& diffuse, const Color4& specular, const Color4& emission, int32_t shininess);
-	void				set_lighting(bool lighting);
+	// Lighting
 	void				set_ambient_light(const Color4& color);
 
-	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				set_backface_culling(bool culling);
 
-	void				set_separate_specular_color(bool separate);
-
+	// Fragment operations
 	void				set_depth_test(bool test);
 	void				set_depth_write(bool write);
 	void				set_depth_func(CompareFunction func);
 
-	void				set_rescale_normals(bool rescale);
-
 	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				set_fog(bool fog);
-	void				set_fog_params(FogMode mode, float density, float start, float end, const Color4& color);
-
-	void				set_alpha_test(bool test);
-	void				set_alpha_params(CompareFunction func, float ref);
-
 	void				set_shading_type(ShadingType type);
-	void				set_polygon_mode(PolygonMode mode);
 	void				set_front_face(FrontFace face);
 
 	void				set_viewport_params(int32_t x, int32_t y, int32_t width, int32_t height);
@@ -236,8 +221,10 @@ private:
 	// Matrices
 	Mat4				m_matrix[MT_COUNT];
 
+	Mat4				m_model_view_matrix;
+	Mat4				m_model_view_projection_matrix;
+
 	// Limits
-	int32_t				m_max_lights;
 	int32_t				m_max_texture_size;
 	int32_t				m_max_texture_units;
 	int32_t				m_max_vertex_indices;
@@ -251,6 +238,9 @@ private:
 	int32_t				m_viewport[4];
 	int32_t				m_scissor[4];
 
+	// Lighting
+	Color4				m_ambient_light_color;
+
 	// Texture management
 	IdTable 			m_textures_id_table;
 	GLTexture			m_textures[MAX_TEXTURES];