1 /*
2  * Modification History
3  *
4  * 2006-December-18   Jason Rohrer
5  * Created.
6  */
7 
8 
9 #include "TexturedPanel.h"
10 
11 #include "minorGems/graphics/filters/BoxBlurFilter.h"
12 
13 #include "minorGems/util/random/StdRandomSource.h"
14 
15 extern StdRandomSource globalRandomSource;
16 
17 
18 
TexturedPanel(double inAnchorX,double inAnchorY,double inWidth,double inHeight,Color * inColor,char inVertical)19 TexturedPanel::TexturedPanel(
20     double inAnchorX, double inAnchorY, double inWidth,
21     double inHeight, Color *inColor, char inVertical )
22     :GUIPanelGL( inAnchorX, inAnchorY, inWidth, inHeight, inColor ),
23      mVertical( inVertical ) {
24 
25 
26     // generate texture
27     int textureSize = 64;
28 
29     Image *textureImage = new Image( textureSize, textureSize, 4, false );
30 
31     double *channels[4];
32 
33     int pixelsPerChannel = textureSize * textureSize;
34 
35     int i;
36     int p;
37     for( i=0; i<4; i++ ) {
38         channels[i] = textureImage->getChannel( i );
39         }
40 
41 
42     // for now, fill randomly
43 
44     double lowColorValue = 0.2;
45     double lowAlphaValue = 0.2;
46     for( p=0; p<pixelsPerChannel; p++ ) {
47 
48         channels[3][p] =
49             globalRandomSource.getRandomBoundedDouble( lowAlphaValue, 1 );
50         channels[0][p] =
51             globalRandomSource.getRandomBoundedDouble( lowColorValue, 1 );
52         channels[1][p] =
53             globalRandomSource.getRandomBoundedDouble( lowColorValue, 1 );
54         channels[2][p] =
55             globalRandomSource.getRandomBoundedDouble( lowColorValue, 1 );
56         }
57 
58 
59     // blur all channels
60     int blurRadius = 1;
61     BoxBlurFilter blur( blurRadius );
62     textureImage->filter( &blur );
63 
64     mBackgroundTexture = new SingleTextureGL( textureImage,
65                                               // wrap
66                                               true );
67 
68     delete textureImage;
69 
70     // set filter for texture to nearest neighbor
71     mBackgroundTexture->enable();
72     //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
73     //glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
74     mBackgroundTexture->disable();
75     }
76 
77 
78 
~TexturedPanel()79 TexturedPanel::~TexturedPanel() {
80 
81     delete mBackgroundTexture;
82     }
83 
84 
85 
fireRedraw()86 void TexturedPanel::fireRedraw() {
87     double alphaBottomRight, alphaTopRight, alphaBottomLeft, alphaTopLeft;
88 
89     double xTextureAnchorMax, yTextureAnchorMax;
90     if( mVertical ) {
91         alphaBottomRight = 0;
92         alphaTopRight = 0;
93 
94         alphaBottomLeft = 1;
95         alphaTopLeft = 1;
96 
97         xTextureAnchorMax = mWidth / mHeight;
98         yTextureAnchorMax = 1;
99         }
100     else {
101         alphaBottomRight = 1;
102         alphaTopRight = 0;
103 
104         alphaBottomLeft = 1;
105         alphaTopLeft = 0;
106 
107         xTextureAnchorMax = 1;
108         yTextureAnchorMax = mHeight / mWidth;
109         }
110 
111 
112     mBackgroundTexture->enable();
113 
114 	glBegin( GL_QUADS ); {
115         double widthToSkip = 0;
116 
117         if( !mVertical ) {
118             // don't draw in left corner to leave room for special block
119             widthToSkip = mHeight;
120             }
121         double widthToSkipFraction = widthToSkip / mWidth;
122 
123 
124         glColor4f( 1, 1, 1, alphaBottomLeft );
125         glTexCoord2f( widthToSkipFraction, 0 );
126 		glVertex2d( mAnchorX + widthToSkip, mAnchorY );
127 
128         glColor4f( 1, 1, 1, alphaBottomRight );
129         glTexCoord2f( xTextureAnchorMax, 0 );
130         glVertex2d( mAnchorX + mWidth, mAnchorY );
131 
132         glColor4f( 1, 1, 1, alphaTopRight );
133         glTexCoord2f( xTextureAnchorMax, yTextureAnchorMax );
134         glVertex2d( mAnchorX + mWidth, mAnchorY + mHeight );
135 
136         glColor4f( 1, 1, 1, alphaTopLeft );
137         glTexCoord2f( widthToSkipFraction, yTextureAnchorMax );
138         glVertex2d( mAnchorX + widthToSkip, mAnchorY + mHeight );
139 
140 
141         if( !mVertical ) {
142             // special block in lower-left corner
143 
144             // so that this panel blends with the left sidebar panel
145 
146             // a square block, mHeight by mHeight
147 
148             double widthFraction = mHeight / mWidth;
149 
150 
151             glColor4f( 1, 1, 1, alphaBottomLeft );
152             glTexCoord2f( 0, 0 );
153             glVertex2d( mAnchorX, mAnchorY );
154 
155             glColor4f( 1, 1, 1, alphaBottomRight );
156             glTexCoord2f( xTextureAnchorMax * widthFraction, 0 );
157             glVertex2d( mAnchorX + mHeight, mAnchorY );
158 
159             glColor4f( 1, 1, 1, alphaTopRight );
160             glTexCoord2f( xTextureAnchorMax * widthFraction,
161                           yTextureAnchorMax );
162             glVertex2d( mAnchorX + mHeight, mAnchorY + mHeight );
163 
164             glColor4f( 1, 1, 1, 1 );
165             glTexCoord2f( 0, yTextureAnchorMax );
166             glVertex2d( mAnchorX, mAnchorY + mHeight );
167             }
168 
169 
170         }
171 	glEnd();
172 
173 
174 
175 
176     mBackgroundTexture->disable();
177 
178 	// call the supercalss redraw
179 	GUIContainerGL::fireRedraw();
180     }
181