Skip to content

Commit 35d75e7

Browse files
Add half finished code.
1 parent 3056306 commit 35d75e7

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed

pygly/attic/fbo.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
class FBO( object ):
3+
4+
def __init__( self ):
5+
super( FBO, self ).__init__()
6+
7+
self.fbo = (GLuint)()
8+
glGenFramebuffers( 1, self.fbo )
9+
10+
self.textures = {}
11+
12+
def __del__( self ):
13+
fbo = getattr( self, 'fbo', None )
14+
if fbo:
15+
glDeleteFramebuffers( 1, fbo )
16+
17+
def bind( self ):
18+
glBindFramebuffer( GL_RENDERBUFFER, self.fbo )
19+
20+
def unbind( self ):
21+
glBindFramebuffer( GL_RENDERBUFFER, 0 )
22+
23+
def set_active( self, targets = None ):
24+
"""Prepares the FBO for drawing.
25+
Calls glDrawFramebuffer.
26+
27+
If targets is None, the entire set of
28+
currently set texture targets will be used.
29+
"""
30+
if not targets:
31+
targets = self.textures.keys()
32+
33+
enums = numpy.array([targets])
34+
data = (GLuint * enums.size)(*enums.flat)
35+
36+
glDrawFramebuffer( enums.size, data )
37+
38+
def attach_texture_2d(
39+
self,
40+
texture,
41+
texture_target = GL_TEXTURE_2D,
42+
fb_target = GL_COLOR_ATTACHMENT0,
43+
level = 0,
44+
):
45+
glFramebufferTexture2d(
46+
GL_FRAMEBUFFER,
47+
fb_target,
48+
texture_target,
49+
texture,
50+
level
51+
)
52+
self.textures[ fb_target ] = texture
53+
54+
class ManagedFBO( FBO ):
55+
def create_texture_2d( self ):
56+
pass
57+
pass
58+

pygly/attic/vertex_array.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from pygly.vertex_buffer import VertexBuffer
2+
3+
4+
class VertexArray( object ):
5+
6+
attrib_pointer_function = {
7+
GL_BYTE: glVertexAttribPointerI,
8+
GL_UNSIGNED_BYTE: glVertexAttribPointerI,
9+
GL_SHORT: glVertexAttribPointerI,
10+
GL_UNSIGNED_SHORT: glVertexAttribPointerI,
11+
GL_INT: glVertexAttribPointerI,
12+
GL_UNSIGNED_INT: glVertexAttribPointerI,
13+
GL_HALF_FLOAT: glVertexAttribPointer,
14+
GL_FLOAT: glVertexAttribPointer,
15+
GL_DOUBLE: glVertexAttribPointerL,
16+
}
17+
18+
def __init__( self ):
19+
super( VertexArray, self ).__init__()
20+
21+
self.id = (GLuint)()
22+
glGenVertexArrays( 1, self.id )
23+
24+
def __del__( self ):
25+
id = getattr( self, 'id', None )
26+
if id and id.value != 0:
27+
glDeleteVertexArrays( 1, id )
28+
29+
def bind( self ):
30+
glBindVertexArray( self.id )
31+
32+
def unbind( self ):
33+
glBindVertexArray( 0 )
34+
35+
def set_vertex_buffer( self, size, type, stride, offset ):
36+
pointer = (GLvoid)( sizeof(gl_type) * offset )
37+
glVertexPointer( size, type, stride, pointer )
38+
39+
def set_normal_buffer( self, size, stride, offset ):
40+
pointer = (GLvoid)( sizeof(gl_type) * offset )
41+
glNormalPointer( size, stride, pointer )
42+
43+
def set_colour_buffer( self, size, type, stride, offset ):
44+
pointer = (GLvoid)( sizeof(gl_type) * offset )
45+
glColourPointer( size, type, stride, pointer )
46+
47+
def set_texture_coordinate_buffer( self, size, type, stride, offset ):
48+
pointer = (GLvoid)( sizeof(gl_type) * offset )
49+
glTexCoordPointer( size, type, stride, pointer )
50+
51+
def set_buffer( self, index, buffer, normalise = False, stride = 0, offset = 0 ):
52+
type_tuple, element_size, usage = VertexBuffer.parse_format( format )
53+
54+
# extract the gl type enum and data type
55+
gl_enum, gl_type = type_tuple
56+
57+
gl_normalise = GL_TRUE if normalise else GL_FALSE
58+
59+
# convert our offset to a pointer
60+
# because it's a pointer, it needs to be converted from
61+
# element offset to memory offset
62+
pointer = (GLvoid)( sizeof(gl_type) * offset )
63+
64+
func = VertexArray.attrib_pointer_function[ gl_enum ]
65+
66+
func(
67+
index,
68+
element_size,
69+
gl_enum,
70+
gl_normalise,
71+
stride,
72+
pointer
73+
)
74+

