aboutsummaryrefslogtreecommitdiff
path: root/mayaPlug/shaveBrushManip.cpp
blob: bc0079b0ec5833eeb369356fc770ce2cf85e7523 (plain) (blame)
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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
// Shave and a Haircut
// (c) 2019 Epic Games
// US Patent 6720962

//Qt headers must be included before any others !!!
#if (defined LINUX) || (defined OSMac_)
#include <QtOpenGL/QGLContext>
#include <QtOpenGL/QGLWidget>

#if QT_VERSION < 0x050000
#  include <QtGui/QWidget>
#else
#  include <QtWidgets/QWidget>
#endif
#endif

#include <maya/M3dView.h>
#include <maya/MDagPath.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnTransform.h>
#include <maya/MHWGeometry.h>
#include <maya/MGlobal.h>
#include <maya/MPoint.h>
#include <maya/MShaderManager.h>
#include <maya/MString.h>
#include <maya/MVector.h>

#if defined(_WIN32) && (MAYA_API_VERSION >= 201600)
#include <Windows.h>
#endif

#ifdef OSMac_MachO_
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
//#   include <AppKit/NSOpenGL.h>
#else
//#define GL_GLEXT_PROTOTYPES
# include <GL/gl.h>
# include <GL/glu.h>
#endif

#include "shaveBrushManip.h"

MTypeId	shaveBrushManip::id(0x001029B8);
MString shaveBrushManip::nodeTypeName = "shaveBrushManip";


//	The 'fast brush' does an XOR draw on top of the view, meaning that it
//	can be drawn and erased without having Maya redraw the entire view.
//
//	The standard brush is drawn normally during a refresh, which is
//	triggered either by moving a proxy transform within the scene to match
//	the mouse movement, or by forcing a refresh.
//
//	The fast brush is enabled by default in shaveGlobals, but on some
//	graphics cards it can cause visual anomalies, in which case the user
//	can switch to the standard brush.

shaveBrushManip::shaveBrushManip()
:	mBrushSize(0.0f)
,	mClearOld(false)
,	mCX(-1)
,	mCY(-1)
,	mFastBrush(true)
,	mIsActive(false)
,	mViewHeight(0)
,	mViewWidth(0)
,	mWindow(0)
{
	aWidget = NULL;
}


shaveBrushManip::~shaveBrushManip()
{
	mProxy = MObject::kNullObj;
}


MStatus shaveBrushManip::connectToDependNode(const MObject& node)
{
	//thisObj = thisMObject();

	finishAddingManips();
	MPxManipContainer::connectToDependNode(node);

	if (!mFastBrush)
	{
		MFnDependencyNode nodeFn(node);
		mProxy = node;
	}

	return MS::kSuccess;
}


MStatus shaveBrushManip::createChildren()
{
	//
	// We don't want any manips because their handles, even though not
	// drawn, can still capture mouse clicks.
	//
    return MS::kSuccess;
}


void shaveBrushManip::draw(
		M3dView&				view,
		const MDagPath&			path,
		M3dView::DisplayStyle	dStyle,
		M3dView::DisplayStatus	dStatus
)
{
	if (mFastBrush)
	{
		//
		// If Maya is asking us to redraw in the same view where we last did a
		// draw, then it must have already cleared the old image, so we should
		// not try to clear it ourselves.
		//
		if (mClearOld && isSameView(view)) mClearOld = false;
	}
	else
	{
		M3dView	activeView = M3dView::active3dView();

		if ((view.window() == activeView.window())
		&&	(mCX >= 0) && (mCY >= 0)
		&&	(mCX < view.portWidth()) && (mCY < view.portHeight()))
		{
			prepareView(activeView);
			drawBrush();
			restoreView(activeView);
		}
	}
}


void shaveBrushManip::getBrushRadii(int& rx, int& ry) const
{
	rx = mRX;
	ry = mRY;
}


