Update Python bindings, tests and example to use Python 3

This commit is contained in:
Matt Williams 2013-04-17 20:48:15 +01:00
parent 60826b4c85
commit cc430ae129
3 changed files with 30 additions and 20 deletions

View File

@ -1,4 +1,4 @@
#! /usr/bin/env python #! /usr/bin/env python3
################################################################################ ################################################################################
# Copyright (c) 2013 Matt Williams # Copyright (c) 2013 Matt Williams
@ -33,7 +33,7 @@ r = pv.Region(pv.Vector3Dint32_t(0,0,0), pv.Vector3Dint32_t(63,63,63))
vol = pv.SimpleVolumeuint8(r) vol = pv.SimpleVolumeuint8(r)
#Now fill the volume with our data (a sphere) #Now fill the volume with our data (a sphere)
v3dVolCenter = pv.Vector3Dint32_t(vol.getWidth() / 2, vol.getHeight() / 2, vol.getDepth() / 2) v3dVolCenter = pv.Vector3Dint32_t(vol.getWidth() // 2, vol.getHeight() // 2, vol.getDepth() // 2)
sphereRadius = 30 sphereRadius = 30
#This three-level for loop iterates over every voxel in the volume #This three-level for loop iterates over every voxel in the volume
for z in range(vol.getDepth()): for z in range(vol.getDepth()):
@ -95,20 +95,20 @@ def run():
screen = pygame.display.set_mode(SCREEN_SIZE, pygame.HWSURFACE|pygame.OPENGL|pygame.DOUBLEBUF) screen = pygame.display.set_mode(SCREEN_SIZE, pygame.HWSURFACE|pygame.OPENGL|pygame.DOUBLEBUF)
#The first thing we do is print some OpenGL details and check that we have a good enough version #The first thing we do is print some OpenGL details and check that we have a good enough version
print "OpenGL Implementation Details:" print("OpenGL Implementation Details:")
if glGetString(GL_VENDOR): if glGetString(GL_VENDOR):
print "\tGL_VENDOR:", glGetString(GL_VENDOR) print("\tGL_VENDOR: {}".format(glGetString(GL_VENDOR).decode()))
if glGetString(GL_RENDERER): if glGetString(GL_RENDERER):
print "\tGL_RENDERER:", glGetString(GL_RENDERER) print("\tGL_RENDERER: {}".format(glGetString(GL_RENDERER).decode()))
if glGetString(GL_VERSION): if glGetString(GL_VERSION):
print "\tGL_VERSION:", glGetString(GL_VERSION) print("\tGL_VERSION: {}".format(glGetString(GL_VERSION).decode()))
if glGetString(GL_SHADING_LANGUAGE_VERSION): if glGetString(GL_SHADING_LANGUAGE_VERSION):
print "\tGL_SHADING_LANGUAGE_VERSION:", glGetString(GL_SHADING_LANGUAGE_VERSION) print("\tGL_SHADING_LANGUAGE_VERSION: {}".format(glGetString(GL_SHADING_LANGUAGE_VERSION).decode()))
major_version = int(glGetString(GL_VERSION).split()[0].split('.')[0]) major_version = int(glGetString(GL_VERSION).decode().split()[0].split('.')[0])
minor_version = int(glGetString(GL_VERSION).split()[0].split('.')[1]) minor_version = int(glGetString(GL_VERSION).decode().split()[0].split('.')[1])
if major_version < 3 or (major_version < 3 and minor_version < 0): if major_version < 3 or (major_version < 3 and minor_version < 0):
print "OpenGL version must be at least 3.0 (found {0})".format(glGetString(GL_VERSION).split()[0]) print("OpenGL version must be at least 3.0 (found {0})".format(glGetString(GL_VERSION).decode().split()[0]))
#Now onto the OpenGL initialisation #Now onto the OpenGL initialisation
@ -121,7 +121,7 @@ def run():
#We create out shaders which do little more than set a flat colour for each face #We create out shaders which do little more than set a flat colour for each face
VERTEX_SHADER = shaders.compileShader(""" VERTEX_SHADER = shaders.compileShader(b"""
#version 130 #version 130
in vec4 position; in vec4 position;
@ -143,7 +143,8 @@ def run():
} }
""", GL_VERTEX_SHADER) """, GL_VERTEX_SHADER)
FRAGMENT_SHADER = shaders.compileShader("""
FRAGMENT_SHADER = shaders.compileShader(b"""
#version 130 #version 130
flat in float theColor; flat in float theColor;
@ -158,8 +159,8 @@ def run():
shader = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER) shader = shaders.compileProgram(VERTEX_SHADER, FRAGMENT_SHADER)
#And then grab our attribute locations from it #And then grab our attribute locations from it
glBindAttribLocation(shader, 0, "position") glBindAttribLocation(shader, 0, b"position")
glBindAttribLocation(shader, 1, "normal") glBindAttribLocation(shader, 1, b"normal")
#Create the Vertex Array Object to hold our volume mesh #Create the Vertex Array Object to hold our volume mesh
vertexArrayObject = GLuint(0) vertexArrayObject = GLuint(0)
@ -182,9 +183,9 @@ def run():
glDisableVertexAttribArray(0) glDisableVertexAttribArray(0)
#Now grab out transformation martix locations #Now grab out transformation martix locations
modelToWorldMatrixUnif = glGetUniformLocation(shader, "modelToWorldMatrix") modelToWorldMatrixUnif = glGetUniformLocation(shader, b"modelToWorldMatrix")
worldToCameraMatrixUnif = glGetUniformLocation(shader, "worldToCameraMatrix") worldToCameraMatrixUnif = glGetUniformLocation(shader, b"worldToCameraMatrix")
cameraToClipMatrixUnif = glGetUniformLocation(shader, "cameraToClipMatrix") cameraToClipMatrixUnif = glGetUniformLocation(shader, b"cameraToClipMatrix")
modelToWorldMatrix = np.array([[1.0,0.0,0.0,-32.0],[0.0,1.0,0.0,-32.0],[0.0,0.0,1.0,-32.0],[0.0,0.0,0.0,1.0]], dtype='f') modelToWorldMatrix = np.array([[1.0,0.0,0.0,-32.0],[0.0,1.0,0.0,-32.0],[0.0,0.0,1.0,-32.0],[0.0,0.0,0.0,1.0]], dtype='f')
worldToCameraMatrix = np.array([[1.0,0.0,0.0,0.0],[0.0,1.0,0.0,0.0],[0.0,0.0,1.0,-50.0],[0.0,0.0,0.0,1.0]], dtype='f') worldToCameraMatrix = np.array([[1.0,0.0,0.0,0.0],[0.0,1.0,0.0,0.0],[0.0,0.0,1.0,-50.0],[0.0,0.0,0.0,1.0]], dtype='f')

