Skip to content

Commit f970a1e

Browse files
committed
changed Vector base to not use PEP 695 generic syntax as I need to use
python 3.11 for renderman and this is not valid. once renderman moves to python >13 we can revert to the generic version. Also added renderman version of look_at to handle right handed cord-systems
1 parent 0728942 commit f970a1e

4 files changed

Lines changed: 63 additions & 4 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "ncca-ngl"
3-
version = "0.5.0"
3+
version = "0.5.1"
44
description = "A Python version of the NGL graphics library."
55
authors = [{ name = "Jon Macey", email = "jmacey@bournemouth.ac.uk" }]
66
requires-python = ">=3.11"

src/ncca/ngl/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from .text import Text
4141
from .texture import Texture
4242
from .transform import Transform, TransformRotationOrder
43-
from .util import PerspMode, calc_normal, clamp, frustum, lerp, look_at, ortho, perspective
43+
from .util import PerspMode, calc_normal, clamp, frustum, lerp, look_at, ortho, perspective, renderman_look_at
4444
from .vao_factory import VAOFactory, VAOType
4545
from .vec2 import Vec2
4646
from .vec2_array import Vec2Array
@@ -110,4 +110,5 @@
110110
FirstPersonCamera,
111111
PySideEventHandlingMixin,
112112
PerspMode,
113+
renderman_look_at,
113114
]

src/ncca/ngl/util.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,55 @@ def hash_combine(seed, h):
161161
seed = (seed + 0x9E3779B9 + ((seed << 6) & 0xFFFFFFFFFFFFFFFF) + (seed >> 2)) & 0xFFFFFFFFFFFFFFFF
162162
seed ^= h
163163
return seed
164+
165+
166+
def renderman_look_at(eye, look, up):
167+
"""
168+
Calculate 4x4 matrix for RenderMan camera lookAt
169+
Accounts for RenderMan's right-handed Y-down, Z-forward coordinate system
170+
171+
Args:
172+
eye: Vec3 - camera position
173+
look: Vec3 - point to look at
174+
up: Vec3 - up vector (typically (0, 1, 0) in world space)
175+
176+
Returns:
177+
Mat4 - 4x4 transformation matrix
178+
"""
179+
# Calculate view direction (from eye to look point)
180+
n = look - eye
181+
n.normalize()
182+
183+
# Calculate right vector
184+
v = n.cross(up)
185+
v.normalize()
186+
187+
# Recalculate orthogonal up vector
188+
u = v.cross(n)
189+
u.normalize()
190+
191+
# Build the matrix for RenderMan's coordinate system
192+
# RenderMan uses Y-down, Z-forward
193+
result = Mat4.identity()
194+
195+
# Right vector (X-axis)
196+
result.m[0][0] = v.x
197+
result.m[1][0] = v.y
198+
result.m[2][0] = v.z
199+
200+
# Up vector (Y-axis) - negated for Y-down convention
201+
result.m[0][1] = -u.x
202+
result.m[1][1] = -u.y
203+
result.m[2][1] = -u.z
204+
205+
# Forward vector (Z-axis) - camera looks down +Z
206+
result.m[0][2] = n.x
207+
result.m[1][2] = n.y
208+
result.m[2][2] = n.z
209+
210+
# Translation (camera position)
211+
result.m[3][0] = -eye.dot(v)
212+
result.m[3][1] = eye.dot(u) # Negated Y component
213+
result.m[3][2] = -eye.dot(n)
214+
215+
return result

src/ncca/ngl/vector_base.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
import ctypes
99
import math
1010
from abc import ABC, abstractmethod
11-
from typing import Any, ClassVar, Self, Tuple
11+
from typing import Any, ClassVar, Generic, Self, Tuple, TypeVar
1212

1313
import numpy as np
1414

1515
from .util import clamp, hash_combine
1616

17+
# Note this has been changed so I can use python 3.11
18+
# as renderman want this. Once >3.11 is used we can get rid and just use
19+
# class VectorBase[T](ABC):
1720

18-
class VectorBase[T](ABC):
21+
T = TypeVar("T", bound="VectorBase")
22+
23+
24+
class VectorBase(ABC, Generic[T]):
1925
"""
2026
Base class for all vector types providing common functionality.
2127

0 commit comments

Comments
 (0)