pygly/attic/vertex_buffer.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from ctypes import *
2+
3+
from pyglet.gl import *
4+
5+
import pygly.utils
6+
7+
8+
class VertexBuffer( object ):
9+
10+
# http://www.khronos.org/files/opengl-quick-reference-card.pdf
11+
types = {
12+
'i8': (GL_BYTE, GLbyte),
13+
'u8': (GL_UNSIGNED_BYTE, GLubyte),
14+
'i16': (GL_SHORT, GLshort),
15+
'u16': (GL_UNSIGNED_SHORT, GLushort),
16+
'i32': (GL_INT, GLint),
17+
'u32': (GL_UNSIGNED_INT, GLuint),
18+
# no native half-float
19+
'f16': (GL_HALF_FLOAT, GLfloat),
20+
'f32': (GL_FLOAT, GLfloat),
21+
'f64': (GL_DOUBLE, GLdouble),
22+
}
23+
24+
uses = {
25+
'stream_draw': GL_STREAM_DRAW,
26+
'stream_read': GL_STREAM_READ,
27+
'stream_copy': GL_STREAM_COPY,
28+
'static_draw': GL_STATIC_DRAW,
29+
'static_read': GL_STATIC_READ,
30+
'static_copy': GL_STATIC_COPY,
31+
'dynamic_draw': GL_DYNAMIC_DRAW,
32+
'dynamic_read': GL_DYNAMIC_READ,
33+
'dynamic_copy': GL_DYNAMIC_COPY,
34+
}
35+
36+
@staticmethod
37+
def parse_format( format ):
38+
values = format.split('/')
39+
type, element_size, usage = pygly.utils.extract_tuple( values, 3 )
40+
41+
return(
42+
VertexBuffer.types[ type ],
43+
int( element_size ),
44+
VertexBuffer.uses[ usage ]
45+
)
46+
47+
def __init__( self, target = GL_ARRAY_BUFFER ):
48+
super( VertexBuffer, self ).__init__()
49+
50+
self.id = (GLuint)()
51+
glGenBuffers( 1, self.id )
52+
53+
self.target = target
54+
55+
self.format = ''
56+
57+
def __del__( self ):
58+
id = getattr( self, 'id', None )
59+
if id and id.value != 0:
60+
glDeleteBuffers( 1, id )
61+
62+
def bind( self ):
63+
glBindBuffer( self.target, self.id )
64+
65+
def unbind( self ):
66+
glBindBuffer( self.target, 0 )
67+
68+
def set_data( self, format, num_elements, data ):
69+
"""
70+
Format is as follows:
71+
data type / values per element / usage
72+
73+
For example:
74+
f32/3/static_draw
75+
"""
76+
# store our format for later
77+
self.format = format
78+
79+
type_tuple, element_size, usage = VertexBuffer.parse_format( format )
80+
81+
# extract the gl type enum and data type
82+
gl_enum, gl_type = type_tuple
83+
84+
glBufferData(
85+
self.target,
86+
len(data) * sizeof( gl_type ),
87+
(gl_type * len(data))(*data),
88+
usage
89+
)
90+

0 commit comments

Comments
 (0)