Jump to content
  • Advertisement
Sign in to follow this  
gazwik

3D Tile based game [XNA] [RESOLVED]

This topic is 3124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, first post on forums. Searched for answer a lot but not found it :( The problem I have I _think_ is less of a coding issue, more of a complete-lack-of-understanding of XNA/DX :D Anyway, I am trying to develop a 3D tile based game (3D as has an angled rotating camera and will have some models in). I have a collection of Texture2D for graphics and each layer of my map consists of 200x200 tiles each storing an X/Y and a graphic Id. I've seen a lot on terrain making so I started by created a triangle strip with shared verticies. I then loop through each visible Tile, set the device.Textures[0] = tileTexture, calculate the starting vertice, and call DrawIndexedPrimitives. This does appear to work, although there is an Effect.Begin/End for EVERY tile which doesn't feel right and its deffo not as smooth as I would have thought. Also the result being I end up with some of the tiles not filling the quad (depending on zoom/camera position), so theres a 1 pixel gap between some of the tiles. If I set the wrapping mode to border I can see a colored line and its actually more frequent than I thought. Am I going about this completely the wrong way? Please enlighten me. Have just been investigating the idea of passing all the textures to a custom shader so there is a single effect pass but wasn't sure if that was the correct approach either as there could be 50x50 textures passed in. Cheers for any ideas/thoughts - feel free to rip into my noobness :] gaz [Edited by - gazwik on November 2, 2009 7:36:15 AM]

Share this post


Link to post
Share on other sites
Advertisement
A standard solution when having a lot of small textures is to use a texture atlas. This divides a single large texture into a lot of small logical textures. It can be a little tricky, but I'm sure googling will bring you some more info.

For your "terrain" I'd suggest using indexed triangle lists without shared vertices. That's simpler than a triangle strip and more applicable to what you're doing.

Share this post


Link to post
Share on other sites
Hi ET3D, thanks for replying.

I did what you suggested, I now have a single texture containing all the tile graphics and I'm only doing one effect pass for all tiles. I also re-build the vertex/indicies list in the Update() code for when camera moves (sound correct?)

The result being its noticably quicker and drawing code is massively simplified - so thanks for that :D

However it seems to suffer from a possible rounding error when setting the texture coords - ocassionaly I get a 1 pixel strip of the next texture. Is this to be expected? I'm sure I can compensate for it

Cheers for your help

gaz

Share this post


Link to post
Share on other sites
gazwik

Where did you found information about using tilesheets?
I also wanted to use texture atlas for one of my project but there is a lack of information how to calculate coords and make alot of small textures out of big and specially how to this in XNA.



Can you share this part of code (ie. drawing code)?
You can PM if you don't want to show it here.


I don't need all of your code, just what works with atlas, calculates texture coords out of it, textures.


Thanks.

Share this post


Link to post
Share on other sites
Hi BytePtr,

Well all my tiles (in this instance) are 64x64 so I simply combined them into a single Texture like so:

Practical Texture Atalases

When creating the vertices, I simply set the Texture coordinates to be: (rough example)

TopLeftCoord.X = (1.0f / <numtilesX>) * <tileindexX>;
TopLeftCoord.Y = (1.0f / <numtilesY>) * <tileindexY>;

I then load the entire texture to the device:

e.g. device.Textures[0] = myTex;

Instead of what I was previously doing and loading each tile seperately and calling a pass for EVERY tile (slow)

To actually create the atlas texture, I coded a seperate module using GDI+ which is seperate to the game.

Hope thats some use? googling "texture atlas" did wonders

gaz

Share this post


Link to post
Share on other sites
I also have textures 64x64. Even 2048x2048 texture atlas.


But can you explain the tileindexX and tileindexY more?


I understand that numtilesX and numtilesY are number of tiles in X,Y?

So if i have 2048x2048 texture where each tile is 64x64 then i have 32 tiles in X and 32 tiles in Y.

So numtilesX = 32, numtilesY = 32.


Correct?


But tileindexX and tileindexY?



I would like to get each texture by ID. So i pass the routine ID and it calculates me correct texture coordinates. This is my plan.

Share this post


Link to post
Share on other sites
tileIndexX and tileIndexY are the "texture by ID" numbers you refer to

You should know which tile you need to get access to and you have to scale it down to be a number between 0.0 and 1.0

Suggestion: start a new thread?



Any takers as to why I "occasionally" get an extra line of the adjacent tile in my atlas drawn? Maybe its the maths I just explained to byte and a possible rounding error?

Share this post


Link to post
Share on other sites

"Solved" my problem. Long discussion @

XNA Forums Post

if anyone else interested.

I took the option of aiming for half a pixel in, and half a pixel from the target far edge.

Thanks ET3D for previous help

gaz

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!