Skip to content

Commit ca800d7

Browse files
committed
Draw 3D translation coord axes on selected objects.
1 parent a31527a commit ca800d7

14 files changed

+407
-253
lines changed

NeonEngine/NeonEngine.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
</Link>
132132
</ItemDefinitionGroup>
133133
<ItemGroup>
134+
<ClCompile Include="src\game_object.cpp" />
134135
<ClCompile Include="src\cylinder.cpp" />
135136
<ClCompile Include="src\geometry.cpp" />
136137
<ClCompile Include="src\rendering.cpp" />
@@ -155,6 +156,7 @@
155156
<ClInclude Include="src\input.h" />
156157
<ClInclude Include="src\neon_engine.h" />
157158
<ClInclude Include="src\user_interface.h" />
159+
<ClInclude Include="src\transform3d.h" />
158160
</ItemGroup>
159161
<ItemGroup>
160162
<None Include="shaders\render_model.frag" />

NeonEngine/NeonEngine.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
<ClCompile Include="src\cylinder.cpp">
4343
<Filter>Source Files</Filter>
4444
</ClCompile>
45+
<ClCompile Include="src\game_object.cpp">
46+
<Filter>Source Files</Filter>
47+
</ClCompile>
4548
</ItemGroup>
4649
<ItemGroup>
4750
<ClInclude Include="src\neon_engine.h">
@@ -86,6 +89,9 @@
8689
<ClInclude Include="src\opengl_utils.h">
8790
<Filter>Header Files</Filter>
8891
</ClInclude>
92+
<ClInclude Include="src\transform3d.h">
93+
<Filter>Header Files</Filter>
94+
</ClInclude>
8995
</ItemGroup>
9096
<ItemGroup>
9197
<None Include="shaders\render_model.frag">

NeonEngine/imgui.ini

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,25 @@ Collapsed=0
2727

2828
[Window][Viewport]
2929
Pos=0,26
30-
Size=1426,702
30+
Size=1374,731
3131
Collapsed=0
3232
DockId=0x00000005,0
3333

3434
[Window][Details]
35-
Pos=1428,558
36-
Size=492,522
35+
Pos=1376,543
36+
Size=544,537
3737
Collapsed=0
3838
DockId=0x00000002,0
3939

4040
[Window][Outlier]
41-
Pos=1428,26
42-
Size=492,530
41+
Pos=1376,26
42+
Size=544,515
4343
Collapsed=0
4444
DockId=0x00000001,0
4545

4646
[Window][Content Browser]
47-
Pos=0,730
48-
Size=1426,350
47+
Pos=0,759
48+
Size=1374,321
4949
Collapsed=0
5050
DockId=0x00000006,0
5151

@@ -56,11 +56,11 @@ Size=1441,1038
5656
Collapsed=0
5757

5858
[Docking][Data]
59-
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=505,565 Size=1920,1054 Split=X Selected=0x13926F0B
60-
DockNode ID=0x00000003 Parent=0x3BC79352 SizeRef=1426,527 Split=Y Selected=0x13926F0B
61-
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=1387,702 CentralNode=1 Selected=0x13926F0B
62-
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=1387,350 Selected=0xBF096F38
63-
DockNode ID=0x00000004 Parent=0x3BC79352 SizeRef=492,527 Split=Y Selected=0x79DBC900
64-
DockNode ID=0x00000001 Parent=0x00000004 SizeRef=531,530 Selected=0x79DBC900
65-
DockNode ID=0x00000002 Parent=0x00000004 SizeRef=531,522 Selected=0xFC3EA205
59+
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=127,123 Size=1920,1054 Split=X Selected=0x13926F0B
60+
DockNode ID=0x00000003 Parent=0x3BC79352 SizeRef=1374,527 Split=Y Selected=0x13926F0B
61+
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=1387,731 CentralNode=1 Selected=0x13926F0B
62+
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=1387,321 Selected=0xBF096F38
63+
DockNode ID=0x00000004 Parent=0x3BC79352 SizeRef=544,527 Split=Y Selected=0x79DBC900
64+
DockNode ID=0x00000001 Parent=0x00000004 SizeRef=531,515 Selected=0x79DBC900
65+
DockNode ID=0x00000002 Parent=0x00000004 SizeRef=531,537 Selected=0xFC3EA205
6666