void shaveBrushManip::leaveView()
{
	//MGlobal::displayInfo("leaveView");

	///////////////////////////
	//return;
	///////////////////////////

	if (mIsActive)
	{
		M3dView	activeView = M3dView::active3dView();

		if (mFastBrush)
			eraseBrush(activeView);
		else
		{
			if(mCX != -1 || mCY != -1) //to avoid exra redraws
			{
				//	This will let us know not to draw the brush.
				mCX = mCY = -1;

				//	Force Maya to redraw the view so that the brush will be
				//	erased.
				activeView.refresh(false, true);
			}
		}
	}
}


void shaveBrushManip::setActive(bool active)
{
	///////////////////////////
	//return;
	///////////////////////////

	if (mIsActive != active)
	{
		//
		// If we're being made active then clear out the old view
		// information.
		//
		// If we're being made inactive then erase the current brush image.
		//
		if (active)
		{
			mWindow = 0;
			mViewHeight = 0;
			mViewWidth = 0;
		}
		else
		{
			M3dView	activeView = M3dView::active3dView();
			eraseBrush(activeView, false);
		}

		mIsActive = active;
	}
}


void shaveBrushManip::setBrushPos(
		int cx, int cy, bool forceRedraw, bool isMayaDraw
)
{
	///////////////////////////
	//return;
	///////////////////////////

	//printf("set pos %i %i\n",cx,cx);fflush(stdout);

	M3dView	activeView = M3dView::active3dView();
	bool	isNewView = !isSameView(activeView);

	// If someone is giving us brush positions, then we must be active.
	setActive(true);

	if (mFastBrush)
	{
		// If we were previously inactive or the view has changed, then we
		// will need to redraw the brush even if its position is unchanged.
		if (isNewView || !mIsActive) 
			forceRedraw = true;

		// If the brush has moved, or this is a forced redraw, then do the
		// draw.
		if (forceRedraw || (mCX != cx) || (mCY != cy))
		{
			bool	activeViewPrepared = eraseBrush(activeView, true);

			if (!activeViewPrepared) prepareView(activeView);

			// If we've changed views, or the view has changed size, then
			// we need to recalculate the radii.
			if (isNewView)
			{
				saveView(activeView);
				calculateRadii();
			}

			mCX = cx;
			mCY = cy;

			if ((mCX >= 0) && (mCY >= 0))
			{
				drawBrush();
				mClearOld = true;
			}

			if (!isMayaDraw) 
				swapGLBuffers(activeView);

			restoreView(activeView);
		}
	}
	else
	{
		//	If the brush has moved, move the proxy transform
		//	correspondingly.
		if ((mCX != cx) || (mCY != cy))
		{
			// If we've changed views, or the view has changed size, then
			// we need to recalculate the radii.
			if (isNewView)
			{
				saveView(activeView);
				calculateRadii();
			}

			MPoint	nearPlane;
			MPoint	farPlane;
			activeView.viewToWorld(cx, cy, nearPlane, farPlane);

			MVector	translation(
						(nearPlane.x + farPlane.x) / 2.0,
						(nearPlane.y + farPlane.y) / 2.0,
						(nearPlane.z + farPlane.z) / 2.0
					);
			MFnTransform	transformFn(mProxy);
			transformFn.setTranslation(translation, MSpace::kObject);

			mCX = cx;
			mCY = cy;
		}
	}

	MHWRender::MRenderer::setGeometryDrawDirty(thisObj);
}


void shaveBrushManip::setBrushSize(float brushSize)
{
	///////////////////////////
	//return;
	///////////////////////////

	if (brushSize != mBrushSize)
	{
		M3dView	view;
		bool	viewPrepared = false;
		bool	foundView = findOldView(view);

		if (foundView) 
			viewPrepared = eraseBrush(view, true);

		mBrushSize = brushSize;

		if (foundView)
		{
			calculateRadii();

			if (!viewPrepared) 
				prepareView(view);

			if ((mCX >= 0) && (mCY >= 0))
			{
				drawBrush();
				mClearOld = true;
			}

			swapGLBuffers(view);
			restoreView(view);
		}
	}
}


//----------------------------------------------------------------------
//
//		Internal Methods
//
//----------------------------------------------------------------------

