3

I am new to opengl programming, but I am doing something very basic, and the difference of quality between a custom opengl code and cocos2d is huge!

I am trying just to load an image and continuously rotate it every frame. With my code, I get a lot of flickering, sharp edges, while cocos2d has it all nice and smooth. I've set up 4x Multi-Sampling Anti-Aliasing using apple's recommended code for iOs 4 on my code, and still it looks very bad in comparison to cocos2d without any MSAA.

You can see the differences here: custom opengl code (with MSAA): custom opengl code

cocos2D (without MSAA): cocos2d

Does anyone know what am I missing to be able to achieve such smooth graphics? By looking at cocos2d code, I found some references that linked aliasing to GL_LINEAR. I've added GL_LINEAR parameters to my textures just like cocos, but it's still looking equally bad.

3
  • Just out of interest, are you learning OpenGL out of curiosity, or is there something you'd like to do that Cocos2D can't? Commented Oct 27, 2011 at 7:45
  • @Danyal, I am also learning OpenGL, but I want to avoid at all costs using cocos2d and objective c because of 1) portability issues (avoiding objective c so I can port the code to android and maybe even webgl) and 2) the language barrier that objective c would impose to me. I understand its basic concepts, but I really don't like the way it works. Commented Oct 27, 2011 at 17:25
  • 1
    Thanks @Waneck, as you can probably tell I was wondering whether I should learn it too! By the way have you seen Cocos2d-x? It is a C++ version of Cocos2d with an Android port. But I guess it won't be without its platform-specific bugs. Commented Oct 28, 2011 at 7:01

1 Answer 1

3

Anti-aliasing does exactly what the name says: it prevents primitives from assuming aliases, such as a straight (diagonal) line turning into a staircase. Because anti-aliasing usually results in 'soft' edges, the term is sometimes used to apply to any algorithmic 'softening', but it's incorrect to do so.

Assuming your source texture already contains some anti-aliasing to render the curved edges of your car onto a pixel grid (so, if you opened the source PNG or whatever in an art program and zoomed in on the edges you'd see some softness), I think that your code is failing to apply multisampling for whatever reason. If you zoom in and look at the top edge of the roof then check out the transition between the very top of the step one in from the right and the one to its left. The harsh dark colour at the top just spontaneously steps up a pixel. That's symptomatic of that edge being in the original texture and it being copied out pixel by pixel.

GL_LINEAR is a filtering parameter that affects how OpenGL answer questions like 'if the source pixel at (0, 0) is one colour and at (0, 1) is another then what colour is at (0, 0.5)?' If you apply linear filtering then when you scale your texture above its native size the extra pixels that OpenGL has to invent will be created using a linear combination of the nearest source pixels. If you'd used GL_NEAREST then it'd be the colour of whichever source pixel is nearest. So that's the difference between textures that scale up to look blurry and low contrast and textures that scale up to look like mosaics with obvious pixels. So it (usually) adds blurriness and softness to the image overall but isn't really anything to do with anti-aliasing.

With respect to why you're not getting anti-aliasing, two possible reasons sprint to mind. You may have some error in your code or you may simply be using a different algorithm from Cocos2D. Apple's hardware multisampling support arrived only in iOS 4 and Cocos2D predates that so may be sticking to a 'software' method (specifically, rendering the whole scene pixel-by-pixel at 4x the size, then getting the GPU to scale it down). The latter would be significantly slower but would prevent the hardware from attempting to optimise the process. One optimisation that some hardware sometimes applies is to multisample only at the edges of geometry (approximately). That obviously wouldn't benefit you at all.

Another possibility is that you're scaling your image down when you draw (though it doesn't look like it) and Cocos2D is generating mip maps whereas you're not. Mip maps precompute certain scales of image and work from there when drawing to the screen. Doing it that way allows a more expensive algorithm to be applied and tends to lead to less aliasing.

Can you post some code?

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Tommy! Thank you very much for your detailed response. I really appreciate it, and I will add the code later today, when I'm back from work. For now I can say that the textures' size is the same as they appear on screen, so I don't think it's a mip mapping issue. As about a software mode, it could really be, though I failed to found any reference to it on the cocos2d source code. I'll post the code when I'm back home! Thank you very much!
Tommy, it seems the problem was really with GL_LINEAR ! I was re-binding the texture later with GL_NEAREST! Oh wow! It makes so much difference! Thank you VERY much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.