Re: ERROR: Function <glutSwapBuffers> called with no current window defined.

Ian Mallett <[email protected]> Mon, 10 Jun 2013 11:55:54 -0700
Newsgroups gmane.comp.python.opengl.user
Message-ID <CAG4NV=sOxiuuqPEonT3OWEZQs4zJcRCFEuDVdf3LW-AEpaCZWw@mail.gmail.com>
Try the attached. I also added a few small changes to make it Python 3
compatible and more best-practicey for OpenGL.
Ian

------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev

_______________________________________________
PyOpenGL Homepage
http://pyopengl.sourceforge.net
_______________________________________________
PyOpenGL-Users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyopengl-users
a.py (application/octet-stream, 4.9 KB)
#! /usr/bin/env python
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
 
from OpenGL.GL.ARB.shader_objects import *
from OpenGL.GL.ARB.vertex_shader import *
from OpenGL.GL.ARB.fragment_shader import *
 
import time, sys
 
program = None
 
def compileShader( source, shaderType ):
    """Compile shader source of given type"""
    shader = glCreateShaderObjectARB(shaderType)
    print("glShaderSourceARB:"+str(bool(glShaderSourceARB)))
    glShaderSourceARB(shader, [source]) #AMD cards in particular require this array syntax
    glCompileShaderARB(shader)
    return shader
 
def compileProgram(vertexSource=None, fragmentSource=None):
    program = glCreateProgramObjectARB()
    if vertexSource:
        vertexShader = compileShader(vertexSource, GL_VERTEX_SHADER_ARB)
        glAttachObjectARB(program, vertexShader)
    if fragmentSource:
        fragmentShader = compileShader(fragmentSource, GL_FRAGMENT_SHADER_ARB)
        glAttachObjectARB(program, fragmentShader)
    glValidateProgramARB(program)
    glLinkProgramARB(program)
    if vertexShader:
        glDeleteObjectARB(vertexShader)
    if fragmentShader:
        glDeleteObjectARB(fragmentShader)
    return program
 
def InitGL(Width, Height):                
    glClearColor(0.0, 0.0, 0.0, 0.0)    
    glClearDepth(1.0)                    
    glDepthFunc(GL_LESS)              
    glEnable(GL_DEPTH_TEST)              
    glShadeModel(GL_SMOOTH)                
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()                    
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
 
    if not glInitShaderObjectsARB():
        print("Missing Shader Objects!")
        sys.exit(1)
    if not glInitVertexShaderARB():
        print("Missing Vertex Shader!")
        sys.exit(1)
    if not glInitFragmentShaderARB():
        print("Missing Fragment Shader!")
        sys.exit(1)
 
    global program
    program = compileProgram(
        """
           varying vec3 normal;
           void main(void) {
               normal = gl_NormalMatrix * gl_Normal;
               gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
           }
        """,
        """
           varying vec3 normal;
           void main(void) {
               float intensity;
               vec4 color;
               vec3 n = normalize(normal);
               vec3 l = normalize(gl_LightSource[0].position).xyz;
         
               // quantize to 5 steps (0, .25, .5, .75 and 1)
               intensity = (floor(dot(l, n) * 4.0) + 1.0)/4.0;
               color = vec4(intensity*1.0, intensity*0.5, intensity*0.5,
                   intensity*1.0);
         
               gl_FragColor = color;
           }
        """
    )
 
 
def ReSizeGLScene(Width, Height):
    if Height == 0:                        
        Height = 1
    glViewport(0, 0, Width, Height)        
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width)/float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
 
def DrawGLScene():
 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLoadIdentity()                    
    glTranslatef(-1.5, 0.0, -6.0)
    if program:
        glUseProgramObjectARB(program)
    glutSolidSphere(1.0,32,32)
    glTranslate( 1,0,2 )
    glutSolidCube( 1.0 )
    glutSwapBuffers()
   
def clear_gl_callbacks():
     glutDisplayFunc(DrawGLScene)
     glutMotionFunc(None)
     glutKeyboardFunc(keyPressed)
 
def start_game_mode():
     if glutGameModeGet(GLUT_GAME_MODE_ACTIVE):
         return # already in game mode
     glutGameModeString("1024x768:32@75")
     if glutGameModeGet(GLUT_GAME_MODE_POSSIBLE):
         clear_gl_callbacks()
         glutEnterGameMode()
         DrawGLScene()
 
def start_windowed_mode():
     if glutGameModeGet(GLUT_GAME_MODE_ACTIVE):
         clear_gl_callbacks()
         glutLeaveGameMode()
         DrawGLScene()
         glutSetWindow(window)
 
def keyPressed(key, x, y):
     if key == '\033':
         if glutGameModeGet(GLUT_GAME_MODE_ACTIVE):
             start_windowed_mode()
     elif key == "f":
         if glutGameModeGet(GLUT_GAME_MODE_ACTIVE):
             start_windowed_mode()
         else:
             start_game_mode()
def main():
    global window
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH)
    window = glutCreateWindow(b"Test glut game ")
    glutInitWindowPosition(100, 100)
    glutInitWindowSize(1024, 768)
    glutInitWindowPosition(0, 0)
    glutGameModeString(b"1024x768:32@75")
    glutEnterGameMode()
    glutDisplayFunc(DrawGLScene)
    glutIdleFunc(DrawGLScene)
    glutReshapeFunc(ReSizeGLScene)
    glutKeyboardFunc(keyPressed)
    InitGL(1024, 768)
    glutMainLoop()
 
if __name__ == "__main__":
    print("Hit ESC key to quit.")
    main()