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)!
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]
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()
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 : |
|
|---|---|
| Styles : |
|
Object center (cx, cy)
Object height
Object position (x, y)
Get/Set the css prefix to use
Object size (width, height)
Get/Set the css state to use
Get/Set the css style to use (normally, its the widget.style property)
Object width
Object X position
Object Y position
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 a graphic element to draw
Create a Circle() object and add it to the canvas. Check Circle() for more information.
Clear all the elements in the canvas
Create a Color() object and add it to the canvas. Check Color() for more information.
Create a CSSRectangle() object and add it to the canvas. Check CSSRectangle() for more information.
Draw all the canvas elements
Create a ImageRectangle() object and add it to the canvas. Check ImageRectangle() for more information.
Create a Line() object and add it to the canvas. Check Line() for more information.
Create a Point() object and add it to the canvas. Check Point() for more information.
Create a Rectangle() object and add it to the canvas. Check Rectangle() for more information.
Remove a graphic element from the list of objects
Restore the previous saved context
Create a RoundedRectangle() object and add it to the canvas. Check RoundedRectangle() for more information.
Push the current context to the stack
Create a Text() object and add it to the canvas. Check Text() for more information.
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 : |
|
|---|
Indicates whether the circle is filled or not
Object position (x, y)
Radius of the circle (double)
Object X position
Object Y position
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 : |
|
|---|
Get/Set the color in tuple format (r, g, b, a)
Bases: object
Handle the saving/restore of the context
TODO: explain more how it works
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 : |
|
|---|
Get/set the colors coordinates data
Get/set the edges data (not used yet.)
Get/set the indexes data (not used yet.)
Get/set the normal coordinates data
Get/set the texture coordinates data
Get/set the vertex coordinates data
Return the format of the graphic in string (eg. “vvttcccc”)
(optional) Use an indice array to draw
Specify how the graphic will be drawed. One of: ‘lines’, ‘line_loop’, ‘line_strip’, ‘triangles’, ‘triangle_fan’, ‘triangle_strip’, ‘quads’, ‘quad_strip’, ‘points’, ‘polygon’
Bases: pymt.c_ext.c_graphics.Rectangle
Draw an Image rectangle, similar to border-image in CSS3.
Borders in pixels of the image
Mode of the drawing (only strech is supported
Bases: pymt.c_ext.c_graphics.GraphicElement
Construct line from points.
| Parameters : |
|
|---|
Add/remove points of the line (list of [x, y, x, y ...])
Bases: pymt.c_ext.c_graphics.GraphicElement
Draw multiple points.
| Parameters : |
|
|---|
Object points (list in the format [x, y, x, y...])
Object radius (float)
Object step (integer)
Texture to use on the object (Texture)
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 : |
|
|---|
Build all the vbos. This is automaticly called when a property changes (position, size, tex_coords...)
Object center (cx, cy)
Colors coordinates for each vertex
Object height
Object position (x, y)
Object size (width, height)
Texture coordinates to use on the object. If nothing is set, it will take the coordinates from the current texture
Texture to use on the object
Object width
Object X position
Object Y position
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 : |
|
|---|
Get/set the corners to draw (tuple of 4 bool)
Get/set the precision of the corner (double)
Get/set the radius of the corner (double)
Bases: pymt.c_ext.c_graphics.Rectangle
Draw a Text/Label.
Supports all the arguments from the getLabel function.
Colors coordinates for each vertex