NeonEngine/src/complex_model.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ class ComplexModel : public Model
3434
bool gammaCorrection;
3535

3636
// constructor, expects a filepath to a 3D model.
37-
ComplexModel(string const& path, bool gamma = false) : gammaCorrection(gamma)
37+
ComplexModel(const string& name, string const& path, bool gamma = false) : gammaCorrection(gamma)
3838
{
39+
this->name = name;
3940
loadModel(path);
4041
}
4142

4243
// draws the model, and thus all its meshes
43-
void draw(Shader& shader, bool is_selected, Rendering* rendering)
44+
void draw(Shader& shader, Rendering* rendering, bool is_selected, bool disable_depth_test)
4445
{
4546
for (unsigned int i = 0; i < meshes.size(); i++) {
46-
meshes[i].draw(shader, is_selected, rendering);
47+
meshes[i].draw(shader, rendering, is_selected, disable_depth_test);
4748
}
4849
}
4950

NeonEngine/src/cylinder.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ const int MIN_STACK_COUNT = 1;
4040
///////////////////////////////////////////////////////////////////////////////
4141
// ctor
4242
///////////////////////////////////////////////////////////////////////////////
43-
Cylinder::Cylinder(float baseRadius, float topRadius, float height, int sectors,
43+
Cylinder::Cylinder(const std::string& name, float baseRadius, float topRadius, float height, int sectors,
4444
int stacks, bool smooth, int up) : interleavedStride(32)
4545
{
46+
this->name = name;
4647
set(baseRadius, topRadius, height, sectors, stacks, smooth, up);
4748
}
4849

@@ -209,10 +210,14 @@ bool Cylinder::intersected_ray(const glm::vec3& orig, const glm::vec3& dir, floa
209210
// draw a cylinder in VertexArray mode
210211
// OpenGL RC must be set before calling it
211212
///////////////////////////////////////////////////////////////////////////////
212-
void Cylinder::draw(Shader& shader, bool is_selected, Rendering* rendering)
213+
void Cylinder::draw(Shader& shader, Rendering* rendering, bool is_selected, bool disable_depth_test)
213214
{
214215
shader.use();
215216

217+
if (disable_depth_test) {
218+
glDisable(GL_DEPTH_TEST);
219+
}
220+
216221
// if mesh is selected then fill stencil buffer values with ones
217222
if (is_selected) {
218223
glStencilMask(0xFF);
@@ -255,6 +260,10 @@ void Cylinder::draw(Shader& shader, bool is_selected, Rendering* rendering)
255260

256261
// always good practice to set everything back to defaults once configured.
257262
glActiveTexture(GL_TEXTURE0);
263+
264+
if (disable_depth_test) {
265+
glEnable(GL_DEPTH_TEST);
266+
}
258267
}
259268

260269

NeonEngine/src/cylinder.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "model.h"
2424

2525
#include <vector>
26+
#include <string>
2627
#include <glad/glad.h>
2728

2829
class Shader;
@@ -32,8 +33,8 @@ class Cylinder : public Model
3233
{
3334
public:
3435
// ctor/dtor
35-
Cylinder(float baseRadius=1.0f, float topRadius=1.0f, float height=1.0f,
36-
int sectorCount=36, int stackCount=1, bool smooth=true, int up=3);
36+
Cylinder(const std::string& name = "", float baseRadius = 1.0f, float topRadius = 1.0f, float height = 1.0f,
37+
int sectorCount = 36, int stackCount = 1, bool smooth = true, int up = 3);
3738
~Cylinder() {}
3839

3940
// getters/setters
@@ -85,7 +86,7 @@ class Cylinder : public Model
8586
unsigned int getSideStartIndex() const { return 0; } // side starts from the begining
8687

8788
// draw in VertexArray mode
88-
void draw(Shader& shader, bool is_selected, Rendering* rendering); // draw all
89+
void draw(Shader& shader, Rendering* rendering, bool is_selected, bool disable_depth_test); // draw all
8990
void drawBase() const; // draw base cap only
9091
void drawTop() const; // draw top cap only
9192
void drawSide() const; // draw side only

NeonEngine/src/game_object.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "game_object.h"
2+
3+
#include "shader.h"
4+
#include "rendering.h"
5+
#include "model.h"
6+
#include "transform3d.h"
7+
8+
#include <glm/glm.hpp>
9+
#include <glm/gtc/matrix_transform.hpp>
10+
#include <glm/gtc/type_ptr.hpp>
11+
12+
//////////////////////////////// GAME_OBJECT //////////////////////////////////////
13+
14+
GameObject::GameObject(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::vec3& scale,
15+
const glm::vec3& rotation, const glm::vec3& color, bool is_selected) {
16+
this->name = name;
17+
this->model_name = model_name;
18+
this->position = position;
19+
this->scale = scale;
20+
this->rotation = rotation;
21+
this->color = color;
22+
this->is_selected = is_selected;
23+
24+
set_model_matrices_standard();
25+
}
26+
27+
GameObject::~GameObject() {
28+
29+
}
30+
31+
void GameObject::set_model_matrices_standard() {
32+
model = glm::mat4(1.0f);
33+
model = glm::translate(model, position);
34+
model = glm::rotate(model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
35+
model = glm::rotate(model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
36+
model = glm::rotate(model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
37+
model = glm::scale(model, scale);
38+
39+
model_inv = glm::inverse(model);
40+
41+
model_normals = glm::mat3(glm::transpose(model_inv));
42+
}
43+
44+
void GameObject::draw(Shader* shader, Rendering* rendering, bool disable_depth_test) {
45+
shader->setVec3("model_color", color);
46+
shader->setMat4("model", model);
47+
shader->setMat3("model_normals", model_normals);
48+
shader->setMat4("model_view_projection", rendering->projection * rendering->view * model);
49+
if (model_name != "") {
50+
rendering->loaded_models[model_name]->draw(*shader, rendering, is_selected, disable_depth_test);
51+
}
52+
}
53+
54+
bool GameObject::intersected_ray(Rendering* rendering, const glm::vec3& ray_dir, const glm::vec3& camera_position, float& t) {
55+
glm::vec3 ray_dir_model = model_inv * glm::vec4(ray_dir, 0.0f);
56+
glm::vec3 ray_origin_model = model_inv * glm::vec4(camera_position, 1.0f);
57+
if (model_name != "" && rendering->loaded_models[model_name]->intersected_ray(ray_origin_model, ray_dir_model, t)) {
58+
return true;
59+
}
60+
return false;
61+
}
62+
63+
void GameObject::set_select_state(bool is_game_obj_selected) {
64+
is_selected = is_game_obj_selected;
65+
}
66+
67+
68+
//////////////////////////////// LIGHTS //////////////////////////////////////
69+
70+
Light::Light(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::vec3& scale, const glm::vec3& rotation, const glm::vec3& color, bool is_selected,
71+
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular)
72+
: GameObject(name, model_name, position, scale, rotation, color, is_selected) {
73+
this->ambient = ambient;
74+
this->diffuse = diffuse;
75+
this->specular = specular;
76+
}
77+
78+
PointLight::PointLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::vec3& scale, const glm::vec3& rotation, const glm::vec3& color, bool is_selected,
79+
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, float constant, float linear, float quadratic)
80+
: Light(name, model_name, position, scale, rotation, color, is_selected, ambient, diffuse, specular) {
81+
this->constant = constant;
82+
this->linear = linear;
83+
this->quadratic = quadratic;
84+
}
85+
86+
DirectionalLight::DirectionalLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::vec3& scale, const glm::vec3& rotation, const glm::vec3& color, bool is_selected,
87+
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, const glm::vec3& direction)
88+
: Light(name, model_name, position, scale, rotation, color, is_selected, ambient, diffuse, specular) {
89+
this->direction = direction;
90+
}
91+
92+
SpotLight::SpotLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::vec3& scale, const glm::vec3& rotation, const glm::vec3& color, bool is_selected,
93+
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, const glm::vec3& direction, float inner_cut_off, float outer_cut_off,
94+
float constant, float linear, float quadratic)
95+
: Light(name, model_name, position, scale, rotation, color, is_selected, ambient, diffuse, specular) {
96+
this->direction = direction;
97+
98+
this->inner_cut_off = inner_cut_off;
99+
this->outer_cut_off = inner_cut_off;
100+
101+
this->constant = constant;
102+
this->linear = linear;
103+
this->quadratic = quadratic;
104+
}

0 commit comments

Comments
 (0)