2013-07-01 02:22:29 +02:00
|
|
|
#define GLEW_STATIC
|
|
|
|
#include <GL/glew.h>
|
|
|
|
|
|
|
|
#include "../framework/LoaderIPL.h"
|
|
|
|
#include "../framework/LoaderIMG.h"
|
|
|
|
#include "../framework/LoaderDFF.h"
|
2013-07-01 05:40:11 +02:00
|
|
|
#include "../framework/TextureLoader.h"
|
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-01 02:22:29 +02:00
|
|
|
constexpr int WIDTH = 800,
|
|
|
|
HEIGHT = 600;
|
|
|
|
|
|
|
|
sf::Window window;
|
|
|
|
|
|
|
|
const char *vertexShaderSource = "#version 130\n"
|
|
|
|
"in vec3 position;"
|
2013-07-01 05:40:11 +02:00
|
|
|
"in vec2 texCoords;"
|
|
|
|
"out vec2 TexCoords;"
|
2013-07-01 02:22:29 +02:00
|
|
|
"uniform mat4 model;"
|
|
|
|
"uniform mat4 view;"
|
|
|
|
"uniform mat4 proj;"
|
|
|
|
"void main()"
|
|
|
|
"{"
|
2013-07-01 05:40:11 +02:00
|
|
|
" TexCoords = texCoords;"
|
|
|
|
" gl_Position = proj * view * model * vec4(position, 1.0);"
|
2013-07-01 02:22:29 +02:00
|
|
|
"}";
|
|
|
|
const char *fragmentShaderSource = "#version 130\n"
|
2013-07-01 05:40:11 +02:00
|
|
|
"in vec2 TexCoords;"
|
|
|
|
"uniform sampler2D texture;"
|
2013-07-01 02:22:29 +02:00
|
|
|
"void main()"
|
|
|
|
"{"
|
2013-07-01 05:40:11 +02:00
|
|
|
// " gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);"
|
|
|
|
" gl_FragColor = texture2D(texture, TexCoords);"
|
2013-07-01 02:22:29 +02:00
|
|
|
"}";
|
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
GLuint uniModel, uniProj, uniView;
|
|
|
|
GLuint posAttrib, texAttrib;
|
|
|
|
|
|
|
|
LoaderDFF dffLoader;
|
|
|
|
TextureLoader textureLoader;
|
|
|
|
LoaderIPL iplLoader;
|
|
|
|
|
|
|
|
std::map<std::string, std::unique_ptr<Model>> models;
|
|
|
|
Model *selectedModel;
|
2013-07-01 05:54:03 +02:00
|
|
|
glm::vec3 selectedModelCenter;
|
2013-07-01 05:40:11 +02:00
|
|
|
|
2013-07-01 02:22:29 +02:00
|
|
|
GLuint compileShader(GLenum type, const char *source)
|
|
|
|
{
|
|
|
|
GLuint shader = glCreateShader(type);
|
|
|
|
glShaderSource(shader, 1, &source, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
GLint status;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
|
|
|
if (status != GL_TRUE) {
|
|
|
|
GLint len;
|
|
|
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
|
|
|
|
GLchar *buffer = new GLchar[len];
|
|
|
|
glGetShaderInfoLog(shader, len, NULL, buffer);
|
|
|
|
|
|
|
|
std::cerr << "ERROR compiling shader: " << buffer << std::endl;
|
|
|
|
delete[] buffer;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
void init(std::string gtapath)
|
2013-07-01 02:22:29 +02:00
|
|
|
{
|
|
|
|
glClearColor(0.2, 0.2, 0.2, 1.0);
|
2013-07-01 05:40:11 +02:00
|
|
|
glEnable(GL_DEPTH_TEST);
|
2013-07-01 02:22:29 +02:00
|
|
|
|
|
|
|
GLuint vertexShader = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
|
|
|
|
GLuint fragmentShader = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
|
|
|
|
GLuint shaderProgram = glCreateProgram();
|
|
|
|
glAttachShader(shaderProgram, vertexShader);
|
|
|
|
glAttachShader(shaderProgram, fragmentShader);
|
|
|
|
glLinkProgram(shaderProgram);
|
|
|
|
glUseProgram(shaderProgram);
|
|
|
|
|
|
|
|
posAttrib = glGetAttribLocation(shaderProgram, "position");
|
2013-07-01 05:40:11 +02:00
|
|
|
texAttrib = glGetAttribLocation(shaderProgram, "texCoords");
|
2013-07-01 02:22:29 +02:00
|
|
|
|
|
|
|
uniModel = glGetUniformLocation(shaderProgram, "model");
|
|
|
|
uniView = glGetUniformLocation(shaderProgram, "view");
|
|
|
|
uniProj = glGetUniformLocation(shaderProgram, "proj");
|
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
glm::mat4 proj = glm::perspective(80.f, (float) WIDTH/HEIGHT, 0.1f, 5000.f);
|
|
|
|
glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
|
|
|
|
|
2013-07-01 02:22:29 +02:00
|
|
|
LoaderIMG imgLoader;
|
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
if (imgLoader.load(gtapath +"/models/gta3")) {
|
|
|
|
for (int i = 0; i < imgLoader.getAssetCount(); i++) {
|
|
|
|
auto &asset = imgLoader.getAssetInfoByIndex(i);
|
2013-07-01 02:22:29 +02:00
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
std::string filename = asset.name;
|
2013-07-01 21:02:01 +02:00
|
|
|
|
|
|
|
if(asset.size == 0)
|
|
|
|
{
|
|
|
|
std::cerr << "Asset: " << filename << " has no size" << std::endl;
|
|
|
|
continue;
|
|
|
|
}
|
2013-07-01 02:22:29 +02:00
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
auto filetype = filename.substr(filename.size() - 3);
|
|
|
|
std::transform(filetype.begin(), filetype.end(), filetype.begin(), ::tolower);
|
|
|
|
if (filetype == "dff") {
|
|
|
|
std::string modelname = filename.substr(0, filename.size() - 4);
|
|
|
|
char *file = imgLoader.loadToMemory(filename);
|
2013-07-01 21:02:01 +02:00
|
|
|
if(file)
|
|
|
|
{
|
|
|
|
models[modelname] = std::move(dffLoader.loadFromMemory(file));
|
|
|
|
delete[] file;
|
|
|
|
}
|
2013-07-01 05:40:11 +02:00
|
|
|
} else if (filetype == "txd") {
|
|
|
|
char *file = imgLoader.loadToMemory(filename);
|
2013-07-01 21:02:01 +02:00
|
|
|
if(file)
|
|
|
|
{
|
|
|
|
textureLoader.loadFromMemory(file);
|
|
|
|
delete[] file;
|
|
|
|
}
|
2013-07-01 02:22:29 +02:00
|
|
|
}
|
|
|
|
}
|
2013-07-01 05:40:11 +02:00
|
|
|
}
|
|
|
|
|
2013-07-01 05:54:03 +02:00
|
|
|
if (iplLoader.load(gtapath +"/data/maps/industSW.ipl")) {
|
2013-07-01 05:40:11 +02:00
|
|
|
printf("IPL Loaded, size: %d\n", iplLoader.m_instances.size());
|
2013-07-01 05:54:03 +02:00
|
|
|
|
|
|
|
// Get the center of the model by averaging all the vertices! Hax!
|
|
|
|
for (int i = 0; i < iplLoader.m_instances.size(); i++) {
|
|
|
|
selectedModelCenter += glm::vec3{
|
|
|
|
iplLoader.m_instances[i].posX,
|
|
|
|
iplLoader.m_instances[i].posY,
|
|
|
|
iplLoader.m_instances[i].posZ
|
|
|
|
};
|
|
|
|
}
|
|
|
|
selectedModelCenter /= iplLoader.m_instances.size();
|
2013-07-01 02:22:29 +02:00
|
|
|
} else {
|
|
|
|
printf("IPL failed to load.\n");
|
2013-07-01 05:40:11 +02:00
|
|
|
exit(1);
|
2013-07-01 02:22:29 +02:00
|
|
|
}
|
2013-07-01 05:40:11 +02:00
|
|
|
|
2013-07-01 21:02:01 +02:00
|
|
|
//textureLoader.loadFromFile("MISC.TXD");
|
2013-07-01 05:40:11 +02:00
|
|
|
|
|
|
|
selectedModel = models["Jetty"].get();
|
2013-07-01 02:22:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void update()
|
|
|
|
{
|
|
|
|
static int i = 0;
|
2013-07-01 05:54:03 +02:00
|
|
|
constexpr float rotspeed = 80;
|
2013-07-01 02:22:29 +02:00
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
glm::mat4 view;
|
|
|
|
glm::vec3 pos{
|
2013-07-01 05:54:03 +02:00
|
|
|
-selectedModelCenter.x + cos(i / rotspeed) * 400,
|
|
|
|
-selectedModelCenter.y + sin(i / rotspeed) * 400,
|
|
|
|
-selectedModelCenter.z - 200,
|
2013-07-01 05:40:11 +02:00
|
|
|
};
|
|
|
|
view = glm::rotate(view, -50.f, glm::vec3(1, 0, 0));
|
2013-07-01 05:54:03 +02:00
|
|
|
view = glm::rotate(view, (i/(6.28f*rotspeed))*360 - 90, glm::vec3(0, 0, -1));
|
2013-07-01 05:40:11 +02:00
|
|
|
view = glm::translate(view, pos);
|
|
|
|
glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
|
2013-07-01 02:22:29 +02:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void render()
|
|
|
|
{
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
for (size_t i = 0; i < iplLoader.m_instances.size(); ++i) {
|
|
|
|
auto &obj = iplLoader.m_instances[i];
|
|
|
|
std::string modelname = obj.model;
|
|
|
|
if (modelname.substr(0, 3) == "LOD")
|
|
|
|
continue;
|
|
|
|
auto &model = models[modelname];
|
|
|
|
// std::cout << "Rendering " << modelname << std::endl;
|
|
|
|
|
|
|
|
for (size_t g = 0; g < model->geometries.size(); g++) {
|
|
|
|
if (model->geometries[g].textures.size() > 0) {
|
|
|
|
// std::cout << model->geometries[g].textures.size() << std::endl;
|
|
|
|
// std::cout << "Looking for " << model->geometries[g].textures[0].name << std::endl;
|
|
|
|
textureLoader.bindTexture(model->geometries[g].textures[0].name);
|
|
|
|
}
|
2013-07-01 02:22:29 +02:00
|
|
|
|
2013-07-01 05:40:11 +02:00
|
|
|
glm::mat4 matrixModel;
|
2013-07-01 21:02:01 +02:00
|
|
|
glm::quat rot{obj.rotW, obj.rotX, obj.rotY, obj.rotZ};
|
|
|
|
matrixModel = glm::translate(matrixModel, glm::vec3(obj.posX, obj.posY, obj.posZ)); //glm::rotate(matrixModel, rot);//glm::angle(rot), glm::axis(rot));
|
2013-07-01 05:40:11 +02:00
|
|
|
matrixModel = glm::scale(matrixModel, glm::vec3(obj.scaleX, obj.scaleY, obj.scaleZ));
|
2013-07-01 21:02:01 +02:00
|
|
|
matrixModel = matrixModel * glm::mat4_cast(rot);
|
2013-07-01 05:40:11 +02:00
|
|
|
glUniformMatrix4fv(uniModel, 1, GL_FALSE, glm::value_ptr(matrixModel));
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, model->geometries[g].VBO);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model->geometries[g].EBO);
|
|
|
|
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
|
|
|
|
glEnableVertexAttribArray(posAttrib);
|
|
|
|
glEnableVertexAttribArray(texAttrib);
|
|
|
|
|
|
|
|
glDrawElements(GL_TRIANGLES, model->geometries[g].triangles.size() * 3, GL_UNSIGNED_SHORT, NULL);
|
|
|
|
}
|
|
|
|
}
|
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-01 02:22:29 +02:00
|
|
|
glewExperimental = GL_TRUE;
|
|
|
|
glewInit();
|
|
|
|
|
|
|
|
window.create(sf::VideoMode(WIDTH, HEIGHT), "GTA3 Viewer", sf::Style::Close);
|
|
|
|
window.setVerticalSyncEnabled(true);
|
|
|
|
|
|
|
|
init(argv[1]);
|
|
|
|
|
|
|
|
while (window.isOpen()) {
|
|
|
|
sf::Event event;
|
|
|
|
while (window.pollEvent(event)) {
|
|
|
|
handleEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
update();
|
|
|
|
|
|
|
|
render();
|
|
|
|
window.display();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|