mikymod 12 лет назад
Родитель
Сommit
9eb3daf040
5 измененных файлов с 249 добавлено и 6 удалено
  1. 1 0
      engine/CMakeLists.txt
  2. 103 4
      engine/gui/Gui.cpp
  3. 10 1
      engine/gui/Gui.h
  4. 1 1
      engine/gui/GuiImage.h
  5. 134 0
      engine/gui/GuiText.h

+ 1 - 0
engine/CMakeLists.txt

@@ -446,6 +446,7 @@ set (GUI_HEADERS
 	gui/GuiRect.h
 	gui/GuiTriangle.h
 	gui/GuiImage.h
+	gui/GuiText.h
 )
 
 set (CROWN_LIBRARIES)

+ 103 - 4
engine/gui/Gui.cpp

@@ -35,10 +35,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Vector3.h"
 #include "Vector2.h"
 #include "Color4.h"
+#include "FontResource.h"
 
 #include "GuiRect.h"
 #include "GuiTriangle.h"
 #include "GuiImage.h"
+#include "GuiText.h"
 
 namespace crown
 {
@@ -50,8 +52,12 @@ GPUProgramId		gui_default_program;
 GPUProgramId		gui_texture_program;
 UniformId			gui_albedo_0;
 
+ShaderId			font_vs;
+ShaderId			font_fs;
+GPUProgramId		font_program;
+UniformId			font_uniform;
+
 static const char* default_vertex =
-	"uniform mat4      	u_model;"
 	"uniform mat4      	u_model_view_projection;"
 
 	"attribute vec4    	a_position;"
@@ -87,6 +93,37 @@ static const char* texture_fragment =
 	"	gl_FragColor = texture2D(u_albedo_0, tex_coord0);"
 	"}";
 
+static const char* sdf_vertex =
+	"uniform mat4      	u_model_view_projection;"
+
+	"attribute vec4		a_position;"
+	"attribute vec2		a_tex_coord0;"
+	"attribute vec4		a_color;"
+
+	"varying vec2		v_tex_coord;"
+	"varying vec4		v_color;"
+
+	"void main(void)"
+	"{"
+	"	gl_Position = u_model_view_projection * a_position;"
+	"	v_tex_coord = a_tex_coord0;"
+	"	v_color = vec4(1, 1, 1, 1);"
+	"}";
+
+static const char* sdf_fragment =
+	"uniform sampler2D u_texture;"
+
+	"varying vec4 v_color;"
+	"varying vec2 v_tex_coord;"
+
+	"const float smoothing = 1.0/16.0;"
+
+	"void main() {"
+		"float distance = texture2D(u_texture, v_tex_coord).a;"
+		"float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);"
+		"gl_FragColor = vec4(v_color.rgb, alpha);"
+	"}";
+
 //-----------------------------------------------------------------------------
 Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
 	: m_render_world(render_world)
@@ -95,6 +132,7 @@ Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
 	, m_rect_pool(default_allocator(), MAX_GUI_RECTS, sizeof(GuiRect), CE_ALIGNOF(GuiRect))
 	, m_triangle_pool(default_allocator(), MAX_GUI_TRIANGLES, sizeof(GuiTriangle), CE_ALIGNOF(GuiTriangle))
 	, m_image_pool(default_allocator(), MAX_GUI_IMAGES, sizeof(GuiImage), CE_ALIGNOF(GuiImage))
+	, m_text_pool(default_allocator(), MAX_GUI_TEXTS, sizeof(GuiText), CE_ALIGNOF(GuiText))
 {
 	// orthographic projection
 	Vector2 size = m_resource->gui_size();
@@ -113,6 +151,13 @@ Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
 	gui_texture_program = m_r.create_gpu_program(gui_default_vs, gui_texture_fs);
 	gui_albedo_0 = m_r.create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
 
+	// FIXME FIXME FIXME -- Shaders init should not be here
+	font_vs = m_r.create_shader(ShaderType::VERTEX, sdf_vertex);
+	font_fs = m_r.create_shader(ShaderType::FRAGMENT, sdf_fragment);
+	font_program = m_r.create_gpu_program(font_vs, font_fs);
+	font_uniform = m_r.create_uniform("u_texture", UniformType::INTEGER_1, 1);
+
+
 	// Gui's rects creation
 	for (uint32_t i = 0; i < m_resource->num_rects(); i++)
 	{
@@ -144,26 +189,36 @@ Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
 
 		create_image(mat, pos, size);
 	}
+
+	// Manage texts creation
+
+	FontResource* res = (FontResource*) device()->resource_manager()->lookup("font", "fonts/veramobi_sdf");
+	create_text("&", res, 100, Vector3(300, 400, 0));
 }
 
 //-----------------------------------------------------------------------------
 Gui::~Gui()
 {
-	for (uint32_t i = 0; i < m_resource->num_rects(); i++)
+	for (uint32_t i = 0; i < m_rects.size(); i++)
 	{
 		CE_DELETE(m_rect_pool, m_rects[i]);
 	}
 
-	for (uint32_t i = 0; i < m_resource->num_triangles(); i++)
+	for (uint32_t i = 0; i < m_triangles.size(); i++)
 	{
 		CE_DELETE(m_triangle_pool, m_triangles[i]);
 	}
 
-	for (uint32_t i = 0; i < m_resource->num_images(); i++)
+	for (uint32_t i = 0; i < m_images.size(); i++)
 	{
 		CE_DELETE(m_image_pool, m_images[i]);
 	}
 
+	for (uint32_t i = 0; i < m_texts.size(); i++)
+	{
+		CE_DELETE(m_text_pool, m_texts[i]);
+	}
+
 	// FIXME FIXME FIXME -- Shaders destruction should not be here
 	m_r.destroy_uniform(gui_albedo_0);
 	m_r.destroy_gpu_program(gui_texture_program);
@@ -171,6 +226,12 @@ Gui::~Gui()
 	m_r.destroy_shader(gui_texture_fs);
 	m_r.destroy_shader(gui_default_fs);
 	m_r.destroy_shader(gui_default_vs);
+
+	// FIXME FIXME FIXME -- Shaders destruction should not be here
+	m_r.destroy_uniform(font_uniform);
+	m_r.destroy_gpu_program(font_program);
+	m_r.destroy_shader(font_vs);
+	m_r.destroy_shader(font_fs);
 }
 
 //-----------------------------------------------------------------------------
@@ -264,6 +325,29 @@ void Gui::destroy_image(GuiImageId id)
 	m_images.destroy(id);
 }
 
