开发者

Using opengl, how do I combine a background image and a smaller distorted texture?

I am attempting to make an image stretch effect on the iphone. I make a vertex array and shift the texture data for each indice within an arbitrary radius of a background image. Then make the following calls to display:

int n = gridSize_.x * gridSize_.y;
glEnableClientState( GL_VERTEX_ARRAY);
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
glVertexPointer(3, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoordinates);
glDrawElements(GL_TRIANGLES, n*6, GL_UNSIGNED_SHORT, indices);

//if(num_myindices > 0)
// glDrawElements(GL_TRIANGLES, num_myindices*6, GL_UNSIGNED_SHORT, myindices);
glDisableClientState(GL_VERTEX_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );

My problem is the edge of area that is stretched looks very choppy with a relatively small grid size like 32x32 or even 64x64. Any larger makes it smoother but much much slower.

So I tried adding a copy of the background image, and开发者_StackOverflow restricted the vertex array to only the affected indices. I use a much smaller grid size but for a smaller area. The drawing code above now looks like:

//glDrawElements(GL_TRIANGLES, n*6, GL_UNSIGNED_SHORT, indices);
if(num_myindices > 0)
glDrawElements(GL_TRIANGLES, num_myindices*6, GL_UNSIGNED_SHORT,myindices);

The affected area is displayed but the background image appears to flash and disappear, my stretched area is floating on a black background. Obviously my background is rendered first and then my code above overwrites and renders only the data in my vertex array.

Does anyone have advice on how to efficiently combine the drawing of the images?

I plan to make a 2nd simple vertex array for the background and make a call to glDrawArray just prior to my if statement so all the drawing is done in a single method. Is this the right way to go about this? Is there a better way? Should all the image data be combined into 1 simple vertex array somehow?


Draw background on one set of primitives.

Draw "enchancments" on the second set of primitives, closer to the camera (if you use orthogonal projection, then its mater of changing value of one dimension).

Can't say why background flash and disappear. There may be many reasons. You forgot to clear opengl state correctly, there are depth conflicts, or any other large number of bugs.

But if you stick to drawing overlapping images on different sets of geometry on different "planes" then you should get effect you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