-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComponentMesh.cpp
More file actions
145 lines (117 loc) · 3.2 KB
/
ComponentMesh.cpp
File metadata and controls
145 lines (117 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "Application.h"
#include "ModuleModelLoader.h"
#include "ComponentTransform.h"
#include "GameObject.h"
#include "ComponentMesh.h"
#include "SceneLoader.h"
#include "SceneImporter.h"
#include "MeshImporter.h"
#include "MathGeoLib/Geometry/LineSegment.h"
#include "MathGeoLib/Geometry/Triangle.h"
using namespace std;
ComponentMesh::ComponentMesh(GameObject* go)
{
myGameObject = go;
myType = MESH;
}
ComponentMesh::ComponentMesh(GameObject * go, ComponentMesh * comp)
{
myGameObject = go;
myType = MESH;
mesh = comp->mesh;
}
ComponentMesh::~ComponentMesh()
{
delete mesh;
}
void ComponentMesh::LoadMesh(Mesh* loadedMesh)
{
mesh = loadedMesh;
}
void ComponentMesh::Draw(const unsigned int program) const
{
mesh->Draw(program);
}
float ComponentMesh::IsIntersectedByRay(const float3 &origin ,const LineSegment & ray)
{
float minDist = -1.0f;
for(unsigned int i = 0; i < mesh->indices.size();i += 3)
{
Triangle tri = Triangle(mesh->vertices[mesh->indices[i]].Position, mesh->vertices[mesh->indices[i+1]].Position, mesh->vertices[mesh->indices[i+2]].Position);
if(tri.Intersects(ray))
{
//If first time assign otherwise only assign if lesser
float dist = tri.Distance(origin);
if (minDist == -1.0f)
minDist = dist;
else if (dist < minDist)
minDist = dist;
}
}
return minDist;
}
void ComponentMesh::OnSave(SceneLoader & loader)
{
loader.AddUnsignedInt("Type", myType);
loader.AddString("meshName", mesh->name.c_str());
}
void ComponentMesh::OnLoad(SceneLoader & loader)
{
string meshName = loader.GetString("meshName", "error");
if (meshName != "error")
{
MeshData data;
Importer->LoadMesh(meshName.c_str(), data);
mesh = new Mesh();
ProcessMeshData(data, *mesh);
mesh->setupMesh();
}
}
void ComponentMesh::ProcessMeshData(const MeshData & data, Mesh & myMesh)
{
for (unsigned int i = 0; i < data.num_vertices; i++)
{
Vertex vertex;
float3 positions;
positions.x = data.positions[i * 3];
positions.y = data.positions[i * 3 + 1];
positions.z = data.positions[i * 3 + 2];
vertex.Position = positions;
float3 normals;
normals.x = data.normals[i * 3];
normals.y = data.normals[i * 3 + 1];
normals.z = data.normals[i * 3 + 2];
vertex.Normal = normals;
//TODO: check if mesh contains texture coords or not
float2 texturesCoords;
texturesCoords.x = data.texture_coords[i * 2];
texturesCoords.y = data.texture_coords[i * 2 + 1];
vertex.TexCoords = texturesCoords;
myMesh.vertices.push_back(vertex);
}
for (unsigned int i = 0; i < data.num_indices; i++)
{
myMesh.indices.push_back(data.indices[i]);
}
myMesh.name = data.name;
}
void ComponentMesh::DrawInspector()
{
if (ImGui::CollapsingHeader("Mesh", ImGuiTreeNodeFlags_DefaultOpen))
{
//TODO: Make that isActive is used
ImGui::Checkbox("Active", &isActive);
ImGui::SameLine();
if (ImGui::Button("Remove Component", ImVec2(130, 20)))
{
LOG("Removing Component Mesh from %s", myGameObject->name);
myGameObject->components.erase(std::find(myGameObject->components.begin(), myGameObject->components.end(), this));
CleanUp();
delete this;
return;
}
ImGui::Text("Path: %s", mesh->name.c_str());
ImGui::Text("Number of triangles: %d", mesh->indices.size() / 3);
}
return;
}