Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 64e3adc

Browse files
Add Entity, Scene, Parse and some models and scenes (#12)
* Add Entity, Scene, Parse an some models and scenes * Add tinyxml to action * little fix on Makefile * Correct sphere (code on another branch) * Use pkg-config in Makefile * Prepare Window for scene loading in runtime * Small code style and formatting fixes * Allow for model sharing among entities * Removed old XML parser * Cleanup scene and move parser there * Adapt window and entry point to new scene * Add model reuse and fix empty nested group bug * Add support for older compilers * Changes res * Fix formatting --------- Co-authored-by: Voidbert <humbertogilgomes@protonmail.com>
1 parent a0747d2 commit 64e3adc

17 files changed

Lines changed: 413 additions & 40 deletions

File tree

.github/workflows/checks.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515

1616
# Don't fail on empty include (not tracked by git)
1717
- run: if ! [ -d include ]; then mkdir include; fi
18-
- run: sudo apt update && sudo apt install -y libglfw3-dev libglm-dev libgl-dev
18+
- run: sudo apt update && sudo apt install -y libglfw3-dev libglm-dev libgl-dev libtinyxml2-dev
1919
- run: make
2020
format:
2121
runs-on: ubuntu-latest
@@ -45,4 +45,4 @@ jobs:
4545

4646
# Don't fail on empty include (not tracked by git)
4747
- run: if ! [ -d include ]; then mkdir include; fi
48-
- run: cppcheck src -I include
48+
- run: cppcheck src -I include -I /usr/include/tinyxml2

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ CPPFLAGS := -Iinclude -std=c++20 -Wall -Wextra -pedantic -Wshadow \
2121
$(shell pkg-config --cflags glfw3) -DGLFW_INCLUDE_NONE \
2222
$(shell pkg-config --cflags glm) \
2323
$(shell pkg-config --cflags gl) \
24+
$(shell pkg-config --cflags tinyxml2) \
2425
-Ilib/include
2526
LIBS := -lm \
26-
$(shell pkg-config --libs glfw3) \
27-
$(shell pkg-config --libs glm) \
28-
$(shell pkg-config --libs gl)
27+
$(shell pkg-config --libs glfw3) \
28+
$(shell pkg-config --libs glm) \
29+
$(shell pkg-config --libs gl) \
30+
$(shell pkg-config --libs tinyxml2)
2931

3032
DEBUG_CPPFLAGS := -O0 -ggdb3
3133
RELEASE_CPPFLAGS := -O2
@@ -50,7 +52,7 @@ LIB_SOURCES = $(shell find "lib" -name '*.c' -type f)
5052
LIB_OBJECTS = $(patsubst lib/%.c, $(OBJDIR)/%.o, $(LIB_SOURCES))
5153
HEADERS = $(shell find "include" -name '*.hpp' -type f)
5254
DEPENDS = $(patsubst src/%.cpp, $(DEPDIR)/%.d, $(SOURCES))
53-
REPORTS = $(patsubst reports/%.tex, %.pdf, $(shell find reports -name '*.tex' -type f))
55+
REPORTS = $(patsubst reports/%.tex, $(BUILDDIR)/%.pdf, $(shell find reports -name '*.tex' -type f))
5456

5557
ifeq ($(DEBUG), 1)
5658
CPPFLAGS += $(DEBUG_CPPFLAGS)

include/engine/Entity.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/// Copyright 2025 Ana Oliveira, Humberto Gomes, Mariana Rocha, Sara Lopes
2+
///
3+
/// Licensed under the Apache License, Version 2.0 (the "License");
4+
/// you may not use this file except in compliance with the License.
5+
/// You may obtain a copy of the License at
6+
///
7+
/// http://www.apache.org/licenses/LICENSE-2.0
8+
///
9+
/// Unless required by applicable law or agreed to in writing, software
10+
/// distributed under the License is distributed on an "AS IS" BASIS,
11+
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
/// See the License for the specific language governing permissions and
13+
/// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <glm/vec4.hpp>
18+
#include <memory>
19+
20+
#include "engine/Model.hpp"
21+
#include "engine/RenderPipeline.hpp"
22+
23+
namespace engine {
24+
class Entity {
25+
protected:
26+
std::shared_ptr<Model> model;
27+
glm::vec4 color;
28+
// TODO - Phase 2 – Add Geometric Transforms
29+
30+
public:
31+
Entity(std::shared_ptr<Model> _model, const glm::vec4 &color);
32+
void draw(const RenderPipeline &pipeline) const;
33+
};
34+
}

include/engine/Scene.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/// Copyright 2025 Ana Oliveira, Humberto Gomes, Mariana Rocha, Sara Lopes
2+
///
3+
/// Licensed under the Apache License, Version 2.0 (the "License");
4+
/// you may not use this file except in compliance with the License.
5+
/// You may obtain a copy of the License at
6+
///
7+
/// http://www.apache.org/licenses/LICENSE-2.0
8+
///
9+
/// Unless required by applicable law or agreed to in writing, software
10+
/// distributed under the License is distributed on an "AS IS" BASIS,
11+
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
/// See the License for the specific language governing permissions and
13+
/// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <filesystem>
18+
#include <memory>
19+
#include <string>
20+
#include <tinyxml2.h>
21+
#include <unordered_map>
22+
#include <vector>
23+
24+
#include "engine/Camera.hpp"
25+
#include "engine/Entity.hpp"
26+
#include "engine/RenderPipeline.hpp"
27+
28+
namespace engine {
29+
30+
class Scene {
31+
private:
32+
int windowWidth, windowHeight;
33+
std::string windowTitle;
34+
Camera camera;
35+
36+
// TODO - Phase 2 - add support for groups (linear scene is going to make it harder for phase 3)
37+
std::vector<std::unique_ptr<Entity>> entities;
38+
39+
public:
40+
Scene(const std::string &file);
41+
Scene(const Scene &scene) = delete;
42+
Scene(Scene &&scene) = delete;
43+
44+
int getWindowWidth() const;
45+
int getWindowHeight() const;
46+
Camera &getCamera();
47+
48+
void draw(const RenderPipeline &pipeline) const;
49+
void setWindowSize(int width, int height);
50+
51+
private:
52+
const tinyxml2::XMLElement *getOnlyOneNodeFromXML(const tinyxml2::XMLNode *parent,
53+
const std::string &name);
54+
glm::vec3 getVectorFromXML(const tinyxml2::XMLElement *element);
55+
56+
void getWindowFromXML(const tinyxml2::XMLElement *worldElement);
57+
void getCameraFromXML(const tinyxml2::XMLElement *worldElement);
58+
59+
void getEntitiesFromWorldXML(
60+
const std::filesystem::path &sceneDirectory,
61+
std::unordered_map<std::string, std::shared_ptr<Model>> &loadedModels,
62+
const tinyxml2::XMLElement *worldElement);
63+
void getEntitiesFromGroupXML(
64+
const std::filesystem::path &sceneDirectory,
65+
std::unordered_map<std::string, std::shared_ptr<Model>> &loadedModels,
66+
const tinyxml2::XMLElement *groupdElement);
67+
};
68+
69+
}

include/engine/SceneWindow.hpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,18 @@
1616

1717
#include <memory>
1818

19-
#include "engine/Camera.hpp"
20-
#include "engine/Model.hpp"
2119
#include "engine/RenderPipeline.hpp"
20+
#include "engine/Scene.hpp"
2221
#include "engine/Window.hpp"
2322

2423
namespace engine {
2524
class SceneWindow : public Window {
2625
private:
2726
RenderPipeline pipeline;
28-
Camera camera;
29-
30-
// TODO - remove, these are for testing purposes only
31-
std::unique_ptr<Model> model;
32-
glm::vec3 translate;
27+
Scene scene;
3328

3429
public:
35-
SceneWindow();
30+
SceneWindow(const std::string &sceneFile);
3631

3732
protected:
3833
void onUpdate(float time, float timeElapsed);

include/engine/Window.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class Window {
3030
~Window();
3131

3232
void runLoop();
33-
int getWidth();
34-
int getHeight();
33+
void resize(int _width, int _height);
34+
35+
int getWidth() const;
36+
int getHeight() const;
3537

3638
protected:
3739
GLFWwindow *getHandle();

res/models/taurus.3d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Só para a pasta não apagar.

res/scenes/scene_box.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<world>
2+
<window width="512" height="512" />
3+
<camera>
4+
<position x="3" y="2" z="1" />
5+
<lookAt x="0" y="0" z="0" />
6+
<up x="0" y="1" z="0" />
7+
<projection fov="60" near="1" far="1000" />
8+
</camera>
9+
<group>
10+
<models>
11+
<model file="../models/box.3d" />
12+
</models>
13+
</group>
14+
</world>

res/scenes/scene_plane.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<world>
2+
<window width="512" height="512" />
3+
<camera>
4+
<position x="3" y="2" z="1" />
5+
<lookAt x="0" y="0" z="0" />
6+
<up x="0" y="1" z="0" />
7+
<projection fov="60" near="1" far="1000" />
8+
</camera>
9+
<group>
10+
<models>
11+
<model file="../models/plane.3d" />
12+
</models>
13+
</group>
14+
</world>

res/scenes/scene_sphere.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<world>
2+
<window width="512" height="512" />
3+
<camera>
4+
<position x="3" y="2" z="1" />
5+
<lookAt x="0" y="0" z="0" />
6+
<up x="0" y="1" z="0" />
7+
<projection fov="60" near="1" far="1000" />
8+
</camera>
9+
<group>
10+
<models>
11+
<model file="../models/sphere.3d" />
12+
</models>
13+
</group>
14+
</world>

0 commit comments

Comments
 (0)