View Full Version : OpenGL Isometric Tile Engine
Stalin55
2006.08.15, 09:27 PM
First of all, I'm not very experienced with OpenGL but I have enough basic knowledge of it to find my way around and figure out how to do stuff. Secondly I have a lot of experience programming 2d isometric tile engines. I am looking into the possibility of creating a 2d isometric engine(much like Diablo) with OpenGl, but I'm not quite sure which is the most practical and efficient way to go about doing it. Can anyone give me a somewhat detailed description of the best for creating a 2d isometric tile engine with OpenGL?
Stalin55
2006.08.15, 10:36 PM
I was just fooling with texturing quads (each quad and texture map is 80x40 pixels) using pre-rendered isometric tile bitmaps (2:1 ratio) as seen in a mock bitmap image below...
00008800000
00888888000
00008800000
....and I guess because the texture image isn't perfectly square OpenGL is not letting me apply the texture, is there some way around this (or perhaps this is the wrong method all together)?
OneSadCookie
2006.08.15, 10:36 PM
Put your tiles in textures and draw them on quads. I'd be tempted to actually use a 3D world and an isometric projection rather than do everything with bitmaps, but you could go either way. If you go the bitmap route, you may need to turn on alpha testing to get things to draw correctly.
OneSadCookie
2006.08.15, 10:38 PM
Textures in OpenGL must have power-of-two dimensions (eg. 128x64), unless you use the (ARB|EXT|NV)_texture_rectangle extension. The extension is available on all hardware newer than the Rage 128.
Stalin55
2006.08.15, 11:20 PM
How do do transparent textures where the white pixels are transparent?
OneSadCookie
2006.08.15, 11:27 PM
You don't; you do transparent textures where the transparent pixels are transparent.
Stalin55
2006.08.15, 11:29 PM
How exactly do I do that?
OneSadCookie
2006.08.15, 11:33 PM
Using alpha testing and/or blending.
Stalin55
2006.08.16, 01:00 AM
I fooled around using all different combinations of glBlend() and I can't seem to find a way to make part of my textures transparent. I'm using tgas and I tried removing the background layer in photoshop because I read that tga supports alpha channels, and that still didn't work.
aarku
2006.08.16, 01:20 AM
You want glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Then draw from farthest to nearest.
You could use the OpenGL Profiler application to confirm that your textures are right as your game is running.
-Jon
Stalin55
2006.08.16, 01:57 AM
You want glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Then draw from farthest to nearest.
Thats exactly the function that I used and is doesn't work.
To be more specific about the program, I have one bitmap texture containing a 2:1 isometric tile image and I texture it to a 128x64 quad on the screen. Then texture another identical quad (over top quarter first quad).
OneSadCookie
2006.08.16, 01:58 AM
The dimensions of the quad are irrelevant; the dimensions of the texture image are what matters.
What precise behavior are you seeing, and what precise code are you using?
imikedaman
2006.08.16, 02:42 AM
Am I the only one who realizes blending the alpha components doesn't work if the source image doesn't have an alpha channel? The guy said multiple times he wants the white pixels to be transparent, which implies the image doesn't already have transparency.
Anyway, when binding your texture you need to have an alpha channel, or 4 bytes per pixel. I'm assuming your image is just RGB right now, but you could probably get away with something like this:
char * data;
int width, height;
// read in RGB image here
// let's assume data now contains the RGB pixels at 3 bytes per pixel
// and width and height contain the width and height of the image
int size = width * height;
char data2[size * 4];
int m, n;
for (n = 0; n < size; n++) {
m = n * 4;
data2[m] = data[m]; data2[m+1] = data[m+1]; data2[m+2] = data[m+2]; // copy the RGB values
if (data[m] == data[m+1] == data[m+2] == 1.0) // if pixel = white
data2[m+3] = 1.0; // make transparent
else
data2[m+3] = 0.0; // make opaque
}
// bind texture as RGBA instead of RGB
The code is untested and could probably stand to be optimized, but that's one way of making perfectly white pixels transparent. However, you're much better off just giving the source image an alpha channel in an image editor and using a texture loader that supports transparency.
OneSadCookie
2006.08.16, 05:38 AM
I said earlier that he should use transparent pixels, and he is using tga, so there's no problem with the image format not supporting it.
So not only is your code broken, and ugly as sin, but it's completely unnecessary.
arekkusu
2006.08.16, 11:51 AM
Am I the only one who realizes blending the alpha components doesn't work if the source image doesn't have an alpha channel?
Sure it does. In the fixed function pipeline, textures are modulated with the current vertex color by default. So regardless of what the texture alpha is, if you glColor4f(1, 1, 1, 0.5) before drawing you should see some blending.
But seriously, why is loading a translucent texture so hard for everyone? I've seen this same thread a dozen times now.
Do we need to write "draw a green triangle" and "draw a texture mapped triangle" sample code?
imikedaman
2006.08.16, 12:27 PM
So not only is your code broken, and ugly as sin
Broken? No. Ugly? Well, you think anything that isn't pure Obj-C is ugly. :D
Stalin55
2006.08.16, 04:14 PM
After checking over my texture format a dozen times in PS, I realized that the alpha mask was inverted, or something like that. Well thanks for all the help anyways.
OneSadCookie
2006.08.16, 06:29 PM
Broken? No. Ugly? Well, you think anything that isn't pure Obj-C is ugly. :D
Broken? Yes... you have confused floating point and integer colors.
Ugly? I don't care what language it's written in, I think anything with an excess of single-character variable names, lack of braces, unhelpful comments, etc. is ugly. ObjC is certainly not my favorite language, by a long way.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.