Skip to content

Commit

Permalink
chore: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
diogogmatos committed Mar 3, 2024
1 parent 6a13c76 commit c06244c
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 12 deletions.
4 changes: 4 additions & 0 deletions engine/include/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "Camera.hpp"
#include "utils.hpp"

#include <string>

class Camera {
public:
Point position;
Expand All @@ -16,6 +18,8 @@ class Camera {
Camera();
Camera(Point position, Point lookAt, Point up, int fov, float near,
float far);

std::string toString();
};

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

#include <vector>

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

#include <vector>
#include <string>

class Configuration {
public:
Window window;
Camera camera;
std::vector<char*> models;
std::vector<std::string> models;

Configuration(Window window, Camera camera, std::vector<std::string> models);

Configuration(Window window, Camera camera, std::vector<char*> models);
std::string toString();
};

#endif // CONFIGURATION_HPP
4 changes: 4 additions & 0 deletions engine/include/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

#include "Window.hpp"

#include <string>

class Window {
public:
float width;
float height;

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

std::string toString();
};

#endif // WINDOW_HPP
4 changes: 3 additions & 1 deletion engine/include/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "Configuration.hpp"
#include "parse.hpp"

Configuration parseConfig(char* filename);
#include <string>

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
14 changes: 14 additions & 0 deletions engine/src/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "Camera.hpp"

#include <string>
#include <sstream>

Camera::Camera() {
this->position = Point();
this->lookAt = Point();
Expand All @@ -18,3 +21,14 @@ Camera::Camera(Point position, Point lookAt, Point up, int fov, float near,
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();
}
21 changes: 20 additions & 1 deletion engine/src/Configuration.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
#include "Configuration.hpp"

#include <string>
#include <sstream>

Configuration::Configuration(Window window, Camera camera,
std::vector<char*> models) {
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();
}
10 changes: 10 additions & 0 deletions engine/src/Window.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "Window.hpp"

#include <string>
#include <sstream>

Window::Window() {
this->width = 0;
this->height = 0;
Expand All @@ -9,3 +12,10 @@ 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();
}
6 changes: 6 additions & 0 deletions engine/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <math.h>

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

float cameraAngle = 90.0f;
float cameraAngleY = 0.0f;
Expand Down Expand Up @@ -94,6 +95,11 @@ void processSpecialKeys(int key, int xx, int yy) {
}

int main(int argc, char** argv) {
std::string filename;
filename.assign("/home/diogo/LEI/3ano/2sem/CG/CG-Project/engine/test.xml");
Configuration c = parseConfig(filename);
printf(c.toString().c_str());

// put GLUT�s init here
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
Expand Down
12 changes: 6 additions & 6 deletions engine/src/parse.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include "parse.hpp"

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

Configuration parseConfig(char* filename) {
Configuration parseConfig(std::string filename) {
// open file in read mode
std::fstream file;
file.open(filename, std::ios::in);
std::ifstream file(filename);

// check if the file was opened successfully
if (!file.is_open()) {
Expand Down Expand Up @@ -58,12 +58,12 @@ Configuration parseConfig(char* filename) {
Camera camera_info = Camera(position, lookAt, up, fov, near, far);

// models
std::vector<char*> models_info;
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(model->first_attribute("file")->value());
models_info.push_back(std::string(model->first_attribute("file")->value()));
}

return Configuration(window_info, camera_info, models_info);
Expand Down
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" />
<camera>
<position x="3" y="2" z="1" />
<lookAt x="0" y="0" z="0" />
<up x="0" y="1" z="0" />
<projection fov="60" near="1" far="1000" />
</camera>
<group>
<models>
<model file="plane.3d" />
<model file="cone.3d" />
</models>
</group>
</world>

0 comments on commit c06244c

Please sign in to comment.