Commit a17295d6 by Martin Turski

various fixed and changes

parent 18c06434
Pipeline #65 skipped
......@@ -16,6 +16,8 @@
#include "Console.VariableHelpers.h"
#include "Camera.h"
namespace krt
{
......@@ -47,6 +49,8 @@ class Game
GameUniversePtr GetUniverse(const std::string& name);
Camera& GetWorldCamera(void) { return this->worldCam; }
private:
void MountUserDirectory();
......@@ -75,6 +79,8 @@ class Game
float timescale;
Camera worldCam; // camera to render the main world in
private:
std::unique_ptr<ConVar<int>> maxFPSVariable;
......
......@@ -58,11 +58,13 @@ class GameUniverse
{
auto it = m_modelIndexMapping.find(localId);
#if _DEBUG
// We kind of do not want this to happen in our testing.
// But if it does, then handle it appropriately.
assert(it != m_modelIndexMapping.end());
#endif
if ( it == m_modelIndexMapping.end() )
if (it == m_modelIndexMapping.end())
return -1;
return it->second;
......
......@@ -57,8 +57,8 @@ struct SectorGrid
LIST_FOREACH_END
}
template <typename callbackType>
inline void VisitSectorsByFrustum( const math::Frustum& frustum, callbackType& cb ) const
template <typename callbackType, typename frustumType>
inline void VisitSectorsByFrustum( const frustumType& frustum, callbackType& cb ) const
{
// For each visible sector, actually call our callback.
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
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
MountUserDirectory();
// run config.cfg
console::ExecuteSingleCommand(ProgramArguments{ "exec", "user:/config.cfg" });
console::ExecuteSingleCommand(ProgramArguments{"exec", "user:/config.cfg"});
// override variables from console
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.
......@@ -70,6 +77,9 @@ Game::Game(const std::vector<std::pair<std::string, std::string>>& setList) : st
Game::~Game(void)
{
// Delete important RW resources owned by the game.
worldCam.Shutdown();
// Delete all our entities.
{
while (!LIST_EMPTY(this->activeEntities.root))
......@@ -96,8 +106,7 @@ void Game::Run()
std::unique_ptr<GameWindow> gameWindow = GameWindow::Create("ATG: TheGame", 1280, 720, &eventSystem);
void* gfxContext = gameWindow->CreateGraphicsContext();
eventSystem.RegisterEventSourceFunction([&] ()
{
eventSystem.RegisterEventSourceFunction([&]() {
gameWindow->ProcessEvents();
});
......@@ -106,8 +115,7 @@ void Game::Run()
uint64_t lastTime = 0;
// exit command
ConsoleCommand quitCommand("quit", [&] ()
{
ConsoleCommand quitCommand("quit", [&]() {
wantsToExit = true;
});
......@@ -115,7 +123,7 @@ void Game::Run()
{
// limit frame rate and handle events
// 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;
uint64_t thisTime = 0;
......@@ -124,14 +132,14 @@ void Game::Run()
{
thisTime = eventSystem.HandleEvents();
millis = (uint32_t)( thisTime - lastTime );
millis = (uint32_t)(thisTime - lastTime);
YieldThreadForShortTime();
} while (millis < minMillis);
// handle time scaling
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)
{
......@@ -167,7 +175,7 @@ void Game::Run()
if (this->universes.size() > 0)
{
//RenderingTest(gfxContext);
theGame->GetWorld()->RenderWorld( gfxContext );
theGame->GetWorld()->RenderWorld(gfxContext);
}
// whatever else might come to mind
......
......@@ -7,18 +7,10 @@
#include "Console.CommandHelpers.h"
namespace krt
{
#include "Camera.h"
static ConsoleCommand cmdWorldStreamTest( "wsector_test",
[&]( void )
namespace krt
{
Game *theGame = krt::theGame;
World *theWorld = theGame->GetWorld();
theWorld->PutEntitiesOnGrid();
});
World::World( void )
{
......@@ -46,23 +38,7 @@ World::~World( void )
inline rw::V3d corvec( rw::V3d rwvec )
{
return rw::V3d( rwvec.x, rwvec.z, rwvec.y );
}
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);
return rwvec;
}
void World::DepopulateEntities( void )
......@@ -99,90 +75,59 @@ static ConsoleCommand putGridCmd( "pgrid",
printf( "populated static entity grid\n" );
});
#include <windows.h>
#include <d3d9.h>
static ConsoleCommand clearGridCmd( "cgrid",
[]( void )
{
theGame->GetWorld()->DepopulateEntities();
printf( "removed entities from static grid\n" );
});
void World::RenderWorld( void *gfxDevice )
{
// 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.
rw::Camera *testCamera = rw::Camera::create();
assert( testCamera != NULL );
testCamera->farPlane = 2000.0f;
testCamera->nearPlane = 1.0f;
//testCamera->projection = rw::Camera::PERSPECTIVE;
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 );
// Set some cool camera direction.
Camera& worldCam = theGame->GetWorldCamera();
{
// Crash in release mode at those coors in gta3
//rw::V3d cameraPosition(500.0f, 50.0f, 100.0f);
//rw::V3d objectPosition(500.0f, 0.0f, 100.0f);
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 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 left = rw::normalize(rw::cross(rw::V3d(0.0f, 0.0f, 1.0f), forward));
rw::V3d up = rw::cross(forward, left);
frame->matrix.right = left;
frame->matrix.up = up;
frame->matrix.at = forward;
frame->matrix.pos = cameraPosition;
frame->updateObjects();
}
rw::Matrix viewMat;
viewMat.setIdentity();
viewMat.right = left;
viewMat.up = up;
viewMat.at = forward;
viewMat.pos = cameraPosition;
worldCam.SetViewMatrix( viewMat );
}
rw::Matrix viewmat;
rw::Matrix::invert(&viewmat, testCamera->getFrame()->getLTM());
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);
// Begin the rendering.
worldCam.BeginUpdate( gfxDevice );
// 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();
......@@ -239,17 +184,8 @@ void World::RenderWorld( void *gfxDevice )
}
});
testCamera->destroy();
testCamera = NULL;
camFrame->destroy();
camFrame = NULL;
device->EndScene();
device->Present(nullptr, nullptr, nullptr, nullptr);
// Present world scene.
worldCam.EndUpdate();
// 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