Loading a map from a texture

Started by
11 comments, last by MajorShredd 18 years, 1 month ago
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.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Advertisement
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.
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.
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!
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
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.
Is there a way to do it after the texture is loaded and not during?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
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.
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.
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.
So what do you recommend?
Thanks.
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky

This topic is closed to new replies.

Advertisement