Friday, May 27, 2011

glDrawArrays example c/c++ java

Name

glDrawArrays - render primitives from array data

C Specification

void glDrawArrays(GLenum mode, GLint first, GLsizei count)

Parameters

mode
Specifies what kind of primitives to render. Symbolic constants GL_POINTS, GL_LINE_STRIP, GL_LINE_LOOP, GL_LINES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, and GL_TRIANGLES are accepted.
first
Specifies the starting index in the enabled arrays.
count
Specifies the number of indices to be rendered.

Description

glDrawArrays specifies multiple geometric primitives with very few subroutine calls. You can prespecify separate arrays of vertices, normals, colors, and texture coordinates and use them to construct a sequence of primitives with a single call to glDrawArrays.
When glDrawArrays is called, it uses count sequential elements from each enabled array to construct a sequence of geometric primitives, beginning with element first. mode specifies what kind of primitives are constructed, and how the array elements construct those primitives. If GL_VERTEX_ARRAY is not enabled, no geometric primitives are generated.
Vertex attributes that are modified by glDrawArrays have an unspecified value after glDrawArrays returns. For example, if GL_COLOR_ARRAY is enabled, the value of the current color is undefined after glDrawArrays executes. Attributes that aren't modified remain well defined.

Errors

GL_INVALID_ENUM is generated if mode is not an accepted value.
GL_INVALID_VALUE is generated if count 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

glClientActiveTexture, glColorPointer, glDrawElements, glNormalPointer, glTexCoordPointer, glVertexPointer

Example of glDrawArrays

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.}