| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- /*
- 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 "RenderWorld.h"
- #include "Device.h"
- #include "Renderer.h"
- #include "Allocator.h"
- #include "Camera.h"
- #include "Resource.h"
- #include "Log.h"
- #include "SpriteResource.h"
- #include "Mesh.h"
- #include "Sprite.h"
- #include "Material.h"
- #include "Config.h"
- #include "Gui.h"
- #include "GuiResource.h"
- namespace crown
- {
- namespace render_world_globals
- {
- static const char* default_vertex =
- "precision mediump float;\n"
- "uniform mat4 u_model_view_projection;\n"
- "attribute vec4 a_position;\n"
- "attribute vec2 a_tex_coord0;\n"
- "attribute vec4 a_color;\n"
- "varying vec2 tex_coord0;\n"
- "varying vec4 color;\n"
- "void main(void)\n"
- "{\n"
- " tex_coord0 = a_tex_coord0;\n"
- " color = a_color;\n"
- " gl_Position = u_model_view_projection * a_position;\n"
- "}\n";
- static const char* default_fragment =
- "precision mediump float;\n"
- "varying vec4 color;\n"
- "void main(void)\n"
- "{\n"
- " gl_FragColor = color;\n"
- "}\n";
- static const char* texture_fragment =
- "precision mediump float;\n"
- "varying vec2 tex_coord0;\n"
- "varying vec4 color;\n"
- "uniform sampler2D u_albedo_0;\n"
- "void main(void)\n"
- "{\n"
- " gl_FragColor = texture2D(u_albedo_0, tex_coord0);\n"
- "}\n";
- static const char* sdf_vertex =
- "precision mediump float;\n"
- "uniform mat4 u_model_view_projection;\n"
- "attribute vec4 a_position;\n"
- "attribute vec2 a_tex_coord0;\n"
- "varying vec2 v_tex_coord;\n"
- "varying vec4 v_color;\n"
- "void main(void)\n"
- "{\n"
- " gl_Position = u_model_view_projection * a_position;\n"
- " v_tex_coord = a_tex_coord0;\n"
- " v_color = vec4(1, 1, 1, 1);\n"
- "}\n";
- static const char* sdf_fragment =
- "precision mediump float;\n"
- "uniform sampler2D u_texture;\n"
- "uniform vec4 u_color;\n"
- "varying vec2 v_tex_coord;\n"
- "const float smoothing = 1.0/16.0;\n"
- "void main() {\n"
- "float distance = texture2D(u_texture, v_tex_coord).a;\n"
- "float alpha = smoothstep(0.5 - smoothing, 0.5 + smoothing, distance);\n"
- "gl_FragColor = vec4(u_color.rgb, alpha);\n"
- "}\n";
- static const char* default_color_fragment =
- "precision mediump float;\n"
- "uniform vec4 u_color;\n"
- "void main(void)\n"
- "{\n"
- " gl_FragColor = u_color;\n"
- "}\n";
- ShaderId default_vs;
- ShaderId default_fs;
- ShaderId texture_fs;
- ShaderId font_vs;
- ShaderId font_fs;
- ShaderId color_fs;
- GPUProgramId texture_program;
- GPUProgramId def_program;
- GPUProgramId font_program;
- GPUProgramId color_program;
- UniformId u_albedo_0;
- UniformId u_font;
- UniformId u_color;
- uint32_t num_refs = 0;
- void init()
- {
- if (num_refs)
- return;
- num_refs++;
- Renderer* r = device()->renderer();
- u_font = r->create_uniform("u_font", UniformType::INTEGER_1, 1);
- u_albedo_0 = r->create_uniform("u_albedo_0", UniformType::INTEGER_1, 1);
- u_color = r->create_uniform("u_color", UniformType::FLOAT_4, 1);
- default_vs = r->create_shader(ShaderType::VERTEX, default_vertex);
- default_fs = r->create_shader(ShaderType::FRAGMENT, default_fragment);
- texture_fs = r->create_shader(ShaderType::FRAGMENT, texture_fragment);
- font_vs = r->create_shader(ShaderType::VERTEX, sdf_vertex);
- font_fs = r->create_shader(ShaderType::FRAGMENT, sdf_fragment);
- color_fs = r->create_shader(ShaderType::FRAGMENT, default_color_fragment);
- def_program = r->create_gpu_program(default_vs, default_fs);
- texture_program = r->create_gpu_program(default_vs, texture_fs);
- font_program = r->create_gpu_program(font_vs, font_fs);
- color_program = r->create_gpu_program(default_vs, color_fs);
- }
- void shutdown()
- {
- num_refs--;
- if (num_refs)
- return;
- Renderer* r = device()->renderer();
- r->destroy_uniform(u_albedo_0);
- r->destroy_uniform(u_font);
- r->destroy_uniform(u_color);
- r->destroy_gpu_program(texture_program);
- r->destroy_gpu_program(def_program);
- r->destroy_gpu_program(font_program);
- r->destroy_gpu_program(color_program);
- r->destroy_shader(default_vs);
- r->destroy_shader(default_fs);
- r->destroy_shader(texture_fs);
- r->destroy_shader(font_vs);
- r->destroy_shader(font_fs);
- r->destroy_shader(color_fs);
- }
- GPUProgramId default_program()
- {
- return def_program;
- }
- GPUProgramId default_texture_program()
- {
- return texture_program;
- }
- GPUProgramId default_font_program()
- {
- return font_program;
- }
- GPUProgramId default_color_program()
- {
- return color_program;
- }
- UniformId default_albedo_uniform()
- {
- return u_albedo_0;
- }
- UniformId default_font_uniform()
- {
- return u_font;
- }
- UniformId default_color_uniform()
- {
- return u_color;
- }
- };
- //-----------------------------------------------------------------------------
- RenderWorld::RenderWorld()
- : m_mesh_pool(default_allocator(), MAX_MESHES, sizeof(Mesh), CE_ALIGNOF(Mesh))
- , m_sprite_pool(default_allocator(), MAX_SPRITES, sizeof(Sprite), CE_ALIGNOF(Sprite))
- , m_material_pool(default_allocator(), MAX_MATERIALS, sizeof(Material), CE_ALIGNOF(Material))
- , m_gui_pool(default_allocator(), MAX_GUIS, sizeof(Gui), CE_ALIGNOF(Gui))
- {
- render_world_globals::init();
- }
- //-----------------------------------------------------------------------------
- RenderWorld::~RenderWorld()
- {
- render_world_globals::shutdown();
- }
- //-----------------------------------------------------------------------------
- MeshId RenderWorld::create_mesh(MeshResource* mr, SceneGraph& sg, int32_t node)
- {
- Mesh* mesh = CE_NEW(m_mesh_pool, Mesh)(sg, node, mr);
- return id_array::create(m_mesh, mesh);
- }
- //-----------------------------------------------------------------------------
- void RenderWorld::destroy_mesh(MeshId id)
- {
- Mesh* mesh = id_array::get(m_mesh, id);
- CE_DELETE(m_mesh_pool, mesh);
- id_array::destroy(m_mesh, id);
- }
- //-----------------------------------------------------------------------------
- Mesh* RenderWorld::get_mesh(MeshId mesh)
- {
- return id_array::get(m_mesh, mesh);
- }
- //-----------------------------------------------------------------------------
- SpriteId RenderWorld::create_sprite(SpriteResource* sr, SceneGraph& sg, int32_t node)
- {
- Sprite* sprite = CE_NEW(m_sprite_pool, Sprite)(*this, sg, node, sr);
- return id_array::create(m_sprite, sprite);
- }
- //-----------------------------------------------------------------------------
- void RenderWorld::destroy_sprite(SpriteId id)
- {
- CE_DELETE(m_sprite_pool, id_array::get(m_sprite, id));
- id_array::destroy(m_sprite, id);
- }
- //-----------------------------------------------------------------------------
- Sprite* RenderWorld::get_sprite(SpriteId id)
- {
- return id_array::get(m_sprite, id);
- }
- //-----------------------------------------------------------------------------
- MaterialId RenderWorld::create_material(MaterialResource* mr)
- {
- Material* mat = CE_NEW(m_material_pool, Material)(mr);
- return id_array::create(m_materials, mat);
- }
- //-----------------------------------------------------------------------------
- void RenderWorld::destroy_material(MaterialId id)
- {
- CE_DELETE(m_material_pool, id_array::get(m_materials, id));
- id_array::destroy(m_materials, id);
- }
- //-----------------------------------------------------------------------------
- Material* RenderWorld::get_material(MaterialId id)
- {
- return id_array::get(m_materials, id);
- }
- //-----------------------------------------------------------------------------
- GuiId RenderWorld::create_gui(uint16_t width, uint16_t height)
- {
- Gui* gui = CE_NEW(m_gui_pool, Gui)(*this, width, height);
- GuiId id = id_array::create(m_guis, gui);
- gui->set_id(id);
- return id;
- }
- //-----------------------------------------------------------------------------
- void RenderWorld::destroy_gui(GuiId id)
- {
- CE_DELETE(m_gui_pool, id_array::get(m_guis, id));
- id_array::destroy(m_guis, id);
- }
- //-----------------------------------------------------------------------------
- Gui* RenderWorld::get_gui(GuiId id)
- {
- return id_array::get(m_guis, id);
- }
- //-----------------------------------------------------------------------------
- void RenderWorld::update(const Matrix4x4& view, const Matrix4x4& projection, uint16_t x, uint16_t y, uint16_t width, uint16_t height, float dt)
- {
- Renderer* r = device()->renderer();
- Matrix4x4 inv_view = view;
- matrix4x4::invert(inv_view);
- r->set_layer_view(0, inv_view);
- r->set_layer_projection(0, projection);
- r->set_layer_viewport(0, x, y, width, height);
- r->set_layer_clear(0, CLEAR_COLOR | CLEAR_DEPTH, Color4(0x353839FF), 1.0f);
- r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_CULL_CCW);
- r->commit(0);
- // Draw all meshes
- for (uint32_t m = 0; m < id_array::size(m_mesh); m++)
- {
- const Mesh* mesh = m_mesh.m_objects[m];
- r->set_state(STATE_DEPTH_WRITE | STATE_COLOR_WRITE | STATE_ALPHA_WRITE | STATE_CULL_CW);
- r->set_vertex_buffer(mesh->m_vbuffer);
- r->set_index_buffer(mesh->m_ibuffer);
- r->set_program(render_world_globals::default_program());
- // r->set_texture(0, render_world_globals::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->world_pose());
- r->commit(0);
- }
- // Draw all sprites
- for (uint32_t s = 0; s < id_array::size(m_sprite); s++)
- {
- r->set_program(render_world_globals::default_texture_program());
- // m_sprite[s]->update(dt);
- m_sprite[s]->render(*r, render_world_globals::u_albedo_0, dt);
- }
- }
- } // namespace crown
|