Graphics: Lower level functions to draw in OpenGL.

Graphics: Lower level functions to draw in OpenGL.

Our previous graphx package relied on OpenGL’s so-called immediate mode. This mode is no longer allowed in OpenGL 3.0 and OpenGL ES. This graphics module is the new and stable way to draw all OpenGL elements in PyMT. We seriously recommend you use these classes (the old graphx package will be deprecated)!

User mode

For every object you want do draw on the screen, you must create them on a canvas before drawing. The canvas object is a class that will store all your graphics elements and draw them efficiently. This method allows for internal optimizations. Use it like this

>>> canvas = Canvas()
>>> canvas.color(1, 0, 0, 1)
>>> canvas.line([50, 50, 100, 100])

Then, to draw the canvas

>>> canvas.draw()

You can also get a handle for any element of the canvas to change it later

>>> myline = canvas.line([50, 50, 100, 100])
>>> myline.points += [0, 150]

Expert mode

You can create your own graphical object. However, you should still use the canvas object.

An example with a line

# in init function
>>> line = Line([50, 50, 100, 100])

# in draw function
>>> line.draw()

# If you want to change the points of the line, you can do
>>> line.points = [80, 80, 100, 100]

# Or even add points to the line
>>> line.points += [58, 35]

An example with a rectangle

# in init function
>>> rect = Rectangle(pos=(50, 50), size=(200, 200))

# in draw function
>>> rect.draw()

# You can change pos, size...
>>> rect.pos = (10, 10)
>>> rect.size = (999, 999)

An example with a rectangle and a texture

# in init function
>>> img = Image('test.png')
>>> rect = Rectangle(size=(100, 100), texture=img.texture)

# in draw function
>>> rect.draw()
class pymt.c_ext.c_graphics.CSSRectangle

Bases: pymt.c_ext.c_graphics.GraphicInstruction

Construct a rectangle that supports a lot of CSS attributes. A CSSRectangle can also be constructed by giving values like

# classical way
>>> CSSRectangle(pos=(0, 0), size=(500, 500), style=self.style)

# alternative way
>>> CSSRectangle(x, y, w, h, style=self.style)
Parameters :
style: dict, default to {}

CSS style dictionnary. Usually, it’s the self.style of a widget.

prefix: str, default to None

Use all the styles with that prefix first.

state: str, default to None

Use all the styles with that state first.

Styles :
  • alpha-background (color)
  • bg-image (filename)
  • border-radius (float)
  • border-radius-precision (float)
  • border-width (float)
  • draw-alpha-background (bool)
  • draw-background (bool)
  • draw-border (bool)
center

Object center (cx, cy)

height

Object height

pos

Object position (x, y)

prefix

Get/Set the css prefix to use

size

Object size (width, height)

state

Get/Set the css state to use

style

Get/Set the css style to use (normally, its the widget.style property)

width

Object width

x

Object X position

y

Object Y position

class pymt.c_ext.c_graphics.Canvas

Bases: object

Create a batch of graphic objects. Can be used to store many graphic instructions and call them for drawing. (Note: This will lead to optimizations in the near future.)

add

Add a graphic element to draw

circle

Create a Circle() object and add it to the canvas. Check Circle() for more information.

clear

Clear all the elements in the canvas

color

Create a Color() object and add it to the canvas. Check Color() for more information.

cssRectangle

Create a CSSRectangle() object and add it to the canvas. Check CSSRectangle() for more information.

draw

Draw all the canvas elements

imageRectangle

Create a ImageRectangle() object and add it to the canvas. Check ImageRectangle() for more information.

line

Create a Line() object and add it to the canvas. Check Line() for more information.

point

Create a Point() object and add it to the canvas. Check Point() for more information.

rectangle

Create a Rectangle() object and add it to the canvas. Check Rectangle() for more information.

remove

Remove a graphic element from the list of objects

restore

Restore the previous saved context

