-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.cpp
More file actions
416 lines (375 loc) · 13.9 KB
/
input.cpp
File metadata and controls
416 lines (375 loc) · 13.9 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include "shape.h"
#include "globals.h"
#include "input.h"
#include "HIERARCHIAL.h"
bool Wireframe = false;
bool tesselationMode = false;
shape_t* getCurrentShape() {
if (currentNode && currentNode->shape) {
return currentNode->shape.get(); // unique_ptr -> raw pointer
}
return nullptr;
}
void applyTransform(int direction) {
if (!currentModel) return;
// Find the selected node
std::shared_ptr<model_node_t> targetNode = nullptr;
if (selectedShapeId != -1)
targetNode = currentModel->findMNodeById(selectedShapeId);
else
targetNode = currentNode;
if (!targetNode) return;
// If parent transform mode is ON, move to parent node
if (transformParentMode && targetNode->parent.lock())
targetNode = targetNode->parent.lock();
float step = 0.1f;
float angle = glm::radians(5.0f);
switch (transformMode) {
case TRANSLATE:
if (activeAxis == 'X') targetNode->translation = glm::translate(targetNode->translation, glm::vec3(direction * step, 0, 0));
if (activeAxis == 'Y') targetNode->translation = glm::translate(targetNode->translation, glm::vec3(0, direction * step, 0));
if (activeAxis == 'Z') targetNode->translation = glm::translate(targetNode->translation, glm::vec3(0, 0, direction * step));
break;
case ROTATE:
if (activeAxis == 'X') targetNode->rotation = glm::rotate(targetNode->rotation, direction * angle, glm::vec3(1, 0, 0));
if (activeAxis == 'Y') targetNode->rotation = glm::rotate(targetNode->rotation, direction * angle, glm::vec3(0, 1, 0));
if (activeAxis == 'Z') targetNode->rotation = glm::rotate(targetNode->rotation, direction * angle, glm::vec3(0, 0, 1));
break;
case SCALE:
if (activeAxis == 'X') targetNode->scale = glm::scale(targetNode->scale, glm::vec3(1 + direction * 0.1f, 1, 1));
if (activeAxis == 'Y') targetNode->scale = glm::scale(targetNode->scale, glm::vec3(1, 1 + direction * 0.1f, 1));
if (activeAxis == 'Z') targetNode->scale = glm::scale(targetNode->scale, glm::vec3(1, 1, 1 + direction * 0.1f));
break;
default:
break;
}
}
void setupOpenGL();
void renderScene(GLuint shaderProgram);
void updateCamera();
void printInstructions();
shape_t* shape = getCurrentShape();
// Key handling implementation
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action != GLFW_PRESS && action != GLFW_REPEAT) return;
if (key == GLFW_KEY_M) {
currentMode = MODELLING;
std::cout << "Mode: MODELLING" << std::endl;
}
else if (key == GLFW_KEY_I) {
currentMode = INSPECTION;
std::cout << "Mode: INSPECTION" << std::endl;
}
else if(key== GLFW_KEY_N){
lightingEnabled = lightingEnabled;
std::cout<<"lighting"<<(lightingEnabled? "ON":"OFF")<<std::endl;
else if (key == GLFW_KEY_W) {
Wireframe = !Wireframe;
if (Wireframe) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
else
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
}
else if (key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
if (currentMode == MODELLING) {
handleModellingKeys(key);
}
else if (currentMode == INSPECTION) {
handleInspectionKeys(key);
}
}
void handleModellingKeys(int key) {
switch (key) {
case GLFW_KEY_LEFT:
cameraAngleY-= 5.0f;
std::cout << "Camera Y angle:"<< cameraAngleY <<" (rotating left)" << std::endl;
break;
case GLFW_KEY_RIGHT:
cameraAngleY += 5.0f;
std::cout<< "Camera cameraAngleY"<< "rotating right"<<std::endl;
break;
// CAMERA ROTATION-Up/Down (Lost op/dam)
case GLFW_KEY_UP:
cameraAngleX+=5.0f;
if( cameraAngleX>89.0f) cameraAngleX=89.0f;
std::cout<<"Camera X angle: "<<cameraAngleX<<"up"<<std::endl;
break;
case GLFW_KEY_DOWN:
cameraAngleX-=5.0f;
if( cameraAngleX<-89.0f) cameraAngleX=-89.0f;
std::cout<<"Camera X angle: "<<cameraAngleX<<"DOWN"<<std::endl;
break;
case GLFW_KEY_Q:
cameraDistance-=0.5f;
if(cameraDistance<1.0f)cameraDistance=1.0f;
std::cout<<"camera distance "<<cameraDistance<<"zoom in"<<std::endl;
break;
case GLFW_KEY_E:
cameraDistanc+-=0.5f;
if(cameraDistance>20.0f)cameraDistance=20.0f;
std::cout<<"camera distance "<<cameraDistance<<"zoom out"<<std::endl;
break;
case GLFW_KEY_V: { // Select shape (robust)
if (!currentModel) {
std::cout << "No model loaded.\n";
break;
}
// Print available shapes and their IDs so the user knows what to type
std::cout << "Available Shapes (ID : Type) ----------------\n";
for (const auto &n : currentModel->getShapes()) {
if (n)
std::cout << " " << n->id << " : " << shapeTypeToString(n->type) << "\n";
}
std::cout << "-------------------------------------------\n";
std::cout << "Enter Shape ID to select: ";
std::string line;
// Use getline to avoid issues with leftover newlines in std::cin
if (!std::getline(std::cin, line)) {
// If getline failed (e.g. stream in bad state), try to clear and read again
std::cin.clear();
if (!std::getline(std::cin, line)) {
std::cout << "No input received.\n";
break;
}
}
// If the first getline returned an empty line because of a leftover newline, read again
if (line.size() == 0) {
if (!std::getline(std::cin, line)) {
std::cout << "No input received.\n";
break;
}
}
try {
int id = std::stoi(line);
auto node = currentModel->findMNodeById(id);
if (node) {
selectedShapeId = id;
currentNode = node; // make it the active node in UI
std::cout << "Selected Shape ID: " << id << " (" << shapeTypeToString(node->type) << ")\n";
} else {
std::cout << "Invalid Shape ID (not found): " << id << "\n";
}
} catch (const std::exception &e) {
std::cout << "Invalid input (not a number): \"" << line << "\"\n";
}
break;
}
case GLFW_KEY_P: { // Toggle parent transform mode
transformParentMode = !transformParentMode;
std::cout << (transformParentMode ? "Parent Transform Mode: ON" : "Parent Transform Mode: OFF") << std::endl;
break;
}
case GLFW_KEY_U: // Move UP to parent
if (currentNode->parent.lock()) {
currentNode = currentNode->parent.lock();
std::cout << "Selected parent node.\n";
}
else {
std::cout << "Already at the root node.\n";
}
break;
case GLFW_KEY_J: // Move DOWN to first child
if (!currentNode->children.empty()) {
currentNode = currentNode->children.front();
std::cout << "Selected first child node.\n";
}
else {
std::cout << "Selected node has no children.\n";
}
break;
// Transform mode selection
case GLFW_KEY_R:
transformMode = ROTATE;
std::cout << "Transform mode: ROTATE\n";
break;
case GLFW_KEY_T:
transformMode = TRANSLATE;
std::cout << "Transform mode: TRANSLATE\n";
break;
case GLFW_KEY_G:
transformMode = SCALE;
std::cout << "Transform mode: SCALE\n";
break;
// Axis selection
case GLFW_KEY_X:
activeAxis = 'X';
std::cout << "Active Axis: X\n";
break;
case GLFW_KEY_Y:
activeAxis = 'Y';
std::cout << "Active Axis: Y\n";
break;
case GLFW_KEY_Z:
activeAxis = 'Z';
std::cout << "Active Axis: Z\n";
break;
// Apply transformations
case GLFW_KEY_KP_ADD:
case GLFW_KEY_EQUAL:
applyTransform(+1);
break;
case GLFW_KEY_KP_SUBTRACT:
case GLFW_KEY_MINUS:
applyTransform(-1);
break;
// Change color
case GLFW_KEY_C: {
float r, g, b;
std::cout << "Enter RGB values (0-1): ";
std::cin >> r >> g >> b;
if (currentNode && currentNode->shape) {
currentNode->shape->setColor(glm::vec4(r, g, b, 1.0f));
}
break;
}
//Tesselation implementation
case GLFW_KEY_A:
tesselationMode = !tesselationMode;
if (tesselationMode) {
std::cout << "TESSELLATION MODE ACTIVATED " << std::endl;
std::cout << "Press number keys 1-4 to set tessellation level" << std::endl;
std::cout << "Press A again to exit tessellation mode" << std::endl;
if (currentNode && currentNode->shape) {
std::cout << "Current tessellation level: " << currentNode->shape->getLevel() << std::endl;
std::cout << "Current triangle count: " << currentNode->shape->indices.size() / 3 << std::endl;
}
else {
std::cout << "No shape selected!" << std::endl;
}
}
else {
std::cout << "TESSELLATION MODE DEACTIVATED " << std::endl;
}
break;
// Add shapes
case GLFW_KEY_1: //add sphere
if (tesselationMode && currentNode && currentNode->shape) {
currentNode->shape->setLevel(1);
}
else if (!tesselationMode) {
currentModel->addShape(std::make_unique<sphere_t>(1));
currentNode = currentModel->getLastNode();
std::cout << "Sphere added\n";
}
break;
case GLFW_KEY_2: //add cylinder
if (tesselationMode && currentNode && currentNode->shape) {
currentNode->shape->setLevel(2);
}
else if (!tesselationMode) {
currentModel->addShape(std::make_unique<cylinder_t>(1));
currentNode = currentModel->getLastNode();
std::cout << "Cylinder added\n";
}
break;
case GLFW_KEY_3: //add box
if (tesselationMode && currentNode && currentNode->shape) {
currentNode->shape->setLevel(3);
}
else if (!tesselationMode) {
currentModel->addShape(std::make_unique<box_t>(1));
currentNode = currentModel->getLastNode();
std::cout << "Box added\n";
}
break;
case GLFW_KEY_4: // add cone
if (tesselationMode && currentNode && currentNode->shape) {
currentNode->shape->setLevel(4);
}
else if (!tesselationMode) {
currentModel->addShape(std::make_unique<cone_t>(1));
currentNode = currentModel->getLastNode();
std::cout << "Cone added\n";
}
break;
case GLFW_KEY_5: // remove last added shape
if (!tesselationMode) {
currentModel->removeLastShape();
currentNode = currentModel->getLastNode();
std::cout << "Last shape removed\n";
}
break;
// Save model
case GLFW_KEY_S: {
std::string filename;
std::cout << "Enter filename (with .mod extension): ";
std::cin >> filename;
if (filename.find(".mod") == std::string::npos) {
filename += ".mod";
}
currentModel->save(filename);
break;
}
}
}
void handleInspectionKeys(int key) {
switch (key) {
// Load model
case GLFW_KEY_L: {
std::string filename;
std::cout << "Enter filename to load: ";
std::cin >> filename;
if (currentModel->load(filename)) {
currentNode = currentModel->getLastNode();
// Reset camera to view loaded model
cameraDistance = 5.0f;
cameraAngleX = 0.0f;
cameraAngleY = 0.0f;
modelRotation = glm::mat4(1.0f);
}
break;
}
// Model rotation mode
case GLFW_KEY_R:
transformMode = ROTATE;
std::cout << "Model rotation mode activated\n";
break;
// Axis selection for model rotation
case GLFW_KEY_X:
activeAxis = 'X';
std::cout << "Model rotation axis: X\n";
break;
case GLFW_KEY_Y:
activeAxis = 'Y';
std::cout << "Model rotation axis: Y\n";
break;
case GLFW_KEY_Z:
activeAxis = 'Z';
std::cout << "Model rotation axis: Z\n";
break;
// Apply model rotation
case GLFW_KEY_KP_ADD:
case GLFW_KEY_EQUAL:
if (transformMode == ROTATE) {
float angle = glm::radians(5.0f);
switch (activeAxis) {
case 'X': modelRotation = glm::rotate(modelRotation, angle, glm::vec3(1, 0, 0)); break;
case 'Y': modelRotation = glm::rotate(modelRotation, angle, glm::vec3(0, 1, 0)); break;
case 'Z': modelRotation = glm::rotate(modelRotation, angle, glm::vec3(0, 0, 1)); break;
}
}
break;
case GLFW_KEY_KP_SUBTRACT:
case GLFW_KEY_MINUS:
if (transformMode == ROTATE) {
float angle = glm::radians(-5.0f);
switch (activeAxis) {
case 'X': modelRotation = glm::rotate(modelRotation, angle, glm::vec3(1, 0, 0)); break;
case 'Y': modelRotation = glm::rotate(modelRotation, angle, glm::vec3(0, 1, 0)); break;
case 'Z': modelRotation = glm::rotate(modelRotation, angle, glm::vec3(0, 0, 1)); break;
}
}
break;
}
}