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

Commit 570d7c4

Browse files
Object Picking (#82)
* Add picking shader program for object selection * Integrate picking shader into render pipeline manager * Add picking draw logic to entities, groups and scene * Implement mouse picking and framebuffer setup in SceneWindow * Add drawRaw method to Model for picking rendering * Display selected entity ID in UI * Expose window handle and add support for ImGui integration * (maybe) fix lint warning * (maybe p.2) fix lint warning * (maybe p.3) fix lint error * (maybe p.4) fix lint error * format fix * Simplify object picking * Add object picking to third person camera * Fix override error lint * Fix useStlAlgorithm error lint * Fix bug when camera is not third person * Add entity name support to picking * Add entity name to menu * Cleanup object picking * Add name generation to Solar System * Regenerate Solar System Sphere was updated as well --------- Co-authored-by: voidbert <humbertogilgomes@protonmail.com>
1 parent 410e7d0 commit 570d7c4

22 files changed

Lines changed: 7764 additions & 7441 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
#include <array>
16+
#include <cstdint>
17+
#include <glad/glad.h>
18+
19+
namespace engine::render {
20+
21+
class Framebuffer {
22+
private:
23+
GLuint fbo, colorTexture, depthRenderBuffer;
24+
int width, height;
25+
26+
public:
27+
Framebuffer(int _width, int _height);
28+
Framebuffer(const Framebuffer &framebuffer) = delete;
29+
Framebuffer(Framebuffer &&framebuffer) = delete;
30+
~Framebuffer();
31+
32+
void use();
33+
std::array<uint8_t, 3> sample(int x, int y);
34+
};
35+
36+
}

include/engine/scene/Entity.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Entity {
3636
render::BoundingSphere boundingSphere;
3737
std::shared_ptr<render::Texture> texture;
3838
Material material;
39+
std::string name;
3940

4041
public:
4142
Entity(const tinyxml2::XMLElement *modelElement,
@@ -48,6 +49,12 @@ class Entity {
4849
void updateBoundingSphere(const glm::mat4 &worldTransform);
4950
const render::BoundingSphere &getBoundingSphere() const;
5051
const render::NormalsPreview &getNormalsPreview() const;
52+
const std::string &getName() const;
53+
54+
void drawSolidColor(render::RenderPipelineManager &pipelineManager,
55+
const glm::mat4 &fullMatrix,
56+
const glm::vec4 &color,
57+
bool fillPolygons) const;
5158

5259
void draw(render::RenderPipelineManager &pipelineManager,
5360
const glm::mat4 &fullMatrix,

include/engine/scene/Group.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <filesystem>
1818
#include <glm/mat4x4.hpp>
19+
#include <glm/vec4.hpp>
1920
#include <memory>
2021
#include <string>
2122
#include <tinyxml2.h>
@@ -62,6 +63,12 @@ class Group {
6263
const glm::mat4 &worldtransform,
6364
bool fillPolygons) const;
6465

66+
int drawForPicking(render::RenderPipelineManager &pipelineManager,
67+
const camera::Camera &camera,
68+
const glm::mat4 &worldTransform,
69+
std::unordered_map<int, std::string> &idToName,
70+
int currentId) const;
71+
6572
private:
6673
const render::BoundingSphere &getBoundingSphere() const;
6774
void updateBoundingSphere(const glm::mat4 &worldTransform);

include/engine/scene/Scene.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <memory>
1818
#include <string>
19+
#include <unordered_map>
1920
#include <vector>
2021

2122
#include "engine/render/Axis.hpp"
@@ -58,6 +59,9 @@ class Scene {
5859
bool showBoundingSpheres,
5960
bool showAnimationLines,
6061
bool showNormals) const;
62+
63+
void drawForPicking(render::RenderPipelineManager &pipelineManager,
64+
std::unordered_map<int, std::string> &idToName) const;
6165
};
6266

6367
}

include/engine/scene/camera/Camera.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <glm/mat4x4.hpp>
1919
#include <glm/vec2.hpp>
2020
#include <glm/vec3.hpp>
21+
#include <unordered_map>
2122

2223
#include "engine/render/BoundingSphere.hpp"
2324
#include "engine/render/RenderPipelineManager.hpp"
@@ -60,6 +61,9 @@ class Camera {
6061
bool showNormals) const;
6162
virtual int drawShadedParts(render::RenderPipelineManager &pipelineManager,
6263
bool fillPolygons) const;
64+
virtual int drawForPicking(render::RenderPipelineManager &pipelineManager,
65+
std::unordered_map<int, std::string> &idToName,
66+
int currentId) const;
6367

6468
bool isInFrustum(const render::BoundingSphere &sphere) const;
6569

include/engine/scene/camera/ThirdPersonCamera.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <glm/vec2.hpp>
1818
#include <glm/vec3.hpp>
1919
#include <memory>
20+
#include <unordered_map>
2021

2122
#include "engine/scene/camera/OrbitalCamera.hpp"
2223
#include "engine/scene/Group.hpp"
@@ -49,6 +50,9 @@ class ThirdPersonCamera : public OrbitalCamera {
4950
bool showNormals) const override;
5051
virtual int drawShadedParts(render::RenderPipelineManager &pipelineManager,
5152
bool fillPolygons) const override;
53+
virtual int drawForPicking(render::RenderPipelineManager &pipelineManager,
54+
std::unordered_map<int, std::string> &idToName,
55+
int currentId) const override;
5256

5357
protected:
5458
virtual void updateWithMotion() override;

include/engine/window/SceneWindow.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class SceneWindow : public Window {
2929
scene::camera::CameraController cameraController;
3030

3131
UI ui;
32+
std::string selectedEntity;
3233
bool showUI;
3334

3435
public:
@@ -41,6 +42,7 @@ class SceneWindow : public Window {
4142
void onRender() override;
4243
void onResize(int _width, int _height) override;
4344
void onKeyEvent(int key, int action) override;
45+
void onMouseButtonEvent(int button, int action) override;
4446
};
4547

4648
}

include/engine/window/UI.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class UI {
2929
showNormals;
3030

3131
public:
32-
UI(Window &window, scene::camera::Camera &_camera, int _entityCount);
32+
UI(const Window &window, scene::camera::Camera &_camera, int _entityCount);
3333
~UI();
3434

3535
bool isCapturingKeyboard() const;
36-
void draw(int renderedEntities);
36+
void draw(int renderedEntities, const std::string &selectedEntity);
3737

3838
bool shouldFillPolygons() const;
3939
bool shouldCullBackFaces() const;

include/engine/window/Window.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ class Window {
3535

3636
int getWidth() const;
3737
int getHeight() const;
38-
GLFWwindow *getHandle();
38+
GLFWwindow *getHandle() const;
3939

4040
protected:
4141
virtual void onUpdate(float time, float timeElapsed) = 0;
4242
virtual void onRender() = 0;
4343
virtual void onResize(int _width, int _height) = 0;
4444
virtual void onKeyEvent(int key, int action) = 0;
45+
virtual void onMouseButtonEvent(int button, int action) = 0;
4546
};
4647

4748
}

0 commit comments

Comments
 (0)