void shaveBrushManip::calculateRadii()
{
	unsigned	radius;

	if (mViewHeight < mViewWidth)
		radius = (unsigned)((float)mViewHeight * mBrushSize / 2.0f + 0.5);
	else
		radius = (unsigned)((float)mViewWidth * mBrushSize / 2.0f + 0.5);

	if (radius < 4) radius = 4;

	//
	// %%%	We eventually need to compensate for non-square pixels by
	//		assigning X and Y different radii.
	//
	mRX = radius;
	mRY = radius;
}


void shaveBrushManip::drawBrush() const
{
	//printf("draw pos %i %i\n",mCX,mCY);fflush(stdout);
	//
	// Draw the outer circle.  We use an ellipse drawing method because
	// eventually we want to support displays with non-square pixels, which
	// will require different X and Y radii.
	//
	drawEllipse(mCX, mCY, mRX, mRY);

#if 0
	//
	// Draw a small crosshair in the center.
	//
	glBegin(GL_LINES);
	glVertex2i(mCX-4, mCY);
	glVertex2i(mCX+4, mCY);

	glVertex2i(mCX, mCY-4);
	glVertex2i(mCX, mCY+4);
	glEnd();
#endif
}


//
// This method uses a DDA to draw an ellipse with the given radii into the
// bitmap.
//
// The algorithm is taken almost verbatim from Tim Kientzle's 'Algorithm
// Alley' column in the July, 1994 issue of Dr. Dobb's Journal.
//
void shaveBrushManip::drawEllipse(int cx, int cy, unsigned rx, unsigned ry)
{
	const unsigned	rxSquared = rx * rx;
	const unsigned	rySquared = ry * ry;
	const unsigned	twoRXSquared = 2 * rxSquared;
	const unsigned	twoRYSquared = 2 * rySquared;

	glBegin(GL_POINTS);

	//
	// Plot the octant from the top of the ellipse to the top-right
	// and reflect it in the other four quadrants.
	//
	int	x = 0;
	int	y = ry;

	int	twoXTimesRYSquared = 0;
	int	twoYTimesRXSquared = (int)(y * twoRXSquared);
	int	error = -(int)(y * rxSquared);

	while (twoXTimesRYSquared <= twoYTimesRXSquared)
	{
		drawMirroredVert(cx, cy, x, y);

		x += 1;
		twoXTimesRYSquared += (int)twoRYSquared;
		error += twoXTimesRYSquared - (int)rySquared;

		if (error >= 0)
		{
			y -= 1;
			twoYTimesRXSquared -= (int)twoRXSquared;
			error -= twoYTimesRXSquared;
		}
	}

	//
	// Plot the octant from the right of the ellipse to the top-right
	// and reflect it in the other four quadrants.
	//
	x = rx;
	y = 0;
	twoXTimesRYSquared = (int)(x * twoRYSquared);
	twoYTimesRXSquared = 0;
	error  = -(int)(x * rySquared);

	while (twoXTimesRYSquared > twoYTimesRXSquared)
	{
		drawMirroredVert(cx, cy, x, y);

		y += 1;
		twoYTimesRXSquared += (int)twoRXSquared;
		error += twoYTimesRXSquared - (int)rxSquared;

		if (error >= 0)
		{
			x -= 1;
			twoXTimesRYSquared -= (int)twoRYSquared;
			error -= twoXTimesRYSquared;
		}
	}

	glEnd();
}


void shaveBrushManip::drawMirroredVert(int cx, int cy, int x, int y)
{
	//
	// Mirror the vert in the four quadrants around the center point.
	//
	// Because we are drawing in XOR mode, we don't want to plot the same
	// point an even number of times as that will be as if we hadn't
	// plotted it at all.  So we have special cases for x==0 and y==0 to
	// prevent that.
	//
	if (x == 0)
	{
		glVertex2i(cx, cy + y);
		glVertex2i(cx, cy - y);
	}
	else if (y == 0)
	{
		glVertex2i(cx + x, cy);
		glVertex2i(cx - x, cy);
	}
	else
	{
		glVertex2i(cx + x, cy + y);
		glVertex2i(cx + x, cy - y);
		glVertex2i(cx - x, cy + y);
		glVertex2i(cx - x, cy - y);
	}
}


