Skip to content

Commit 99255b8

Browse files
committed
Added rotation capability.
1 parent 4a232b8 commit 99255b8

File tree

9 files changed

+241
-56
lines changed

9 files changed

+241
-56
lines changed

NeonEngine/NeonEngine.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
<ClCompile Include="src\stb_image.cpp" />
144144
</ItemGroup>
145145
<ItemGroup>
146+
<ClInclude Include="src\disk_border.h" />
146147
<ClInclude Include="src\cube.h" />
147148
<ClInclude Include="src\quad.h" />
148149
<ClInclude Include="src\opengl_utils.h" />

NeonEngine/NeonEngine.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@
104104
<ClInclude Include="src\sphere.h">
105105
<Filter>Header Files</Filter>
106106
</ClInclude>
107+
<ClInclude Include="src\disk_border.h">
108+
<Filter>Header Files</Filter>
109+
</ClInclude>
107110
</ItemGroup>
108111
<ItemGroup>
109112
<None Include="shaders\phong_lighting.frag">

NeonEngine/imgui.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Size=1441,1038
5656
Collapsed=0
5757

5858
[Docking][Data]
59-
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=658,530 Size=1920,1054 Split=X Selected=0x13926F0B
59+
DockSpace ID=0x3BC79352 Window=0x4647B76E Pos=239,299 Size=1920,1054 Split=X Selected=0x13926F0B
6060
DockNode ID=0x00000003 Parent=0x3BC79352 SizeRef=1388,527 Split=Y Selected=0x13926F0B
6161
DockNode ID=0x00000005 Parent=0x00000003 SizeRef=1387,772 CentralNode=1 Selected=0x13926F0B
6262
DockNode ID=0x00000006 Parent=0x00000003 SizeRef=1387,280 Selected=0xBF096F38

NeonEngine/src/disk_border.h

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#pragma once
2+
3+
#include "model.h"
4+
#include "shader.h"
5+
6+
#include <vector>
7+
#include <glad/glad.h>
8+
#define _USE_MATH_DEFINES
9+
#include <math.h>
10+
11+
class DiskBorder : public Model {
12+
public:
13+
unsigned int VAO;
14+
std::vector<float> vertices;
15+
std::vector<unsigned int> indices;
16+
17+
DiskBorder(const std::string& name, float max_angle = M_PI / 2.0f, float inner_radius = 1.0f, float outer_radius = 2.0f, int sector_count = 100) {
18+
this->name = name;
19+
set(max_angle, sector_count, inner_radius, outer_radius);
20+
21+
GLuint vbo, ebo;
22+
23+
glGenVertexArrays(1, &VAO);
24+
glGenBuffers(1, &vbo);
25+
glGenBuffers(1, &ebo);
26+
27+
glBindVertexArray(VAO);
28+
29+
glBindBuffer(GL_ARRAY_BUFFER, vbo);
30+
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
31+
32+
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
33+
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), indices.data(), GL_STATIC_DRAW);
34+
35+
// vertex Positions
36+
glEnableVertexAttribArray(0);
37+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
38+
// vertex normals
39+
glEnableVertexAttribArray(1);
40+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(sizeof(float) * 3));
41+
42+
glBindBuffer(GL_ARRAY_BUFFER, 0);
43+
glBindVertexArray(0);
44+
}
45+
46+
void set(float max_angle, int sector_count, float inner_radius, float outer_radius) {
47+
float x, y, z = 0.0f, theta;
48+
float sector_step = max_angle / sector_count;
49+
float nx = 0.0f, ny = 0.0f, nz = 1.0f;
50+
for (int i = 0; i <= sector_count; i++) {
51+
theta = sector_step * i;
52+
x = inner_radius * cos(theta);
53+
y = inner_radius * sin(theta);
54+
55+
vertices.push_back(x);
56+
vertices.push_back(y);
57+
vertices.push_back(z);
58+
59+
vertices.push_back(nx);
60+
vertices.push_back(ny);
61+
vertices.push_back(nz);
62+
}
63+
for (int i = 0; i <= sector_count; i++) {
64+
theta = sector_step * i;
65+
x = outer_radius * cos(theta);
66+
y = outer_radius * sin(theta);
67+
68+
vertices.push_back(x);
69+
vertices.push_back(y);
70+
vertices.push_back(z);
71+
72+
vertices.push_back(nx);
73+
vertices.push_back(ny);
74+
vertices.push_back(nz);
75+
}
76+
77+
int k1 = 0;
78+
int k2 = sector_count + 1;
79+
for (int i = 0; i < sector_count; i++, k1++, k2++) {
80+
indices.push_back(k1);
81+
indices.push_back(k2);
82+
indices.push_back(k2 + 1);
83+
84+
indices.push_back(k1);
85+
indices.push_back(k2 + 1);
86+
indices.push_back(k1 + 1);
87+
}
88+
}
89+
90+
bool intersected_ray(const glm::vec3& orig, const glm::vec3& dir, float& t) {
91+
return false;
92+
}
93+
94+
void draw(Shader* shader, bool is_selected, bool disable_depth_test, bool render_only_ambient, bool render_one_color) {
95+
if (disable_depth_test) {
96+
glDisable(GL_DEPTH_TEST);
97+
}
98+
99+
shader->setInt("render_only_ambient", render_only_ambient);
100+
shader->setInt("render_one_color", render_one_color);
101+
shader->setInt("paint_selected_texture", is_selected);
102+
103+
// draw mesh
104+
glBindVertexArray(VAO);
105+
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
106+
glBindVertexArray(0);
107+
108+
// always good practice to set everything back to defaults once configured.
109+
glActiveTexture(GL_TEXTURE0);
110+
111+
if (disable_depth_test) {
112+
glEnable(GL_DEPTH_TEST);
113+
}
114+
}
115+
};

