|
| 1 | + |
| 2 | +#Based on the PyVRML97 module, copyright (c) 1995-2012, Michael C. Fletcher and Contributors |
| 3 | +import math |
| 4 | + |
| 5 | +def rotMatrix(x, y, z, a): |
| 6 | + """Generate an opengl rotation matrix |
| 7 | + x,y,z -- rotational vector axis |
| 8 | + a -- angle in radians |
| 9 | + """ |
| 10 | + # normalize the rotation vector! |
| 11 | + squared = x*x + y*y + z*z |
| 12 | + if squared != 1.0: |
| 13 | + length = squared ** .5 |
| 14 | + x /= squared |
| 15 | + y /= squared |
| 16 | + z /= squared |
| 17 | + c = math.cos( a ) |
| 18 | + c1 = math.cos( -a ) |
| 19 | + s = math.sin( a ) |
| 20 | + s1 = math.sin( -a ) |
| 21 | + t = 1-c |
| 22 | + R = [ |
| 23 | + [ t*x*x+c, t*x*y+s*z, t*x*z-s*y, 0.], |
| 24 | + [ t*x*y-s*z, t*y*y+c, t*y*z+s*x, 0.], |
| 25 | + [ t*x*z+s*y, t*y*z-s*x, t*z*z+c, 0.], |
| 26 | + [ 0., 0., 0., 1.] |
| 27 | + ] |
| 28 | + return R |
| 29 | + |
| 30 | +def scaleMatrix(x, y, z): |
| 31 | + """Generate scale matrix |
| 32 | + x,y,z -- scale vector |
| 33 | + """ |
| 34 | + S = [ [x,0.,0.,0.], [0.,y,0.,0.], [0.,0.,z,0.], [0.,0.,0.,1.] ] |
| 35 | + return S |
| 36 | + |
| 37 | +def transMatrix(x, y, z): |
| 38 | + """Generate translation matrix |
| 39 | + x,y,z -- scale vector |
| 40 | + """ |
| 41 | + T = [ [1.,0.,0.,0.], [0.,1.,0.,0.], [0.,0.,1.,0.], [x,y,z,1] ] |
| 42 | + return T |
| 43 | + |
| 44 | +def identityMatrix(): |
| 45 | + return [[1.,0.,0.,0.], [0.,1.,0.,0.], [0.,0.,1.,0.], [0.,0.,0.,1.]] |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + #Check functions against opengl API |
| 49 | + from OpenGL.GLUT import * |
| 50 | + from OpenGL.GLUT.freeglut import * |
| 51 | + import OpenGL.GL as gl |
| 52 | + import numpy as np |
| 53 | + import math |
| 54 | + |
| 55 | + glutInit(sys.argv) |
| 56 | + glutInitDisplayMode(GLUT_RGBA) |
| 57 | + glutInitWindowSize(640, 480) |
| 58 | + window = glutCreateWindow("Hello world!") |
| 59 | + |
| 60 | + gl.glMatrixMode(gl.GL_MODELVIEW) |
| 61 | + gl.glLoadIdentity() |
| 62 | + mat = np.array(identityMatrix()) |
| 63 | + |
| 64 | + for i in range(10): |
| 65 | + |
| 66 | + vec = list(100. * np.random.random((1,3))[0] - 50.) |
| 67 | + gl.glTranslated(*vec) |
| 68 | + correct = gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX) |
| 69 | + print "correct 1 ", correct |
| 70 | + |
| 71 | + trans = np.array(transMatrix(*vec)) |
| 72 | + mat = np.dot(trans, mat) |
| 73 | + print "predicted 1", mat |
| 74 | + gl.glLoadMatrixf(list(mat.reshape(mat.size))) |
| 75 | + |
| 76 | + #vec = list(4. * np.random.random((1,3))[0] - 2.) |
| 77 | + vec = [1., 0., 0.] |
| 78 | + ang = 4. * math.pi * np.random.random() |
| 79 | + print vec, ang |
| 80 | + gl.glRotated(vec[0], vec[1], vec[2], ang) |
| 81 | + correct = gl.glGetFloatv(gl.GL_MODELVIEW_MATRIX) |
| 82 | + print "correct 2", correct |
| 83 | + |
| 84 | + trans = np.array(rotMatrix(vec[0], vec[1], vec[2], math.radians(ang))) |
| 85 | + mat = np.dot(mat, trans) |
| 86 | + print "predicted 2", mat |
| 87 | + gl.glLoadMatrixf(list(mat.reshape(mat.size))) |
| 88 | + |
0 commit comments