Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-25-2011, 01:12 PM   #1 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 104
Sakura is on a distinguished road
Default ColorPointer + DrawElements

Hi,

I have been using glColor4f() to set each of my sprites alpha value as well as RGB values... I am now trying to get rid of the glColor4f() calls and make each sprite (Quad) set it's own colors values using glColorPointer...

I am drawing sprites using glDrawElements which is using some sort of indices initialized as follows:

Code:
//COMMENT: 100 is the max quads number
indices = malloc( sizeof(indices[0]) * 100 * 6);
bzero( indices, sizeof(indices[0]) * 100 * 6);
for( NSUInteger i=0;i<100;i++) {
	indices[i*6+0] = i*4+0;
	indices[i*6+1] = i*4+1;
	indices[i*6+2] = i*4+2;
	indices[i*6+5] = i*4+1;
	indices[i*6+4] = i*4+2;
	indices[i*6+3] = i*4+3;			
}
The drawing routine is as follows:
Code:
glEnableClientState(GL_COLOR_ARRAY);

glBindTexture(GL_TEXTURE_2D,TextureName);
	
glColorPointer(4, GL_FLOAT,sizeof(Filters[0]), Filters);		

glVertexPointer(2, GL_FLOAT, 0, Vertices);

glTexCoordPointer(2, GL_FLOAT, 0, TexCoords);

glDrawElements(GL_TRIANGLES, DataNbr*6, GL_UNSIGNED_SHORT, indices);
		
DataNbr=0;  //How Many quads to be draw
Declaration of Filters:

Code:
typedef struct _CFilter {
	float r;
	float g;
	float b;
	float a;
} CFilter;

CFilter *Filters;
Adding a Quad to the vertex and coords arrays:
Code:
-(void)AddToVertices:(Quad2)ver AndTexCoord:(Quad2)cord Filter:(CFilter)fil{
	Vertices[DataNbr]=ver;
	TexCoords[DataNbr]=cord;
	Filters[DataNbr]=fil;	
	DataNbr++;
}
Quad declaration:
Code:
typedef struct _Quad2 {
	float tl_x, tl_y;
	float tr_x, tr_y;
	float bl_x, bl_y;
	float br_x, br_y;
} Quad2;
And finaly setting the filters as follow before passing it as argument to the -(void)AddToVertices: (Quad2)ver AndTexCoord: (Quad2)cord Filter Method
Code:
Quad2 Ver=[self CalculateVertices];
Quad2 TexCoord=[self CalculateTextCoords];

CFilter Colors;
Colors.a=0.5;
Colors.r=1;
Colors.g=1;
Colors.b=1;

[self AddToVertices:Ver AndTexCoord:TexCoord Filter:Colors]


Now when I use glColorf() everything works like a charm, but as soon as I enable the COLOR_ARRAY, I get a strange results...

Could anybody please help me and let me know what's wrong with my code... I've been struggling in order to make this work but I only made myself half crazy...

I would really apreciate any help. thank you
Sakura is offline   Reply With Quote
Old 02-25-2011, 02:54 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 234
warmi is on a distinguished road
Default

Quote:
Originally Posted by Sakura View Post
Hi,

I have been using glColor4f() to set each of my sprites alpha value as well as RGB values... I am now trying to get rid of the glColor4f() calls and make each sprite (Quad) set it's own colors values using glColorPointer...

I am drawing sprites using glDrawElements which is using some sort of indices initialized as follows:

Code:
//COMMENT: 100 is the max quads number
indices = malloc( sizeof(indices[0]) * 100 * 6);
bzero( indices, sizeof(indices[0]) * 100 * 6);
for( NSUInteger i=0;i<100;i++) {
	indices[i*6+0] = i*4+0;
	indices[i*6+1] = i*4+1;
	indices[i*6+2] = i*4+2;
	indices[i*6+5] = i*4+1;
	indices[i*6+4] = i*4+2;
	indices[i*6+3] = i*4+3;			
}
The drawing routine is as follows:
Code:
glEnableClientState(GL_COLOR_ARRAY);

glBindTexture(GL_TEXTURE_2D,TextureName);
	
glColorPointer(4, GL_FLOAT,sizeof(Filters[0]), Filters);		

glVertexPointer(2, GL_FLOAT, 0, Vertices);

glTexCoordPointer(2, GL_FLOAT, 0, TexCoords);

glDrawElements(GL_TRIANGLES, DataNbr*6, GL_UNSIGNED_SHORT, indices);
		
DataNbr=0;  //How Many quads to be draw
Declaration of Filters:

Code:
typedef struct _CFilter {
	float r;
	float g;
	float b;
	float a;
} CFilter;

CFilter *Filters;
Adding a Quad to the vertex and coords arrays:
Code:
-(void)AddToVertices:(Quad2)ver AndTexCoord:(Quad2)cord Filter:(CFilter)fil{
	Vertices[DataNbr]=ver;
	TexCoords[DataNbr]=cord;
	Filters[DataNbr]=fil;	
	DataNbr++;
}
Quad declaration:
Code:
typedef struct _Quad2 {
	float tl_x, tl_y;
	float tr_x, tr_y;
	float bl_x, bl_y;
	float br_x, br_y;
} Quad2;
And finaly setting the filters as follow before passing it as argument to the -(void)AddToVertices: (Quad2)ver AndTexCoord: (Quad2)cord Filter Method
Code:
Quad2 Ver=[self CalculateVertices];
Quad2 TexCoord=[self CalculateTextCoords];

