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

improve RenderWorld according to Sprite implementation

mikymod 12 лет назад
Родитель
Сommit
fe5abd14fe
2 измененных файлов с 85 добавлено и 7 удалено
  1. 64 1
      engine/RenderWorld.cpp
  2. 21 6
      engine/RenderWorld.h

+ 64 - 1
engine/RenderWorld.cpp

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Renderer.h"
 #include "Allocator.h"
 #include "Camera.h"
+#include "Resource.h"
 
 namespace crown
 {
@@ -87,6 +88,7 @@ static const char* texture_fragment =
 //-----------------------------------------------------------------------------
 RenderWorld::RenderWorld()
 	: m_mesh(default_allocator())
+	, m_sprite(default_allocator())
 {
 	Renderer* r = device()->renderer();
 
@@ -165,12 +167,31 @@ void RenderWorld::update(Camera& camera, float /*dt*/)
 		r->set_vertex_buffer(mesh.m_vbuffer);
 		r->set_index_buffer(mesh.m_ibuffer);
 		r->set_program(default_program);
-/*		r->set_texture(0, u_albedo_0, grass_texture, TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_CLAMP_EDGE);
+		/*r->set_texture(0, u_albedo_0, grass_texture, TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_CLAMP_EDGE);
 		r->set_uniform(u_brightness, UNIFORM_FLOAT_1, &brightness, 1);*/
 
 		r->set_pose(mesh.m_local_pose);
 		r->commit(0);
 	}
+
+	r->set_layer_view(0, camera_view);
+	r->set_layer_projection(0, camera.m_projection);
+	r->set_layer_viewport(0, 0, 0, 1000, 625);
+	r->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4::LIGHTBLUE, 1.0f);
+
+	for (uint32_t s = 0; s < m_sprite.size(); s++)
+	{
+		const Sprite& sprite = m_sprite[s];
+
+		r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
+		r->set_vertex_buffer(sprite.m_vb);
+		r->set_index_buffer(sprite.m_ib);
+		r->set_program(sprite.m_prog);
+		r->set_texture(0, sprite.m_albedo, sprite.m_texture, TEXTURE_FILTER_LINEAR | TEXTURE_WRAP_CLAMP_EDGE);
+
+		r->set_pose(sprite.m_local_pose);
+		r->commit(0);
+	}
 }
 
 //-----------------------------------------------------------------------------
@@ -192,4 +213,46 @@ void RenderWorld::deallocate_mesh(MeshId /*id*/)
 {
 }
 
+//-----------------------------------------------------------------------------
+SpriteId RenderWorld::create_sprite(const char* name, int32_t node, const Vector3& pos, const Quaternion& rot)
+{
+	TextureResource* tr = (TextureResource*) device()->resource_manager()->lookup(TEXTURE_EXTENSION, name);
+
+	SpriteId sprite = allocate_sprite(tr, node, pos, rot);
+
+	return sprite;
+}
+
+//-----------------------------------------------------------------------------
+void RenderWorld::destroy_sprite(SpriteId /*id*/)
+{
+	// Stub
+}
+
+//-----------------------------------------------------------------------------
+Sprite*	RenderWorld::lookup_sprite(SpriteId id)
+{
+	return &m_sprite[m_sprite_sparse_to_packed[id.index]];
+}
+
+//-----------------------------------------------------------------------------
+SpriteId RenderWorld::allocate_sprite(TextureResource* tr, int32_t node, const Vector3& pos, const Quaternion& rot)
+{
+	SpriteId id = m_sprite_table.create();
+
+	Sprite sprite;
+	sprite.create(tr, node, pos, rot);
+
+	uint32_t index = m_sprite.push_back(sprite);
+	m_sprite_sparse_to_packed[id.index] = index;
+
+	return id;
+}
+
+//-----------------------------------------------------------------------------
+void RenderWorld::deallocate_sprite(SpriteId /*id*/)
+{
+	// Stub
+}
+
 } // namespace crown

+ 21 - 6
engine/RenderWorld.h

@@ -28,16 +28,19 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "IdTable.h"
 #include "Mesh.h"
+#include "Sprite.h"
 #include "List.h"
 #include "Vector3.h"
 #include "Quaternion.h"
 
 #define MAX_MESHES 100
+#define MAX_SPRITES 256
 
 namespace crown
 {
 
 typedef Id MeshId;
+typedef Id SpriteId;
 struct Camera;
 
 class RenderWorld
@@ -47,21 +50,33 @@ public:
 	RenderWorld();
 	~RenderWorld();
 
-	MeshId create_mesh(const char* mesh, int32_t node = -1, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
-	void destroy_mesh(MeshId id);
+	MeshId 		create_mesh(const char* mesh, int32_t node = -1, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
+	void 		destroy_mesh(MeshId id);
+	Mesh* 		lookup_mesh(MeshId mesh);
 
-	Mesh* lookup_mesh(MeshId mesh);
+	void 		update(Camera& camera, float dt);
 
-	void update(Camera& camera, float dt);
+	MeshId 		allocate_mesh(MeshResource* mr, int32_t node, const Vector3& pos, const Quaternion& rot);
+	void 		deallocate_mesh(MeshId id);
 
-	MeshId allocate_mesh(MeshResource* mr, int32_t node, const Vector3& pos, const Quaternion& rot);
-	void deallocate_mesh(MeshId id);
+public:	// Sprites - Testing
+
+	SpriteId	create_sprite(const char* name, int32_t node = -1, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
+	void		destroy_sprite(SpriteId id);
+	Sprite*		lookup_sprite(SpriteId id);
+
+	SpriteId	allocate_sprite(TextureResource* tr, int32_t node, const Vector3& pos, const Quaternion& rot);
+	void 		deallocate_sprite(SpriteId id);
 
 private:
 
 	IdTable<MAX_MESHES>		m_mesh_table;
 	uint32_t				m_sparse_to_packed[MAX_MESHES];
 	List<Mesh>				m_mesh;
+
+	IdTable<MAX_SPRITES>	m_sprite_table;
+	uint32_t				m_sprite_sparse_to_packed[MAX_SPRITES];
+	List<Sprite>			m_sprite;
 };
 
 } // namespace crown