+//-----------------------------------------------------------------------------
+GuiTextId Gui::create_text(const char* str, const FontResource* font, uint32_t font_size, const Vector3& pos)
+{
+	GuiText* text = CE_NEW(m_text_pool, GuiText)(m_render_world, m_r, font, str, font_size, pos);
+	return m_texts.create(text);
+}
+
+//-----------------------------------------------------------------------------
+void Gui::update_text(GuiTextId id)
+{
+	// Must be implemented
+}
+
+//-----------------------------------------------------------------------------
+void Gui::destroy_text(GuiTextId id)
+{
+	CE_ASSERT(m_texts.has(id), "GuiText does not exists");
+
+	GuiText* text = m_texts.lookup(id);
+	CE_DELETE(m_text_pool, text);
+	m_texts.destroy(id);
+}
+
 //-----------------------------------------------------------------------------
 void Gui::render()
 {
@@ -319,6 +403,21 @@ void Gui::render()
 
 		m_images[i]->render(gui_albedo_0);
 	}
+
+	for (uint32_t i = 0; i < m_texts.size(); i++)
+	{
+		m_r.set_program(font_program);
+		m_r.set_state(STATE_DEPTH_WRITE 
+		| STATE_COLOR_WRITE 
+		| STATE_ALPHA_WRITE 
+		| STATE_CULL_CW
+		| STATE_PRIMITIVE_TRIANGLES
+		| STATE_BLEND_EQUATION_ADD 
+		| STATE_BLEND_FUNC(STATE_BLEND_FUNC_SRC_ALPHA, STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA));
+		m_r.set_pose(m_pose);
+
+		m_texts[i]->render(gui_albedo_0);
+	}
 }
 
 } // namespace crown

+ 10 - 1
engine/gui/Gui.h

@@ -36,6 +36,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #define MAX_GUI_RECTS 64
 #define MAX_GUI_TRIANGLES 64
 #define MAX_GUI_IMAGES 64