CFilter Colors;
Colors.a=0.5;
Colors.r=1;
Colors.g=1;
Colors.b=1;

[self AddToVertices:Ver AndTexCoord:TexCoord Filter:Colors]


Now when I use glColorf() everything works like a charm, but as soon as I enable the COLOR_ARRAY, I get a strange results...

Could anybody please help me and let me know what's wrong with my code... I've been struggling in order to make this work but I only made myself half crazy...

I would really apreciate any help. thank you
What sort of strange results are you getting ?

Well, looking at your code this is generally how are you supposed to do it.
Now, you may want to verify that your Filters array which you pass to glColorPointer(4, GL_FLOAT,sizeof(Filters[0]), Filters); actually contains color values you specified ( just walk it and display floats).

Unrelated to your problem but as a general advice ... .don't use GL_FLOAT to specify colors .. it is a waste since it consumes 16 bytes per vertex while you get the same results with going with an unsigned int (GL_UNSIGNED_BYTE - 4 bytes per vertex).

This is even mentioned in Apple's own GLES guidelines.
warmi is offline   Reply With Quote
Old 02-25-2011, 03:32 PM   #3 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 104
Sakura is on a distinguished road
Default

Thank you very much for your answer... The strange result is that it seems that the sprite (which vertices are represented as Quad2) have R,G,B and Alpha value all mixed together (e.g: the upper half is redish and opaque while the bottom half is partialy transparent...)

I guess the problem is that I should specify one RGBA for each vertex in the Quad to be rendered... But I am not sure how to do this... the sprite is represented as four 2D vertices, but glDrawElements is actually rendering 6 of them (Something related to the Indices[ ] array initialization):

glDrawElements(GL_TRIANGLES, DataNbr*6, GL_UNSIGNED_SHORT, indices);

As for the issue of replacing GL_FLOAT with GL_UNSIGNED_BYTE... How I am supposed to specify an alpha value of 0.5 (float) using unisgned byte?


I am really sorry about my ignorance, and I really apreciate your kind help.
Sakura is offline   Reply With Quote
Old 02-25-2011, 03:50 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 234
warmi is on a distinguished road
Default

Quote:
Originally Posted by Sakura View Post
Thank you very much for your answer... The strange result is that it seems that the sprite (which vertices are represented as Quad2) have R,G,B and Alpha value all mixed together (e.g: the upper half is redish and opaque while the bottom half is partialy transparent...)

I guess the problem is that I should specify one RGBA for each vertex in the Quad to be rendered... But I am not sure how to do this... the sprite is represented as four 2D vertices, but glDrawElements is actually rendering 6 of them (Something related to the Indices[ ] array initialization):

glDrawElements(GL_TRIANGLES, DataNbr*6, GL_UNSIGNED_SHORT, indices);

As for the issue of replacing GL_FLOAT with GL_UNSIGNED_BYTE... How I am supposed to specify an alpha value of 0.5 (float) using unisgned byte?


I am really sorry about my ignorance, and I really apreciate your kind help.
glDrawElements only renders 4 unique vertices, you have six values there because you are rendering triangles(some vertices are shared) but if you examine your index values for this quad you will see that it refers only to 4 unique vertex offsets

As far as colors, you should simply replicate your color values for each vertex that is part of the quad.
In other words, if your quad definition contains 4 vertices - there should 4 colors with the same value replicated 4 times in the color array ( you can make them different if you want your quad to have some sort of gradient effect)

To convert a float into 4 fixed point values you can use code like this ( this is C++, get rid of static_cast<> to compile as C code):

Code:
		inline unsigned int rgba() const
		{
			unsigned char val8;
			unsigned int val32 = 0;

	
			val8 = static_cast<unsigned char>(a* 255);
			val32 = val8 << 24;

			val8 = static_cast<unsigned char>(b * 255);
			val32 += val8 << 16;

			val8 = static_cast<unsigned char>(g * 255);
			val32 += val8 << 8;

			val8 = static_cast<unsigned char>(r * 255);
			val32 += val8;

			return val32;
		}

PS.

Frankly for better performance you should convert everything into an interleaved array:

instead of :

VertexArray (v1,v2,v3)
ColorArray (c1,c2,c3)
UvArray (uv1,uv2,uv3)

switch to:

VertexArray ( v1,c1,uv1, v2,c2,uv2,v3,c3,uv3) etc

Last edited by warmi; 02-25-2011 at 03:53 PM.
warmi is offline   Reply With Quote
Old 02-26-2011, 08:26 AM   #5 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 104
Sakura is on a distinguished road
Default

Thanks a lot for your help. Your answers are always a guide for me to improve my engine further more.

The results of ColorPointer are correct, now that I set the color data for each 4 vertices of my Quad... I have also changed the colors data to unsigned bytes instead of floats.

Next thing for me to do would be working on the interleaved array as you suggested.

Thank you very much for you much appreciated help.
Sakura is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 423
7 members and 416 guests
chemistry, ChrisYates, hussain1982, Retouchable, skrew88, SLIC, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:13 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0