-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
312 lines (261 loc) · 10.8 KB
/
main.cpp
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
// Brett Layman
// 2/21/2018
#include <iostream>
#include <sstream>
#include <fstream>
#include <cmath>
#include <list>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <csci441/shader.h>
#include "GameState.h"
#include "Matrix.h"
#include "keyHandler.h"
#include "Selector.h"
int SCREEN_WIDTH = 1920;
int SCREEN_HEIGHT = 1440;
using namespace std;
// handle key presses
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
KeyHandler::handlePress(key,action,window);
}
// convert to normalized device coordinates from window coordinates
void convertToNDC(double screenX, double screenY, float width, float height,float& NDCX,float& NDCY){
NDCX = -1 + screenX * (2/width);
NDCY = 1 - screenY * (2/height);
}
void mouseCallback(GLFWwindow* window, int button, int action, int mods){
// mouse clicked
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS ){
double x;
double y;
glfwGetCursorPos(window, &x, &y);
// set start position of mouse
GameState::startDragX = x;
GameState::startDragY = y;
GameState::mouseDragged = true;
// if making custom shape, add coordinates as new vertex in shape
if(GameState::mode == custom){
float x;
float y;
convertToNDC(GameState::startDragX, GameState::startDragY, SCREEN_WIDTH, SCREEN_HEIGHT, x, y);
CustomShape::currentCustom->addPoint(x,y);
}
}
// mouse released
else if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
GameState::mouseDragged = false;
// store previous view location
GameState::vyPrev = GameState::vy;
GameState::vxPrev = GameState::vx;
// get mouse release location
glfwGetCursorPos(window, &GameState::finishDragX, &GameState::finishDragY);
GameState::selected = true;
}
}
void framebufferSizeCallback(GLFWwindow* window, int width, int height) {
glViewport(0, 0, width, height);
if((float)width/height > (float)SCREEN_WIDTH/SCREEN_HEIGHT){
GameState::orthoWidth = (GameState::orthoHeight * ((float)SCREEN_WIDTH/SCREEN_HEIGHT));
}
else if((float)width/height < (float)SCREEN_WIDTH/SCREEN_HEIGHT){
GameState::orthoHeight = (GameState::orthoWidth * ((float)SCREEN_HEIGHT/SCREEN_WIDTH));
}
SCREEN_HEIGHT = height;
SCREEN_WIDTH = width;
}
void errorCallback(int error, const char* description) {
fprintf(stderr, "GLFW Error: %s\n", description);
}
// translate view using mouse drag in view mode
void translateView(GLFWwindow* window){
double mx;
double my;
glfwGetCursorPos(window, &mx, &my);
// continuously change view based on difference in mouse position
GameState::vx = .02 * (GameState::startDragX - mx) + GameState::vxPrev;
GameState::vy = .02 * (my - GameState::startDragY) + GameState::vyPrev;
}
/** start main **/
int main(void) {
GLFWwindow* window;
glfwSetErrorCallback(errorCallback);
/* Initialize the library */
if (!glfwInit()) { return -1; }
#ifdef __APPLE__
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Lab 4", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
// tell glfw what to do on resize
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
// init glad
if (!gladLoadGL()) {
std::cerr << "Failed to initialize OpenGL context" << std::endl;
glfwTerminate();
return -1;
}
// create the shaders
Shader shader("../vert.glsl", "../frag.glsl");
/** My code starts here **/
// set callback for keyboard input
glfwSetKeyCallback(window, key_callback);
// mouse input callback
glfwSetMouseButtonCallback(window, mouseCallback);
// for keeping track of multiple models
ModelHandler modelHandler = ModelHandler();
// initialize view matrix and camera position
TranslateMatrix negCameraPos = TranslateMatrix(4,4,0,0,0);
Matrix vMatrix = Matrix(4,4);
// orthagonal
OrthoMatrix oMatrix = OrthoMatrix(4,4);
oMatrix.createMatrix( -(GameState::orthoWidth/2), (GameState::orthoWidth/2),
-(GameState::orthoHeight/2), (GameState::orthoHeight/2), 0.0f, 50.0f);
// perspective
PerspMatrix pMatrix = PerspMatrix(4,4);
pMatrix.createMatrix(8.0f,SCREEN_HEIGHT/SCREEN_WIDTH,2.0f,100.0f);
// class for selecting models on screen
Selector selector = Selector();
// use z-buffering
glEnable(GL_DEPTH_TEST);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window)) {
// process keys being pressed
KeyHandler::handlePressed(window);
/* Render here */
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// activate shader
shader.use();
// translate to negative camera position
negCameraPos.setXYZ(-GameState::vx,-GameState::vy,-GameState::vz);
// set orthographic bounding box
oMatrix.createMatrix( -(GameState::orthoWidth/2), (GameState::orthoWidth/2),
-(GameState::orthoHeight/2), (GameState::orthoHeight/2), 0.0f, 50.0f);
// depending on what mode the user is in
switch(GameState::mode){
// view mode: for navigating space
case view:
if (GameState::changedMode){
GameState::changedMode = false;
modelHandler.deselectAllModels();
}
// move view when mouse dragged
if(GameState::mouseDragged){
translateView(window);
// set new cameraPosition
negCameraPos.setXYZ(-GameState::vx,-GameState::vy,-GameState::vz);
}
// reset orthographic bounding box
oMatrix.createMatrix( -(GameState::orthoWidth/2), (GameState::orthoWidth/2),
-(GameState::orthoHeight/2), (GameState::orthoHeight/2), 0.0f, 50.0f);
break;
// mode for creating new primitives
case stamp:
// if just changed to stamp mode
if(GameState::changedMode){
modelHandler.deselectAllModels();
if(!modelHandler.models.empty()) {
modelHandler.models.back()->setGameStateToModel();
}
GameState::changedMode = false;
}
// create any new objects
modelHandler.handleObjectCreation();
if(!modelHandler.models.empty()) {
modelHandler.setLastTransform();
}
break;
// shape modifying mode
case modify:
// if just changed to modify mode
if (GameState::changedMode){
GameState::changedMode = false;
modelHandler.deselectAllModels();
GameState::selected = false;
}
// if a selection area has been drawn, check for objects in that area using the selector
if(GameState::selected){
float startX;
float startY;
convertToNDC(GameState::startDragX, GameState::startDragY, SCREEN_WIDTH, SCREEN_HEIGHT, startX, startY);
float endX;
float endY;
convertToNDC(GameState::finishDragX, GameState::finishDragY, SCREEN_WIDTH, SCREEN_HEIGHT, endX, endY);
selector.findSelected(modelHandler, negCameraPos, oMatrix, startX, startY,endX,endY);
}
// set model transform for selected object
if(!modelHandler.models.empty()) {
modelHandler.setSelectedTransform();
}
break;
// mode for grouping shapes together
case group:
// if just changed to group mode
if (GameState::changedMode){
GameState::changedMode = false;
modelHandler.deselectAllModels();
GameState::selected = false;
}
// if a selection area has been drawn, check for objects in that area using the selector
if(GameState::selected){
float startX;
float startY;
convertToNDC(GameState::startDragX, GameState::startDragY, SCREEN_WIDTH, SCREEN_HEIGHT, startX, startY);
float endX;
float endY;
convertToNDC(GameState::finishDragX, GameState::finishDragY, SCREEN_WIDTH, SCREEN_HEIGHT, endX, endY);
selector.findSelected(modelHandler, negCameraPos, oMatrix, startX, startY,endX,endY);
}
// set model transform for group of objects
if(!modelHandler.models.empty()) {
modelHandler.setSelectedGroupTransform();
}
break;
case custom:
// create any objects the user has drawn
modelHandler.handleObjectCreation();
break;
}
// final view matrix
vMatrix = negCameraPos;
// if there are models to display, then update and display them
if(!modelHandler.models.empty()){
modelHandler.displayModels(shader,vMatrix,pMatrix,oMatrix);
}
// draw selection box for modify and group modes
if(GameState::mouseDragged && (GameState::mode == modify || GameState::mode == group)){
double winx;
double winy;
float x;
float y;
float startX;
float startY;
glfwGetCursorPos(window, &winx, &winy);
convertToNDC(winx, winy, SCREEN_WIDTH, SCREEN_HEIGHT, x, y);
convertToNDC(GameState::startDragX, GameState::startDragY, SCREEN_WIDTH, SCREEN_HEIGHT, startX, startY);
float* verts = selector.getBox(x,y,startX,startY);
Model* box = new Model(verts,144);
IdentityMatrix identity = IdentityMatrix(4,4);
box->bind(shader,identity,identity,identity);
box->display();
delete[] verts;
delete (box);
}
/* Swap front and back and poll for io events */
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}