bool shaveBrushManip::eraseBrush(
		M3dView& currentView, bool dontRestoreCurrentView
)
{
	bool	currentViewIsPrepared = false;


	if (mFastBrush && mClearOld)
	{
		//
		// If the previous brush image was drawn in a different view from
		// the one passed to us by the caller, then we must find that view.
		//
		if (!isSameView(currentView))
		{
			M3dView		oldView;

			if (findOldView(oldView))
			{
				prepareView(oldView);
				drawBrush();
				swapGLBuffers(oldView);
				restoreView(oldView);
			}
		}
		else
		{
			prepareView(currentView);
			drawBrush();

			if (dontRestoreCurrentView)
				currentViewIsPrepared = true;
			else
			{
				swapGLBuffers(currentView);
				restoreView(currentView);
			}
		}

		mClearOld = false;
	}

	return currentViewIsPrepared;
}


bool shaveBrushManip::findOldView(M3dView& view) const
{
	if (mIsActive)
	{
		unsigned	numViews = M3dView::numberOf3dViews();
		unsigned	i;

		for (i = 0; i < numViews; i++)
		{
			M3dView::get3dView(i, view);

			if (isSameView(view)) return true;
		}
	}

	return false;
}


bool shaveBrushManip::isSameView(M3dView& view) const
{
	if ((view.window() != mWindow)
	||	(view.portHeight() != mViewHeight)
	||	(view.portWidth() != mViewWidth))
	{
		return false;

	}

	
#if OSMac_
	QWidget* wgt = view.widget();
	return (wgt == aWidget);
#endif

	return true;
}

void shaveBrushManip::prepareView(M3dView& view)
{
	view.beginGL();

	glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	gluOrtho2D(
		0.0, (GLdouble)view.portWidth(),
		0.0, (GLdouble)view.portHeight()
	);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();

	// This bit of magic is from marqueeTool.cpp in the devkit.
	glTranslatef(0.375, 0.375, 0.0);

	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	if (mFastBrush)
	{
		glEnable(GL_COLOR_LOGIC_OP);
		glLogicOp(GL_XOR);

		//	Because we're doing XOR, if we set the draw color to 12 (red)
		//	it won't appear red when over the default grey background of
		//	Maya's views. By choosing light green XOR will give us
		//	reddish-orange against a grey background, which is at least
		//	close to red.
		view.setDrawColor(18, M3dView::kActiveColors);
	}
	else
		view.setDrawColor(12, M3dView::kActiveColors);	// red
}


void shaveBrushManip::restoreView(M3dView& view)
{
	glPopMatrix();
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
	glPopAttrib();

	view.endGL();
}


void shaveBrushManip::saveView(M3dView& view)
{
	mWindow = view.window();
	mViewHeight = view.portHeight();
	mViewWidth = view.portWidth();

#ifdef OSMac_
	//
	//	On OSX the views do not have their own windows but are merely
	//	regions within the main window.  So to know if we have the correct
	//	view we must compare not just the window but the position of the
	//	region.
	//
	aWidget = view.widget();
#endif
}


void shaveBrushManip::swapGLBuffers(M3dView& view)
{
#if defined(LINUX)
	glXSwapBuffers(view.display(), view.window());
#elif defined(OSMac_)
	//there is no good way to swap buffers now
	//so we grey out 'fast brush' checkbox for 2011

	//const QGLContext* qgl = QGLContext::currentContext();
	//if(qgl)
	//	qgl->swapBuffers();
	//else
	//	MGlobal::displayInfo("null QGLContext");

	//NSOpenGLContext* nsgl = view.display();
	//[nsgl flushBuffer]; //cocoa
	//nsgl->flushBuffer(); 
	//CGLContextObj* cgl = nsgl->CGLContextObj();
#elif defined(_WIN32)
	SwapBuffers(view.deviceContext());
#else
	#error	Unsupported OS type.
#endif
}

MString   shaveBrushManip::drawDbClassification("drawdb/geometry/shaveBrushManip");
MString   shaveBrushManip::drawRegistrantId("shave_and_haircut_brush");

MObject shaveBrushManip::trigger;

