PDA

View Full Version : texure mapping anti-alias



fireman2
12-24-2011, 01:50 PM
I have an 512 by 512 image like this:
http://farm8.staticflickr.com/7007/6565758341_f7c1339696_z.jpg
I mapped it to a 3D plane:

http://farm8.staticflickr.com/7013/6565758413_9ec5d54ca0_z.jpg
The location and perspective are all correct. But you can see that the edge is very jigsaw. Does not Open GL has anti-alias or I just did not turn it on?

I am on iPhone ios.

Here is my code:






CGContextRef spriteContext = CGBitmapContextCreate(spriteData, spriteWidth, spriteHeight, 8, spriteWidth * 4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);

CGContextTranslateCTM(spriteContext, 0, spriteHeight);

CGContextScaleCTM(spriteContext, 1, -1);

CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, (CGFloat)spriteWidth, (CGFloat)spriteHeight), spriteImage);

CGContextRelease(spriteContext);



glDeleteTextures(1, &spriteTexture);

glGenTextures(1, &spriteTexture);

// Bind the texture name.

glBindTexture(GL_TEXTURE_2D, spriteTexture);

// Set the texture parameters to use a minifying filter and a linear filer (weighted average)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_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 );



glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, spriteWidth, spriteHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);



glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

glClearColor(.0f, 0.0f, 0.0f, .0f);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



//Setup model view matrix

glMatrixMode(GL_MODELVIEW);



GLfloat teapot_vertices1[12];




glLoadMatrixf(m);





teapot_vertices1[0] = frames;

teapot_vertices1[1] = frameh;

teapot_vertices1[2] = 0;

teapot_vertices1[3] = frames + framel;;

teapot_vertices1[4] = frameh;

teapot_vertices1[5] = 0;

teapot_vertices1[6] = 0;

teapot_vertices1[7] = 0;

teapot_vertices1[8] = 0;

teapot_vertices1[9] = framel;

teapot_vertices1[10] = 0;

teapot_vertices1[11] = 0;







glVertexPointer(3 ,GL_FLOAT, 0, teapot_vertices1);






for(int i = 0; i < num_teapot_indices; i += new_teapot_indicies[i] + 1)

{

glDrawElements(GL_TRIANGLE_STRIP, new_teapot_indicies[i], GL_UNSIGNED_SHORT, &amp;new_teapot_indicies[i+1]);

}





//glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);



glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);

[context presentRenderbuffer:GL_RENDERBUFFER_OES];

malexander
12-24-2011, 06:08 PM
Linear interpolation doesn't produce good antialiasing of textures when the texture is much larger than the screen area it covers. Mipmapping is a better approach. Try creating several mip layers for the texture and set the min filter to GL_LINEAR_MIPMAP_LINEAR (trilinear filtering).

fireman2
12-25-2011, 12:51 PM
I tried your approach and now the edge inside is much better:
http://farm8.staticflickr.com/7150/6570573095_283185c307_z.jpg

But the border edge is still jigsaw. Any way we can fix this?

thank you very very much for your help.

Fireman

malexander
12-26-2011, 08:12 AM
Multisampling smooths out edges of geometry. I'm not sure what modes are available on iOS, but you need a framebuffer with multiple samples, or a multisample renderbuffer/texture with an FBO. Then call glEnable(GL_MULTISAMPLE) before drawing your geometry. This does cause more of a performance hit than trilinear filtering, however.