Skip to content

Commit

Permalink
feat: xml parsing (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
diogogmatos committed Mar 4, 2024
1 parent b32fe46 commit 86812ff
Show file tree
Hide file tree
Showing 20 changed files with 4,055 additions and 6,196 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ engine/CMakeFiles/*
.idea/*
.vscode/*
generator/generator
engine/engine
engine/engine
build
25 changes: 25 additions & 0 deletions engine/include/Camera.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef CAMERA_HPP
#define CAMERA_HPP

#include <string>

#include "Camera.hpp"
#include "utils.hpp"

class Camera {
public:
Point position;
Point lookAt;
Point up;
int fov;
float near;
float far;

Camera();
Camera(Point position, Point lookAt, Point up, int fov, float near,
float far);

std::string toString();
};

#endif // CAMERA_HPP
22 changes: 22 additions & 0 deletions engine/include/Configuration.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef CONFIGURATION_HPP
#define CONFIGURATION_HPP

#include <string>
#include <vector>

#include "Camera.hpp"
#include "Configuration.hpp"
#include "Window.hpp"

class Configuration {
public:
Window window;
Camera camera;
std::vector<std::string> models;
Configuration() = default;
Configuration(Window window, Camera camera, std::vector<std::string> models);

std::string toString();
};

#endif // CONFIGURATION_HPP
19 changes: 19 additions & 0 deletions engine/include/Window.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef WINDOW_HPP
#define WINDOW_HPP

#include <string>

#include "Window.hpp"

class Window {
public:
float width;
float height;

Window();
Window(float width, float height);

std::string toString();
};

#endif // WINDOW_HPP
12 changes: 12 additions & 0 deletions engine/include/parse.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PARSE_HPP
#define PARSE_HPP

#include <string>

#include "../../lib/rapidxml-1.13/rapidxml.hpp"
#include "Configuration.hpp"
#include "parse.hpp"

Configuration parseConfig(std::string filename);

#endif // PARSE_HPP
6 changes: 6 additions & 0 deletions engine/include/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ typedef struct Point {
// Constructor
Point(float x_val = 0.0f, float y_val = 0.0f, float z_val = 0.0f)
: x(x_val), y(y_val), z(z_val) {}

std::string toString() {
std::stringstream ss;
ss << "Point(" << x << ", " << y << ", " << z << ")";
return ss.str();
}
} Point;

// Function to convert Point to string
Expand Down
34 changes: 34 additions & 0 deletions engine/src/Camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "Camera.hpp"

#include <sstream>
#include <string>

Camera::Camera() {
this->position = Point();
this->lookAt = Point();
this->up = Point();
this->fov = 0;
this->near = 0;
this->far = 0;
}

Camera::Camera(Point position, Point lookAt, Point up, int fov, float near,
float far) {
this->position = position;
this->lookAt = lookAt;
this->up = up;
this->fov = fov;
this->near = near;
this->far = far;
}

std::string Camera::toString() {
std::stringstream ss;
ss << "\tPosition: " << position.toString() << std::endl
<< "\tLookAt: " << lookAt.toString() << std::endl
<< "\tUp: " << up.toString() << std::endl
<< "\tFOV: " << std::to_string(fov).c_str() << std::endl
<< "\tNear: " << std::to_string(near).c_str() << std::endl
<< "\tFar: " << std::to_string(far).c_str() << std::endl;
return ss.str();
}
29 changes: 29 additions & 0 deletions engine/src/Configuration.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Configuration.hpp"

#include <sstream>
#include <string>

Configuration::Configuration(Window window, Camera camera,
std::vector<std::string> models) {
this->window = window;
this->camera = camera;
this->models = models;
}

std::string vectorToString(std::vector<std::string> v) {
std::stringstream ss;
for (auto i : v) {
ss << i << " ";
}
return ss.str();
}

std::string Configuration::toString() {
std::stringstream ss;
ss << "Window:\n"
<< window.toString() << std::endl
<< "Camera:\n"
<< camera.toString() << std::endl
<< "Models: " << vectorToString(models) << std::endl;
return ss.str();
}
21 changes: 21 additions & 0 deletions engine/src/Window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "Window.hpp"

#include <sstream>
#include <string>

Window::Window() {
this->width = 0;
this->height = 0;
}

Window::Window(float width, float height) {
this->width = width;
this->height = height;
}

std::string Window::toString() {
std::stringstream ss;
ss << "\tWidth: " << width << std::endl
<< "\tHeight: " << height << std::endl;
return ss.str();
}
26 changes: 16 additions & 10 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,25 @@
#include <math.h>

#include "draw.hpp"
#include "parse.hpp"

float cameraAngle = 90.0f;
float cameraAngleY = 0.0f;
char* file = "";

Configuration c;

void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you can�t make a window with zero width).
if (h == 0) h = 1;

float ratio = w * 1.0f / h;
float ratio = c.window.width * 1.0f / c.window.height;
// Set the projection matrix as current
glMatrixMode(GL_PROJECTION);
// Load the identity matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
glViewport(0, 0, c.window.width, c.window.height);
// Set the perspective
gluPerspective(45.0f, ratio, 1.0f, 1000.0f);
// return to the model view matrix mode
Expand All @@ -34,12 +36,12 @@ void changeSize(int w, int h) {
void renderScene(void) {
// clear buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// set camera
glLoadIdentity();
// gluLookAt(0.0f, 0.0f, 5.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
gluLookAt(5.0 * sin(cameraAngle), 5.0 * cos(cameraAngleY),
5.0 * cos(cameraAngle), 0.0, 0.0, 0.0, 0.0f, 1.0f, 0.0f);
gluLookAt(c.camera.position.x, c.camera.position.y, c.camera.position.z,
c.camera.lookAt.x, c.camera.lookAt.y, c.camera.lookAt.z,
c.camera.up.x, c.camera.up.y, c.camera.up.z);

// put drawing instructions here
glBegin(GL_LINES);
Expand All @@ -58,7 +60,9 @@ void renderScene(void) {
glEnd();

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
drawFile(file);
for (std::string model : c.models) {
drawFile(model.data());
}

// End of frame
glutSwapBuffers();
Expand Down Expand Up @@ -94,16 +98,18 @@ void processSpecialKeys(int key, int xx, int yy) {
}

int main(int argc, char** argv) {
std::string filename;
filename.assign(argv[1]);
c = parseConfig(filename);

// put GLUT�s init here
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 800);
glutInitWindowSize(c.window.width, c.window.height);
glutCreateWindow("CG@DI");

// put callback registry here
file = argv[1];
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutDisplayFunc(renderScene);
glutReshapeFunc(reshape);
Expand Down
70 changes: 70 additions & 0 deletions engine/src/parse.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "parse.hpp"

#include <fstream>
#include <iostream>
#include <string>

Configuration parseConfig(std::string filename) {
// open file in read mode
std::ifstream file(filename);

// check if the file was opened successfully
if (!file.is_open()) {
std::cerr << "Error opening the file!" << std::endl;
exit(1);
}

// read the XML file content into a string
std::string xmlContent((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();

// parse the XML string using RapidXML
rapidxml::xml_document<> doc;
doc.parse<0>(&xmlContent[0]);

// access the root node
rapidxml::xml_node<>* root = doc.first_node("world");

// window information
char* width = root->first_node("window")->first_attribute("width")->value();
char* height = root->first_node("window")->first_attribute("height")->value();

Window window_info = Window(std::stof(width), std::stof(height));

// camera information
rapidxml::xml_node<>* camera = root->first_node("camera");

rapidxml::xml_node<>* position_n = camera->first_node("position");
Point position = Point(std::stof(position_n->first_attribute("x")->value()),
std::stof(position_n->first_attribute("y")->value()),
std::stof(position_n->first_attribute("z")->value()));

rapidxml::xml_node<>* lookAt_n = camera->first_node("lookAt");
Point lookAt = Point(std::stof(lookAt_n->first_attribute("x")->value()),
std::stof(lookAt_n->first_attribute("y")->value()),
std::stof(lookAt_n->first_attribute("z")->value()));

rapidxml::xml_node<>* up_n = camera->first_node("up");
Point up = Point(std::stof(up_n->first_attribute("x")->value()),
std::stof(up_n->first_attribute("y")->value()),
std::stof(up_n->first_attribute("z")->value()));

rapidxml::xml_node<>* projection = camera->first_node("projection");
int fov = std::stoi(projection->first_attribute("fov")->value());
float near = std::stof(projection->first_attribute("near")->value());
float far = std::stof(projection->first_attribute("far")->value());

Camera camera_info = Camera(position, lookAt, up, fov, near, far);

// models
std::vector<std::string> models_info;
rapidxml::xml_node<>* models =
root->first_node("group")->first_node("models");
for (rapidxml::xml_node<>* model = models->first_node("model"); model;
model = model->next_sibling("model")) {
models_info.push_back(std::string(model->first_attribute("file")->value()));
}

return Configuration(window_info, camera_info, models_info);
}
15 changes: 15 additions & 0 deletions engine/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<world>
<window width="512" height="512" /> <!-- gluPerspective Ratio -->
<camera>
<position x="5" y="5" z="5" /> <!-- gluLookAt eye -->
<lookAt x="0" y="0" z="0" /> <!-- gluLookAt center-->
<up x="0" y="1" z="0" /> <!-- gluLookAt up-->
<projection fov="60" near="1" far="1000" /> <!--gluPerspective-->
</camera>
<group>
<models>
<model file="cone.3d" />
<model file="sphere.3d" /> <!-- generator cone 1 2 4 3 cone.3d -->
</models>
</group>
</world>
Loading

0 comments on commit 86812ff

Please sign in to comment.