NeonEngine/src/game_object.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
ColorGenerator* GameObject::color_generator = new ColorGenerator(100000);
1515

16-
GameObject::GameObject(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::vec3& rotation,
16+
GameObject::GameObject(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation,
1717
const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color) {
1818
this->name = name;
1919
this->model_name = model_name;
@@ -37,9 +37,7 @@ GameObject::~GameObject() {
3737
void GameObject::set_model_matrices_standard() {
3838
model = glm::mat4(1.0f);
3939
model = glm::translate(model, position);
40-
model = glm::rotate(model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
41-
model = glm::rotate(model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
42-
model = glm::rotate(model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
40+
model *= glm::mat4_cast(rotation);
4341
model = glm::scale(model, scale);
4442

4543
model_inv = glm::inverse(model);
@@ -79,32 +77,32 @@ void GameObject::clean() {
7977

8078
//////////////////////////////// LIGHTS //////////////////////////////////////
8179

82-
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, bool render_only_ambient, bool render_one_color,
80+
Light::Light(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color,
8381
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular)
84-
: GameObject(name, model_name, position, scale, rotation, color, is_selected, render_only_ambient, render_one_color) {
82+
: GameObject(name, model_name, position, rotation, scale, color, is_selected, render_only_ambient, render_one_color) {
8583
this->ambient = ambient;
8684
this->diffuse = diffuse;
8785
this->specular = specular;
8886
}
8987

90-
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, bool render_only_ambient, bool render_one_color,
88+
PointLight::PointLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color,
9189
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, float constant, float linear, float quadratic)
92-
: Light(name, model_name, position, scale, rotation, color, is_selected, render_only_ambient, render_one_color, ambient, diffuse, specular) {
90+
: Light(name, model_name, position, rotation, scale, color, is_selected, render_only_ambient, render_one_color, ambient, diffuse, specular) {
9391
this->constant = constant;
9492
this->linear = linear;
9593
this->quadratic = quadratic;
9694
}
9795

98-
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, bool render_only_ambient, bool render_one_color,
96+
DirectionalLight::DirectionalLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color,
9997
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, const glm::vec3& direction)
100-
: Light(name, model_name, position, scale, rotation, color, is_selected, render_only_ambient, render_one_color, ambient, diffuse, specular) {
98+
: Light(name, model_name, position, rotation, scale, color, is_selected, render_only_ambient, render_one_color, ambient, diffuse, specular) {
10199
this->direction = direction;
102100
}
103101

104-
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, bool render_only_ambient, bool render_one_color,
102+
SpotLight::SpotLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color,
105103
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, const glm::vec3& direction, float inner_cut_off, float outer_cut_off,
106104
float constant, float linear, float quadratic)
107-
: Light(name, model_name, position, scale, rotation, color, is_selected, render_only_ambient, render_one_color, ambient, diffuse, specular) {
105+
: Light(name, model_name, position, rotation, scale, color, is_selected, render_only_ambient, render_one_color, ambient, diffuse, specular) {
108106
this->direction = direction;
109107

110108
this->inner_cut_off = inner_cut_off;

NeonEngine/src/game_object.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class GameObject {
1919
std::string model_name;
2020
glm::vec3 position;
2121
glm::vec3 scale;
22-
glm::vec3 rotation;
22+
glm::quat rotation;
2323
glm::mat4 model;
2424
glm::mat4 model_inv;
2525
glm::mat3 model_normals;
@@ -31,11 +31,10 @@ class GameObject {
3131

3232
static ColorGenerator* color_generator;
3333

34-
GameObject(const std::string& name = "", const std::string& model_name = "", const glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f), const glm::vec3& rotation = glm::vec3(0.0f, 0.0f, 0.0f),
34+
GameObject(const std::string& name = "", const std::string& model_name = "", const glm::vec3& position = glm::vec3(0.0f, 0.0f, 0.0f), const glm::quat& rotation = glm::angleAxis(glm::radians(0.0f), glm::vec3(1.0f, 0.0f, 0.0f)),
3535
const glm::vec3& scale = glm::vec3(1.0f, 1.0f, 1.0f), const glm::vec3& color = glm::vec3(0.0f, 0.0f, 0.0f), bool is_selected = false, bool render_only_ambient = false, bool render_one_color = false);
3636
~GameObject();
3737
void set_model_matrices_standard();
38-
void set_model_matrices_type1(GameObject* parent);
3938
void draw(Shader* shader, bool disable_depth_test);
4039
bool intersected_ray(const glm::vec3& ray_dir, const glm::vec3& camera_position, float& t);
4140
void set_select_state(bool is_game_obj_selected);
@@ -51,7 +50,7 @@ class Light : public GameObject {
5150

5251
Light() {}
5352

54-
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, bool render_only_ambient, bool render_one_color,
53+
Light(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color,
5554
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular);
5655
};
5756

@@ -61,15 +60,15 @@ class PointLight : public Light {
6160
float linear;
6261
float quadratic;
6362

64-
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, bool render_only_ambient, bool render_one_color,
63+
PointLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color,
6564
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, float constant, float linear, float quadratic);
6665
};
6766

6867
class DirectionalLight : public Light {
6968
public:
7069
glm::vec3 direction;
7170

72-
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, bool render_only_ambient, bool render_one_color,
71+
DirectionalLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color,
7372
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, const glm::vec3& direction);
7473
};
7574

@@ -84,7 +83,7 @@ class SpotLight : public Light {
8483
float linear;
8584
float quadratic;
8685

87-
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, bool render_only_ambient, bool render_one_color,
86+
SpotLight(const std::string& name, const std::string& model_name, const glm::vec3& position, const glm::quat& rotation, const glm::vec3& scale, const glm::vec3& color, bool is_selected, bool render_only_ambient, bool render_one_color,
8887
const glm::vec3& ambient, const glm::vec3& diffuse, const glm::vec3& specular, const glm::vec3& direction, float inner_cut_off, float outer_cut_off,
8988
float constant, float linear, float quadratic);
9089
};

NeonEngine/src/input.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ void Input::process_viewport_input() {
5151
Camera* camera_viewport = rendering->camera_viewport;
5252
float& deltaTime = neon_engine->deltaTime;
5353
bool& firstMouse = neon_engine->firstMouse;
54-
ImVec2 current_mouse_pos = ImGui::GetMousePos();
54+
//ImVec2 current_mouse_pos = ImGui::GetMousePos();
55+
double current_mouse_pos_x, current_mouse_pos_y;
56+
glfwGetCursorPos(neon_engine->window, &current_mouse_pos_x, &current_mouse_pos_y);
57+
ImVec2 current_mouse_pos(current_mouse_pos_x, current_mouse_pos_y);
5558

5659
if (transforming_selected_object) {
5760
if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
@@ -60,6 +63,9 @@ void Input::process_viewport_input() {
6063
last_mouse_pos_transforming = current_mouse_pos;
6164
}
6265
else {
66+
if (rendering->transform3d->type != TRANSLATION) {
67+
glfwSetInputMode(neon_engine->window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
68+
}
6369
transforming_selected_object = false;
6470
}
6571
}
@@ -73,6 +79,9 @@ void Input::process_viewport_input() {
7379
rendering->last_selected_object_transform3d = selected_object_transform3d;
7480
rendering->transform3d->set_highlight(true);
7581
if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
82+
if (rendering->transform3d->type != TRANSLATION) {
83+
glfwSetInputMode(neon_engine->window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
84+
}
7685
transforming_selected_object = true;
7786
last_mouse_pos_transforming = current_mouse_pos;
7887
}

0 commit comments

Comments
 (0)