Drawing only part of a two-dimensional image

Started by Ahzraei, November 08, 2005, 04:07:48

Previous topic - Next topic

Ahzraei

I'm working on a mostly two-dimensional, tile-based game. I've already written a working map editor in C++ (I already had some of it written before I learned about LWJGL, so I just didn't bother to port it) which uses a master tileset image (260 tiles in all, and with 32x32 tiles, it's 416x640 pixels). The map editor, which uses a very small directx wrapper that I didn't write myself, has support for drawing only a small rectangle of the larger image (s single 32x32 tile, obviously) on the screen. The editor then saves the map as data that can be read from my java app.
I've examined some of the sample code available on the lwjgl website, specifically the Space Invaders example, and I've adapted the two-dimensional converting code (even though I don't really understand it ... if anyone has tips on 2d adaptions I'd appreciate it), but I can't figure out how to draw only a specific section of the image that I've loaded. After messing with the rendering code for a while, all I was able to do was scale the image (not really useful).

I'm sure there's a way. Does anyone know it? I really don't want to chop up my 416x640 image in Paint or Photoshop and save 260 tiles individually (and I am planning to make this dynamic for even larger tilesets, not to mention that I'd like to easily make changes to the graphic).

hvor

Quote from: "Ahzraei"but I can't figure out how to draw only a specific section of the image that I've loaded.
There is a way. Each point in space (glVertex2f, glVertex3f...) has texture coordinate connected with it (glTexCoord2f...).  Texture coordinates are :

0,1    1,1

0,0    1,0

imagine them on top of your image. So if you want to map only bottom half of your texture onto quad, for example, you will have 4 points in space (glVertex) and 4 texture coordinates attached to them, but in range:

0,0.5   1,0.5

0,0      1,0

example of full image maped on quad (done by triangle_strip):
               GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
                {
                    GL11.glTexCoord2f(1.0f, 1.0f);
                    GL11.glVertex3f(x + quadSize, y + quadSize, z);
                    GL11.glTexCoord2f(0.0f, 1.0f);
                    GL11.glVertex3f(x - quadSize, y + quadSize, z);
                    GL11.glTexCoord2f(1.0f, 0.0f);
                    GL11.glVertex3f(x + quadSize, y - quadSize, z);
                    GL11.glTexCoord2f(0.0f, 0.0f);
                    GL11.glVertex3f(x - quadSize, y - quadSize, z);
                }
                GL11.glEnd();
b]Hvor Games[/b]

Mr EEK

Ahzraei,

What hvor says makes sense, but I want to point out that OpenGL usually likes textures to have dimensions that are a power of two.  Your "master" image size is not really appropriate; something like 512x512 would be ideal.

I believe there is an OpenGL extension that allows non-power-of-two textures, but unfortunately I don't know anything about it (being something of a noob myself  :D )

Also, I believe that many implementations do not allow textures greater than 512 pixels either.

Hope this helps.

Ahzraei

thanks, hvor, I'll try that out.

I am familiar with the image dimensions issue, not sure why that is, but directx and I guess opengl trip out when I tell them to draw images that aren't powers of two. Anyway, I should have made it clear, all the images I'm using are extended to the nearest power of two (so my tileset, for example, is actually 512x1024, only the rest of it is transparent)