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

164 lines
3.4 KiB
C++

#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>
constexpr int WIDTH = 800,
HEIGHT = 600;
constexpr double PiOver180 = 3.1415926535897932384626433832795028/180;
sf::Window window;
LoaderDFF dffLoader;
GTAEngine* gta = nullptr;
glm::vec3 plyPos;
glm::vec2 plyLook;
void handleEvent(sf::Event &event)
{
switch (event.type) {
case sf::Event::KeyPressed:
switch (event.key.code) {
case sf::Keyboard::Escape:
window.close();
break;
default: break;
}
break;
default: break;
}
}
void init(std::string gtapath)
{
glClearColor(0.2, 0.2, 0.2, 1.0);
glEnable(GL_DEPTH_TEST);
// 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->loadItems(gtapath + "/data/maps/industsw/industSW.ipl");
gta->loadItems(gtapath + "/data/maps/industnw/industNW.ipl");
gta->loadItems(gtapath + "/data/maps/industse/industSE.ipl");
gta->loadItems(gtapath + "/data/maps/industne/industNE.ipl");
plyPos = gta->itemCentroid / (float) gta->instances.size();
}
void update(float dt)
{
static int i = 0;
constexpr float moveSpeed = 20;
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;
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;
}
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));
if (glm::length(movement) > 0.f) {
plyPos += dt * moveSpeed * (glm::inverse(glm::mat3(view)) * movement);
}
view = glm::translate(view, -plyPos);
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;
glEnable(GL_CULL_FACE);
gta->renderer.renderWorld(gta);
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();
window.create(sf::VideoMode(WIDTH, HEIGHT), "GTA3 Viewer", sf::Style::Close);
window.setVerticalSyncEnabled(true);
init(argv[1]);
sf::Clock clock;
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
handleEvent(event);
}
update(clock.restart().asSeconds());
render();
window.display();
}
return 0;
}