Commit a17295d6 by Martin Turski

various fixed and changes

parent 18c06434
Pipeline #65 skipped
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
#include "Console.VariableHelpers.h" #include "Console.VariableHelpers.h"
#include "Camera.h"
namespace krt namespace krt
{ {
...@@ -47,6 +49,8 @@ class Game ...@@ -47,6 +49,8 @@ class Game
GameUniversePtr GetUniverse(const std::string& name); GameUniversePtr GetUniverse(const std::string& name);
Camera& GetWorldCamera(void) { return this->worldCam; }
private: private:
void MountUserDirectory(); void MountUserDirectory();
...@@ -75,6 +79,8 @@ class Game ...@@ -75,6 +79,8 @@ class Game
float timescale; float timescale;
Camera worldCam; // camera to render the main world in
private: private:
std::unique_ptr<ConVar<int>> maxFPSVariable; std::unique_ptr<ConVar<int>> maxFPSVariable;
......
...@@ -58,11 +58,13 @@ class GameUniverse ...@@ -58,11 +58,13 @@ class GameUniverse
{ {
auto it = m_modelIndexMapping.find(localId); auto it = m_modelIndexMapping.find(localId);
#if _DEBUG
// We kind of do not want this to happen in our testing. // We kind of do not want this to happen in our testing.
// But if it does, then handle it appropriately. // But if it does, then handle it appropriately.
assert(it != m_modelIndexMapping.end()); assert(it != m_modelIndexMapping.end());
#endif
if ( it == m_modelIndexMapping.end() ) if (it == m_modelIndexMapping.end())
return -1; return -1;
return it->second; return it->second;
......
...@@ -57,8 +57,8 @@ struct SectorGrid ...@@ -57,8 +57,8 @@ struct SectorGrid
LIST_FOREACH_END LIST_FOREACH_END
} }
template <typename callbackType> template <typename callbackType, typename frustumType>
inline void VisitSectorsByFrustum( const math::Frustum& frustum, callbackType& cb ) const inline void VisitSectorsByFrustum( const frustumType& frustum, callbackType& cb ) const
{ {
// For each visible sector, actually call our callback. // For each visible sector, actually call our callback.
LIST_FOREACH_BEGIN( Sector, this->sectorList.root, node ) LIST_FOREACH_BEGIN( Sector, this->sectorList.root, node )
......
...@@ -49,16 +49,23 @@ Game::Game(const std::vector<std::pair<std::string, std::string>>& setList) : st ...@@ -49,16 +49,23 @@ Game::Game(const std::vector<std::pair<std::string, std::string>>& setList) : st
gta::attachPlugins(); gta::attachPlugins();
// Prepare main world camera. (NOT FINAL).
worldCam.Initialize();
worldCam.SetAspectRatio(16.0f / 9.0f);
worldCam.SetFOV(65.0f);
worldCam.SetFarClip(1500.0f);
// mount the user directory // mount the user directory
MountUserDirectory(); MountUserDirectory();
// run config.cfg // run config.cfg
console::ExecuteSingleCommand(ProgramArguments{ "exec", "user:/config.cfg" }); console::ExecuteSingleCommand(ProgramArguments{"exec", "user:/config.cfg"});
// override variables from console // override variables from console
for (auto& pair : setList) for (auto& pair : setList)
{ {
console::ExecuteSingleCommand(ProgramArguments{ "set", pair.first, pair.second }); console::ExecuteSingleCommand(ProgramArguments{"set", pair.first, pair.second});
} }
// Set up game related things. // Set up game related things.
...@@ -70,6 +77,9 @@ Game::Game(const std::vector<std::pair<std::string, std::string>>& setList) : st ...@@ -70,6 +77,9 @@ Game::Game(const std::vector<std::pair<std::string, std::string>>& setList) : st
Game::~Game(void) Game::~Game(void)
{ {
// Delete important RW resources owned by the game.
worldCam.Shutdown();
// Delete all our entities. // Delete all our entities.
{ {
while (!LIST_EMPTY(this->activeEntities.root)) while (!LIST_EMPTY(this->activeEntities.root))
...@@ -96,8 +106,7 @@ void Game::Run() ...@@ -96,8 +106,7 @@ void Game::Run()
std::unique_ptr<GameWindow> gameWindow = GameWindow::Create("ATG: TheGame", 1280, 720, &eventSystem); std::unique_ptr<GameWindow> gameWindow = GameWindow::Create("ATG: TheGame", 1280, 720, &eventSystem);
void* gfxContext = gameWindow->CreateGraphicsContext(); void* gfxContext = gameWindow->CreateGraphicsContext();
eventSystem.RegisterEventSourceFunction([&] () eventSystem.RegisterEventSourceFunction([&]() {
{
gameWindow->ProcessEvents(); gameWindow->ProcessEvents();
}); });
...@@ -106,8 +115,7 @@ void Game::Run() ...@@ -106,8 +115,7 @@ void Game::Run()
uint64_t lastTime = 0; uint64_t lastTime = 0;
// exit command // exit command
ConsoleCommand quitCommand("quit", [&] () ConsoleCommand quitCommand("quit", [&]() {
{
wantsToExit = true; wantsToExit = true;
}); });
...@@ -115,7 +123,7 @@ void Game::Run() ...@@ -115,7 +123,7 @@ void Game::Run()
{ {
// limit frame rate and handle events // limit frame rate and handle events
// TODO: non-busy wait? // TODO: non-busy wait?
uint32_t minMillis = (this->maxFPS > 0) ? (1000u / (uint32_t)this->maxFPS) : 1u; // we can cast to unsigned because its bigger than zero. uint32_t minMillis = (this->maxFPS > 0) ? (1000u / (uint32_t) this->maxFPS) : 1u; // we can cast to unsigned because its bigger than zero.
uint32_t millis = 0; uint32_t millis = 0;
uint64_t thisTime = 0; uint64_t thisTime = 0;
...@@ -124,14 +132,14 @@ void Game::Run() ...@@ -124,14 +132,14 @@ void Game::Run()
{ {
thisTime = eventSystem.HandleEvents(); thisTime = eventSystem.HandleEvents();
millis = (uint32_t)( thisTime - lastTime ); millis = (uint32_t)(thisTime - lastTime);
YieldThreadForShortTime(); YieldThreadForShortTime();
} while (millis < minMillis); } while (millis < minMillis);
// handle time scaling // handle time scaling
float scale = this->timescale; // to be replaced by a console value, again float scale = this->timescale; // to be replaced by a console value, again
millis = (uint32_t)( (float)millis * scale ); millis = (uint32_t)((float)millis * scale);
if (millis < 1) if (millis < 1)
{ {
...@@ -167,7 +175,7 @@ void Game::Run() ...@@ -167,7 +175,7 @@ void Game::Run()
if (this->universes.size() > 0) if (this->universes.size() > 0)
{ {
//RenderingTest(gfxContext); //RenderingTest(gfxContext);
theGame->GetWorld()->RenderWorld( gfxContext ); theGame->GetWorld()->RenderWorld(gfxContext);
} }
// whatever else might come to mind // whatever else might come to mind
......
...@@ -7,18 +7,10 @@ ...@@ -7,18 +7,10 @@
#include "Console.CommandHelpers.h" #include "Console.CommandHelpers.h"
namespace krt #include "Camera.h"
{
static ConsoleCommand cmdWorldStreamTest( "wsector_test", namespace krt
[&]( void )
{ {
Game *theGame = krt::theGame;
World *theWorld = theGame->GetWorld();
theWorld->PutEntitiesOnGrid();
});
World::World( void ) World::World( void )
{ {
...@@ -46,23 +38,7 @@ World::~World( void ) ...@@ -46,23 +38,7 @@ World::~World( void )
inline rw::V3d corvec( rw::V3d rwvec ) inline rw::V3d corvec( rw::V3d rwvec )
{ {
return rw::V3d( rwvec.x, rwvec.z, rwvec.y ); return rwvec;
}
inline math::Frustum getCameraFrustum( rw::Camera *camera, float fov_angle )
{
rw::Matrix viewMat;
rw::Matrix cameraWorldMat = *camera->getFrame()->getLTM();
rw::Matrix::invert(&viewMat, &cameraWorldMat);
// why???
rw::Matrix cameraProjMat;
memcpy(&cameraProjMat, camera->projMat, sizeof(rw::Matrix));
rw::Matrix viewProj;
rw::Matrix::mult(&viewProj, &cameraProjMat, &viewMat);
return math::Frustum(viewProj);
} }
void World::DepopulateEntities( void ) void World::DepopulateEntities( void )
...@@ -99,90 +75,59 @@ static ConsoleCommand putGridCmd( "pgrid", ...@@ -99,90 +75,59 @@ static ConsoleCommand putGridCmd( "pgrid",
printf( "populated static entity grid\n" ); printf( "populated static entity grid\n" );
}); });
#include <windows.h> static ConsoleCommand clearGridCmd( "cgrid",
#include <d3d9.h> []( void )
{
theGame->GetWorld()->DepopulateEntities();
printf( "removed entities from static grid\n" );
});
void World::RenderWorld( void *gfxDevice ) void World::RenderWorld( void *gfxDevice )
{ {
// Thank you Bas for figuring out that rendering gunk! // Thank you Bas for figuring out that rendering gunk!
// perform rendering
IDirect3DDevice9* device = reinterpret_cast<IDirect3DDevice9*>(gfxDevice);
device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0x99, 0xff), 1.0f, 0);
device->BeginScene();
device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
device->SetRenderState(D3DRS_ALPHABLENDENABLE, 1);
device->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
// Create a test frustum and see how it intersects with things. // Create a test frustum and see how it intersects with things.
rw::Camera *testCamera = rw::Camera::create();
assert( testCamera != NULL ); // Set some cool camera direction.
Camera& worldCam = theGame->GetWorldCamera();
testCamera->farPlane = 2000.0f; {
testCamera->nearPlane = 1.0f; // Crash in release mode at those coors in gta3
//testCamera->projection = rw::Camera::PERSPECTIVE; //rw::V3d cameraPosition(500.0f, 50.0f, 100.0f);
//rw::V3d objectPosition(500.0f, 0.0f, 100.0f);
float fov_angle = 65.0f;
testCamera->setFOV( fov_angle, 16.0f / 9.0f );
rw::Frame *camFrame = rw::Frame::create();
assert( camFrame != NULL );
rw::Matrix cam_mat;
cam_mat.setIdentity();
cam_mat.pos = rw::V3d( -15, 100, -1890 );
cam_mat.posw = 1;
camFrame->matrix = cam_mat;
camFrame->updateObjects();
testCamera->setFrame( camFrame );
testCamera->updateProjectionMatrix(); rw::V3d cameraPosition(500.0f, 50.0f, 100.0f);
rw::V3d objectPosition(0.0f, 50.0f, 100.0f);
{ #if 0
// Bas' settings.
rw::V3d cameraPosition(500.0f, 50.0f, 100.0f); rw::V3d cameraPosition(500.0f, 50.0f, 100.0f);
rw::V3d objectPosition(0.0f, 0.0f, 0.0f); rw::V3d objectPosition(0.0f, 0.0f, 0.0f);
#endif
rw::Frame* frame = testCamera->getFrame();
if (frame)
{
rw::V3d forward = rw::normalize(rw::sub(objectPosition, cameraPosition)); rw::V3d forward = rw::normalize(rw::sub(objectPosition, cameraPosition));
rw::V3d left = rw::normalize(rw::cross(rw::V3d(0.0f, 0.0f, 1.0f), forward)); rw::V3d left = rw::normalize(rw::cross(rw::V3d(0.0f, 0.0f, 1.0f), forward));
rw::V3d up = rw::cross(forward, left); rw::V3d up = rw::cross(forward, left);
frame->matrix.right = left; rw::Matrix viewMat;
frame->matrix.up = up; viewMat.setIdentity();
frame->matrix.at = forward;
frame->matrix.pos = cameraPosition; viewMat.right = left;
frame->updateObjects(); viewMat.up = up;
} viewMat.at = forward;
viewMat.pos = cameraPosition;
worldCam.SetViewMatrix( viewMat );
} }
rw::Matrix viewmat; // Begin the rendering.
rw::Matrix::invert(&viewmat, testCamera->getFrame()->getLTM()); worldCam.BeginUpdate( gfxDevice );
viewmat.right.x = -viewmat.right.x;
viewmat.rightw = 0.0;
viewmat.up.x = -viewmat.up.x;
viewmat.upw = 0.0;
viewmat.at.x = -viewmat.at.x;
viewmat.atw = 0.0;
viewmat.pos.x = -viewmat.pos.x;
viewmat.posw = 1.0;
device->SetTransform(D3DTS_VIEW, (D3DMATRIX*)&viewmat);
device->SetTransform(D3DTS_PROJECTION, (D3DMATRIX*)testCamera->projMat);
// Get its frustum. // Get its frustum.
math::Frustum frustum = getCameraFrustum( testCamera, fov_angle ); // For now it does not matter which; both promise that things that should be visible are indeed visible.
// The difference is that the complex frustum culls away more objects than the simple frustum.
//math::Frustum frustum = worldCam.GetSimpleFrustum();
math::Quader frustum = worldCam.GetComplexFrustum();
streaming::StreamMan& streaming = theGame->GetStreaming(); streaming::StreamMan& streaming = theGame->GetStreaming();
...@@ -239,17 +184,8 @@ void World::RenderWorld( void *gfxDevice ) ...@@ -239,17 +184,8 @@ void World::RenderWorld( void *gfxDevice )
} }
}); });
testCamera->destroy(); // Present world scene.
worldCam.EndUpdate();
testCamera = NULL;
camFrame->destroy();
camFrame = NULL;
device->EndScene();
device->Present(nullptr, nullptr, nullptr, nullptr);
// OK. // OK.
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment