Debugging can often be very frustrating, but should be embraced as a normal development activity. The frustration often come from some complex entanglement that wont reveal itself. When this happen one need to use all the tricks of the trade. First one need to make sure ones own code is 100% correct. If one believes this to be true one can start to make a minimal demo that also display the same bug. At this time one can pass the demo to the next line of developers, if it may be colleagues or some third party support. But, there also come times when there is no one to pass this demo to. For instance when working on larger projects that’s been running a long time there can be hard to find developers that still understand the code. If the bug can’t be fixed, there is also the option to make workarounds. When doing workarounds one really need to have a good judgment in creating the workaround and document it so it will be easy to find and understand. In some complex distributed systems I’ve been working with some fun debugging sessions included tricks like disabling code with
#ifdef’s in a manual binary search way. Or those times when hunting some pointer errors by changing the order of variables. Well, it’s fun now long after, and a real lesson is that the harder the bug is the more stubborn developers are needed.
Recently I noticed a rendering problem in my AC3D render library when running on the iPhone 3GS. The problem was related to
GL_TRIANGLE_STRIP’s. The normals just seemed to be mixed. So first I dived deep into my own code and traced it heavily and reread OpenGL documentations making sure I got it right. But, fact was it run fine in the simulator and on the previous two iPhones and iPod touches. Could there actually be a bug in the iPhone 3GS OpenGL ES implementation? I was skeptical, but, I started to investigate if other programs show the same problem. I downloaded the GLGravity sample code and changed the shading model to
GL_FLAT. What? It did also render differently on the iPhone 3GS. It was easiest to see when viewing the teapot from above. Time to file a bug report.
Today it’s 2 month since I reported the bug. I can understand that some special OpenGL combination that few uses might not be that highly prioritized by Apple. So, this weekend I thought I could try to find a workaround. My gut feeling was that there might be a simple solution like just shifting the normals in their array. A little extra problem in my implementation is that the render library build interleaved arrays where several surfaces can be stored in a single array. But this is a later problem I thought and looked into making a demo of a simple cube of a single triangle strip. Said and done. But when running on the iPhone 3GS there was no problem!?! What? Well, each cube side is made of two triangles and when entering them I started with a single side so the first two triangles are on the same side. Quick try after moving the first triangle and normal last in the triangle strip showed the problem on the iPhone 3GS. Now I had a simple demo with a representation that could be used to investigate if a workaround could be made. The first tries of shifting the normals up and down the array failed. I was confused, my gut feeling was wrong.
My first thought was to make a more simplistic test to figure out the exact order the normals was used. This time I should be able to get the exact index of each normal used. My approach was to make an array of normals where all except one points in one direction and the other 90 degrees from the rest. Now it would show up as a single triangle differently shaded than the rest. Well, I also changed the Y position of the first vertex just to have an indicator where the triangle strip start. It worked perfectly in the simulator.
Feeling pretty satisfied with this approach I fired up the demo in my iPhone 3GS. What I saw definitely told me I reached the end of the road, I’m pretty stubborn I guess. When using the demo on the iPhone 3GS I could see that 2 (two!) triangles used the same normal, somehow it skip every other normal. This can’t be worked around, unless I disable the triangle strip generation all together for flat shaded triangle strips for the iPhone 3GS. Curious to run this demo? Grab the project here and run it on your iPhone 3GS
So, for now I recommend not doing flat shaded surfaces in AC3D when using our AC3 render library or if you are drawing OpenGL on the iPhone, stay away from flat shaded triangle strips (not a big sacrifice I guess)By Edward Patel, 05 Oct 2009