MStatus	shaveBrushManip::initialize()
{
	MStatus stat;
	MFnNumericAttribute na;

	trigger  = na.create("atrigger", "tr", MFnNumericData::kInt,0,&stat);
	if(stat != MStatus::kSuccess)
	{
		MGlobal::displayError("shaveBrushManip: can't create attribute");
	}
	na.setStorable(false);
    na.setHidden(true);

	stat = addAttribute(trigger);
	if(stat != MStatus::kSuccess)
	{
		MGlobal::displayError("shaveBrushManip: can't add attribute");
	}

	return MStatus::kSuccess;
	//return MPxManipContainer::initialize();
}


// pre-draw callback
static void shaveBrushPreDrawCallback(
	MHWRender::MDrawContext& context,
	const MHWRender::MRenderItemList& renderItemList,
	MHWRender::MShaderInstance *shaderInstance
	)
{
	//printf("Pre-draw callback triggered for render item with name '%s'\n", renderItem->name().asChar());
}
// post-draw callback
static void shaveBrushPostDrawCallback(
	MHWRender::MDrawContext& context,
	const MHWRender::MRenderItemList& renderItemList,
	MHWRender::MShaderInstance *shaderInstance
	)
{
	//printf("Post-draw callback triggered for render item with name '%s'\n", renderItem->name().asChar());
}

shaveBrushOverride::shaveBrushOverride(const MObject& obj)
: MPxGeometryOverride(obj)
, brush(NULL)
{
	MStatus status;
	MFnDependencyNode node(obj, &status);
	if (status)
	{
		brush = dynamic_cast<shaveBrushManip*>(node.userNode());
	}
}

shaveBrushOverride::~shaveBrushOverride()
{
}

MHWRender::DrawAPI shaveBrushOverride::supportedDrawAPIs() const
{
	//return MHWRender::kAllDevices;
	return MHWRender::kOpenGL;
}

void shaveBrushOverride::updateDG()
{
	if (brush)
	{
		//nothing to do at the moment
	}
}

//#define BRUSH_BY_SHADER //geom shaders are 

static const bool debugShader = false;//true;

void shaveBrushOverride::updateRenderItems(const MDagPath& path,
											  MHWRender::MRenderItemList& list)
{
	MHWRender::MRenderer* renderer = MHWRender::MRenderer::theRenderer();
	if (!renderer) return;
	
	const MHWRender::MShaderManager* shaderMgr = renderer->getShaderManager();
	if (!shaderMgr) return;


	MHWRender::MRenderItem* item = NULL;
	int index = list.indexOf(
		"shaveBrush",
//#ifdef BRUSH_BY_SHADER
//		MHWRender::MGeometry::kPoints,
//#else
		MHWRender::MGeometry::kLines,
//#endif
		MHWRender::MGeometry::kAll);

	if (index < 0)
	{
		item = MHWRender::MRenderItem::Create
		(	"shaveBrush",
//#ifdef BRUSH_BY_SHADER
//			MHWRender::MGeometry::kPoints,
//#else
			MHWRender::MGeometry::kLines,
//#endif
			MHWRender::MGeometry::kAll,
			false);
		list.append(item);

		//MHWRender::MShaderInstance* shader = shaderMgr->getStockShader(
		//	MHWRender::MShaderManager::k3dSolidShader,
		//	debugShader ? shaveBrushPreDrawCallback : NULL,
		//	debugShader ? shaveBrushPostDrawCallback : NULL);
		MHWRender::MShaderInstance* shader = shaderMgr->getEffectsFileShader("shaveHair.cgfx","Brush");
		if (shader)
		{
			//static const float theColor[] = {0.0f, 0.01f, 0.27f, 1.0f};
			//shader->setParameter("solidColor", theColor);

			// assign shader
			item->setShader(shader);

			// sample debug code
			if (debugShader)
			{
				MStringArray params;
				shader->parameterList(params);
				unsigned int numParams = params.length();
				printf("DEBUGGING SHADER, BEGIN PARAM LIST OF LENGTH %d\n", numParams);
				for (unsigned int i=0; i<numParams; i++)
				{
					printf("ParamName='%s', ParamType=", params[i].asChar());
					switch (shader->parameterType(params[i]))
					{
						case MHWRender::MShaderInstance::kInvalid:
							printf("'Invalid', ");
						break;
						case MHWRender::MShaderInstance::kBoolean:
							printf("'Boolean', ");
						break;
						case MHWRender::MShaderInstance::kInteger:
							printf("'Integer', ");
						break;
						case MHWRender::MShaderInstance::kFloat:
							printf("'Float', ");
						break;
						case MHWRender::MShaderInstance::kFloat2:
							printf("'Float2', ");
						break;
						case MHWRender::MShaderInstance::kFloat3:
							printf("'Float3', ");
						break;
						case MHWRender::MShaderInstance::kFloat4:
							printf("'Float4', ");
						break;
						case MHWRender::MShaderInstance::kFloat4x4Row:
							printf("'Float4x4Row', ");
						break;
						case MHWRender::MShaderInstance::kFloat4x4Col:
							printf("'Float4x4Col', ");
						break;
						case MHWRender::MShaderInstance::kTexture1:
							printf("'1D Texture', ");
						break;
						case MHWRender::MShaderInstance::kTexture2:
							printf("'2D Texture', ");
						break;
						case MHWRender::MShaderInstance::kTexture3:
							printf("'3D Texture', ");
						break;
						case MHWRender::MShaderInstance::kTextureCube:
							printf("'Cube Texture', ");
						break;
						case MHWRender::MShaderInstance::kSampler:
							printf("'Sampler', ");
						break;
						default:
							printf("'Unknown', ");
						break;
					}
					printf("IsArrayParameter='%s'\n", shader->isArrayParameter(params[i]) ? "YES" : "NO");
				}
				printf("END PARAM LIST\n");
				fflush(stdout);
			}
		}
	}
}



