Distort a Bitmap / resizing

I'm looking for a way to distort a Bitmap photo. I'm working with DevC++ and Allegro to create a game.

I want to turn a square photo I have in Bitmap format into a trapezius of even a triangle at max by giving it new corner values like x, xb, y, yb

I had such a function in Flash but can't find it for c++?!

I mean you could do it pixel line by pixel line with a simple resize function so I'm sure there's a good function for it...

BITMAP *a,*b;
a = load_bitmap("tile.bmp",NULL);
b = create_bitmap(lalax,lalay);
stretch_blit(a, b, 0, 0, a->w, a->h, 0, 0, b->w, b->h);
destroy_bitmap(a);
To my knowledge there is no built-in function to do it in WinGDI.

Usually, functions like this are part of a graphical library. Something like DirectX or OpenGL would be able to do this easily (and let the video card take care of the work instead of making the processor do it). But if you're sticking to WinGDI then to my knowledge you're pretty much out of luck unless you want to do it manually (ie: line by line as for your example, or manipulate the pixels directly, or somesuch).
Yes I would like to use OpenGL, I have Allegro GL installed already but just can't find an example of the code?!

When you type it for java or flash on google you get about 1000 hits but browsing for 6 hours today got me nothing?! I've been looking into the GL guides as well but all I see is the full 3D functions with z.

If you can help me this would be great.
http://nehe.gamedev.net <--- OpenGL tutorials. Note that the tutorials here for setting up OpenGL may not apply to you if you use Allegro. For that you're better off looking up an Allegro tutorial on how to set up OpenGL. I can't help you there as I have no experience with Allegro.

Note that OpenGL is a 3D lib, but that doesn't mean you can't use it for 2D (and in fact, 3D libs are great for 2D because they make things like rotation, stretching, etc very simple)

Instead of Blitting (which is the traditional 2D way to draw), 3D graphics involve textured surfaces. The concept is similar, but is executed differently. Blitting copies a portion of one surface to another, whereas in OpenGL you draw a textured polygon to the frame buffer. The result is more or less the same, except polygons can be twisted around and rotated and have all sorts of other cool effects done to them.

Follow the OpenGL tutorials until you can create textures and draw textured polygons. From there, all you need to do to stretch a polygon is move its verteces without changing its texture coordinates.
I followed the guide but all it explains is how to DRAW a rectangle and I want to use a photo in Bitmap format. This should be possible using the same piece of code but I really don't have a clue how to make it work for a bitmap. If someone can show me how to change it to spin a bitmap instead of the drawing this would be really appreciated.

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer

glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(1.5f,0.0f,-6.0f); // Move Right 1.5 Units And Into The Screen 6.0
glRotatef(rquad,1.0f,0.0f,0.0f); // Rotate The Quad On The X axis ( NEW )
glColor3f(0.5f,0.5f,1.0f); // Set The Color To Blue One Time Only
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd(); // Done Drawing The Quad

rquad-=0.15f; // Decrease The Rotation Variable For The Quad ( NEW )
return TRUE; // Keep Going
}
Yes and it loads it on an entire qube and that's not what I'm looking for?! There's also an easy way which doesn't involve having to rewrite my whole game onto a 3D engine I don't want to use. As I wrote in my first post Flash has a class for it there's multiple filters for Java so there's one for c++ I can understand as well.
Yes and it loads it on an entire qube and that's not what I'm looking for?!


It shows you the concept. It's up to you how to apply it to your particular problem.

1) Create a texture
2) bind it
3) use glTexCoord before you use glVertex to plot the text coords to use
4) when the quad is rendered, it will be textured

Basically glTexCoord is the points for the source rectangle, and glVertex is the points for the dest rectangle. Since you can set points independently, you can flip/stretch/rotate/whathaveyou.

You just have to play around with it.

There's also an easy way which doesn't involve having to rewrite my whole game onto a 3D engine I don't want to use. As I wrote in my first post Flash has a class for it there's multiple filters for Java


Flash is 3D vectoring image software. It basically is a 3D library.

Java has 3D functionality built in as well, which is undoubtedly uses to do stretching/rotating.

There probably are libs for C++, but they all wrap around other 3D libs (everything is 3D these days). There's really no practical way to do it otherwise, as all other ways are too computationally expensive to do in realtime, whereas 3D rendering is done by the video hardware and is very fast.

If you're looking for a "training wheels" lib that is aimed at making 2D features easier, I don't have any recommendations. Sorry. I'd say just bite the bullet and tinker around with OpenGL until you understand it. It really isn't all that difficult -- it's just very different from traditional 2D.

Maybe someone else has a lib recommendation.


EDIT:

Blah -- give me a bit -- maybe I'll whip something up you can use or at least learn from.


EDIT AGAIN:

Here. I didn't test this but it should be good:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//--------------------------------------
//  when the program first starts up and you're setting up your display

glMatrixMode( GL_PROJECTION );      // we want a 2D ortagonal matrix for the projection
glLoadIdentity();
glOrtho(0, width, 0, height, 0, 1); // this is width/height of the display window in pixels
  //  note that in 3D graphics, positive Y is up, but in 2D graphics, positive Y is down
  //  if you want to make the image upside-down so it's more like 2D, you can do this instead:
  //  glOrtho(0, width, height-1, -1, 0, 1);

//---  do all the other stuff the NeHe tutorial told you to do, like
//  enabling 2D texturing, and all that stuff



//-------------------------------------
//-------------------------------------
//-------------------------------------

// here's a simple struct to represent a texture:
struct Texture
{
  GLuint   id;   // the ID of the texture (as given to you by glGenTextures)
  double   invw; // set this to "1.0 / image_width" when you load your bitmap
  double   invh; // set this to "1.0 / image_height"
};



//-------------------------------------

// here's a simple function to prepare a texture for drawing

void PrepareTexture(const Texture& tx)  // pass this the Texture you want to draw with
{
  glBindTexture( GL_TEXTURE_2D, tx.id );

  glMatrixMode( GL_TEXTURE );
  glLoadIdentity();
  glScaled(tx.invw,tx.invh,1.0);
}

//-------------------------------------
//-------------------------------------
//-------------------------------------

// Then when you want to draw a quad, you'd do this:
//   This example draws a 'wd' by 'ht' square from coords 'srcx','srcy' on the texture
//   and draws them to coords 'dstx','dsty' on the screen
//
// Note this assumes that positive Y = up

PrepareTexture( whatever_texture_you_want_to_draw_with );
glBegin( GL_QUADS );

// upper left corner
glTexCoord2i( srcx, srcy );   glVertex2i( dstx, dsty - ht );

// upper right corner
glTexCoord2i( srcx + wd, srcy );  glVertex2i( dstx + wd, dsty - ht );

// lower right corner
glTexCoord2i( srcx + wd, srcy + ht );  glVertex2i( dstx + wd, dsty );

// lower left corner
glTexCoord2i( srcx, srcy + ht );  glVertex2i( dstx, dsty );

glEnd();


Note that one of the caveats with OpenGL is that textures MUST have power of 2 dimensions. That is, a texture that's 256x128 is fine because those are both powers of 2. However 256x200 is no good.
Last edited on
Thanx for the help it is really appreciated but I'm just going to write the game in Flash. With all this OpenGL code I just don't understand my game anymore and I do with the distort class in flash.
Topic archived. No new replies allowed.