2013-07-01 02:22:29 +02:00
|
|
|
#define GLEW_STATIC
|
|
|
|
#include <GL/glew.h>
|
|
|
|
|
2013-07-02 08:40:43 +02:00
|
|
|
#include <renderwure/engine/GTAEngine.hpp>
|
|
|
|
#include <renderwure/loaders/LoaderDFF.hpp>
|
2013-07-21 06:12:27 +02:00
|
|
|
#include <renderwure/render/DebugDraw.hpp>
|
2013-08-11 22:42:54 +02:00
|
|
|
#include <renderwure/ai/GTAAIController.hpp>
|
2013-08-12 23:35:23 +02:00
|
|
|
#include <renderwure/ai/GTAPlayerAIController.hpp>
|
2013-07-01 02:22:29 +02:00
|
|
|
|
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
|
|
|
|
|
|
#include <SFML/Graphics.hpp>
|
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
#include <memory>
|
2013-07-02 12:48:01 +02:00
|
|
|
#include <sstream>
|
2013-07-04 01:35:03 +02:00
|
|
|
#include <getopt.h>
|
2013-07-01 05:40:11 +02:00
|
|
|
|
2013-07-01 02:22:29 +02:00
|
|
|
constexpr int WIDTH = 800,
|
|
|
|
HEIGHT = 600;
|
|
|
|
|
2013-07-02 00:16:02 +02:00
|
|
|
constexpr double PiOver180 = 3.1415926535897932384626433832795028/180;
|
|
|
|
|
2013-07-05 20:50:04 +02:00
|
|
|
sf::RenderWindow window;
|
2013-07-01 02:22:29 +02:00
|
|
|
|
2013-07-02 08:06:03 +02:00
|
|
|
GTAEngine* gta = nullptr;
|
2013-07-01 05:40:11 +02:00
|
|
|
|
2013-08-12 23:35:23 +02:00
|
|
|
GTAPlayerAIController* player = nullptr;
|
|
|
|
GTACharacter* playerCharacter = nullptr;
|
|
|
|
|
2013-07-22 03:38:43 +02:00
|
|
|
DebugDraw* debugDrawer = nullptr;
|
|
|
|
|
2013-07-02 00:16:02 +02:00
|
|
|
glm::vec3 plyPos;
|
|
|
|
glm::vec2 plyLook;
|
2013-07-02 18:29:20 +02:00
|
|
|
float moveSpeed = 20.0f;
|
2013-07-04 11:56:23 +02:00
|
|
|
bool inFocus = false;
|
2013-07-04 11:57:31 +02:00
|
|
|
bool mouseGrabbed = true;
|
2013-07-30 17:59:44 +02:00
|
|
|
int debugMode = 0;
|
2013-07-02 00:16:02 +02:00
|
|
|
|
2013-07-05 20:50:04 +02:00
|
|
|
sf::Font font;
|
|
|
|
|
2013-07-01 02:22:29 +02:00
|
|
|
void handleEvent(sf::Event &event)
|
|
|
|
{
|
|
|
|
switch (event.type) {
|
|
|
|
case sf::Event::KeyPressed:
|
|
|
|
switch (event.key.code) {
|
|
|
|
case sf::Keyboard::Escape:
|
|
|
|
window.close();
|
|
|
|
break;
|
2013-07-02 18:29:20 +02:00
|
|
|
case sf::Keyboard::Space:
|
|
|
|
moveSpeed = 60.f;
|
|
|
|
break;
|
2013-07-04 11:57:31 +02:00
|
|
|
case sf::Keyboard::M:
|
|
|
|
mouseGrabbed = ! mouseGrabbed;
|
|
|
|
break;
|
2013-07-21 06:12:27 +02:00
|
|
|
case sf::Keyboard::P:
|
2013-07-30 17:59:44 +02:00
|
|
|
debugMode = debugMode++;
|
|
|
|
while(debugMode > 2) debugMode -= 3;
|
2013-07-21 06:12:27 +02:00
|
|
|
break;
|
2013-07-01 02:22:29 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
break;
|
2013-07-02 18:29:20 +02:00
|
|
|
case sf::Event::KeyReleased:
|
|
|
|
switch(event.key.code) {
|
|
|
|
case sf::Keyboard::Space:
|
|
|
|
moveSpeed = 20.f;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2013-07-04 11:56:23 +02:00
|
|
|
case sf::Event::GainedFocus:
|
|
|
|
inFocus = true;
|
|
|
|
break;
|
|
|
|
case sf::Event::LostFocus:
|
|
|
|
inFocus = false;
|
|
|
|
break;
|
2013-07-01 02:22:29 +02:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
void init(std::string gtapath)
|
2013-07-01 02:22:29 +02:00
|
|
|
{
|
2013-07-02 08:06:03 +02:00
|
|
|
// GTA GET
|
|
|
|
gta = new GTAEngine(gtapath);
|
|
|
|
|
|
|
|
// This is harcoded in GTA III for some reason
|
|
|
|
gta->gameData.loadIMG("/models/gta3");
|
|
|
|
|
|
|
|
gta->load();
|
|
|
|
|
|
|
|
// Test out a known IPL.
|
2013-07-02 18:29:20 +02:00
|
|
|
/*gta->placeItems(gtapath + "/data/maps/industsw/industSW.ipl");
|
2013-07-02 14:49:20 +02:00
|
|
|
gta->placeItems(gtapath + "/data/maps/industnw/industNW.ipl");
|
|
|
|
gta->placeItems(gtapath + "/data/maps/industse/industSE.ipl");
|
2013-07-02 18:29:20 +02:00
|
|
|
gta->placeItems(gtapath + "/data/maps/industne/industNE.ipl");*/
|
2013-07-02 08:06:03 +02:00
|
|
|
|
2013-07-04 01:23:12 +02:00
|
|
|
plyPos = gta->itemCentroid / (float) gta->itemCount + glm::vec3(0, 0, 2);
|
2013-07-02 19:37:19 +02:00
|
|
|
|
2013-07-04 01:23:12 +02:00
|
|
|
glm::vec3 spawnPos = plyPos + glm::vec3(-5, -20, 0);
|
2013-07-02 19:37:19 +02:00
|
|
|
size_t k = 1;
|
|
|
|
// Spawn every vehicle, cause why not.
|
|
|
|
for(std::map<uint16_t, std::shared_ptr<LoaderIDE::CARS_t>>::iterator it = gta->vehicleTypes.begin();
|
|
|
|
it != gta->vehicleTypes.end(); ++it) {
|
|
|
|
gta->createVehicle(it->first, spawnPos);
|
|
|
|
spawnPos += glm::vec3(5, 0, 0);
|
2013-07-24 01:42:50 +02:00
|
|
|
if((k++ % 4) == 0) { spawnPos += glm::vec3(-20, -15, 0); }
|
2013-07-02 19:37:19 +02:00
|
|
|
}
|
2013-07-24 01:42:50 +02:00
|
|
|
|
|
|
|
spawnPos = plyPos + glm::vec3(-5, 20 + (2.5 * gta->pedestrianTypes.size()/4), 0);
|
|
|
|
k = 1;
|
|
|
|
// Spawn every pedestrian.
|
|
|
|
for(auto it = gta->pedestrianTypes.begin();
|
|
|
|
it != gta->pedestrianTypes.end(); ++it) {
|
|
|
|
gta->createPedestrian(it->first, spawnPos);
|
|
|
|
spawnPos += glm::vec3(2.5, 0, 0);
|
|
|
|
if((k++ % 6) == 0) { spawnPos += glm::vec3(-15, -2.5, 0); }
|
|
|
|
}
|
2013-07-21 06:12:27 +02:00
|
|
|
|
2013-07-22 03:38:43 +02:00
|
|
|
debugDrawer = new DebugDraw;
|
|
|
|
debugDrawer->setShaderProgram(gta->renderer.worldProgram);
|
|
|
|
debugDrawer->setDebugMode(btIDebugDraw::DBG_DrawWireframe);
|
|
|
|
gta->dynamicsWorld->setDebugDrawer(debugDrawer);
|
2013-07-01 02:22:29 +02:00
|
|
|
}
|
|
|
|
|
2013-07-02 11:39:52 +02:00
|
|
|
void update(float dt)
|
2013-07-01 02:22:29 +02:00
|
|
|
{
|
2013-07-04 11:56:23 +02:00
|
|
|
if (inFocus) {
|
2013-07-04 11:57:31 +02:00
|
|
|
if (mouseGrabbed) {
|
|
|
|
sf::Vector2i screenCenter{sf::Vector2i{window.getSize()} / 2};
|
|
|
|
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
|
|
|
|
sf::Vector2i deltaMouse = mousePos - screenCenter;
|
|
|
|
sf::Mouse::setPosition(screenCenter, window);
|
|
|
|
|
|
|
|
plyLook.x += deltaMouse.x / 10.0;
|
|
|
|
plyLook.y += deltaMouse.y / 10.0;
|
|
|
|
|
|
|
|
if (plyLook.y > 90)
|
|
|
|
plyLook.y = 90;
|
|
|
|
else if (plyLook.y < -90)
|
|
|
|
plyLook.y = -90;
|
|
|
|
}
|
2013-07-04 11:56:23 +02:00
|
|
|
|
|
|
|
glm::vec3 movement;
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
|
|
|
movement.z = -1;
|
|
|
|
}
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
|
|
|
movement.z = 1;
|
|
|
|
}
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
|
|
|
movement.x = -1;
|
|
|
|
}
|
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
|
|
|
movement.x = 1;
|
|
|
|
}
|
2013-08-12 23:35:23 +02:00
|
|
|
if (sf::Keyboard::isKeyPressed(sf::Keyboard::H)) {
|
|
|
|
if( player == nullptr ) {
|
|
|
|
playerCharacter = gta->createPedestrian(1, plyPos);
|
|
|
|
player = new GTAPlayerAIController(playerCharacter);
|
|
|
|
}
|
|
|
|
}
|
2013-07-02 00:16:02 +02:00
|
|
|
|
2013-07-04 11:56:23 +02:00
|
|
|
glm::mat4 view;
|
|
|
|
view = glm::rotate(view, -90.f, glm::vec3(1, 0, 0));
|
|
|
|
view = glm::rotate(view, plyLook.y, glm::vec3(1, 0, 0));
|
|
|
|
view = glm::rotate(view, plyLook.x, glm::vec3(0, 0, 1));
|
2013-07-02 00:16:02 +02:00
|
|
|
|
2013-08-12 23:35:23 +02:00
|
|
|
if( player != nullptr ) {
|
|
|
|
glm::quat playerCamera(glm::vec3(0.f, 0.f, -plyLook.x * PiOver180));
|
2013-08-13 18:15:28 +02:00
|
|
|
movement.y = -movement.z;
|
|
|
|
movement.z = 0.f;
|
2013-08-12 23:35:23 +02:00
|
|
|
player->updateCameraDirection(playerCamera);
|
|
|
|
player->updateMovementDirection(movement);
|
|
|
|
player->setRunning(moveSpeed > 21.f);
|
|
|
|
|
|
|
|
glm::vec3 localView = glm::inverse(glm::mat3(view)) * glm::vec3(0.f, -0.5f, -2.5f);
|
|
|
|
view = glm::translate(view, -playerCharacter->position + localView);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (glm::length(movement) > 0.f) {
|
|
|
|
plyPos += dt * moveSpeed * (glm::inverse(glm::mat3(view)) * movement);
|
|
|
|
}
|
|
|
|
view = glm::translate(view, -plyPos);
|
2013-07-04 11:56:23 +02:00
|
|
|
}
|
2013-07-05 03:15:29 +02:00
|
|
|
|
|
|
|
gta->gameTime += dt;
|
2013-07-02 00:16:02 +02:00
|
|
|
|
2013-07-04 11:56:23 +02:00
|
|
|
gta->renderer.camera.worldPos = plyPos;
|
|
|
|
gta->renderer.camera.frustum.view = view;
|
2013-08-11 22:50:29 +02:00
|
|
|
|
|
|
|
for( size_t p = 0; p < gta->pedestrians.size(); ++p ) {
|
2013-08-11 22:42:54 +02:00
|
|
|
if(gta->pedestrians[p]->controller) {
|
|
|
|
gta->pedestrians[p]->controller->update(dt);
|
|
|
|
}
|
|
|
|
gta->pedestrians[p]->updateCharacter();
|
|
|
|
gta->pedestrians[p]->updateAnimation(dt);
|
2013-08-11 22:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
gta->dynamicsWorld->stepSimulation(dt);
|
2013-07-02 00:16:02 +02:00
|
|
|
}
|
2013-07-01 02:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void render()
|
|
|
|
{
|
2013-07-02 10:09:40 +02:00
|
|
|
// Update aspect ratio..
|
|
|
|
gta->renderer.camera.frustum.aspectRatio = window.getSize().x / (float) window.getSize().y;
|
2013-07-02 10:58:01 +02:00
|
|
|
|
2013-07-06 02:39:54 +02:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
//glEnable(GL_CULL_FACE);
|
2013-07-01 02:22:29 +02:00
|
|
|
|
2013-07-30 17:59:44 +02:00
|
|
|
switch( debugMode ) {
|
|
|
|
case 0:
|
2013-07-24 01:42:50 +02:00
|
|
|
gta->renderer.renderWorld();
|
2013-07-30 17:59:44 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 1: {
|
|
|
|
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
|
|
|
|
glUseProgram(gta->renderer.worldProgram);
|
|
|
|
glm::mat4 proj = gta->renderer.camera.frustum.projection();
|
|
|
|
glm::mat4 view = gta->renderer.camera.frustum.view;
|
|
|
|
glUniformMatrix4fv(gta->renderer.uniView, 1, GL_FALSE, glm::value_ptr(view));
|
|
|
|
glUniformMatrix4fv(gta->renderer.uniProj, 1, GL_FALSE, glm::value_ptr(proj));
|
|
|
|
gta->renderer.renderPaths();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
|
|
|
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT);
|
|
|
|
glUseProgram(gta->renderer.worldProgram);
|
|
|
|
glm::mat4 proj = gta->renderer.camera.frustum.projection();
|
|
|
|
glm::mat4 view = gta->renderer.camera.frustum.view;
|
|
|
|
glUniformMatrix4fv(gta->renderer.uniView, 1, GL_FALSE, glm::value_ptr(view));
|
|
|
|
glUniformMatrix4fv(gta->renderer.uniProj, 1, GL_FALSE, glm::value_ptr(proj));
|
|
|
|
gta->dynamicsWorld->debugDrawWorld();
|
|
|
|
debugDrawer->drawAllLines();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-05 20:50:04 +02:00
|
|
|
window.resetGLStates();
|
2013-07-06 02:39:54 +02:00
|
|
|
|
2013-07-05 20:50:04 +02:00
|
|
|
std::stringstream ss;
|
2013-07-06 02:39:54 +02:00
|
|
|
ss << fmod(floor(gta->gameTime), 24.f) << ":" << (floor(fmod(gta->gameTime, 1.f) * 60.f)) << " (" << gta->gameTime << ")";
|
|
|
|
sf::Text text(ss.str(), font, 15);
|
|
|
|
text.setPosition(10, 10);
|
|
|
|
window.draw(text);
|
|
|
|
|
|
|
|
while( gta->log.size() > 0 && gta->log.front().time + 10.f < gta->gameTime ) {
|
|
|
|
gta->log.pop_front();
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Vector2f tpos(10.f, 40.f);
|
|
|
|
text.setCharacterSize(15);
|
|
|
|
for(auto it = gta->log.begin(); it != gta->log.end(); ++it) {
|
|
|
|
text.setString(it->message);
|
|
|
|
switch(it->type) {
|
|
|
|
case GTAEngine::LogEntry::Error:
|
|
|
|
text.setColor(sf::Color::Red);
|
|
|
|
break;
|
|
|
|
case GTAEngine::LogEntry::Warning:
|
|
|
|
text.setColor(sf::Color::Yellow);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
text.setColor(sf::Color::White);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interpolate the color
|
|
|
|
auto c = text.getColor();
|
|
|
|
c.a = (gta->gameTime - it->time > 5.f) ? 255 - (((gta->gameTime - it->time) - 5.f)/5.f) * 255 : 255;
|
|
|
|
text.setColor(c);
|
|
|
|
|
|
|
|
text.setPosition(tpos);
|
|
|
|
window.draw(text);
|
|
|
|
tpos.y += text.getLocalBounds().height;
|
|
|
|
}
|
2013-07-05 20:50:04 +02:00
|
|
|
|
2013-07-02 10:58:01 +02:00
|
|
|
static size_t fc = 0;
|
|
|
|
if(fc++ == 60)
|
|
|
|
{
|
|
|
|
std::cout << "Rendered: " << gta->renderer.rendered << " / Culled: " << gta->renderer.culled << std::endl;
|
|
|
|
fc = 0;
|
|
|
|
}
|
2013-07-01 02:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2013-07-01 05:40:11 +02:00
|
|
|
if (argc < 2) {
|
|
|
|
std::cout << "Usage: " << argv[0] << " <path to GTA3 root folder>" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
2013-07-05 20:50:04 +02:00
|
|
|
|
2013-07-06 02:39:54 +02:00
|
|
|
if(! font.loadFromFile("DejaVuSansMono.ttf")) {
|
|
|
|
std::cerr << "Failed to load font" << std::endl;
|
2013-07-05 20:50:04 +02:00
|
|
|
}
|
2013-07-01 05:40:11 +02:00
|
|
|
|
2013-07-01 02:22:29 +02:00
|
|
|
glewExperimental = GL_TRUE;
|
|
|
|
glewInit();
|
2013-07-04 01:35:03 +02:00
|
|
|
|
|
|
|
size_t w = WIDTH, h = HEIGHT;
|
|
|
|
int c;
|
|
|
|
while( (c = getopt(argc, argv, "w:h:")) != -1) {
|
|
|
|
switch(c) {
|
|
|
|
case 'w':
|
|
|
|
w = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
h = atoi(optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-07-01 02:22:29 +02:00
|
|
|
|
2013-07-04 01:35:03 +02:00
|
|
|
window.create(sf::VideoMode(w, h), "GTA3 Viewer", sf::Style::Close);
|
2013-07-01 02:22:29 +02:00
|
|
|
window.setVerticalSyncEnabled(true);
|
|
|
|
|
2013-07-04 01:35:03 +02:00
|
|
|
init(argv[optind]);
|
2013-07-02 11:39:52 +02:00
|
|
|
|
|
|
|
sf::Clock clock;
|
2013-07-01 02:22:29 +02:00
|
|
|
|
|
|
|
while (window.isOpen()) {
|
|
|
|
sf::Event event;
|
|
|
|
while (window.pollEvent(event)) {
|
|
|
|
handleEvent(event);
|
|
|
|
}
|
|
|
|
|
2013-07-02 12:20:18 +02:00
|
|
|
update(clock.restart().asSeconds());
|
2013-07-02 11:39:52 +02:00
|
|
|
|
2013-07-01 02:22:29 +02:00
|
|
|
render();
|
|
|
|
window.display();
|
2013-07-02 11:39:52 +02:00
|
|
|
|
2013-07-01 02:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|