-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollageGraphics.cpp
752 lines (608 loc) · 21 KB
/
CollageGraphics.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
///////////////////////////////////////////////////////////////////////////////////////////////
//
// Name: CollageGraphics.cpp
//
// Author: David Borland
//
// Description: Graphics for Collage application.
//
///////////////////////////////////////////////////////////////////////////////////////////////
#include "CollageGraphics.h"
#include <VideoFile.h>
#include <iostream>
#include <fstream>
#include <time.h>
#include <algorithm>
CollageGraphics::CollageGraphics(Image::Behavior imageBehaviorType)
: RenciGraphics(), imageBehavior(imageBehaviorType) {
layoutManagerFactory = NULL;
layoutManager = NULL;
sceneManager = NULL;
imageLoadCounter = 0;
// OpenGL attributes
attribList = new int[3];
attribList[0] = WX_GL_RGBA;
attribList[1] = WX_GL_DOUBLEBUFFER;
attribList[2] = 0;
// TODO: font selection? Add later when context menu is available
/*
font = new FTPixmapFont("C:\\WINDOWS\\Fonts\\ARIAL.TTF");
// If something went wrong, bail out.
if(font->Error()) {
std::cout << "CollageGraphics::Constructor : font missing" << std::endl;
} else {
std::cout << "CollageGraphics::CollageGraphics() : font loaded" << std::endl;
}
*/
}
CollageGraphics::~CollageGraphics() {
// Clean up
for (int i = 0; i < (int)images.size(); i++) {
delete images[i];
}
for (int i = 0; i < (int)videos.size(); i++) {
delete videos[i];
}
if (sceneManager) delete sceneManager;
delete attribList;
// delete font;
}
void CollageGraphics::SetSceneManager(SceneManager* manager) {
if (sceneManager) {
delete sceneManager;
}
sceneManager = manager;
}
int* CollageGraphics::GetAttribList() {
return attribList;
}
void CollageGraphics::Update() {
for (int i = 0; i < (int)videos.size(); i++) {
videos[i]->Update();
}
}
void CollageGraphics::PreRender() {
glClear(GL_COLOR_BUFFER_BIT);
}
void CollageGraphics::Render() {
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
for (int i = 0; i < (int)images.size(); i++) {
images[i]->Render();
}
glPopMatrix();
}
void CollageGraphics::RenderStereo() {
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
for (int i = 0; i < (int)images.size(); i++) {
images[i]->RenderStereo();
}
glPopMatrix();
}
void CollageGraphics::OnKey(wxKeyEvent& e) {
int c = e.GetKeyCode();
float translateIncrement = 0.05f;
if (c == 9) {
// Tab
if (!images.empty()) {
ClearCurrent();
AddToCurrent(0);
}
}
else if (c == 127) {
// Delete
Delete();
}
else if (c == 32) {
// space bar - show image titles
showTitle = !showTitle;
}
else if (c == 314) {
// Left arrow
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->Translate(Vec2(-translateIncrement, 0.0));
}
}
else if (c == 316) {
// right arrow
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->Translate(Vec2(translateIncrement, 0.0));
}
}
else if (c == 315) {
// Up arrow
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->Translate(Vec2(0.0, translateIncrement));
}
}
else if (c == 317) {
// Down arrow
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->Translate(Vec2(0.0, -translateIncrement));
}
}
else if (c == '1') {
// Layout method 1
SetLayoutManager(CollageLayoutManagerFactory::LM_SIMPLE_SINGLE);
DoLayout();
}
else if (c == '2') {
// Layout method 1
SetLayoutManager(CollageLayoutManagerFactory::LM_SIMPLE_DOUBLE);
DoLayout();
}
else if (c == '3') {
// Layout method 3
SetLayoutManager(CollageLayoutManagerFactory::LM_SMART_SINGLE);
DoLayout();
}
else if (c == '4') {
// Layout method 4
SetLayoutManager(CollageLayoutManagerFactory::LM_RANDOM);
DoLayout();
}
else if (c == '5') {
// Layout method 5
SetLayoutManager(CollageLayoutManagerFactory::LM_FILL_ROOM);
DoLayout();
}
else if (c == 'a') {
// Select all
for (int i = (int)images.size() - 1; i >= 0; i--) {
AddToCurrent(i);
}
}
else if (c == 'f') {
// Fit to screen
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->FitToScreen();
}
}
else if (c == 'n') {
// Native resolution
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->NativeResolution();
}
}
else if (c == 's') {
// Stereo
if (currentImages.size() == 1) {
if (currentImages[0]->HasStereoImage()) {
// Switch left and right
CollageImage* image1 = currentImages[0];
CollageImage* image2 = image1->RemoveStereoImage();
// Switch in current images
currentImages[0] = image2;
// Switch in image list
for (int i = 0; i < (int)images.size(); i++) {
if (images[i] == image1) {
images[i] = image2;
break;
}
}
// Make them a stereo pair
image2->SetStereoImage(image1);
}
}
else if (currentImages.size() == 2) {
if (!currentImages[0]->HasStereoImage() && !currentImages[1]->HasStereoImage()) {
// Make stereo
CollageImage* image1 = currentImages[0];
CollageImage* image2 = currentImages[1];
// Remove image2 from current list
RemoveFromCurrent(image2);
// Remove image2 from image list
for (int i = 0; i < (int)images.size(); i++) {
if (images[i] == image2) {
images.erase(images.begin() + i);
break;
}
}
// Make them a stereo pair
image1->SetStereoImage(image2);
}
}
}
else if (c == 'u') {
// Move up in stereo depth
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->IncreaseStereoDepth();
}
}
else if (c == 'd') {
// Move down in stereo depth
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->DecreaseStereoDepth();
}
}
//*************** test keys *****************************
// TODO: i, o, p are for testing, figure out a better way to sort and remove keys (use context menu)
else if (c == 'p') {
// sort
SortDisplay(metadataSortFilename);
DoLayout();
}
else if (c == '[') {
// sort
SortDisplay(metadataSortTimestamp);
DoLayout();
}
else if (c == ']') {
// sort by path
SortDisplay(metadataSortPath);
DoLayout();
}
// ************** end test keys *************************
else if (c == 'l') {
DoLayout();
}
}
void CollageGraphics::OnMouse(wxMouseEvent& e) {
RenciGraphics::OnMouse(e);
// Convert from window to view
Vec2 position;
position.X() = (float)e.GetX() / (float)(windowWidth - 1) * viewWidth;
position.Y() = (1.0 - (float)e.GetY() / (float)(windowHeight - 1)) * viewHeight;
if (e.LeftDown()) {
oldEventPosition = position;
// Collision detection
for (int i = (int)images.size() - 1; i >= 0; i--) {
if (images[i]->Intersect(position)) {
if (!e.ControlDown() && !InCurrent(images[i])) {
// Clear the current selection
ClearCurrent();
}
else if (e.ControlDown() && InCurrent(images[i])) {
// Remove from current selection
RemoveFromCurrent(images[i]);
return;
}
AddToCurrent(i);
return;
}
}
// No collision
ClearCurrent();
// Start drawing selection rectangle
// XXX
}
else if (e.Dragging()) {
if (e.LeftIsDown()) {
// Move the current images
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->Translate(position - oldEventPosition);
}
oldEventPosition = position;
}
}
else if (abs(e.GetWheelRotation()) != 0) {
// Get an integer proportional to the scroll wheel speed
int scrollAmount = e.GetWheelRotation() / e.GetWheelDelta();
for (int i = 0; i < (int)currentImages.size(); i++) {
// Get the current scale
double scale = currentImages[i]->GetScale();
// Get the scale direction
double scaleAmount = scrollAmount > 0 ? 1.0 / 1.1 : 1.1;
// Scale multiple times based on scroll wheel speed
for (int j = 0; j < abs(scrollAmount); j++) {
scale *= scaleAmount;
}
// Set the new scale
currentImages[i]->SetScale(scale);
}
}
}
// Public method to access the images array for manipulation (e.g. layout)
std::vector<CollageImage*> CollageGraphics::GetImages() {
return images;
}
// using the current CollageLayoutManager, lay out the images using the specific algorithm. The layout
// manager is selected in SetLayoutManager();
void CollageGraphics::DoLayout(unsigned int start) {
// Layout the screen according to the currently selected layout.
// If no layout is specified, use the defualt scheme.
try {
if (this->layoutManager == NULL) {
std::cout << "CollageGraphics::DoLayout() : No layout manager specified...defaulting" << std::endl;
this->SetLayoutManager(CollageLayoutManagerFactory::LM_SIMPLE_SINGLE);
layoutManager->Layout(start);
}
else {
std::cout << "CollageGraphics::DoLayout() : Doing layout" << std::endl;
layoutManager->Layout(start);
}
}
catch(...) {
std::cout << "CollageGraphics::DoLayout() : Exception doing layout" << std::endl;
}
}
void CollageGraphics::LoadImage(const std::string& fileName) {
std::cout << "CollageGraphics::LoadImage() : Loading " << fileName << std::endl;
// Check to see if we can load this image type
if (!wxImage::CanRead(fileName)) {
std::cout << "CollageGraphics::LoadImage() : Cannot load this image type." << std::endl;
return;
}
// Load the image
wxImage image(fileName.c_str());
// Check for validity
if (!image.IsOk()) {
std::cout << "CollageGraphics::LoadImage() : Could not load image." << std::endl;
return;
}
// get the file name and parse out parts
int posDot = fileName.find_last_of('.');
int posFileSlash = fileName.find_last_of('\\');
// extension is backwards to the '.'
// file name is between the . and the first '\'
// path is everything to the left of the last '\'
std::string extension = fileName.substr(posDot + 1);
std::string fileNamePart = fileName.substr(posFileSlash + 1, posDot);
// need to trim off the slash(start at next pos)
//std::string filePathPart = fileName.substr(0, posFileSlash + 1);
// get the file timestamp
wxFSFile* file = fs.OpenFile(wxString(fileName));
wxDateTime fileTime = file->GetModificationTime();
// Flip the image
image = image.Mirror(false);
// Get image info
int width = image.GetWidth();
int height = image.GetHeight();
// Check the pixel format
Image::PixelFormat pixelFormat;
unsigned char* imageData;
if (image.HasAlpha()) {
// wxImage does not store alpha along with RGB, so need to insert it
imageData = new unsigned char[width * height * 4];
unsigned char* tempRGB = image.GetData();
unsigned char* tempAlpha = image.GetAlpha();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
imageData[i * width * 4 + j * 4 + 0] = tempRGB[i * width * 3 + j * 3 + 0]; // Red
imageData[i * width * 4 + j * 4 + 1] = tempRGB[i * width * 3 + j * 3 + 1]; // Green
imageData[i * width * 4 + j * 4 + 2] = tempRGB[i * width * 3 + j * 3 + 2]; // Blue
imageData[i * width * 4 + j * 4 + 3] = tempAlpha[i * width + j]; // Alpha
}
}
pixelFormat = Image::RGBA;
}
else {
// GL_TEXTURE_RECTANGLE_ARB does not appear to work correctly for non-RGBA images with odd dimensions, so pad with an alpha channel.
imageData = new unsigned char[width * height * 4];
unsigned char* tempRGB = image.GetData();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
imageData[i * width * 4 + j * 4 + 0] = tempRGB[i * width * 3 + j * 3 + 0]; // Red
imageData[i * width * 4 + j * 4 + 1] = tempRGB[i * width * 3 + j * 3 + 1]; // Green
imageData[i * width * 4 + j * 4 + 2] = tempRGB[i * width * 3 + j * 3 + 2]; // Blue
imageData[i * width * 4 + j * 4 + 3] = 255; // Alpha
}
}
pixelFormat = Image::RGBA;
}
// Create the image
images.push_back(new CollageImage(imageBehavior));
if (!images.back()->SetTextureInfo(width, height, pixelFormat)) {
std::cout << "CollageGraphics::LoadImage() : Could not create texture." << std::endl;
delete images.back();
images.pop_back();
delete imageData;
return;
}
if (!images.back()->SetTextureData(imageData)) {
std::cout << "CollageGraphics::LoadImage() : Could not set texture data." << std::endl;
delete images.back();
images.pop_back();
delete imageData;
return;
}
images.back()->SetViewExtents(0.0, viewWidth, 0.0, viewHeight);
images.back()->SetWindowHeight(windowHeight);
images.back()->NativeResolution();
// initialize image metadata
CollageItemMetadata* metadata = images.back()->GetCollageItemMetadata();
metadata->fileName = fileNamePart;
metadata->fileNameExtension = extension;
metadata->path = fileName;
metadata->itemSetOrder = 0;
metadata->itemLoadOrder = imageLoadCounter++;
metadata->itemTimestamp = fileTime.GetTicks();
// add a reference to CollageGraphics to the image
images.back()->SetCollageGraphics(this);
delete imageData;
}
void CollageGraphics::LoadVideo(const std::string& fileName, bool quickTime) {
std::cout << "CollageGraphics::LoadVideo() : Loading " << fileName << std::endl;
// Create an image
images.push_back(new CollageImage(imageBehavior));
// TODO: add code to insert metadata
// Create the video
videos.push_back(new VideoFile());
VideoStream::VideoType videoType = quickTime ? VideoStream::RGBA : VideoStream::RGB;
if (!videos.back()->Initialize(fileName, images.back(), videoType)) {
std::cout << "CollageGraphics::LoadVideo() : Video initialization failed." << std::endl;
delete images.back();
images.pop_back();
delete videos.back();
videos.pop_back();
return;
}
static_cast<VideoFile*>(videos.back())->SetLoop(true);
// Finish image setup
images.back()->SetViewExtents(0.0, viewWidth, 0.0, viewHeight);
images.back()->SetWindowHeight(windowHeight);
images.back()->NativeResolution();
// Play the image
videos.back()->Play();
}
bool CollageGraphics::InCurrent(CollageImage* image) {
for (int i = 0; i < (int)currentImages.size(); i++) {
if (image == currentImages[i]) return true;
}
return false;
}
void CollageGraphics::ClearCurrent() {
for (int i = 0; i < (int)currentImages.size(); i++) {
currentImages[i]->BorderOff();
}
currentImages.clear();
}
void CollageGraphics::AddToCurrent(int i) {
// If valid, add this to the current images
if (i >= 0 && i < (int)images.size()) {
CollageImage* temp = images[i];
temp->BorderOn();
// Reorder so it renders on top
images.erase(images.begin() + i);
images.push_back(temp);
// If not in current images, add it
if (!InCurrent(temp)) currentImages.push_back(temp);
}
}
void CollageGraphics::RemoveFromCurrent(CollageImage* image) {
for (int i = 0; i < (int)currentImages.size(); i++) {
if (image == currentImages[i]) {
image->BorderOff();
currentImages.erase(currentImages.begin() + i);
return;
}
}
}
void CollageGraphics::Delete() {
for (int i = 0; i < (int)images.size(); i++) {
for (int j = 0; j < (int)currentImages.size(); j++) {
// Check image
if (images[i] == currentImages[j]) {
// Search videos for this image
for (int k = 0; k < (int)videos.size(); k++) {
if (videos[k]->GetImage() == images[i]) {
// Remove video
delete videos[k];
videos.erase(videos.begin() + k);
break;
}
}
delete images[i];
images.erase(images.begin() + i);
i--;
currentImages.erase(currentImages.begin() + j);
j--;
break;
}
}
}
}
bool CollageGraphics::InitGL() {
// Initialize Glew for checking OpenGL extensions.
GLenum err = glewInit();
if (err != GLEW_OK) {
std::cout << "CollageGraphics::InitGL() : " << glewGetErrorString(err) << std::endl;
return false;
}
// Check extensions
if (!GLEW_ARB_texture_rectangle) {
std::cout << "CollageGraphics::InitGL() : GL_ARB_texture_rectangle not supported on this graphics card." << std::endl;
return false;
}
// Turn off depth testing
glDisable(GL_DEPTH_TEST);
// Turn off lighting
glDisable(GL_LIGHTING);
// Turn on blending
glEnable(GL_BLEND);
// Set the blending function
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Set the background
// glClearColor(0.21f, 0.21f, 0.19f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Set projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, viewWidth, 0.0, viewHeight, -1.0, 1.0);
return true;
}
// Returns a handle to the SceneManager, which describes the display environment
SceneManager* CollageGraphics::GetSceneManager(void) {
return this->sceneManager;
}
// Using the CollageLayoutManagerFactory, create a new layout manager based on the selected type. The
// collageLayoutManager is set to this factory, and subsequent calls to DoLayout() will be invoked on that
// manager
void CollageGraphics::SetLayoutManager(CollageLayoutManagerFactory::LayoutType layoutType) {
try {
if (this->layoutManagerFactory == NULL) {
std::cout << "CollageGraphics::SetLayoutManager() : Creating layout manager factory." << std::endl;
this->layoutManagerFactory = new CollageLayoutManagerFactory(this);
}
//std::cout << "CollageGraphics::SetLayoutManager() : layout manager pointer for test: ", this->collageLayoutManager << std::endl;
if (this->layoutManager != NULL) {
// Delete the old layout manager, will switch to a new one
std::cout << "CollageGraphics::SetLayoutManager() : Clearing old collage layout manager." << std::endl;
delete this->layoutManager;
}
std::cout << "CollageGraphics::SetLayoutManager() : Setting new layout manager of type " << layoutType << std::endl;
layoutManager = layoutManagerFactory->CreateLayoutManager(layoutType);
}
catch(...) {
std::cout << "CollageGraphics::SetLayoutManager() : Exception setting layout manager." << std::endl;
}
}
// TODO: finish implementing all sort options in CollageItemMetadata
// Sort the images based on the sort option, using the metadata associated with the images
void CollageGraphics::SortDisplay(MetadataSortOption option) {
switch(option) {
case metadataSortFilename:
std::sort(images.begin(), images.end(), CompareFileName);
break;
case metadataSortTimestamp:
std::sort(images.begin(), images.end(), CompareTimestamp);
break;
case metadataSortPath:
std::sort(images.begin(), images.end(), ComparePath);
break;
}
}
// TODO: implement load from text file
// Load collage based on a selected metadata text file
void CollageGraphics::LoadImagesFromMetadataTextFile(std::string metadataTextFileName) {
}
void CollageGraphics::SetBackgroundColor(float r, float g, float b) {
glClearColor(r, g, b, 1.0);
}
/*
FTFont* CollageGraphics::GetFont() {
return font;
}
*/
bool CollageGraphics::IsShowTitle() {
return showTitle;
}
/*
// Method called when rendering for the 'right' display. This method will set a flag indicating which canvas is being rendered.
// This flag is used in Collage to do rendering of fonts, which are calculated in screen space, versus the normal transformations for
// other graphic content in collage.
void CollageGraphics::RenderExitRight() {
renderLeft = false;
}
// Method called when rendering for the 'right' display. This method will set a flag indicating which canvas is being rendered.
// This flag is used in Collage to do rendering of fonts, which are calculated in screen space, versus the normal transformations for
// other graphic content in collage.
void CollageGraphics::RenderExitLeft() {
renderLeft = true;
}
// Return a bool that indicates whether the left screen is being rendered
bool CollageGraphics::IsRenderLeft() {
return renderLeft;
}
*/
// *********************** Sort binary predicates *******************************************
bool CompareFileName(CollageImage* imageA, CollageImage* imageB) {
return (imageA->GetCollageItemMetadata()->fileName < imageB->GetCollageItemMetadata()->fileName);
}
bool ComparePath(CollageImage* imageA, CollageImage* imageB) {
return (imageA->GetCollageItemMetadata()->path < imageB->GetCollageItemMetadata()->path);
}
bool CompareTimestamp(CollageImage* imageA, CollageImage* imageB) {
return (imageA->GetCollageItemMetadata()->itemTimestamp < imageB->GetCollageItemMetadata()->itemTimestamp);
}