+#define MAX_GUI_TEXTS 64
 
 namespace crown
 {
@@ -44,17 +45,19 @@ typedef Id UniformId;
 typedef Id GuiRectId;
 typedef Id GuiTriangleId;
 typedef Id GuiImageId;
+typedef Id GuiTextId;
 
 struct Renderer;
 struct RenderWorld;
-struct Vector3;
 struct GuiResource;
 struct GuiRect;
 struct GuiTriangle;
 struct GuiImage;
+struct GuiText;
 struct Vector3;
 struct Vector2;
 struct Color4;
+struct FontResource;
 
 struct Gui
 {
@@ -76,6 +79,10 @@ struct Gui
 	void				update_image(GuiImageId id, const Vector3& pos, const Vector2& size);
 	void				destroy_image(GuiImageId);
 
+	GuiTextId			create_text(const char* str, const FontResource* font, uint32_t font_size, const Vector3& pos);
+	void				update_text(GuiTextId id);
+	void				destroy_text(GuiTextId id);
+
 	void				render();
 
 public:
@@ -90,10 +97,12 @@ public:
 	PoolAllocator		m_rect_pool;
 	PoolAllocator		m_triangle_pool;
 	PoolAllocator		m_image_pool;
+	PoolAllocator		m_text_pool;
 
 	IdArray<MAX_GUI_RECTS, GuiRect*> m_rects;
 	IdArray<MAX_GUI_TRIANGLES, GuiTriangle*> m_triangles;
 	IdArray<MAX_GUI_IMAGES, GuiImage*> m_images;
+	IdArray<MAX_GUI_TEXTS, GuiText*> m_texts;
 };
 
 } // namespace crown

+ 1 - 1
engine/gui/GuiImage.h

@@ -99,7 +99,7 @@ public:
 	RenderWorld& m_render_world;
 	Renderer& m_r;
 
-	float m_vertices[6*4];
+	float m_vertices[4*4];
 	uint16_t m_indices[2*3];
 
 	MaterialId m_material;

+ 134 - 0
engine/gui/GuiText.h

@@ -0,0 +1,134 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Matrix4x4.h"
+#include "RendererTypes.h"
+#include "StringUtils.h"
+#include "Log.h"
+#include "FontResource.h"
+
+namespace crown
+{
+
+struct Vector3;
+struct Vector2;
+struct Color4;
+
+struct GuiText
+{
+	//-------------------------------------------------------------------------
+	GuiText(RenderWorld& render_world, Renderer& r, const FontResource* fr, const char* str, uint32_t font_size, const Vector3& pos)
+		: m_render_world(render_world)
+		, m_r(r)
+		, m_resource(fr)
+	{
+		MaterialResource* mat = (MaterialResource*) device()->resource_manager()->data(m_resource->material());
+		m_material = m_render_world.create_material(mat);
+
+		FontGlyphData g = m_resource->get_glyph(str[0]);
+
+		// update(pos, Vector2(100, 100));
+		float x 		= (float) g.x / 512.0f;
+		float y 		= (float) (512 - g.y) / 512.0f;
+		float width 	= (float) g.width / 512.0f;
+		float height 	= (float) g.height / 512.0f;
+
+		float u0 = x;
+		float v0 = y;
+		float u1 = x + width;
+		float v1 = y - height;
+
+		m_vertices[0] = pos.x;
+		m_vertices[1] = pos.y;
+		m_vertices[2] = u0;
+		m_vertices[3] = v1;
+
+		m_vertices[4] = pos.x + font_size;
+		m_vertices[5] = pos.y;
+		m_vertices[6] = u1; 
+		m_vertices[7] = v1;
+
+		m_vertices[8] = pos.x + font_size; 
+		m_vertices[9] = pos.y - font_size;
+		m_vertices[10] = u1;
+		m_vertices[11] = v0;
+
+		m_vertices[12] = pos.x; 
+		m_vertices[13] = pos.y - font_size;
+		m_vertices[14] = u0;
+		m_vertices[15] = v0;
+
+		m_indices[0] = 0; m_indices[1] = 1;
+		m_indices[2] = 2; m_indices[3] = 0;
+		m_indices[4] = 2; m_indices[5] = 3;
+
+		m_vb = m_r.create_vertex_buffer(4, VertexFormat::P2_T2, m_vertices);
+		m_ib = m_r.create_index_buffer(6, m_indices);
+	}
+
+	//-------------------------------------------------------------------------
+	~GuiText()
+	{
+		m_r.destroy_vertex_buffer(m_vb);
+		m_r.destroy_index_buffer(m_ib);
+
+		m_render_world.destroy_material(m_material);
+	}
+
+	//-------------------------------------------------------------------------
+	void update(const Vector3& pos, const Vector2& size)
+	{
+	}
+
+	//-------------------------------------------------------------------------
+	void render(UniformId uniform)
+	{
+		Material* material = m_render_world.lookup_material(m_material);
+		material->bind(m_r, uniform);
+
+		m_r.set_vertex_buffer(m_vb);
+		m_r.set_index_buffer(m_ib);
+		m_r.commit(1);
+	}
+
+public:
+
+	RenderWorld& m_render_world;
+	Renderer& m_r;
+	const FontResource* m_resource;
+
+	float m_vertices[4*4];
+	uint16_t m_indices[2*3];
+
+	VertexBufferId m_vb;
+	IndexBufferId m_ib;
+	MaterialId m_material;
+};
+
+} // namespace crown
+