From bfada5d92057523599b4d8c0be22329950ff7cd9 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Tue, 1 May 2012 13:40:56 +0000 Subject: [PATCH] core: move some of the camera view logic from the Video class to the Scene and Camera classes. --- src/camera.cpp | 4 +++ src/entity.h | 1 + src/gradient.cpp | 4 +-- src/scene.cpp | 93 ++++++++++++++++++++++++++++++------------------ src/scene.h | 14 ++++++-- src/ticker.cpp | 15 ++++---- src/video.cpp | 15 +------- src/video.h | 2 -- 8 files changed, 85 insertions(+), 63 deletions(-) diff --git a/src/camera.cpp b/src/camera.cpp index 5006f821..be55bf75 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -29,6 +29,7 @@ Camera::Camera(vec3 const &position, vec3 const &target, vec3 const &up) m_up(up) { m_gamegroup = GAMEGROUP_BEFORE; + m_drawgroup = DRAWGROUP_CAMERA; SetPosition(position); } @@ -64,6 +65,9 @@ void Camera::TickGame(float seconds) void Camera::TickDraw(float seconds) { WorldEntity::TickDraw(seconds); + + Scene::GetDefault()->SetViewMatrix(m_view_matrix); + Scene::GetDefault()->SetProjMatrix(m_proj_matrix); } } /* namespace lol */ diff --git a/src/entity.h b/src/entity.h index 11c25997..3b97001b 100644 --- a/src/entity.h +++ b/src/entity.h @@ -54,6 +54,7 @@ protected: enum { DRAWGROUP_BEFORE = GAMEGROUP_END, + DRAWGROUP_CAMERA, DRAWGROUP_DEFAULT, DRAWGROUP_HUD, DRAWGROUP_CAPTURE, diff --git a/src/gradient.cpp b/src/gradient.cpp index 9970e22d..a5211b17 100644 --- a/src/gradient.cpp +++ b/src/gradient.cpp @@ -186,9 +186,9 @@ void Gradient::TickDraw(float seconds) data->shader->Bind(); uni_mat = data->shader->GetUniformLocation("proj_matrix"); - data->shader->SetUniform(uni_mat, Video::GetProjMatrix()); + data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetProjMatrix()); uni_mat = data->shader->GetUniformLocation("view_matrix"); - data->shader->SetUniform(uni_mat, Video::GetViewMatrix()); + data->shader->SetUniform(uni_mat, Scene::GetDefault()->GetViewMatrix()); uni_mat = data->shader->GetUniformLocation("model_matrix"); data->shader->SetUniform(uni_mat, model_matrix); diff --git a/src/scene.cpp b/src/scene.cpp index ba7d12da..9377046c 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -35,8 +35,6 @@ struct Tile int id, o; }; -static Shader *stdshader = NULL; - /* * Scene implementation class */ @@ -54,12 +52,14 @@ private: return t2->prio - t1->prio; } - mat4 model_matrix; + mat4 m_model_matrix; + mat4 m_view_matrix; + mat4 m_proj_matrix; Tile *tiles; int ntiles; - float angle; + Shader *m_shader; VertexDeclaration *m_vdecl; VertexBuffer **bufs; int nbufs; @@ -73,13 +73,18 @@ Scene *SceneData::scene = NULL; * Public Scene class */ -Scene::Scene(float angle) +Scene::Scene() : data(new SceneData()) { + data->m_model_matrix = mat4(1.f); + data->m_view_matrix = mat4(1.f); + data->m_proj_matrix = mat4::ortho(0, Video::GetSize().x, + 0, Video::GetSize().y, -1000.f, 1000.f); + data->tiles = 0; data->ntiles = 0; - data->angle = angle; + data->m_shader = 0; data->bufs = 0; data->nbufs = 0; @@ -91,10 +96,9 @@ Scene::~Scene() { /* FIXME: this must be done while the GL context is still active. * Change the code architecture to make sure of that. */ - /* XXX: The test is necessary because of a crash with PSGL. */ - for (int i = 0; i < data->nbufs; i++) - delete data->bufs[i]; - free(data->bufs); + /* FIXME: also, make sure we do not add code to Reset() that will + * reallocate stuff */ + Reset(); delete data->m_vdecl; delete data; @@ -103,15 +107,37 @@ Scene::~Scene() Scene *Scene::GetDefault() { if (!SceneData::scene) - SceneData::scene = new Scene(0.0f); + SceneData::scene = new Scene(); return SceneData::scene; } void Scene::Reset() { - if (SceneData::scene) - delete SceneData::scene; - SceneData::scene = NULL; + for (int i = 0; i < data->nbufs; i++) + delete data->bufs[i]; + free(data->bufs); + data->bufs = 0; + data->nbufs = 0; +} + +void Scene::SetViewMatrix(mat4 const &m) +{ + data->m_view_matrix = m; +} + +void Scene::SetProjMatrix(mat4 const &m) +{ + data->m_proj_matrix = m; +} + +mat4 const &Scene::GetViewMatrix(void) +{ + return data->m_view_matrix; +} + +mat4 const &Scene::GetProjMatrix(void) +{ + return data->m_proj_matrix; } void Scene::AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale) @@ -131,10 +157,10 @@ void Scene::AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale) void Scene::Render() // XXX: rename to Blit() { - if (!stdshader) + if (!data->m_shader) { #if !defined _XBOX && !defined __CELLOS_LV2__ && !defined USE_D3D9 - stdshader = Shader::Create( + data->m_shader = Shader::Create( # if !defined HAVE_GLES_2X "#version 130\n" # endif @@ -216,7 +242,7 @@ void Scene::Render() // XXX: rename to Blit() " gl_FragColor = col;\n" "}\n"); #else - stdshader = Shader::Create( + data->m_shader = Shader::Create( "void main(float4 in_Position : POSITION," " float2 in_TexCoord : TEXCOORD0," " uniform float4x4 proj_matrix," @@ -288,35 +314,34 @@ void Scene::Render() // XXX: rename to Blit() qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare); // XXX: debug stuff - data->model_matrix = mat4::translate(320.0f, 240.0f, 0.0f); - data->model_matrix *= mat4::rotate(-data->angle, 1.0f, 0.0f, 0.0f); + data->m_model_matrix = mat4::translate(320.0f, 240.0f, 0.0f); #if 0 static float f = 0.0f; f += 0.01f; - data->model_matrix *= mat4::rotate(6.0f * sinf(f), 1.0f, 0.0f, 0.0f); - data->model_matrix *= mat4::rotate(17.0f * cosf(f), 0.0f, 0.0f, 1.0f); + data->m_model_matrix *= mat4::rotate(6.0f * sinf(f), 1.0f, 0.0f, 0.0f); + data->m_model_matrix *= mat4::rotate(17.0f * cosf(f), 0.0f, 0.0f, 1.0f); #endif - data->model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f); + data->m_model_matrix *= mat4::translate(-320.0f, -240.0f, 0.0f); // XXX: end of debug stuff ShaderUniform uni_mat, uni_tex; ShaderAttrib attr_pos, attr_tex; - attr_pos = stdshader->GetAttribLocation("in_Position", VertexUsage::Position, 0); - attr_tex = stdshader->GetAttribLocation("in_TexCoord", VertexUsage::TexCoord, 0); + attr_pos = data->m_shader->GetAttribLocation("in_Position", VertexUsage::Position, 0); + attr_tex = data->m_shader->GetAttribLocation("in_TexCoord", VertexUsage::TexCoord, 0); - stdshader->Bind(); + data->m_shader->Bind(); - uni_mat = stdshader->GetUniformLocation("proj_matrix"); - stdshader->SetUniform(uni_mat, Video::GetProjMatrix()); - uni_mat = stdshader->GetUniformLocation("view_matrix"); - stdshader->SetUniform(uni_mat, Video::GetViewMatrix()); - uni_mat = stdshader->GetUniformLocation("model_matrix"); - stdshader->SetUniform(uni_mat, data->model_matrix); + uni_mat = data->m_shader->GetUniformLocation("proj_matrix"); + data->m_shader->SetUniform(uni_mat, data->m_proj_matrix); + uni_mat = data->m_shader->GetUniformLocation("view_matrix"); + data->m_shader->SetUniform(uni_mat, data->m_view_matrix); + uni_mat = data->m_shader->GetUniformLocation("model_matrix"); + data->m_shader->SetUniform(uni_mat, data->m_model_matrix); #if defined USE_D3D9 || defined _XBOX #else - uni_tex = stdshader->GetUniformLocation("in_Texture"); - stdshader->SetUniform(uni_tex, 0); + uni_tex = data->m_shader->GetUniformLocation("in_Texture"); + data->m_shader->SetUniform(uni_tex, 0); #if !defined HAVE_GLES_2X glEnable(GL_TEXTURE_2D); @@ -385,7 +410,7 @@ void Scene::Render() // XXX: rename to Blit() data->tiles = 0; data->ntiles = 0; - stdshader->Unbind(); + data->m_shader->Unbind(); #if defined USE_D3D9 || defined _XBOX /* TODO */ diff --git a/src/scene.h b/src/scene.h index 95fa3c44..60a9dabd 100644 --- a/src/scene.h +++ b/src/scene.h @@ -28,15 +28,23 @@ class SceneData; class Scene { public: - Scene(float angle); + Scene(); ~Scene(); static Scene *GetDefault(); - static void Reset(); - void AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale); + void SetViewMatrix(mat4 const &m); + void SetProjMatrix(mat4 const &m); + mat4 const &GetViewMatrix(void); + mat4 const &GetProjMatrix(void); + + void Reset(); void Render(); + /* FIXME: this should be deprecated -- it doesn't really match + * the architecture we want to build */ + void AddTile(TileSet *tileset, int id, vec3 pos, int o, vec2 scale); + private: SceneData *data; }; diff --git a/src/ticker.cpp b/src/ticker.cpp index e0fae9f5..b5c7e340 100644 --- a/src/ticker.cpp +++ b/src/ticker.cpp @@ -342,17 +342,16 @@ void Ticker::TickDraw() Profiler::Start(Profiler::STAT_TICK_DRAW); - Video::Clear(); - - Scene::GetDefault(); - /* Tick objects for the draw loop */ for (int i = Entity::DRAWGROUP_BEGIN; i < Entity::DRAWGROUP_END; i++) { switch (i) { + case Entity::DRAWGROUP_BEGIN: + Scene::GetDefault()->Reset(); + Video::Clear(); + break; case Entity::DRAWGROUP_HUD: - Scene::GetDefault()->Render(); Video::SetDepth(false); break; default: @@ -375,10 +374,10 @@ void Ticker::TickDraw() e->m_tickstate = Entity::STATE_IDLE; #endif } - } - Scene::GetDefault()->Render(); - Scene::Reset(); + /* Do this render step */ + Scene::GetDefault()->Render(); + } Profiler::Stop(Profiler::STAT_TICK_DRAW); Profiler::Start(Profiler::STAT_TICK_BLIT); diff --git a/src/video.cpp b/src/video.cpp index 85b07bc5..a20440b3 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -51,7 +51,7 @@ class VideoData friend class Video; private: - static mat4 proj_matrix, view_matrix; + static mat4 proj_matrix; static ivec2 saved_viewport; #if defined USE_D3D9 static IDirect3D9 *d3d_ctx; @@ -65,7 +65,6 @@ private: }; mat4 VideoData::proj_matrix; -mat4 VideoData::view_matrix; ivec2 VideoData::saved_viewport(0, 0); #if defined USE_D3D9 @@ -194,8 +193,6 @@ void Video::SetFov(float theta) mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist); VideoData::proj_matrix = proj * trans; } - - VideoData::view_matrix = mat4(1.0f); } void Video::SetDepth(bool set) @@ -297,15 +294,5 @@ ivec2 Video::GetSize() #endif } -mat4 const & Video::GetProjMatrix() -{ - return VideoData::proj_matrix; -} - -mat4 const & Video::GetViewMatrix() -{ - return VideoData::view_matrix; -} - } /* namespace lol */ diff --git a/src/video.h b/src/video.h index 5917a722..83f5e2c9 100644 --- a/src/video.h +++ b/src/video.h @@ -33,8 +33,6 @@ public: static void Clear(); static void Capture(uint32_t *buffer); static ivec2 GetSize(); - static mat4 const & GetProjMatrix(); - static mat4 const & GetViewMatrix(); }; } /* namespace lol */