2013-02-05 05:10:58 +00:00
|
|
|
#
|
2013-09-26 19:04:57 +00:00
|
|
|
# Copyright 2013 Pixar
|
2013-02-05 05:10:58 +00:00
|
|
|
#
|
2013-09-26 19:04:57 +00:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "Apache License")
|
|
|
|
# with the following modification; you may not use this file except in
|
|
|
|
# compliance with the Apache License and the following modification to it:
|
|
|
|
# Section 6. Trademarks. is deleted and replaced with:
|
2013-02-05 05:10:58 +00:00
|
|
|
#
|
2013-09-26 19:04:57 +00:00
|
|
|
# 6. Trademarks. This License does not grant permission to use the trade
|
|
|
|
# names, trademarks, service marks, or product names of the Licensor
|
|
|
|
# and its affiliates, except as required to comply with Section 4(c) of
|
|
|
|
# the License and to reproduce the content of the NOTICE file.
|
2013-02-05 05:10:58 +00:00
|
|
|
#
|
2013-09-26 19:04:57 +00:00
|
|
|
# You may obtain a copy of the Apache License at
|
2013-02-05 05:10:58 +00:00
|
|
|
#
|
2013-09-26 19:04:57 +00:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2013-07-18 21:19:50 +00:00
|
|
|
#
|
2013-09-26 19:04:57 +00:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the Apache License with the above modification is
|
|
|
|
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
# KIND, either express or implied. See the Apache License for the specific
|
|
|
|
# language governing permissions and limitations under the Apache License.
|
2013-02-05 05:10:58 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
from OpenGL.GL import *
|
|
|
|
from time import time
|
|
|
|
import math
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
from numpy import linalg as LA
|
|
|
|
|
|
|
|
# Provide a terse way to get a uniform location from its name
|
|
|
|
def U(name):
|
|
|
|
p = glGetIntegerv(GL_CURRENT_PROGRAM)
|
|
|
|
return glGetUniformLocation(p, name)
|
|
|
|
|
|
|
|
# Provide a terse way to create a f32 numpy 3-tuple
|
|
|
|
def V3(x, y, z):
|
|
|
|
return np.array([x, y, z], 'f')
|
|
|
|
|
|
|
|
def translation(direction):
|
|
|
|
M = np.identity(4, 'f')
|
|
|
|
M[:3, 3] = direction[:3]
|
|
|
|
return M
|
|
|
|
|
|
|
|
def unit_vector(data, axis=None, out=None):
|
|
|
|
if out is None:
|
|
|
|
data = np.array(data, dtype=np.float32, copy=True)
|
|
|
|
if data.ndim == 1:
|
|
|
|
data /= math.sqrt(np.dot(data, data))
|
|
|
|
return data
|
|
|
|
else:
|
|
|
|
if out is not data:
|
|
|
|
out[:] = np.array(data, copy=False)
|
|
|
|
data = out
|
|
|
|
length = np.atleast_1d(np.sum(data*data, axis))
|
|
|
|
np.sqrt(length, length)
|
|
|
|
if axis is not None:
|
|
|
|
length = np.expand_dims(length, axis)
|
|
|
|
data /= length
|
|
|
|
if out is None:
|
|
|
|
return data
|
|
|
|
|
|
|
|
def rotation3(angle, direction):
|
|
|
|
sina = math.sin(angle)
|
|
|
|
cosa = math.cos(angle)
|
|
|
|
direction = unit_vector(direction[:3])
|
|
|
|
R = np.diag([cosa, cosa, cosa])
|
|
|
|
R += np.outer(direction, direction) * (1.0 - cosa)
|
|
|
|
direction *= sina
|
|
|
|
R += np.array([[ 0.0, -direction[2], direction[1]],
|
|
|
|
[ direction[2], 0.0, -direction[0]],
|
|
|
|
[-direction[1], direction[0], 0.0]])
|
|
|
|
return R
|
|
|
|
|
|
|
|
def rotation(angle, direction):
|
|
|
|
R = rotation3(angle, direction)
|
|
|
|
M = np.identity(4, 'f')
|
|
|
|
M[:3, :3] = R
|
|
|
|
return M
|
|
|
|
|
|
|
|
def look_at(eye, target, up):
|
|
|
|
F = target[:3] - eye[:3]
|
|
|
|
f = F / LA.norm(F)
|
|
|
|
U = up / LA.norm(up)
|
|
|
|
s = np.cross(f, U)
|
|
|
|
u = np.cross(s, f)
|
|
|
|
M = np.matrix(np.identity(4))
|
|
|
|
M[:3,:3] = [s,u,-f]
|
|
|
|
T = translation(-eye)
|
|
|
|
return np.matrix(M * T, 'f')
|
|
|
|
|
|
|
|
def perspective(fovy, aspect, f, n):
|
|
|
|
s = 1.0/math.tan(math.radians(fovy)/2.0)
|
|
|
|
sx, sy = s / aspect, s
|
|
|
|
zz = (f+n)/(n-f)
|
|
|
|
zw = 2*f*n/(n-f)
|
|
|
|
m = np.matrix([[sx,0,0,0],
|
|
|
|
[0,sy,0,0],
|
|
|
|
[0,0,zz,zw],
|
|
|
|
[0,0,-1,0]], 'f')
|
|
|
|
return m
|