Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Giving tiled maps perspective?
- - By Rukiri Date 2015-09-05 16:11
Fig.1 - your standard tilemap

Fig .2 - tilemap with perspective


Hey, I got a pretty simple question and I'm sure openGL is needed.
But does anyone know how to give their tilemaps perspective by just using the camera and scaling?

if (floor != null) {
      for (int y = (int) horizon; y < height; y++) {
        float distance = (camera.z * scale.y) / (y - horizon);
        float ratio = distance / scale.x;

        double dx = -sin * ratio;
        double dy = cos * ratio;

        double sx = camera.x + distance * cos - width / 2 * dx;
        double sy = camera.y + distance * sin - width / 2 * dy;

        for (int x = 0; x < width; x++) {
          int cx = (int) Math.abs(sx % floor.getWidth());
          int cy = (int) Math.abs(sy % floor.getHeight());
          int color = floor.getPixel(cx, cy);

          setColor(color);
          drawPixel(x, y);

          sx += dx;
          sy += dy;
        }
      }
    }

This was from an old example I had laying around, tried converting it into ruby and without success :(
I'm using TileD to make my maps if that means anything.

And to make this easy to understand, the effect I'm trying to recreate is Snes's Mode7 effect.

Thanks
Parent - - By RunnerPack Date 2015-09-05 19:41 Edited 2015-09-05 20:25
You don't really need to do stuff like that pixel-by-pixel with OpenGL or Gosu. They take care of mapping texture pixels to screen pixels for you.

Gosu has "Image#draw_as_quad". Just figure out where the four corners of your image should be in screen space, and send the coordinates to "draw_as_quad".

EDIT: Actually, I just tried this, and there is no perspective correction, so it looks badwrong :P It might depend on the OpenGL implementation, though. It should still be possible to do it using OpenGL directly, or by drawing the image as a number of quads/triangles (i.e. a "triangle fan").

Loading a TileD map in Gosu - or converting it to a picture Gosu can load - is another topic. A forum search will probably turn up some results.
Parent - By jlnr (dev) Date 2015-09-05 22:01 Edited 2015-09-05 22:31
I don't think there's an easy way around the perspective correction issue, either. Gosu does not use Z coordinates in its OpenGL code – the Z ordering happens in software. It's probably easier to use OpenGL and this flag:

GL.Hint(GL::GL_PERSPECTIVE_CORRECTION_HINT, GL::GL_NICEST)
Up Topic Gosu / Gosu Exchange / Giving tiled maps perspective?

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill