1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-07 11:22:45 +01:00
openrw/viewer/main.cpp

200 lines
4.2 KiB
C++
Raw Normal View History

#define GLEW_STATIC
#include <GL/glew.h>
#include <renderwure/engine/GTAEngine.hpp>
#include <renderwure/loaders/LoaderDFF.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <SFML/Graphics.hpp>
#include <memory>
2013-07-02 12:48:01 +02:00
#include <sstream>
2013-07-04 01:35:03 +02:00
#include <getopt.h>
constexpr int WIDTH = 800,
HEIGHT = 600;
constexpr double PiOver180 = 3.1415926535897932384626433832795028/180;
sf::Window window;
LoaderDFF dffLoader;
2013-07-02 08:06:03 +02:00
GTAEngine* gta = nullptr;
glm::vec3 plyPos;
glm::vec2 plyLook;
float moveSpeed = 20.0f;
void handleEvent(sf::Event &event)
{
switch (event.type) {
case sf::Event::KeyPressed:
switch (event.key.code) {
case sf::Keyboard::Escape:
window.close();
break;
case sf::Keyboard::Space:
moveSpeed = 60.f;
break;
default: break;
}
break;
case sf::Event::KeyReleased:
switch(event.key.code) {
case sf::Keyboard::Space:
moveSpeed = 20.f;
break;
}
break;
default: break;
}
}
void init(std::string gtapath)
{
glClearColor(0.2, 0.2, 0.2, 1.0);
glEnable(GL_DEPTH_TEST);
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.
/*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");
gta->placeItems(gtapath + "/data/maps/industne/industNE.ipl");*/
2013-07-02 08:06:03 +02:00
plyPos = gta->itemCentroid / (float) gta->itemCount + glm::vec3(0, 0, 2);
glm::vec3 spawnPos = plyPos + glm::vec3(-5, -20, 0);
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);
if((k++ % 4) == 0) { spawnPos += glm::vec3(-20, -15, 0); }
}
}
void update(float dt)
{
static int i = 0;
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-02 08:39:56 +02:00
glm::vec3 movement;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
2013-07-02 08:39:56 +02:00
movement.z = -1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
2013-07-02 08:39:56 +02:00
movement.z = 1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
2013-07-02 08:39:56 +02:00
movement.x = -1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
2013-07-02 08:39:56 +02:00
movement.x = 1;
}
2013-07-02 08:39:56 +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 08:39:56 +02:00
if (glm::length(movement) > 0.f) {
plyPos += dt * moveSpeed * (glm::inverse(glm::mat3(view)) * movement);
}
view = glm::translate(view, -plyPos);
2013-07-02 09:53:23 +02:00
2013-07-02 14:49:20 +02:00
gta->renderer.camera.worldPos = plyPos;
2013-07-02 10:58:01 +02:00
gta->renderer.camera.frustum.view = view;
i++;
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update aspect ratio..
gta->renderer.camera.frustum.aspectRatio = window.getSize().x / (float) window.getSize().y;
2013-07-02 10:58:01 +02:00
glEnable(GL_CULL_FACE);
2013-07-02 09:53:23 +02:00
gta->renderer.renderWorld(gta);
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;
}
}
int main(int argc, char *argv[])
{
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <path to GTA3 root folder>" << std::endl;
exit(1);
}
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-04 01:35:03 +02:00
window.create(sf::VideoMode(w, h), "GTA3 Viewer", sf::Style::Close);
window.setVerticalSyncEnabled(true);
2013-07-04 01:35:03 +02:00
init(argv[optind]);
sf::Clock clock;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
handleEvent(event);
}
update(clock.restart().asSeconds());
render();
window.display();
}
return 0;
}