Răsfoiți Sursa

implement Gui

mikymod 12 ani în urmă
părinte
comite
0483f8ffc4
2 a modificat fișierele cu 196 adăugiri și 0 ștergeri
  1. 130 0
      engine/Gui.cpp
  2. 66 0
      engine/Gui.h

+ 130 - 0
engine/Gui.cpp

@@ -0,0 +1,130 @@
+/*
+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.
+*/
+
+#include "Gui.h"
+#include "Assert.h"
+#include "Renderer.h"
+#include "Vector3.h"
+#include "Vector2.h"
+#include "GuiResource.h"
+#include "RenderWorld.h"
+
+namespace crown
+{
+
+static const char* default_vertex =
+	"uniform mat4      	u_model;"
+	"uniform mat4      	u_model_view_projection;"
+
+	"in vec4           	a_position;"
+	"in vec4           	a_normal;"
+	"in vec2           	a_tex_coord0;"
+	"in vec4           	a_color;"
+
+	"varying out vec2	tex_coord0;"
+	"varying out vec4	color;"
+
+	"void main(void)"
+	"{"
+	"	tex_coord0 = a_tex_coord0;"
+	"   color = a_color;"
+	"	gl_Position = u_model_view_projection * a_position;"
+	"}";
+
+static const char* default_fragment =
+	"in vec2            tex_coord0;"
+	"in vec4            color;"
+
+	"uniform sampler2D  u_albedo_0;"
+
+	"void main(void)"
+	"{"
+	"	gl_FragColor = color;"
+	"}";
+
+//-----------------------------------------------------------------------------
+Gui::Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r)
+	: m_render_world(render_world)
+	, m_resource(gr)
+	, m_r(r)
+{
+	Vector2 size = m_resource->gui_size();
+	// orthographic projection
+	m_projection.build_projection_ortho_rh(0, size.x, size.y, 0, -0.01f, 100.0f);
+
+	// pose
+	Vector3 pos = m_resource->gui_position();
+	m_pose.load_identity();
+	m_pose.set_translation(pos);
+
+	m_default_vs = m_r.create_shader(ShaderType::VERTEX, default_vertex);
+	m_default_fs = m_r.create_shader(ShaderType::FRAGMENT, default_fragment);
+	m_default_program = m_r.create_gpu_program(m_default_vs, m_default_fs);
+}
+
+//-----------------------------------------------------------------------------
+Gui::~Gui()
+{
+	m_r.destroy_gpu_program(m_default_program);
+	m_r.destroy_shader(m_default_fs);
+	m_r.destroy_shader(m_default_vs);
+}
+
+//-----------------------------------------------------------------------------
+void Gui::move(const Vector3& pos)
+{
+	m_pose.load_identity();
+	m_pose.set_translation(pos);
+}
+
+//-----------------------------------------------------------------------------
+void Gui::render(Renderer& r, UniformId uniform)
+{
+	Vector2 size = m_resource->gui_size();
+
+	r.set_layer_view(1, Matrix4x4::IDENTITY);
+	r.set_layer_projection(1, m_projection);
+	r.set_layer_viewport(1, m_pose.translation().x, m_pose.translation().y, size.x, size.y);
+	r.set_program(m_default_program);
+	r.set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW | STATE_PRIMITIVE_LINES);	
+	r.set_pose(m_pose);
+
+	// draw gui's rects
+	r.set_vertex_buffer(m_resource->rect_vertex_buffer());
+	r.set_index_buffer(m_resource->rect_index_buffer());
+	r.commit(1);
+
+	r.set_program(m_default_program);
+	r.set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW | STATE_PRIMITIVE_LINES);	
+	r.set_pose(m_pose);
+
+	// draw gui's triangles
+	r.set_vertex_buffer(m_resource->triangle_vertex_buffer());
+	r.set_index_buffer(m_resource->triangle_index_buffer());
+	r.commit(1);
+}
+
+} // namespace crown

+ 66 - 0
engine/Gui.h

@@ -0,0 +1,66 @@
+/*
+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"
+
+namespace crown
+{
+
+typedef Id MaterialId;
+typedef Id UniformId;
+typedef Id VertexBufferId;
+typedef Id IndexBufferId;
+
+struct Renderer;
+struct RenderWorld;
+struct Vector3;
+struct GuiResource;
+
+struct Gui
+{
+						Gui(RenderWorld& render_world, GuiResource* gr, Renderer& r);
+						~Gui();
+	void				move(const Vector3& pos);
+	void				render(Renderer& r, UniformId uniform);
+
+public:
+
+	RenderWorld&		m_render_world;
+	const GuiResource*	m_resource;
+	Renderer&			m_r;
+
+	Matrix4x4			m_projection;
+	Matrix4x4			m_pose;
+
+	ShaderId			m_default_vs;
+	ShaderId			m_default_fs;
+	GPUProgramId		m_default_program;
+};
+
+} // namespace crown