-
Notifications
You must be signed in to change notification settings - Fork 0
/
C3DShaderPane.cpp
executable file
·645 lines (514 loc) · 16.7 KB
/
C3DShaderPane.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
/*
C3DShaderPane.cpp
Author: Tom Naughton
Description: <describe the C3DShaderPane class here>
*/
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <UStandardDialogs.h>
#include <glut.h>
#include <gl.h>
#include <glu.h>
#include <LDragAndDrop.h>
#include "CDragTask.h"
#include "C3DShaderPane.h"
#include "utilities.h"
#include "CShader.h"
#include "CGLImage.h"
#include "AppConstants.h"
#include "CPakRatApp.h"
#include "CPreferences.h"
#include "CResourceManager.h"
#include "CTextures.h"
#include "CPict.h"
#include "CDragTask.h"
static float mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
static float mat_shininess[] = { 20.0f };
static float light_ambient[] = { 0.9f, 0.9f, 0.9f, 1.0f };
static float light0_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
static float light0_position[] = { 55.0f, -50.0f, -5.0f, 0.0f };
static float light1_diffuse[] = { 0.5f, 0.5f, 1.0f, 1.0f };
static float light1_position[] = { -50.0f, 45.0f, 15.0f, 0.0f };
// ---------------------------------------------------------------------------
// ¥ C3DShaderPane Parameterized Constructor [public]
// ---------------------------------------------------------------------------
// desc is a string of pixel format attributes
C3DShaderPane::C3DShaderPane( const SPaneInfo& inPaneInfo, LStr255& desc)
: C3DPane(inPaneInfo, desc)
{
_retainedShader = false;
_image = nil;
_shader = nil;
dprintf("C3DShaderPane::C3DShaderPane( const SPaneInfo& inPaneInfo, LStr255& desc) : GLPane(inPaneInfo, desc)\n");
}
// ---------------------------------------------------------------------------
// ¥ C3DShaderPane Stream Constructor [public]
// ---------------------------------------------------------------------------
C3DShaderPane::C3DShaderPane( LStream* inStream)
: C3DPane(inStream)
{
_retainedShader = false;
_image = nil;
_shader = nil;
float theTime = (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0;
dprintf("C3DShaderPane::C3DShaderPane( LStream* inStream) : GLPane(inStream)\n");
}
C3DShaderPane::C3DShaderPane() : C3DPane()
{
_retainedShader = false;
_shader = nil;
_image = nil;
}
C3DShaderPane::~C3DShaderPane()
{
aglSetCurrentContext(mCtx);
if (_retainedShader) {
if (_shader)
delete _shader; // shader will delete the image
} else {
if (_image)
delete _image;
StopIdling();
}
dprintf("~C3DShaderPane\n");
}
#pragma mark -
void C3DShaderPane::SetupRendering()
{
FocusDraw();
aglSetCurrentContext(mCtx);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glEnable(GL_BLEND);
glEnable(GL_NORMALIZE);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_DONT_CARE);
glShadeModel(GL_SMOOTH);
}
void C3DShaderPane::SetShader(CPakStream *item, const char* name, int modifiers)
{
Boolean commandDown = (modifiers & cmdKey) != 0;
Boolean shiftDown = (modifiers & shiftKey) != 0;
Boolean optionDown = (modifiers & optionKey) != 0;
FocusDraw();
//aglSetCurrentContext(mCtx);
// load the image, check it's size
_image = CTextures::loadTexture(item);
// resize window
Rect bounds, size;
bounds = _image->bounds();
size.left = 0;
size.right = bounds.right - bounds.left;
size.top = 0;
size.bottom = bounds.bottom - bounds.top;
#warning broken for Carbon
#if PP_Target_Carbon
LWindow *myWindow = LWindow::FetchWindowObject(UQDGlobals::GetCurrentWindowPort());
#else
GrafPtr myPort = GetMacPort();
LWindow *myWindow = LWindow::FetchWindowObject(myPort);
#endif
SDimension16 dims;
dims.width = size.right;
dims.height = size.bottom;
myWindow->ResizeWindowTo(dims.width, dims.height);
// myWindow->SetStandardSize(dims);
// get rid of the file extension
string strippedName = stripExtension(lowerString(fixSlashes(name)));
_shaderName = strippedName;
// if the item doesn't belong to an archive, make a shader for it
if (!item->rootArchive() || optionDown) {
_image->uploadGLImage();
_shader = new CShader(_resources, strippedName.c_str(), SHADER_MD3, _image);
_retainedShader = true;
} else {
_shader = _resources->shaderWithName((const char*)strippedName.c_str());
_resources->parseShaders();
StartIdling();
}
}
#pragma mark -
// ---------------------------------------------------------------------------
// SpendTime
// ---------------------------------------------------------------------------
void
C3DShaderPane::SpendTime(const EventRecord &inMacEvent)
{
CWav *teleport = nil;
C3DPane::SpendTime(inMacEvent);
Boolean optionDown = (inMacEvent.modifiers & optionKey) != 0;
Boolean commandDown = (inMacEvent.modifiers & cmdKey) != 0;
Redraw();
_drawInterval = 0; // in Ticks
}
void C3DShaderPane::ReshapeGL(int width, int height)
{
FocusDraw();
//aglSetCurrentContext(mCtx);
glViewport(0, 0, width, height);
aglUpdateContext(mCtx);
// dprintf("ReshapeGL error: %s\n",aglErrorString(aglGetError()));
}
void C3DShaderPane::DrawGL()
{
FocusDraw();
float theTime = (float)glutGet(GLUT_ELAPSED_TIME) / 1000.0;
if (CShader::_animateShaders) g_frametime = (double)glutGet(GLUT_ELAPSED_TIME) / 1000.0 - g_starttime;
// set up drawing
//prepare the canvas for rendering
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
if (_shaderName.length()) {
float z= -1.0;
renderingAttributes_t rendering;
memset(&rendering, 0, sizeof(renderingAttributes_t));
rendering._textured = true;
rendering._renderOpaque = true;
rendering._renderBlend = true;
int *outElems = gShaderArrays.elems;
float *outVerts = (float*)gShaderArrays.verts;
float *outTexvecs = (float*)gShaderArrays.tex_st;
float *outNormals = (float*)gShaderArrays.norms;
gShaderArrays.numcolours = gShaderArrays.numverts = gShaderArrays.numelems = 0;
int gridSize = 20;
float unit = 2.0 / gridSize;
for (int y = 0; y <= gridSize; y++)
for (int x = 0; x <= gridSize; x++) {
*outTexvecs++ = 1.0 - unit*x/2.0;
*outTexvecs++ = unit*y/2.0;
*outVerts++ = 1.0 - unit*x;
*outVerts++ = 1.0 - unit*y;
*outVerts++ = z;
*outNormals++ = 0.0 ;
*outNormals++ = 0.0 ;
*outNormals++ = 0.0 ;
gShaderArrays.numverts++;
}
gridSize++;
for (int y = 0; y < gridSize-1; y++)
for (int x = 0; x < gridSize-1; x++) {
*outElems++ = (gridSize * y ) + x;
*outElems++ = (gridSize * y ) + gridSize + x;
*outElems++ = (gridSize * y ) + x + 1;
gShaderArrays.numelems += 3;
*outElems++ = (gridSize * y ) + x + 1;
*outElems++ = (gridSize * y ) + gridSize + x;
*outElems++ = (gridSize * y ) + gridSize + x + 1;
gShaderArrays.numelems += 3;
}
glEnable( GL_TEXTURE_2D );
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
if (_shader)
_shader->renderFlush(&rendering, 0);
}
}
#pragma mark -
// ---------------------------------------------------------------------------
// ¥ PutOnDuty
// ---------------------------------------------------------------------------
// A Commander is going on duty.
//
// inNewTarget is the Commander that is becoming the Target, which is
// this Commander or one of its SubCommanders.
//
// Subclasses should override this function if they wish to behave
// differently when on duty than when off duty
void
C3DShaderPane::PutOnDuty(
LCommander* inNewTarget)
{
#pragma unused (inNewTarget)
}
// ---------------------------------------------------------------------------
// ¥ TakeOffDuty
// ---------------------------------------------------------------------------
// A Commander is going off duty
//
// Subclasses should override this function if they wish to behave
// differently when on duty than when off duty
void
C3DShaderPane::TakeOffDuty()
{
}
// ---------------------------------------------------------------------------
// ¥ ObeyCommand [public]
// ---------------------------------------------------------------------------
// Respond to commands
Boolean
C3DShaderPane::ObeyCommand(
CommandT inCommand,
void* ioParam)
{
Boolean cmdHandled = true;
switch (inCommand) {
default:
cmdHandled = LCommander::ObeyCommand(inCommand, ioParam);
break;
}
return cmdHandled;
}
// ---------------------------------------------------------------------------
// ¥ FindCommandStatus [public]
// ---------------------------------------------------------------------------
// Pass back whether a Command is enabled and/or marked (in a Menu)
void
C3DShaderPane::FindCommandStatus(
CommandT inCommand,
Boolean& outEnabled,
Boolean& outUsesMark,
UInt16& outMark,
Str255 outName)
{
Boolean isMd3 = true;
CPreferences *prefs = gApplication->preferences();
switch (inCommand) {
default:
LCommander::FindCommandStatus(inCommand, outEnabled,
outUsesMark, outMark, outName);
break;
}
}
// ---------------------------------------------------------------------------
// ¥ HandleKeyPress [public]
// ---------------------------------------------------------------------------
Boolean
C3DShaderPane::HandleKeyPress(
const EventRecord &inKeyEvent)
{
#pragma unused (inKeyEvent)
return false;
}
#pragma mark -
void C3DShaderPane::PasteItem()
{ // private
LClipboard* theClipboard = LClipboard::GetClipboard();
SignalIf_(theClipboard == nil);
Handle theDataH = ::NewHandle(0);
theClipboard->GetData(cImageType, theDataH);
// CDDItem* thePastedItem = new CDDItem(this, *(ItemData*) *theDataH);
// ThrowIf_(thePastedItem == nil);
// SwitchTarget(thePastedItem);
}
void C3DShaderPane::Click(SMouseDownEvent& inMouseDown)
{
inMouseDown.delaySelect = false;
C3DPane::Click(inMouseDown);
if (sClickCount == 2) {
CShader *shader = _resources->selectedShader();
// dprintf("shader name %s in file %s\n", &shader->name, &shader->filename);
string file = (char*)&_shader->filename;
string item = (char*)&_shader->name;
if (file.length() > 0)
gApplication->OpenEditorForItem(_resources->pak(), file.c_str(), item.c_str(), cmdKey);
}
inMouseDown.delaySelect = true;
}
void
C3DShaderPane::ClickSelf(const SMouseDownEvent& inMouseDown )
{ // protected, virtual
//SwitchTarget(this); // Make this item active
//
// Track item long enough to distinguish between a click to
// select, and the beginning of a drag
//
Boolean isDrag = ::WaitMouseMoved(inMouseDown.macEvent.where);
// no dragging in low memory situations
if (LGrowZone::GetGrowZone()->MemoryIsLow())
return;
if (isDrag) {
//
// If we leave the window, the drag manager will be changing thePort,
// so we'll make sure thePort remains properly set.
//
mSuperView->FocusDraw();
ApplyForeAndBackColors();
CreateDragEvent(inMouseDown);
mSuperView->OutOfFocus(nil);
}
}
void C3DShaderPane::HiliteDropArea(DragReference inDragRef)
{
//mPane->
ApplyForeAndBackColors();
Rect dropRect;
//mPane->
CalcLocalFrameRect(dropRect);
::MacInsetRect(&dropRect, -1, -1);
StRegion dropRgn(dropRect);
::ShowDragHilite(inDragRef, dropRgn, true);
//LDropArea::HiliteDropArea(inDragRef);
}
void C3DShaderPane::CreateDragEvent(const SMouseDownEvent &inMouseDown)
{ // private
Rect theRect = _image->bounds();
CalcLocalFrameRect(theRect);
//
// Build a structure to contain the data we'll be passing to the drag event.
//
ItemData theFlavorData;
theFlavorData.vPointerToSourceObject = _image;
//
// Begin the drag task
//
//
// Create a new drag task
//
CDragTask theDragTask (inMouseDown.macEvent, _image);
theDragTask.DoDrag();
//if (theDragTask.DropLocationIsFinderTrash()) {
// DeleteSelf();
//}
}
Boolean
C3DShaderPane::ItemIsAcceptable(DragReference inDragRef, ItemReference inItemRef)
//
// ItemIsAcceptable will be called whenever the Drag Manager wants to know if
// the item the user is currently dragging contains any information that we
// can accept.
//
// In our case, the only thing we'll accept are cImageType items.
//
{
#pragma unused (inDragRef, inItemRef )
// FlavorFlags theFlags;
// if (GetFlavorFlags(inDragRef, inItemRef, 'PICT', &theFlags) == noErr)
// return true;
return false;
}
void
C3DShaderPane::EnterDropArea(DragReference inDragRef, Boolean inDragHasLeftSender)
//
// The cursor has just entered our area.
//
{ // protected, virtual
// Let LDragAndDrop do its thing (hilight the area)
LDragAndDrop::EnterDropArea(inDragRef, inDragHasLeftSender);
// And we'll do ours.
//PlaySound(gPlayEnterSound, rsrc_EnterSound);
}
void
C3DShaderPane::LeaveDropArea (DragReference inDragRef)
//
// The cursor has just left the building. I repeat, the cursor has left the building.
//
{ // protected, virtual
// Let LDragAndDrop do its thing (removes the hilighting)
LDragAndDrop::LeaveDropArea (inDragRef);
// And we'll do ours.
//PlaySound(gPlayExitSound, rsrc_ExitSound);
}
void
C3DShaderPane::InsideDropArea (DragReference inDragRef)
//
// The cursor is still in our area.
//
{ // protected, virtual
// dprintf("C3DShaderPane::InsideDropArea\n");
//
// Let LDragAndDrop do its thing - this is not really necessary, since
// the inherited version doesn't do anything. But it's safer this
// way because someday it might.
//
LDragAndDrop::InsideDropArea(inDragRef);
//
// And we'll do ours - we'll just read the mouse coordinates, but for this
// demo we won't do anything with them.
//
// The mouse location is where the mouse actually is on the screen. The
// alternative is the pinned location, which is _usually_ the same location,
// but can be different if the cursor is being constrained by a tracking handler.
// This is useful when you want an area within a view to be 'off-limits' to
// the ongoing drag.
//
// If we did want to do something based on where the cursor currently is in
// our area (such as indicating an insertion point or something), it would
// usually be best to use the pinned location for that work.
//
// Both mouse locations are returned in global screen coordinates
//
Point theMouseLocation;
Point thePinnedLocation;
::GetDragMouse(inDragRef, &theMouseLocation, &thePinnedLocation);
}
void
C3DShaderPane::ReceiveDragItem(
DragReference inDragRef,
DragAttributes /* inDragAttrs */,
ItemReference inItemRef,
Rect &inItemBounds) // In Local coordinates
//
// The user has dropped something in this view.
//
{
#pragma unused (inItemBounds)
// FIXME - put up memory warning
if (LGrowZone::GetGrowZone()->MemoryIsLow())
return;
#warning removed for Carbon
#if PP_Target_Carbon
#else
::InvalRect(&inItemBounds);
#endif
//
// Information about the drag contents we'll be needing.
//
FlavorFlags theFlags; // We actually only use the flags to see if a flavor exists
Size theDataSize; // How much data there is for us.
OSErr err;
err = ::GetFlavorFlags(inDragRef, inItemRef, 'PICT', &theFlags);
if (err == noErr) {
err = ::GetFlavorDataSize(inDragRef, inItemRef, 'PICT', &theDataSize);
if (err == noErr) {
if (theDataSize) {
PicHandle thePicH = (PicHandle) ::NewHandle(theDataSize);
err = ::GetFlavorData(inDragRef, inItemRef, 'PICT', *thePicH, &theDataSize, 0L);
CPict* inImage = new CPict(thePicH);
// SetMyGWorld(inImage->CreateGWorld());
}
}
}
}
Boolean
C3DShaderPane::CheckForOptionKey(DragReference inDragRef)
{ // private
//
// We'll check whether the option key was down at either the beginning _or_ the
// end of the drag, since (a) it's the preferred behaviour and (b) its so easy to do.
//
SInt16 theModifiersNow; // The state of the modifier keys right now
SInt16 theModifiersAtMouseDown; // The state of the modifier keys when the drag began
SInt16 theModifiersAtMouseUp; // The state of the modifier keys when the drag ended
::GetDragModifiers(inDragRef, &theModifiersNow, &theModifiersAtMouseDown, &theModifiersAtMouseUp);
return ((theModifiersAtMouseDown & optionKey) || (theModifiersAtMouseUp & optionKey));
}
Boolean
C3DShaderPane::CheckIfViewIsAlsoSender(DragReference inDragRef)
{ // private
//
// Just a note: While we are using the drag attributes only at the end of the
// drag, they are also available to you during the drag.
//
// Drag Attributes are described in MD+DDK, page 2-31.
//
DragAttributes theDragAttributes;
::GetDragAttributes(inDragRef, &theDragAttributes);
return (theDragAttributes & kDragInsideSenderWindow);
}