~matobet/pyopengl/Python3

« back to all changes in this revision

Viewing changes to GLU/projection.py

  • Committer: matobet at gmail
  • Date: 2010-06-26 14:11:04 UTC
  • Revision ID: matobet@gmail.com-20100626141104-k011ofmltgiiu60g
Initial

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""glu[Un]Project[4] convenience wrappers"""
 
2
from OpenGL.platform import GL
 
3
from OpenGL.raw import GLU as simple
 
4
from OpenGL import GL, arrays
 
5
from OpenGL.lazywrapper import lazy
 
6
import ctypes 
 
7
POINTER = ctypes.POINTER
 
8
 
 
9
@lazy( simple.gluProject )
 
10
def gluProject( baseFunction, objX, objY, objZ, model=None, proj=None, view=None ):
 
11
    """Convenience wrapper for gluProject
 
12
    
 
13
    Automatically fills in the model, projection and viewing matrices
 
14
    if not provided.
 
15
    
 
16
    returns (winX,winY,winZ) doubles
 
17
    """
 
18
    if model is None:
 
19
        model = GL.glGetDoublev( GL.GL_MODELVIEW_MATRIX )
 
20
    if proj is None:
 
21
        proj = GL.glGetDoublev( GL.GL_PROJECTION_MATRIX )
 
22
    if view is None:
 
23
        view = GL.glGetIntegerv( GL.GL_VIEWPORT )
 
24
    winX = simple.GLdouble( 0.0 )
 
25
    winY = simple.GLdouble( 0.0 )
 
26
    winZ = simple.GLdouble( 0.0 )
 
27
    result = baseFunction( 
 
28
        objX,objY,objZ,
 
29
        model,proj,view,
 
30
        winX,winY,winZ,
 
31
    )
 
32
    # On Ubuntu 9.10 we see a None come out of baseFunction,
 
33
    # despite it having a return-type specified of GLint!
 
34
    if result is not None and result != simple.GLU_TRUE:
 
35
        raise ValueError( """Projection failed!""" )
 
36
    return winX.value, winY.value, winZ.value 
 
37
 
 
38
@lazy( simple.gluUnProject )
 
39
def gluUnProject( baseFunction, winX, winY, winZ, model=None, proj=None, view=None ):
 
40
    """Convenience wrapper for gluUnProject
 
41
    
 
42
    Automatically fills in the model, projection and viewing matrices
 
43
    if not provided.
 
44
    
 
45
    returns (objX,objY,objZ) doubles
 
46
    """
 
47
    if model is None:
 
48
        model = GL.glGetDoublev( GL.GL_MODELVIEW_MATRIX )
 
49
    if proj is None:
 
50
        proj = GL.glGetDoublev( GL.GL_PROJECTION_MATRIX )
 
51
    if view is None:
 
52
        view = GL.glGetIntegerv( GL.GL_VIEWPORT )
 
53
    objX = simple.GLdouble( 0.0 )
 
54
    objY = simple.GLdouble( 0.0 )
 
55
    objZ = simple.GLdouble( 0.0 )
 
56
    result = baseFunction( 
 
57
        winX,winY,winZ,
 
58
        model,proj,view,
 
59
        ctypes.byref(objX),ctypes.byref(objY),ctypes.byref(objZ),
 
60
    )
 
61
    if not result:
 
62
        raise ValueError( """Projection failed!""" )
 
63
    return objX.value, objY.value, objZ.value 
 
64
@lazy( simple.gluUnProject4 )
 
65
def gluUnProject4(
 
66
    baseFunction,
 
67
    winX, winY, winZ, clipW, 
 
68
    model=None, proj=None, view=None, 
 
69
    near=0.0, far=1.0
 
70
):
 
71
    """Convenience wrapper for gluUnProject
 
72
    
 
73
    Automatically fills in the model, projection and viewing matrices
 
74
    if not provided.
 
75
    
 
76
    returns (objX,objY,objZ) doubles
 
77
    """
 
78
    if model is None:
 
79
        model = GL.glGetDoublev( GL.GL_MODELVIEW_MATRIX )
 
80
    if proj is None:
 
81
        proj = GL.glGetDoublev( GL.GL_PROJECTION_MATRIX )
 
82
    if view is None:
 
83
        view = GL.glGetIntegerv( GL.GL_VIEWPORT )
 
84
    objX = simple.GLdouble( 0.0 )
 
85
    objY = simple.GLdouble( 0.0 )
 
86
    objZ = simple.GLdouble( 0.0 )
 
87
    objW = simple.GLdouble( 0.0 )
 
88
    result = baseFunction( 
 
89
        winX,winY,winZ,
 
90
        model,proj,view,
 
91
        ctypes.byref(objX),ctypes.byref(objY),ctypes.byref(objZ),ctypes.byref(objW)
 
92
    )
 
93
    if not result:
 
94
        raise ValueError( """Projection failed!""" )
 
95
    return objX.value, objY.value, objZ.value, objW.value
 
96
 
 
97
__all__ = (
 
98
    'gluProject',
 
99
    'gluUnProject',
 
100
    'gluUnProject4',
 
101
)