Friday, May 27, 2011

glScissor example c/c++ java objc

Name

glScissor - define the scissor box

C Specification

void glScissor(GLint x, GLint y, GLsizei width, GLsizei height)

Parameters

x, y
Specify the lower left corner of the scissor box, in pixels. The initial value is (0, 0).
width, height
Specify the width and height of the scissor box. When a GL context is first attached to a surface (e.g. window), width and height are set to the dimensions of that surface.

Description

glScissor defines a rectangle, called the scissor box, in window coordinates. The first two arguments, x and y, specify the lower left corner of the box. width and height specify the width and height of the box.
To enable and disable the scissor test, call glEnable and glDisable with argument GL_SCISSOR_TEST. The scissor test is initially disabled. While scissor test is enabled, only pixels that lie within the scissor box can be modified by drawing commands. Window coordinates have integer values at the shared corners of frame buffer pixels. glScissor(0, 0, 1, 1) allows modification of only the lower left pixel in the window, and glScissor(0, 0, 0, 0) doesn't allow modification of any pixels in the window.
When the scissor test is disabled, it is as though the scissor box includes the entire window.

Errors

GL_INVALID_VALUE is generated if either width or height is negative.

Copyright

Copyright © 2003 Silicon Graphics, Inc.
This document is licensed under the SGI Free Software B License. For details, see http://oss.sgi.com/projects/FreeB/.

See Also

glEnable, glViewport

Example of glScissor

