Draw a textured quad in allegro5
cecco4

Hi,
I'm trying to develop a simple "3D engine", i have already done an algorithm that provide a list of quads to be draw on screen to create a 3d view; the problem is how texturize them.
I'm using al_draw_prim with ALLEGRO_PRIM_TRIANGLE_FAN, but i can't see how properly set it to get a good textured quad and not this crap:

{"name":"L3n5KdT.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/8\/b800b45cc0a0e8cf867bb06e9754d50c.png","w":800,"h":600,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/b\/8\/b800b45cc0a0e8cf867bb06e9754d50c"}L3n5KdT.png
All the vertexes setting are printed in the image.

Is it possible in Allegro? or i have to use OpenGL?

Arthur Kalliokoski

I'm pretty sure you need to use z values for the vertex locations as well, and rotate them with a matrix, otherwise you're trying to texture a trapezoid, which causes the effects shown.

cecco4

Defines the generic vertex type, with a 3D position, color and texture coordinates for a single texture. Note that at this time, the software driver for this addon cannot render 3D primitives. If you want a 2D only primitive, set z to 0. Note that when you must initialize all members of this struct when you're using it. One exception to this rule are the u and v variables which can be left uninitialized when you are not using textures.

Can this be a problem?

Also, i didn't find any example of al_draw_prim using the z value :-/.

Todd Cope

These functions will help you get started:

al_perspective_transform
al_set_projection_transform

If you have already calculated the 3D coordinates (x, y, z) for all of your vertices, you should just be able to pass them to al_draw_prim() and let it handle the rest.

Thomas Fjellstrom

A split texture like that makes me think you have the verticies in the wrong order. I think you want to make sure they are in CCW? And I prefer TRIANGLES vs TRIANGLE_FAN or TRIANGLE_STRIP. More data, but easier to process.

Arthur Kalliokoski

This image shows a trapezoid 2d layout of a brick wall on the left half, and the right half shows a proper rectangular image that's rotated.

{"name":"609058","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/6\/268557f9316ad50b613540449d867ad8.png","w":810,"h":629,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/2\/6\/268557f9316ad50b613540449d867ad8"}609058

The program and image (warp.c and brick2.jpg) are in the paperclip.

[EDIT]

I left an orthogonal matrix in it (glFrustum on line 124), change it to

  gluPerspective(90.0,4.0/3.0,1.0,30.0);

and it'll look a bit better but the effects still the same.

cecco4

I tried to change some z value but it doesn't affect anything in the texture since i go off-bounds (-1:+1) and it start to disappear:
{"name":"2TZiPlq.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/68ce9eb4c8f8fd2631195afd7a4262f1.png","w":800,"h":600,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/8\/68ce9eb4c8f8fd2631195afd7a4262f1"}2TZiPlq.png
I'm working with Allegro 5.0, i have to upgrade?

Arthur Kalliokoski's example works fine but i never used OpenGL and i have some problem with the vertex coordinate.
Maybe it's time to stop procrastinating and start to study it...

Arthur Kalliokoski

The parts that exceed -1.0 - 1.0 are "disappearing" because the normal 2D transform has set the viewing frustum near and far values to -1 and 1. You need to use al_perspective_transform() as Todd Cope said.

I haven't learned how to use A5 built in transform stuff myself, but I'll try to hack on it for 30 minutes to see if I can get it to work as in the OpenGL example above.

[EDIT]

AAAaannd I failed to do it. I was looking at ex_projection.c in the examples. Maybe somebody who knows can help?

cecco4

I studied a bit openGL and understood the basic coordinate system, however i think i simply need some image manipulation that took the bmp and stretch it in four point.
I can do it manually just by taking every columns of the bitmap and resize it to the wall projected height, but it is too slow.

Arthur Kalliokoski

The perspective correct texture mapping uses the rotation matrix to do that automatically using several "math chips" at once. Or should I say it does scan lines per triangle, which the orthographic trapezoid shows where the edges are, but the calculation differs for each triangle.

cecco4

I think this can be useful, but i don't understand the x,y,z used.

Thread #614834. Printed from Allegro.cc