View File

@ -23,7 +23,7 @@ option(ENABLE_BINDINGS "Build Python bindings" ON)
if(ENABLE_BINDINGS) if(ENABLE_BINDINGS)
find_package(SWIG) find_package(SWIG)
mark_as_advanced(SWIG_DIR SWIG_VERSION) mark_as_advanced(SWIG_DIR SWIG_VERSION)
find_package(PythonLibs) find_package(PythonLibs 3)
if(CMAKE_VERSION VERSION_LESS "2.8.6") if(CMAKE_VERSION VERSION_LESS "2.8.6")
set_package_info(SWIG "Bindings generator" http://www.swig.org) set_package_info(SWIG "Bindings generator" http://www.swig.org)
set_package_info(PythonLibs "Programming language" http://www.python.org) set_package_info(PythonLibs "Programming language" http://www.python.org)
@ -43,6 +43,7 @@ if(ENABLE_BINDINGS)
set_source_files_properties(PolyVoxCore.i PROPERTIES CPLUSPLUS ON) set_source_files_properties(PolyVoxCore.i PROPERTIES CPLUSPLUS ON)
#set_source_files_properties(PolyVoxCore.i PROPERTIES SWIG_FLAGS "-builtin") #set_source_files_properties(PolyVoxCore.i PROPERTIES SWIG_FLAGS "-builtin")
set(SWIG_MODULE_PolyVoxCorePython_EXTRA_FLAGS "-py3")
swig_add_module(PolyVoxCorePython python PolyVoxCore.i) swig_add_module(PolyVoxCorePython python PolyVoxCore.i)
swig_link_libraries(PolyVoxCorePython ${PYTHON_LIBRARIES} PolyVoxCore) swig_link_libraries(PolyVoxCorePython ${PYTHON_LIBRARIES} PolyVoxCore)
set_target_properties(${SWIG_MODULE_PolyVoxCorePython_REAL_NAME} PROPERTIES OUTPUT_NAME _PolyVoxCore) set_target_properties(${SWIG_MODULE_PolyVoxCorePython_REAL_NAME} PROPERTIES OUTPUT_NAME _PolyVoxCore)

View File

@ -50,8 +50,16 @@ REMOVE_DEFINITIONS(-DQT_GUI_LIB) #Make sure the tests don't link to the QtGui
# Python tests # Python tests
IF(BUILD_BINDINGS) IF(BUILD_BINDINGS)
ADD_TEST(PythonSurfaceExtractorTest python ${CMAKE_CURRENT_SOURCE_DIR}/TestSurfaceExtractor.py) FIND_PACKAGE(PythonInterp 3)
ADD_TEST(PythonRaycastTest python ${CMAKE_CURRENT_SOURCE_DIR}/TestRaycast.py) IF(CMAKE_VERSION VERSION_LESS "2.8.6")
set_package_info(PythonInterp "Python Interpereter" http://www.python.org "Running the Python tests")
ELSE()
set_package_properties(PythonInterp PROPERTIES URL http://www.python.org DESCRIPTION "Python Interpereter" TYPE OPTIONAL PURPOSE "Running the Python tests")
ENDIF()
IF(PYTHONINTERP_FOUND)
ADD_TEST(PythonSurfaceExtractorTest ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/TestSurfaceExtractor.py)
ADD_TEST(PythonRaycastTest ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/TestRaycast.py)
ENDIF()
ENDIF() ENDIF()
# AmbientOcclusionGenerator tests # AmbientOcclusionGenerator tests