[][src]Crate egaku2d

Overview

A library that lets you draw various simple 2d geometry primitives and sprites fast using vertex buffer objects with a safe api (provided no other libray is calling opengl functions). Uses the builder pattern for a convinient api. The main design goal is to be able to draw thousands of shapes efficiently. Uses glutin and opengl es 2.0.

Pipeline

The egaku2d drawing pipeline works as follows:

    1. Pick a drawing type (a particular shape or a sprite) and set mandatory values for the particular shape or sprite.
    1. Build up a large group of verticies by calling add()
    • 2.1 Optionally save off verticies to a static vbo on the gpu for fast drawing at a later time by calling save().
    1. Set mandatory shader uniform values bt calling uniforms()
    • 3.1 Set optional uniform values e.g. with_color().
    1. Send the vertex data to the gpu and draw by calling send_and_draw()

Additionally, there is a way to draw the vertices we saved off to the gpu. To do that, instead of steps 1 and 2, we use the saved off verticies, and then set the uniform values and then draw by calling draw(). Drawing in this case is faster since the vertex data already exists on the gpu.

Using this pipeline, the user can efficiently draw thousands of circles, for example, with the caveat that they all will be the same radius and color/transparency values. This api does not allow the user to efficiently draw thousands of circles where each circle has a different color or radius. This was a design decision to make each vertex as lightweight as possible (just a x and y position), making it more efficient to set and send to the gpu.

Using Shapes

The user can draw the following:

ShapeRepresentation
Circles(point,radius)
Axis Aligned Rectangles(startx,endx,starty,endy)
Axis Aligned Squares(point,radius)
Lines(point,point,thickness)
Arrows(point_start,point_end,thickness)

Using Sprites

You can also draw sprites! You can upload a tileset texture to the gpu and then draw thousands of sprites using a similar api to the shape drawing api. The sprites are point sprites drawn using the opengl POINTS primitive in order to cut down on the data that needs to be sent to the gpu. The only information that is sent to the gpu on a sprite by sprite basis is its position, and its tile index.

While the user can pick different tile coorinates to draw different sprints within the texture they upload to the gpu, they cannot rotate the sprite. The sprite is drawn centered at the position the user specifies. They can change the size of all sprites in the draw session by changing its radius. See the example below.

View

The top left corner is the origin (0,0) and x and y grow to the right and downwards respectively.

In windowed mode, the dimenions of the window defaults to scale exactly to the world. For example, if the user made a window of size 800,600, and then drew a circle at 400,300, the circle would appear in the center of the window. Similarily, if the user had a monitor with a resolution of 800,600 and started in fullscreen mode, and drew a circle at 400,300, it would also appear in the center of the screen.

The ratio between the scale of x and y are fixed to be 1:1 so that there is no distortion in the shapes. The user can manually set the scale either by x or y and the other axis is automaically inferred so that to keep a 1:1 ratio.

Fullscreen

Fullscreen is kept behind a feature gate since on certain platforms like wayland linux it does not work. I suspect this is a problem with glutin, so I have just disabled it for the time behing in the hope that once glutin leaves alpha it will work. I think the problem is that when the window is resized, I can't manually change the size of the context to match using resize().

Example

use axgeom::*;
let events_loop = glutin::event_loop::EventLoop::new();
let mut glsys = egaku2d::WindowedSystem::new([600, 480], &events_loop,"test window");

//Make a tileset texture from a png that has 64 different tiles.
let food_texture = glsys.texture("food.png",[8,8]).unwrap();

let canvas = glsys.canvas_mut();

//Make the background dark gray.
canvas.clear_color([0.2,0.2,0.2]);

//Push some squares to a static vertex buffer object on the gpu.
let rect_save = canvas.squares()
  .add([40., 40.])
  .add([40., 40.])
  .save();

//Draw the squares we saved.
rect_save.uniforms(canvas,5.0).with_color([0.0, 1.0, 0.1, 0.5]).draw();

//Draw some arrows.
canvas.arrows(5.0)
  .add([40., 40.], [40., 200.])
  .add([40., 40.], [200., 40.])
  .uniforms().send_and_draw();

//Draw some circles.
canvas.circles()
  .add([5.,6.])
  .add([7.,8.])
  .add([9.,5.])
  .uniforms(4.0).with_color([0., 1., 1., 0.1]).send_and_draw();

//Draw some circles from f32 primitives.
canvas.circles()
  .add([5.,6.])
  .add([7.,8.])
  .add([9.,5.])
  .uniforms(4.0).with_color([0., 1., 1., 0.1]).send_and_draw();

//Draw the first tile in the top left corder of the texture.
canvas.sprites().add([100.,100.],food_texture.coord_to_index([0,0])).uniforms(&food_texture,4.0).send_and_draw();

//Swap buffers on the opengl context.
glsys.swap_buffers();

Re-exports

pub use glutin;

Modules

shapes

Contains all the shape drawing session and save objects They all follow the same api outlined in the crate documentation.

sprite

Contains all the texture/sprite drawing code. The api is described in the crate documentation.

uniforms

Contains the objects used for the uniform setting stage of the egaku2d drawing pipeline.

Structs

RefreshTimer

A timer to determine how often to refresh the screen. You pass it the desired refresh rate, then you can poll with is_ready() to determine if it is time to refresh.

SimpleCanvas

Allows the user to start drawing shapes. The top left corner is the origin. y grows as you go down. x grows as you go right.

WindowedSystem

A version where the user can control the size of the window.