Skip to content

Commit c8a9c1b

Browse files
author
Tim Sheerman-Chase
committed
Adding internal rotation function but gets strange result
1 parent def65ae commit c8a9c1b

File tree

2 files changed

+117
-7
lines changed

2 files changed

+117
-7
lines changed

README.md

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,35 @@ Based on examples by Cyrille Rossant http://cyrille.rossant.net/shaders-opengl/
2929
<h2>Copying</h2>
3030

3131
Copyright (c) 2012-2015, Tim Sheerman-Chase, Cyrille Rossant, Patrick Skjennum
32+
Copyright (c) 1995-2012, Michael C. Fletcher and Contributors
3233
All rights reserved.
3334

34-
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
35-
36-
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
37-
38-
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
39-
40-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35+
Redistribution and use in source and binary forms, with or without
36+
modification, are permitted provided that the following conditions
37+
are met:
38+
39+
Redistributions of source code must retain the above copyright
40+
notice, this list of conditions and the following disclaimer.
41+
42+
Redistributions in binary form must reproduce the above
43+
copyright notice, this list of conditions and the following
44+
disclaimer in the documentation and/or other materials
45+
provided with the distribution.
46+
47+
The name of Michael C. Fletcher, or the name of any Contributor,
48+
may not be used to endorse or promote products derived from this
49+
software without specific prior written permission.
50+
51+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52+
``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
54+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
55+
COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
56+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
57+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
58+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
60+
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
61+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
62+
OF THE POSSIBILITY OF SUCH DAMAGE.
4163

transutils.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)