From dc49718352a284df3619884dcc0eccb8b0ed8273 Mon Sep 17 00:00:00 2001 From: Dorian Pula Date: Thu, 4 Sep 2014 08:47:15 -0400 Subject: [PATCH] First steps to drawing a checkboard with pieces using OpenGL. --- justcheckers/ui/game_view.py | 83 +++++++++++++++++++++--------------- requirements.txt | 2 +- 2 files changed, 49 insertions(+), 36 deletions(-) diff --git a/justcheckers/ui/game_view.py b/justcheckers/ui/game_view.py index ab5d36f..df48a92 100644 --- a/justcheckers/ui/game_view.py +++ b/justcheckers/ui/game_view.py @@ -19,60 +19,50 @@ from PySide import QtGui +from OpenGL import GL from PySide.QtOpenGL import QGLWidget + from OpenGL.GL import * from OpenGL.GLUT import * from OpenGL.GLU import * -# TODO Borrowed from: http://biospud.blogspot.ca/2012/02/proof-of-concept-opengl-program-in.html -def override(interface_class): - """ - Method to implement Java-like derived class method override annotation. - Courtesy of mkorpela's answer at - http://stackoverflow.com/questions/1167617/in-python-how-do-i-indicate-im-overriding-a-method - """ - def override(method): - assert(method.__name__ in dir(interface_class)) - return method - return override - - # Install freeglut3 for the example. class SphereTestGLWidget(QGLWidget): + "GUI rectangle that displays a teapot" - @override(QGLWidget) def initializeGL(self): - "runs once, after OpenGL context is created" - glEnable(GL_DEPTH_TEST) - glClearColor(1,1,1,0) # white background - glShadeModel(GL_SMOOTH) - glEnable(GL_COLOR_MATERIAL) - glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) - glMaterialfv(GL_FRONT, GL_SHININESS, [50.0]) - glLightfv(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]) - glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) - glLightfv(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) - glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [1.0, 1.0, 1.0, 0.0]) - glEnable(GL_LIGHTING) - glEnable(GL_LIGHT0) - self.orientCamera() - glutInit() - gluLookAt(0, 0, -10, # camera - 0, 0, 0, # focus - 0, 1, 0) # up vector + "runs once, after OpenGL context is created" + glEnable(GL_DEPTH_TEST) + glClearColor(1,1,1,0) # white background + glShadeModel(GL_SMOOTH) + glEnable(GL_COLOR_MATERIAL) + glMaterialfv(GL_FRONT, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) + glMaterialfv(GL_FRONT, GL_SHININESS, [50.0]) + glLightfv(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0]) + glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0]) + glLightfv(GL_LIGHT0, GL_SPECULAR, [1.0, 1.0, 1.0, 1.0]) + glLightModelfv(GL_LIGHT_MODEL_AMBIENT, [1.0, 1.0, 1.0, 0.0]) + glEnable(GL_LIGHTING) + glEnable(GL_LIGHT0) + self.orientCamera() + glutInit() + gluLookAt(0, 0, -10, # camera + 0, 0, 0, # focus + 0, 1, 0) # up vector + super(SphereTestGLWidget, self).initializeGL() - @override(QGLWidget) def paintGL(self): "runs every time an image update is needed" glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) self.paintTeapot() + super(SphereTestGLWidget, self).paintGL() - @override(QGLWidget) def resizeGL(self, w, h): "runs every time the window changes size" glViewport(0, 0, w, h) self.orientCamera() + super(SphereTestGLWidget, self).resizeGL(w, h) def orientCamera(self): "update projection matrix, especially when aspect ratio changes" @@ -94,6 +84,28 @@ class GameBoardWidget(QGLWidget): def __init__(self, parent): QGLWidget.__init__(self, parent) + def initializeGL(self): + GL.glEnable(GL.GL_DEPTH_TEST) + GL.glClearColor(0, 1, 0, 1) + super(GameBoardWidget, self).initializeGL() + + def paintGL(self): + GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) + self.paint_checkboard_state() + super(GameBoardWidget, self).paintGL() + + def resizeGL(self, width, height): + """Resizes the game board whenever the window is resized. + + :param width: The width of the newly sized QGLWidget. + :param width: The height of the newly sized of QGLWidget. + """ + GL.glViewport(0, 0, width, height) + super(GameBoardWidget, self).resizeGL(width, height) + + def paint_checkboard_state(self): + pass + class GameView(QtGui.QWidget): """Info viewer for the game's license, etc.""" @@ -106,7 +118,8 @@ class GameView(QtGui.QWidget): def setup_components(self): - self.game_board = SphereTestGLWidget(self) + # self.game_board = SphereTestGLWidget(self) + self.game_board = GameBoardWidget(self) exit_button = QtGui.QPushButton('Back to Menu', self) exit_button.setFixedHeight(50) diff --git a/requirements.txt b/requirements.txt index 001bb95..80a2b46 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ # User interface PySide==1.2.2 Markdown==2.4.1 -PyOpenGL +PyOpenGL>=3.0.0 # Core logic enum34==0.9.19