How To Circumvent Matrix Stack Limits?

I have a subroutine that calls itself recursively to generate something that looks like a segmented worm. The routine is called with the number of segments I want to generate. Nested pushes and pops are involved. It works well until I get up to about 30 segments. Then things go haywire. I’m pretty sure this is because I’m overflowing the ModelView matrix stack. Has anyone found a workaround for this problem? Thanks.

Use your own stack, and glLoadMatrix the result, without any glPush/glPop.

If the worm is just a sequence of segments where each segment has just one parent/one child but no sibling, you don’t even need push and pop. Just on push before the first segment, one pop after the last segment to isolate the worm from the other objects/transform of your scene. You have a degenerated tree/hierarchy which turns to be just a flat list.

I thought of that but was hoping for something easier.

Bingo. You are correct and I am embarrassed. Should have realized this myself. Each segment has only a parent and a child. There are no siblings. I commented out the pushes and pops, and the routine works fine. Thanks. A screen capture showing 3 tentacles with 49 segments each should be attached …