roundedRectangle

Create a RoundedRectangle() object and add it to the canvas. Check RoundedRectangle() for more information.

save

Push the current context to the stack

text

Create a Text() object and add it to the canvas. Check Text() for more information.

class pymt.c_ext.c_graphics.Circle

Bases: pymt.c_ext.c_graphics.GraphicElement

Construct a circle from position and radius. The circle can be either filled or not.

Warning

Each time you change a property of the circle, the vertex list is rebuilt automatically at the next draw() call.

Parameters :
pos: list, defaults to (0, 0)

Position of the circle

radius: int, defaults to 5

Radius of the circle

filled: list, default to False

Can be used to specify a color for each vertex drawn.

filled

Indicates whether the circle is filled or not

pos

Object position (x, y)

radius

Radius of the circle (double)

x

Object X position

y

Object Y position

class pymt.c_ext.c_graphics.Color

Bases: pymt.c_ext.c_graphics.GraphicInstruction

Define color to be used in the following (floats between 0 and 1)

>>> c = Canvas()
>>> c.color(1., 0.4, 0., 1.)
>>> c.rectangle(pos=(50, 50), size=(100, 100))

>>> c.draw()
Parameters :
*color : list

Can have 3 or 4 float values (between 0 and 1)

sfactor : opengl factor, default to GL_SRC_ALPHA

Default source factor to be used if blending is activated

dfactor : opengl factor, default to GL_ONE_MINUS_SRC_ALPHA

Default destination factor to be used if blending is activated

blend : boolean, default to None

Set True if you really want to activate blending, even if the alpha color is 1 (which means no blending in theory)

color

Get/Set the color in tuple format (r, g, b, a)

class pymt.c_ext.c_graphics.GraphicContext

Bases: object

Handle the saving/restore of the context

TODO: explain more how it works

class pymt.c_ext.c_graphics.GraphicElement

Bases: pymt.c_ext.c_graphics.GraphicInstruction

This is the lowest graphical element you can use. It’s an abstraction to Vertex Buffer Object, and you can push your vertex, color, texture ... and draw them easily.

The format of the buffer is specified in character code. For example, ‘vvcccc’ means that you’ll have 2 vertex + 4 colors coordinates. You have 6 differents components that you can use:

  • v: vertex
  • c: color
  • t: texture
  • n: normal
  • i: index (not yet used)
  • e: edge (not yet used)

For each component, VBOs are separated.

Parameters :
format: string, default to None

The format must be specified at start, and cannot be changed once the graphic is created.

type: string, default to None

Specify how the graphic will be drawn. One of: ‘lines’, ‘line_loop’, ‘line_strip’, ‘triangles’, ‘triangle_fan’, ‘triangle_strip’, ‘quads’, ‘quad_strip’, ‘points’, ‘polygon’

usage: string, default to ‘GL_DYNAMIC_DRAW’

Specify the usage of VBO. Can be one of ‘GL_STREAM_DRAW’, ‘GL_STREAM_READ’, ‘GL_STREAM_COPY’, ‘GL_STATIC_DRAW’, ‘GL_STATIC_READ’, ‘GL_STATIC_COPY’, ‘GL_DYNAMIC_DRAW’, ‘GL_DYNAMIC_READ’, or ‘GL_DYNAMIC_COPY’. Infos: http://www.openorg/sdk/docs/man/xhtml/glBufferData.xml

target: string, default to ‘GL_ARRAY_BUFFER’

Target of the VBO. Can be one of ‘GL_ARRAY_BUFFER’, ‘GL_ELEMENT_ARRAY_BUFFER’, ‘GL_PIXEL_PACK_BUFFER’, or ‘GL_PIXEL_UNPACK_BUFFER’. Infos: http://www.openorg/sdk/docs/man/xhtml/glBufferData.xml

data_c

Get/set the colors coordinates data

data_e

Get/set the edges data (not used yet.)