void shaveBrushOverride::populateGeometry(
	const MHWRender::MGeometryRequirements& requirements,
	const MHWRender::MRenderItemList& itemList,
		MHWRender::MGeometry& data
		)
{
	MHWRender::MVertexBuffer* positionBuffer = NULL;
	float* positions = NULL;

	const MHWRender::MVertexBufferDescriptorList& descList = requirements.vertexRequirements();
	int numVertexReqs = descList.length();
	MHWRender::MVertexBufferDescriptor desc;

#ifndef BRUSH_BY_SHADER
	int nsegs = 24;
	int nverts = nsegs*2;
#endif	

	for (int reqNum=0; reqNum<numVertexReqs; reqNum++)
	{
		if (!descList.getDescriptor(reqNum, desc))
		{
			continue;
		}

		switch (desc.semantic())
		{
			case MHWRender::MGeometry::kPosition:
			{
				positionBuffer = data.createVertexBuffer(desc);
				if (positionBuffer)
#ifdef BRUSH_BY_SHADER
						positions = (float*)positionBuffer->acquire(2);
#else
						positions = (float*)positionBuffer->acquire(nverts);
#endif
			}
			break;

			case MHWRender::MGeometry::kColor:
			case MHWRender::MGeometry::kTexture:
			case MHWRender::MGeometry::kNormal:
			case MHWRender::MGeometry::kTangent:
			case MHWRender::MGeometry::kBitangent:
			default:
			break;
		}
	}
	

	if (positions)
	{
#ifdef BRUSH_BY_SHADER
		
		M3dView	activeView = M3dView::active3dView();
		float H = (float)activeView.portHeight();
		float W = (float)activeView.portWidth();

		float x = (brush->mCX - 0.5f*W)/(0.5f*W);
		float y = (brush->mCY - 0.5f*H)/(0.5f*H);

		//MVector C(0.0f, 0.0f, 0.0f);
		MVector C(x, y, 0.0f);
		//MVector C(brush->mCX, brush->mCY, 0.0f);

		positions[0] = C.x;
		positions[1] = C.y;
		positions[2] = 0.0f;

		
		float w = brush->mRX/(0.5f*W);
		float h = brush->mRY/(0.5f*H);
		
		MVector P(w, h, 0.0f);
		//MVector P(brush->mRX, brush->mRY, 0.0f);

		positions[3] = P.x;
		positions[4] = P.y;
		positions[5] = 0.0f;
#else
		float R = 0.3f;

		M3dView	activeView = M3dView::active3dView();
		float H = (float)activeView.portHeight();
		float W = (float)activeView.portWidth();

		float x = (brush->mCX - 0.5f*W)/(0.5f*W);
		float y = (brush->mCY - 0.5f*H)/(0.5f*H);
		MVector C(x, y, 0.0f);

		float _w = brush->mRX/(0.5f*W);
		float _h = brush->mRY/(0.5f*H);
		MVector P(_w, _h, 0.0f);

		const float pi = 3.14159f;
		float dstep = 360.0f/nsegs;
		float rstep = dstep*pi/(180.0f);
		int pid = 0;
		for (int h = 0; h < nsegs; h++)
		{
			int hh = (h+1)%nsegs;
			float a1 = rstep*(float)h;
			float a2 = rstep*(float)hh;

			//float x1 = C.x + R*sin(a1);
			//float y1 = C.y + R*cos(a1);

			//float x2 = C.x + R*sin(a2);
			//float y2 = C.y + R*cos(a2);

			float x1 = (float)(C.x + P.x*sin(a1));
			float y1 = (float)(C.y + P.y*cos(a1));

			float x2 = (float)(C.x + P.x*sin(a2));
			float y2 = (float)(C.y + P.y*cos(a2));

			positions[pid++] = x1;
			positions[pid++] = y1;
			positions[pid++] = 0.0f;

			positions[pid++] = x2;
			positions[pid++] = y2;
			positions[pid++] = 0.0f;
		}
#endif
		positionBuffer->commit(positions);
	}

	// index data
	MHWRender::MIndexBuffer* indexBuffer = NULL; 
	int numItems = itemList.length();
	for (int i=0; i<numItems; i++)
	{
		const MHWRender::MRenderItem* item = itemList.itemAt(i);
		if (!item) continue;

		// Enable to debug vertex buffers that are associated with each render item.
		// Can also use to generate indexing better, but we don't need that here.
		// Also debugs custom data on the render item.
		static const bool debugStuff = false;
		if (debugShader)
		{
			const MHWRender::MVertexBufferDescriptorList& itemBuffers =item->requiredVertexBuffers();
			int numBufs = itemBuffers.length();
			MHWRender::MVertexBufferDescriptor desc;
			for (int bufNum=0; bufNum<numBufs; bufNum++)
			{
				if (itemBuffers.getDescriptor(bufNum, desc))
				{
					printf("Buffer Required for Item #%d ('%s'):\n", i, item->name().asChar());
					printf("\tBufferName: %s\n", desc.name().asChar());
					printf("\tDataType: %s (dimension %d)\n", MHWRender::MGeometry::dataTypeString(desc.dataType()).asChar(), desc.dimension());
					printf("\tSemantic: %s\n", MHWRender::MGeometry::semanticString(desc.semantic()).asChar());
					printf("\n");
				}
			}
			fflush(stdout);
		}

		//same indexing for all 
		if (item->name() == "shaveBrush")
		{
			if (!indexBuffer)
			{
				indexBuffer = data.createIndexBuffer(MHWRender::MGeometry::kUnsignedInt32);
				if (indexBuffer)
				{
#ifdef BRUSH_BY_SHADER
					unsigned int* buffer = (unsigned int*)indexBuffer->acquire(2);
					if (buffer)
					{
						buffer[0] = 0;
						buffer[1] = 1;
						indexBuffer->commit(buffer);
					}
#else
					unsigned int* buffer = (unsigned int*)indexBuffer->acquire(nsegs*2);
					if (buffer)
					{
						int idx = 0;
						for (int h = 0; h < nsegs; h++)
						{
							buffer[idx] = idx;
							buffer[idx+1] = idx+1;
							idx += 2;
						}
						indexBuffer->commit(buffer);
					}
#endif
				}
			}

			// Associate same index buffer with either render item
			if (indexBuffer)
			{
				item->associateWithIndexBuffer(indexBuffer);

			}
		}
	}

}

void shaveBrushOverride::cleanUp()
{

}