1
\$\begingroup\$

I'm trying to start with OpenGL, using Python and PyGame.

I'm going to use PyGame instead of GLUT to do all the initializing, windows opening, input handling, etc.

However, my shaders are failing to compile, unless I specify exactly the version of OpenGL and profile.

They do compile with GLUT initialization from the book:

glutInit()
glutInitDisplayMode(GLUT_RGBA)
glutInitWindowSize(400, 400)

# this is what I need
glutInitContextVersion(3, 3)
glutInitContextProfile(GLUT_CORE_PROFILE)

glutCreateWindow("main")

But, with simple PyGame initialization like this:

pygame.init()
display = (400, 400)
pygame.display.set_mode(display, pygame.DOUBLEBUF|pygame.OPENGL)

which doesn't specify exact OpenGL version 3.3 and CORE_PROFILE, the same program would fail when trying to compile shaders:

RuntimeError: ('Shader compile failure (0): 0:2(10): error: GLSL 3.30 is not supported. Supported versions are: 1.10, 1.20, 1.30, 1.00 ES, and 3.00 ES\n', ['\n #version 330 core\n layout(location = 0) in vec4 position;\n void main()\n {\n gl_Position = position;\n }\n '], GL_VERTEX_SHADER)

My question is: how do I do this initialization with PyGame?

\$\endgroup\$
  • \$\begingroup\$ I just ran your three lines of PyGame code and they run without any error output?? Can you give a complete, runnable, minimal example showing the error? \$\endgroup\$ – Trilarion Sep 15 '16 at 14:10
1
\$\begingroup\$

Okay I don't think this can be done with standard pygame since it's built on top of SDL 1, and if SDL supports the GL core profile, it's not exposed in pygame from what i can tell.

However, there is a port of pygame to SDL 2 on github which is a drop in replacement which does expose the required attributes so you can specify a specific context.

So basically you will need the below changes, this got me a 3d triangle using 330 shaders on a linux laptop.

import pygame_sdl2
pygame_sdl2.init()
pygame_sdl2.display.gl_set_attribute(GL_ALPHA_SIZE, 8)
pygame_sdl2.display.gl_set_attribute(pygame_sdl2.GL_CONTEXT_MAJOR_VERSION, 3)
pygame_sdl2.display.gl_set_attribute(pygame_sdl2.GL_CONTEXT_MINOR_VERSION, 3)

pygame_sdl2.display.set_mode((self.viewport[2], self.viewport[3]), OPENGL|DOUBLEBUF)

Hope this helps.

\$\endgroup\$
  • \$\begingroup\$ Works for me. Note the constant in the third line should read pygame_sdl2.GL_ALPHA_SIZE, and the constants in the last line are also missing the pygame_sdl2 module prefix. Turns out pygame_sdl2 is slightly faster than glut on my system. \$\endgroup\$ – user127963 Apr 25 at 15:23
-1
\$\begingroup\$

Try the Kivy framework for Python. It's easy. Alternativley, I work on Ubuntu; compile with the command python main.py via the terminal source main.

    # -*- coding: utf-8 -*-
"""
Created on Thu Jan 15 21:08:24 2015

@author: fmannan
"""
from __future__ import division
import sys
import numpy as np
import scipy.misc
try:
  from OpenGL.GLUT import *
  from OpenGL.GL import *
  from OpenGL.GLU import *
except:
  print '''
ERROR: PyOpenGL not installed properly.
        '''
  sys.exit()

class GLRenderShape:
    def Triangle(self):
        glBegin(GL_TRIANGLES)
        glColor3f(0., 1., 1.)
        glVertex3f(-1, -1, 0.)
        glColor3f(1., 0., 1.)
        glVertex3f(1., -1., 0.)
        glColor3f(1., 1., 1.)
        glVertex3f(0, 1, 0.)
        glEnd()

class Scene:
    def __init__(self):
        self.quadric = gluNewQuadric()
        self.render_shapes = GLRenderShape()
        self.init()
        self.eye = [0.25, .25, -100]
        self.center = [0.2, -.3, 0]
        self.up = [0, 1, 0]



    def display(self):
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        glPushMatrix()
        glRotatef(45, 0., 0. , 1.)
        glScalef(0.5, 0.5, 0.5)
        self.render_shapes.Triangle();
        glPopMatrix()

        glPushAttrib(GL_COLOR_BUFFER_BIT)
        glColor3f(0.4, 0.2, 0.2)
        glPushMatrix()
        glTranslatef(0, .25, 0)
        glutSolidTeapot(0.25)
        glPopMatrix()
        glPopAttrib()

        glPushMatrix()
        glColor3f(1., 0., 0.)
        glTranslatef(-.5, -.5, -1)
        gluSphere(self.quadric, 0.5, 32, 32)
        glPopMatrix()

        glutSwapBuffers()

    def reshape(self, width, height):
        self.width = width
        self.height = height
        glViewport(0, 0, self.width, self.height)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()

    def keyboard(self, key, x, y ):
        if key == '\033':
            sys.exit()
        elif key == 's' or 'S':
            filename = 'a1.png'
            print('saving screen shot to %s' %filename)
            glReadBuffer(GL_FRONT)
            im = glReadPixels(0, 0, self.width, self.height, 
                                   GL_RGBA, GL_UNSIGNED_INT)
            scipy.misc.imsave('a1.png', np.flipud(im))
            im_depth = glReadPixels(0, 0, self.width, self.height, GL_DEPTH_COMPONENT, GL_FLOAT)
            scipy.misc.imsave('test_depth.png', np.flipud(im_depth))

#            import matplotlib.pyplot as plt
#            plt.imshow(np.flipud(im_depth))
#            plt.show()
            print('done')

    def mouse(self, button, state, x, y):
        print(button, state, x, y)

    def motion(self, x, y):
        #print(x, y)
        pass

    def init(self):
        glClearColor(0.0, 0.0, 0.0, 1.0)
        glEnable(GL_DEPTH_TEST)


if __name__ == '__main__':
    glutInit()
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA)
    w1 = glutCreateWindow('COMP557 Sample')
    glutReshapeWindow(512,512)
    scene = Scene()
    glutReshapeFunc(scene.reshape)
    glutDisplayFunc(scene.display)
    glutKeyboardFunc(scene.keyboard)
    glutMouseFunc(scene.mouse)
    glutMotionFunc(scene.motion)
    scene.init()
    glutMainLoop()
\$\endgroup\$
  • 2
    \$\begingroup\$ "Use my favourite framework instead, it's really easy!" followed by a bunch of deprecated GL 1.1 code is not being helpful to an OP who wants a GL 3.3 core context. \$\endgroup\$ – Maximus Minimus Feb 14 '17 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.