mirror of
https://github.com/PixarAnimationStudios/OpenSubdiv
synced 2024-12-03 00:20:09 +00:00
136 lines
5.0 KiB
Python
136 lines
5.0 KiB
Python
#
|
|
# Copyright (C) Pixar. All rights reserved.
|
|
#
|
|
# This license governs use of the accompanying software. If you
|
|
# use the software, you accept this license. If you do not accept
|
|
# the license, do not use the software.
|
|
#
|
|
# 1. Definitions
|
|
# The terms "reproduce," "reproduction," "derivative works," and
|
|
# "distribution" have the same meaning here as under U.S.
|
|
# copyright law. A "contribution" is the original software, or
|
|
# any additions or changes to the software.
|
|
# A "contributor" is any person or entity that distributes its
|
|
# contribution under this license.
|
|
# "Licensed patents" are a contributor's patent claims that read
|
|
# directly on its contribution.
|
|
#
|
|
# 2. Grant of Rights
|
|
# (A) Copyright Grant- Subject to the terms of this license,
|
|
# including the license conditions and limitations in section 3,
|
|
# each contributor grants you a non-exclusive, worldwide,
|
|
# royalty-free copyright license to reproduce its contribution,
|
|
# prepare derivative works of its contribution, and distribute
|
|
# its contribution or any derivative works that you create.
|
|
# (B) Patent Grant- Subject to the terms of this license,
|
|
# including the license conditions and limitations in section 3,
|
|
# each contributor grants you a non-exclusive, worldwide,
|
|
# royalty-free license under its licensed patents to make, have
|
|
# made, use, sell, offer for sale, import, and/or otherwise
|
|
# dispose of its contribution in the software or derivative works
|
|
# of the contribution in the software.
|
|
#
|
|
# 3. Conditions and Limitations
|
|
# (A) No Trademark License- This license does not grant you
|
|
# rights to use any contributor's name, logo, or trademarks.
|
|
# (B) If you bring a patent claim against any contributor over
|
|
# patents that you claim are infringed by the software, your
|
|
# patent license from such contributor to the software ends
|
|
# automatically.
|
|
# (C) If you distribute any portion of the software, you must
|
|
# retain all copyright, patent, trademark, and attribution
|
|
# notices that are present in the software.
|
|
# (D) If you distribute any portion of the software in source
|
|
# code form, you may do so only under this license by including a
|
|
# complete copy of this license with your distribution. If you
|
|
# distribute any portion of the software in compiled or object
|
|
# code form, you may only do so under a license that complies
|
|
# with this license.
|
|
# (E) The software is licensed "as-is." You bear the risk of
|
|
# using it. The contributors give no express warranties,
|
|
# guarantees or conditions. You may have additional consumer
|
|
# rights under your local laws which this license cannot change.
|
|
# To the extent permitted under your local laws, the contributors
|
|
# exclude the implied warranties of merchantability, fitness for
|
|
# a particular purpose and non-infringement.
|
|
#
|
|
|
|
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
|