Hi there,
I’ve been struggling for a long time now… and I can not see what is wrong with my code.
I’ve got a bunch of vertices that are modified from click input. I also have the texCoordinates that are modified from click input.
Finaly I draw all my elements.
I would like to know how to check the state of my opengl draw function and how to check for each line… meaning, how to be sure that the array I passed thru the various function are consistent. Here is the code :
-(void)ripple_redraw{
//no need to enable texture 2D, cocos do it
glBindTexture(GL_TEXTURE_2D, [texture_ name]);
// Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
// Needed states: GL_VERTEX_ARRAY,
// Unneeded states: GL_TEXTURE_2D, GL_TEXTURE_COORD_ARRAY, GL_COLOR_ARRAY
//by default in cocos2D
//glEnableClientState( GL_VERTEX_ARRAY );
//glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glVertexPointer ( 2, GL_FLOAT, sizeof( struct Ripple_VERTEX ), ripple_vertex[0][0].x );
glTexCoordPointer( 2, GL_FLOAT, sizeof( struct Ripple_VERTEX ), ripple_vertex[0][0].t );
glDrawElements ( GL_TRIANGLES, GRID_SIZE_X * GRID_SIZE_Y * 6, GL_UNSIGNED_SHORT, ripple_Indices );
}
Here is the init for my surfaces and vertices that I call before the draw :
-(void) initRipple{
posX = 0;
posY = 0;
CGSize winSize = [[CCDirector sharedDirector] winSize];
int i, j, index;
ripple_max = (int) sqrt( (float) ( 768 * 768 + 1024 * 1024 ) );
for ( i = 0; i < RIPPLE_COUNT; i++ )
{
t [i] = ripple_max + RIPPLE_LENGTH;
cx [i] = 0;
cy [i] = 0;
max[i] = 0;
}
for ( i = 0; i < GRID_SIZE_X; i++ )
{
for ( j = 0; j < GRID_SIZE_Y; j++ )
{
ripple_vertex[i][j].x [0] = i / ( GRID_SIZE_X - 1.f ) * gscrx;
ripple_vertex[i][j].x [1] = j / ( GRID_SIZE_Y - 1.f ) * gscry;
ripple_vertex[i][j].dt[0] = i / ( GRID_SIZE_X - 1.f );
ripple_vertex[i][j].dt[1] = j / ( GRID_SIZE_Y - 1.f );
}
}
for ( i = 0; i < GRID_SIZE_Y-1; i++ )
{
for ( j = 0; j < GRID_SIZE_X-1; j++ )
{
index = i * GRID_SIZE_X + j;
ripple_Indices[index * 6 + 0] = ( i ) * GRID_SIZE_X + j;
ripple_Indices[index * 6 + 1] = ( i + 1 ) * GRID_SIZE_X + j;
ripple_Indices[index * 6 + 2] = ( i + 1 ) * GRID_SIZE_X + j + 1;
ripple_Indices[index * 6 + 3] = ( i ) * GRID_SIZE_X + j;
ripple_Indices[index * 6 + 4] = ( i + 1 ) * GRID_SIZE_X + j + 1;
ripple_Indices[index * 6 + 5] = ( i ) * GRID_SIZE_X + j + 1;
}
}
[self ripple_reshape:winSize.width height:winSize.height];
[self schedule:@selector(ripple_dynamics:)];
NSLog(@"initRipple End");
}
Here is the init for GL :
-(void) initGL{
// ask director the the window size
//CGSize winSize = [[CCDirector sharedDirector] winSize];
//image = [CCSprite node];
//[image setPosition:ccp(winSize.width/2, winSize.height/2)];
//asign pixelData from loaded image it is equivalent to
[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage: @"image.jpg"];
if( texture ) {
CGRect rect = CGRectZero;
rect.size = texture.contentSize;
[texture_ release];
texture_ = [texture retain];
}
/*No need to call this, cocos2d do it.
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );*/
NSLog(@"initGL end");
}
Finaly if I had a simple test case in the draw like this :
ccVertex2F newPoint[5];
bool closePolygon = FALSE;
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindTexture(GL_TEXTURE_2D, [texture_ name]);
if( sizeof(CGPoint) == sizeof(ccVertex2F) ) {
// convert to pixels ?
if( CC_CONTENT_SCALE_FACTOR() != 1 ) {
memcpy( newPoint, vertices, 5 * sizeof(ccVertex2F) );
for( NSUInteger i=0; i<5;i++)
newPoint[i] = (ccVertex2F) { vertices[i].x * CC_CONTENT_SCALE_FACTOR(), vertices[i].y * CC_CONTENT_SCALE_FACTOR() };
glVertexPointer(2, GL_FLOAT, 0, newPoint);
} else
glVertexPointer(2, GL_FLOAT, 0, vertices);
}else {
for( NSUInteger i=0; i<5;i++)
newPoint[i] = (ccVertex2F) { vertices[i].x, vertices[i].y };
glVertexPointer(2, GL_FLOAT, 0, newPoint );
}
if( closePolygon )
glDrawArrays(GL_LINE_LOOP, 0, 5);
else
glDrawArrays(GL_LINE_STRIP, 0, 5);
I can see the texture drawn on the line between my vertices.
The whole thing is in 2D.
The thing is that I do not know where to look, I wish I could test my bunch of vertices and textures coordinates…
If you have cocos2d you can try to run a test from this file :
http://demo.ovh.net/fr/142c945fa1e9512ce5f70bd47fe7ae8f/
Thank you very much for any hints or infos
Have a nice day