tranformations with A5
William Labbett

Hi,

I've read the bits of the docs about transforms and looked at ex_transform.

I still don't really understand what they do and how they should be used.

Is there a tutorial about them anywhere?

Mark Oates

Just to ask, are you interested in transforms that are applied to the display (like controlling a camera), or transforms to establish the position different objects in a scene? There's a bit of a difference between the two.

Also, 2d or 3d?

William Labbett

I'm just trying to learn how to use the function al_draw_prim so I started trying to understand the code in ex_prim.c . I wanted to understand this function :

1static void LowPrimitives(int mode) 2{ 3 static ALLEGRO_VERTEX vtx[13]; 4 static ALLEGRO_VERTEX vtx2[13]; 5 if (mode == INIT) { 6 int ii = 0; 7 ALLEGRO_COLOR color; 8 for (ii = 0; ii < 13; ii++) { 9 float x, y; 10 x = 200 * cosf((float)ii / 13.0f * 2 * ALLEGRO_PI); 11 y = 200 * sinf((float)ii / 13.0f * 2 * ALLEGRO_PI); 12 13 color = al_map_rgb((ii + 1) % 3 * 64, (ii + 2) % 3 * 64, (ii) % 3 * 64); 14 15 vtx[ii].x = x; vtx[ii].y = y; vtx[ii].z = 0; 16 vtx2[ii].x = 0.1 * x; vtx2[ii].y = 0.1 * y; 17 vtx[ii].color = color; 18 vtx2[ii].color = color; 19 } 20 } else if (mode == LOGIC) { 21 Theta += Speed; 22 al_build_transform(&MainTrans, ScreenW / 2, ScreenH / 2, 1, 1, Theta); 23 } else if (mode == DRAW) { 24 if (Blend) 25 al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE); 26 else 27 al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); 28 29 al_use_transform(&MainTrans); 30 31 al_draw_prim(vtx, 0, 0, 0, 4, ALLEGRO_PRIM_LINE_LIST); 32 al_draw_prim(vtx, 0, 0, 4, 9, ALLEGRO_PRIM_LINE_STRIP); 33 al_draw_prim(vtx, 0, 0, 9, 13, ALLEGRO_PRIM_LINE_LOOP); 34 al_draw_prim(vtx2, 0, 0, 0, 13, ALLEGRO_PRIM_POINT_LIST); 35 36 al_use_transform(&Identity); 37 } 38}

What I actually want to do is draw some lines on a display's backbuffer with low level routines.

Edgar Reynaldo

https://liballeg.org/a5docs/trunk/transformations.html#al_build_transform

al_build_transform(&MainTrans, ScreenW / 2, ScreenH / 2, 1, 1, Theta);

This call creates a transform matrix that does this :

1) Rotate by theta
2) Scale by 1,1
3) Translate by sw/2 , sh/2

It is equivalent to doing the same thing sequentially.

Most the time you want to precede rotation and scaling by a centering translation, unto the center of the object you are drawing. This way it isn't skewed by the scaling.

In the case of the function you are asking about, the model it is drawing is already centered on 0,0 so rotation and scaling are applied evenly to the whole figure.

William Labbett

I see. The screen is a coordinate system with pixel (0, 0) as it's origin. The translation shifts the origin to the center of the screen. The scaling makes things stay the same size and the rotation rotated everything around the center of the screen.

Thanks a lot.

Edgar Reynaldo
William Labbett

Thanks Edgar.

Can I also use al_draw_prim to draw lines and other geometrical objects with different translucencies?

Edgar Reynaldo

Yes. In the ALLEGRO_VERTEX struct is a member called color or col or ... simply use al_map_rgba instead of al_map_rgb to create your vertex color.

kenmasters1976

I still struggle with Allegro transformations every time I need to use them. Given that Allegro is a multiplatform library, I wonder why it didn't adopt OpenGL style transformations.

Edgar Reynaldo

kenmasters1976 - the only difference between them is the order they are applied. Allegro transformations are pre-multiplied, and OpenGL transformations are post-multiplied. All you do is reverse the order you apply them in.

Personally, I think OpenGL is backwards. :/

William Labbett

Thanks both of you.

I managed to work out how to draw with blending by experimentation. The potential of allegro is slowly dawning on me.

kenmasters1976

Personally, I think OpenGL is backwards. :/

I've read that before. However, I learned some basic OpenGL ever since Allegro 4 with AllegroGL so I guess I'm so used to OpenGL that I can't easily think of transformations the other way around. That said, I can use Allegro 5 transformations whenever I need them, they just don't seem intuitive to me since OpenGL is what I'm used to.

Edgar Reynaldo

Generally, transformations follow this pattern :

1) Translate the center of your object to the origin
2) Rotate
3) Scale
4) Untranslate back to the correct position of the center of your object

This prevents your object from stretching or skewing or spiraling off course.

kenmasters1976, what is that in OpenGL? Something like this??

```glMatrixMode(GL_MODELVIEW);
glIdentity();
glTranslate(cx,cy,cz);
glScale(sx,sy,sz);
glRotate(rx , ry , rz)
glTranslate(-cx , -cy , -cz)
glMultiply(CameraMatrix)
```

Doesn't this require you to know the camera matrix?