First steps to drawing a checkboard with pieces using OpenGL.

This commit is contained in:
Dorian 2014-09-04 08:47:15 -04:00
parent f9468b0562
commit dc49718352
2 changed files with 49 additions and 36 deletions

View File

@ -19,29 +19,18 @@
from PySide import QtGui from PySide import QtGui
from OpenGL import GL
from PySide.QtOpenGL import QGLWidget from PySide.QtOpenGL import QGLWidget
from OpenGL.GL import * from OpenGL.GL import *
from OpenGL.GLUT import * from OpenGL.GLUT import *
from OpenGL.GLU 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. # Install freeglut3 for the example.
class SphereTestGLWidget(QGLWidget): class SphereTestGLWidget(QGLWidget):
"GUI rectangle that displays a teapot" "GUI rectangle that displays a teapot"
@override(QGLWidget)
def initializeGL(self): def initializeGL(self):
"runs once, after OpenGL context is created" "runs once, after OpenGL context is created"
glEnable(GL_DEPTH_TEST) glEnable(GL_DEPTH_TEST)
@ -61,18 +50,19 @@ class SphereTestGLWidget(QGLWidget):
gluLookAt(0, 0, -10, # camera gluLookAt(0, 0, -10, # camera
0, 0, 0, # focus 0, 0, 0, # focus
0, 1, 0) # up vector 0, 1, 0) # up vector
super(SphereTestGLWidget, self).initializeGL()
@override(QGLWidget)
def paintGL(self): def paintGL(self):
"runs every time an image update is needed" "runs every time an image update is needed"
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
self.paintTeapot() self.paintTeapot()
super(SphereTestGLWidget, self).paintGL()
@override(QGLWidget)
def resizeGL(self, w, h): def resizeGL(self, w, h):
"runs every time the window changes size" "runs every time the window changes size"
glViewport(0, 0, w, h) glViewport(0, 0, w, h)
self.orientCamera() self.orientCamera()
super(SphereTestGLWidget, self).resizeGL(w, h)
def orientCamera(self): def orientCamera(self):
"update projection matrix, especially when aspect ratio changes" "update projection matrix, especially when aspect ratio changes"
@ -94,6 +84,28 @@ class GameBoardWidget(QGLWidget):
def __init__(self, parent): def __init__(self, parent):
QGLWidget.__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): class GameView(QtGui.QWidget):
"""Info viewer for the game's license, etc.""" """Info viewer for the game's license, etc."""
@ -106,7 +118,8 @@ class GameView(QtGui.QWidget):
def setup_components(self): 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 = QtGui.QPushButton('Back to Menu', self)
exit_button.setFixedHeight(50) exit_button.setFixedHeight(50)

View File

@ -3,7 +3,7 @@
# User interface # User interface
PySide==1.2.2 PySide==1.2.2
Markdown==2.4.1 Markdown==2.4.1
PyOpenGL PyOpenGL>=3.0.0
# Core logic # Core logic
enum34==0.9.19 enum34==0.9.19