Giovanni Durante
Giovanni Durante

Reputation: 15

window crash and partial rendering in opengl

When executed not all lines are printed on the window. Moreover the window crashes after glutSwapBuffers() in function showScreen. Here's an image : https://i.sstatic.net/xuFch.jpg

w, h, g = 1200, 900, 1

def plot(coordinates, stringa, g, Px, Px1):
    x, y = coordinates
    while Px >= Px1:
        glBegin(GL_LINES)
        if stringa[Px] == "A":
            glVertex2f(x, y)
            glVertex2f(x, y - g)
            y -= g
        elif stringa[Px] == "B":
            glVertex2f(x, y)
            glVertex2f(x + g, y)
            x += g
        elif stringa[Px] == "C":
            glVertex2f(x, y)
            glVertex2f(x, y + g)
            y += g
        elif stringa[Px] == "D":
            glVertex2f(x, y)
            glVertex2f(x - g, y)
            x -= g
        glEnd()
        Px -= 1

def iterate():
    glViewport(0, 0, 500, 500)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, 500, 0.0, 500, 0.0, 1.0)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

def showScreen(coordinates, stringa, g, Px, Px1):
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()
    iterate()
    glColor3f(1.0, 0.0, 3.0)
    plot(coordinates, stringa, g, Px, Px1)
    glutSwapBuffers()
    time.sleep(5)

glutInit()
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE |GLUT_RGBA)
glutInitWindowSize(w, h)
window = glutCreateWindow("dragon curve in Opengl")
glutDisplayFunc(showScreen([300, 400], stringa, x, len(stringa)-1, 0))
glutIdleFunc(showScreen([300, 400], stringa, x, len(stringa)-1, 0)) 
end = time.perf_counter()
print(f'Terminated in {round(end - start, 2)} seconds')
glutMainLoop() 

I get this error in the function showScreen after glutSwapBuffers()

freeglut (foo): Fatal error in program.  NULL display callback not permitted in GLUT 3.0+ or freeglut 2.0.1+

What does this error mean and how to fix it?

Upvotes: 1

Views: 190

Answers (2)

ContinuousLoad
ContinuousLoad

Reputation: 4912

The error means you called glutDisplayFunc but did not provide a function as the first argument.

I know it appears as you did, but take a closer look at this line:

glutDisplayFunc(showScreen(...))

Here's what happens:

  1. first, showScreen(...) is called and returns None
  2. This value is used as the first argument of glutDisplayFunc: glutDisplayFunc(None)
  3. An error occurs because glutDisplayFunc expects a function but got None

The same error will happen with glutIdleFunc, too.

To fix this for both of them, give glutDisplayFunc and glutIdleFunc just the name of the function:

glutDisplayFunc(showScreen)
glutIdleFunc(showScreen)

Now there is still a problem, because glut will not provide any arguments to showScreen. It will run it like this: showScreen().

This is a problem because your showScreen function expects a couple arguments. But it actually needs to be defined with none: def showScreen(): That means you will have to provide those variables in another way.

datenwolf is suggesting that you create a new function that accepts no arguments and just runs showScreen(...)

def doShowScreen():
  showScreen(showScreen([300, 400], stringa, x, len(stringa)-1, 0))

glutDisplayFunc(doShowScreen)
glutIdleFunc(doShowScreen)

Upvotes: 1

datenwolf
datenwolf

Reputation: 162164

The error is pretty much telling you what the problem is. You didn't supply a display function. This is what you wrote:

glutDisplayFunc(showScreen([300, 400], stringa, x, len(stringa)-1, 0))

What this does is, that it calls the function showScreen, and what is returned from this call is then passed to glutDisplayFunction. Your showScreen function doesn't return anything.

This is not what you want. You must either use a lambda to capture the parameters to show screen, or use a helper function. E.g. this

glutDisplayFunc(lambda: showScreen([300, 400], stringa, x, len(stringa)-1, 0))

However given the fact that you're passing in the viewport and other parameters to showScreen it is very unlikely, that this really what you want!

Upvotes: 4

Related Questions