data_i

Get/set the indexes data (not used yet.)

data_n

Get/set the normal coordinates data

data_t

Get/set the texture coordinates data

data_v

Get/set the vertex coordinates data

format

Return the format of the graphic in string (eg. “vvttcccc”)

indices

(optional) Use an indice array to draw

type

Specify how the graphic will be drawed. One of: ‘lines’, ‘line_loop’, ‘line_strip’, ‘triangles’, ‘triangle_fan’, ‘triangle_strip’, ‘quads’, ‘quad_strip’, ‘points’, ‘polygon’

class pymt.c_ext.c_graphics.ImageRectangle

Bases: pymt.c_ext.c_graphics.Rectangle

Draw an Image rectangle, similar to border-image in CSS3.

borders

Borders in pixels of the image

mode

Mode of the drawing (only strech is supported

class pymt.c_ext.c_graphics.Line

Bases: pymt.c_ext.c_graphics.GraphicElement

Construct line from points.

Parameters :
points: list

List of points, in the format [x, y, x, y...]

points

Add/remove points of the line (list of [x, y, x, y ...])

class pymt.c_ext.c_graphics.Point

Bases: pymt.c_ext.c_graphics.GraphicElement

Draw multiple points.

Parameters :
texture: texture, default to None

Specify the texture to use to draw the points

radius: float, default to 1.

Size of the point to draw, in pixel.

steps: int, default to None

Number of steps between 2 points

points

Object points (list in the format [x, y, x, y...])

radius

Object radius (float)

step

Object step (integer)

texture

Texture to use on the object (Texture)

class pymt.c_ext.c_graphics.Rectangle

Bases: pymt.c_ext.c_graphics.GraphicElement

Construct a rectangle from position and size. This can be use to draw the shape of a rectangle, a filled rectangle, a textured rectangle, a rounded rectangle...

Warning

Each time you change a property of the rectangle, the vertex list is rebuilt automatically at the next draw() call.

Parameters :
*values: list, default to None

Can be used to provide a tuple of (x, y, w, h)

pos: list, default to (0, 0)

Position of the rectangle

size: list, default to (1, 1)

Size of the rectangle

texture: texture, default to None

Specify the texture to use for the rectangle

tex_coords: list, default to None

If a texture is specified, the tex_coords will be taken from the texture argument. Otherwise, it will be set on 0-1 range.

colors_coords: list, default to None

Can be used to specify a color for each vertex drawn.

build

Build all the vbos. This is automaticly called when a property changes (position, size, tex_coords...)

center

Object center (cx, cy)

colors_coords

Colors coordinates for each vertex

height

Object height

pos

Object position (x, y)

size

Object size (width, height)

tex_coords

Texture coordinates to use on the object. If nothing is set, it will take the coordinates from the current texture

texture

Texture to use on the object

width

Object width

x

Object X position

y

Object Y position

class pymt.c_ext.c_graphics.RoundedRectangle

Bases: pymt.c_ext.c_graphics.Rectangle

Draw a rounded rectangle

Warning

Rounded rectangle supports only vertex, not other things right now. It may change in the future.

Parameters :
radius : int, default to 5

Radius of the corners

precision : float, default to 0.5

Precision of corner angle

corners : tuple of bool, default to (True, True, True, True)

Indicate which corners are to be rounded. Bools in the order: bottom-left, bottom-right, top-right, top-left

corners

Get/set the corners to draw (tuple of 4 bool)

precision

Get/set the precision of the corner (double)

radius

Get/set the radius of the corner (double)

class pymt.c_ext.c_graphics.Text

Bases: pymt.c_ext.c_graphics.Rectangle

Draw a Text/Label.

Supports all the arguments from the getLabel function.

label

Colors coordinates for each vertex

Table Of Contents

Previous topic

Accelerate: provide acceleration for some critical function of PyMT

Next topic

Graphx: acceleration module

This Page