001.static KDvoid LoadFont ( KDvoid )
002.{
003.XM_GET_TLS ( XMTLS, tls )
004. 
005.FT_Library       lib;
006.FT_Face          face;
007. 
008.GLubyte*         pixels;
009. 
010.GLuint           tex_w;
011.GLuint           tex_h;
012. 
013.GLint            it;   
014.GLuint           x, y;
015. 
016.if ( FT_Init_FreeType ( &lib ) )
017.{
018.XM_ASSERT_FAIL ( "* FT_Init_FreeType failed." );
019.}
020. 
021.if ( FT_New_Face ( lib, "/res/COOPBL.TTF", 0, &face ) )
022.{
023.XM_ASSERT_FAIL ( "* FT_New_Face failed. " );
024.}
025. 
026.FT_Set_Char_Size ( face, XM_CHAR_HEIGHT << 6, XM_CHAR_HEIGHT << 6, 96, 96 );
027. 
028.glGenTextures ( XM_CHAR_NUMBER, tls->fnt );
029. 
030.for ( it = 0; it < XM_CHAR_NUMBER; it++ )
031.{
032.if ( FT_Load_Char ( face, it, FT_LOAD_RENDER ) )
033.{
034.XM_ASSERT_FAIL ( "* FT_Load_Char failed. " );
035.}
036. 
037.tls->fnt_w[ it ] = face->glyph->bitmap.width; 
038.tls->fnt_h[ it ] = face->glyph->bitmap.rows;
039. 
040.tls->fnt_ax[ it ] = face->glyph->advance.x >> 6;
041.tls->fnt_ay[ it ] = face->glyph->advance.y >> 6;
042. 
043.tls->fnt_top[ it ] = face->glyph->bitmap_top;
044. 
045.tex_w = 1;
046.tex_h = 1;
047. 
048.while ( tex_w < tls->fnt_w[ it ] ) tex_w = tex_w << 1;
049.while ( tex_h < tls->fnt_h[ it ] ) tex_h = tex_h << 1;
050. 
051.pixels = ( GLubyte* ) kdMalloc ( tex_w * tex_h );
052.if ( !pixels )
053.{
054.XM_ASSERT_FAIL ( "* Memory allocation failed" );
055.}
056. 
057.for ( y = 0; y < tex_h; y++ )
058.{
059.for ( x = 0; x < tex_w; x++ )
060.{              
061.pixels[ y * tex_w + x ] = x >= tls->fnt_w[ it ] || y >= tls->fnt_h[ it ] ? 0 : face->glyph->bitmap.buffer[ y * face->glyph->bitmap.pitch + x ];
062.}
063.}
064. 
065.glBindTexture   ( GL_TEXTURE_2D, tls->fnt[ it ] );
066.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
067.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
068.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
069.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
070. 
071.glTexImage2D ( GL_TEXTURE_2D, 0, GL_ALPHA, tex_w, tex_h, 0, GL_ALPHA, GL_UNSIGNED_BYTE, pixels );
072. 
073.kdFree ( pixels );
074.}
075. 
076.FT_Done_Face ( face );
077.FT_Done_FreeType ( lib );
078.}
079. 
080.KDvoid xmEventCreate ( KDvoid )
081.{
082.XM_SET_TLS ( XMTLS )
083. 
084.glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );  
085.glClearColor ( 0.0f, 0.0f, 0.25f, 1.0f );  
086.glColor4f ( 0.0f, 0.8f, 0.2f, 1.0f );
087. 
088.glEnable ( GL_BLEND );
089.glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );   
090. 
091.LoadFont ( );
092.}
093. 
094.KDvoid xmEventDestroy ( KDvoid )
095.{  
096.XM_GET_TLS ( XMTLS, tls )
097. 
098.glDeleteTextures ( XM_CHAR_NUMBER, tls->fnt );
099.}
100. 
101.KDvoid xmEventRedraw ( KDvoid )
102.{
103.XM_GET_TLS ( XMTLS, tls )
104. 
105.const KDchar*  str = "XMFT2 : FreeType v2.4.2";
106. 
107.GLint      it;
108.GLint      len;
109.GLint      off;
110.GLfloat    pos_x;
111.GLfloat    pos_y;
112. 
113.GLuint     tw, th;
114.GLfloat    vw, vh;
115.GLfloat    cw, ch;
116. 
117.glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
118.glLoadIdentity ( );
119. 
120.glTranslatef ( 40.0f, ( XM_SCREEN_H - XM_CHAR_HEIGHT ) / 2.0f, 0.0f );
121.glTranslatef ( 0.0f, kdSinf ( tls->trans ) / 4.0f * XM_SCREEN_H, 0.0f );
122. 
123.glEnable ( GL_TEXTURE_2D );
124.glEnableClientState ( GL_VERTEX_ARRAY );
125.glEnableClientState ( GL_TEXTURE_COORD_ARRAY );
126. 
127.pos_x = 0;
128.len = kdStrlen ( str );
129. 
130.for ( it = 0; it < len; it++ )
131.{
132.off = str[ it ];
133. 
134.vw = ( GLfloat ) tls->fnt_w[ off ];
135.vh = ( GLfloat ) tls->fnt_h[ off ];
136. 
137.tw = 1;
138.th = 1;
139. 
140.while ( tw < tls->fnt_w[ off ] ) tw = tw << 1;
141.while ( th < tls->fnt_h[ off ] ) th = th << 1;
142. 
143.cw = vw / tw;
144.ch = vh / th;
145. 
146.pos_y = ( GLfloat ) tls->fnt_top[ off ];
147. 
148.{
149.GLfloat    arr_vertex[] =
150.{
151.pos_x +  0, pos_y +  0, 0,
152.pos_x +  0, pos_y - vh, 0,
153.pos_x + vw, pos_y - vh, 0,
154.pos_x + vw, pos_y +  0, 0,         
155.};
156. 
157.GLfloat    arr_coord[] =
158.{
159.0,  0,
160.0, ch,
161.cw, ch,
162.cw,  0,
163.};
164. 
165.glBindTexture ( GL_TEXTURE_2D, tls->fnt[ off ] );           
166. 
167.glVertexPointer   ( 3, GL_FLOAT, 0, arr_vertex );
168.glTexCoordPointer ( 2, GL_FLOAT, 0, arr_coord  );
169. 
170.glDrawArrays ( GL_TRIANGLE_FAN, 0, 4 );
171. 
172.pos_x += tls->fnt_ax[ off ];
173.}
174.}
175. 
176.glDisableClientState ( GL_TEXTURE_COORD_ARRAY );
177.glDisableClientState ( GL_VERTEX_ARRAY );
178.glDisable ( GL_TEXTURE_2D );
179. 
180.glFlush ( );
181.}
182. 
183.KDvoid xmEventUpdate ( KDuint msec )
184.{
185.XM_GET_TLS ( XMTLS, tls )
186. 
187.if ( msec - tls->msec > 20 )
188.{
189.tls->msec = msec;   
190.tls->trans += 0.1f;
191.}  
192.}
193. 
194.KDvoid xmEventResize ( KDint width, KDint height )
195.{
196.XM_GET_TLS ( XMTLS, tls )
197. 
198.tls->wnd_w = width;
199.tls->wnd_h = height;
200. 
201.glViewport ( 0, 0, width, height );
202.glScissor  ( 0, 0, width, height );
203. 
204.glMatrixMode ( GL_PROJECTION );
205.glLoadIdentity ( );
206. 
207.glOrthof ( 0, XM_SCREEN_W, 0, XM_SCREEN_H, -1.0f, 100.0f );
208. 
209.glMatrixMode ( GL_MODELVIEW );
210.glLoadIdentity ( );
211.}