Sign in to follow this  
daniel_i_l

Loading a map from a texture

Recommended Posts

Lets say I want to load a texture and use it as a map to place trees and buildings (green=tree,red-building,..). I have an array of may map that holds an ID number (tree =1, building=2..). How do I use the texture (saved as a GLuint) to get data for the map? Before I posted my own code (it might still be at the bottom) and it didn't seem to work. What is the best (or simplest) way to do this? Thanks.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I am a little confused where you are having problems.

All you would do is read your image file. For instance there are plenty LoadBMP() or LoadTGA() functions that will read a bitmap or targa file (can be found on Nehe.gamedev.net's tutorials sections) that would give you access to pixel data as well as width and height. Once it is loaded, (not so much applyed to a texture on a mesh) read in the width and height of the image and start going through each pixel's color and increment a col and row counter so you know where to place the object that you find in the image. If the color is red place a building at (col,heightmap-y-value,row) in your 3d or (col,row) for your 2d environment.

Now if your image is 256x256 but your world is 512x256 you will need to decide if you want to continue loading the game or figure out where to put your objects based on stretching or shrinking the place where the object is positioned. x = (col * (worldmap_width / objectmap_width)) and y = (row * (worldmap_height / objectmap_height)). Formulas are not tested.


Now there is another thing to worry about which you did not mention.... how big are these green and red dots.... are they always 1x1 pixels and if you have a cluster of 2x4 pixel of trees is that a forest or a really big tree. likewise with buildings.


Another thing I just thought of while placing buildings. Trees you can get away with, but buildings usually have a door on one side and windows, etc.... anyways you need a direction of which way that door is facing, if you don't mind where the door is facing then this will work, but if it does not, you will need to come up with anohter format that will tell the program which direction the house is facing.

A posible format could be

[Buildings]
number of buildings
col, row, type of building, direction it is facing, color, etc
col, row, type of building, direction it is facing, color, etc
col, row, type of building, direction it is facing, color, etc
col, row, type of building, direction it is facing, color, etc
....

[Trees]
number of trees
col, row, type of tree
col, row, type of tree
col, row, type of tree
col, row, type of tree
...

An XML file would work but depending on the setup, this could be a fairly huge file that could become a savegame file in the future if you happen to chop down these trees or destroy or start building buildings, etc and cannot finish the game in one sitting.

Share this post


Link to post
Share on other sites
Why use the numbers 1,2? for ID's shouldn't they be the numbers of the colors you used in your texture image? unless you want to have those ID's look for a specific color. I would think using grayscale with 0-255 value would be better but thats just me that way each value would represent a different object. Or you could use red green blue and have more then 256 objects.

Share this post


Link to post
Share on other sites
I know how to load the texture, I'm using NeHe's IPicture. What I'm having trouble with is reading the pixel data after loading the texture. Lets say I loaded the map into GLuint texture[0] with the IPicture, how can I read the pixel data or as you said:
"read in the width and height of the image and start going through each pixel's color and increment a col and row counter so you know where to place the object that you find in the image. If the color is red place a building at (col,heightmap-y-value,row) in your 3d or (col,row) for your 2d environment."

How do I do that?
Thanks a lot!

Share this post


Link to post
Share on other sites
Ah, creativity. Gotta love how it can both inspire and frustrate at the same time :)

EDIT: you posted just seconds before me =P Anyway, if you're using someone else's library, it must have some sort of pixel indexing scheme (I'm assuming it holds the raster data and at least the width/height or width/number of bytes in memory). If it is possible, try extending the class to give you access to each pixel as an RGB value. That way you can just pick the RGB values out of the object and do what you will with them.

Share this post


Link to post
Share on other sites
there is nothing stopping you from finding the data after.... unless your clearing the texture[] array???? what exactly is your problem I don't think any of us really understand. How about posting some of the code you are having problems with so we can maybe help you a little bit more with your issue.

Share this post


Link to post
Share on other sites
Quote:
Original post by daniel_i_l
Lets say I loaded the map into GLuint texture[0]


Very bad things....

You are Not using the image as a texture, you are only using it as a Map Data Format; is never actually used as a texture to draw on stuff, Right?

then WHY? are you loading it into OpenGL's video memory?? where it becomes slow and difficult to read the pixel data back from it?

Quote:
Original post by daniel_i_l
Is there a way to do it after the texture is loaded and not during?
Thanks.


Really, instead of loading the texture with opengl's stuff, you should just save the bitfield someplace conveneient... most likely into your own map struct or class. Then reading from it is a simple matter of... well, reading from it.

Share this post


Link to post
Share on other sites
Quote:
Original post by DreamGhost
there is nothing stopping you from finding the data after.... unless your clearing the texture[] array???? what exactly is your problem I don't think any of us really understand. How about posting some of the code you are having problems with so we can maybe help you a little bit more with your issue.


He's using NeHe tutorial code to load his image. The problem is that NeHe is most likely sending the data to the video card, then clearing whatever temporary texture[] buffer was used in system memory. And I doubt NeHe ever bothered to explain the concept...

consequently, daniel here has his 'map texture' sent off to the GPU where he can't get at it, and doesn't know the details of the loader code well enough himself to modify it to instead save that texture[] buffer someplace where he can keep using it.

Share this post


Link to post
Share on other sites
Quote:
Original post by daniel_i_l
So what do you recommend?
Thanks.


Cut Nehe's image loader code in half.
You only want the first part, where it reads the image file.
The second part, where it sends the data to OpenGL you should throw away.

After reading the file, copy the data into your own Map Structure.
Your own Map Structure will be custom tailored to give you easy access to the data you need, and it should be saved long after the image file itself is closed.
Live happily ever after.


The Details of Implementation are left as an exercise for the reader... I do not use NeHe's code so I am not familiar with the specifics of what lines need to be changed.

Share this post


Link to post
Share on other sites
Ah I see well I wasn't understanding him I just figured he was placing it into some sort of array, and couldn't figure out how to get the data out of it. Yeah its bad to place things into video memory if your not going to be using it for texturing and actually to begin with shouldn't you always load it into some structure on system memory before loading it into video memory anyhow especially when you want to do things such as mipmapping and manipulate the way an image looks before storing it onto the card's memory. Atleast that is my understanding.

Share this post


Link to post
Share on other sites
Best advice possible (and I'm surprised nobody mentioned this) - if you're a beginner, learn to write your own code before using someone else's. Not only will you get better at solving problems, but you will (surprisingly) save yourself headaches!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this