Skip to main content
gamedev.net
🔒 Locked

Texture Format

Started by Omniscience Oct 3, 2008 at 9:03 PM 12 replies 1.2k views
Original Post
Omniscience
Omniscience
Hey, what kind of internal format for textures are created when you use the lil' texture function that does all the crap for you? For example, if I were to take a class containing a vector of raw rgb 0-255 integers (lets say a 2 x 2 with all 12 color values 0, 4 pixels, 3 color values each, meaning black), how would I have to modify or format this data to where OpenGL can accept it as an image? The reason I ask is that I want to load images using my own Image class, and store raw rgb in each class. I'll write my own image loading, so that's not a problem. Assume I already have the image loaded into raw rgb.
------------------------------I wish I could change the world...But they won't give me the source code.
deadstar
deadstar
Simple as this?

glTexImage2D(GL_TEXTURE_2D, 0, 3, ImageWidth, ImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, RAWPixeldata);


I hope so anyway.
"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk
Omniscience
Omniscience
Quote:
Original post by deadstar
Simple as this?

*** Source Snippet Removed ***

I hope so anyway.


RAWPixeldata

Yeah, I saw that, but how exactly do I generate a "RAWPixeldata"? How is the data passed to it?
------------------------------I wish I could change the world...But they won't give me the source code.
Yann L
Yann L
Quote:
Original post by deadstar
glTexImage2D(GL_TEXTURE_2D, 0, 3, ImageWidth, ImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, RAWPixeldata);

Aaargh ! Death of OpenGL due to NeHe... [wink]

Seriously people, please stop doing this. I've seen code posted using this at least four times during the last couple of weeks. The third parameter of glTexImage2D is not a channel count ! It's an internal format: GL_RGBA8, GL_INTENSITY8, RGB16F_ARB, etc.

Quote:

RAWPixeldata

Yeah, I saw that, but how exactly do I generate a "RAWPixeldata"? How is the data passed to it?

It's just a void pointer to your raw image data.
V-man
V-man
http://www.opengl.org/wiki/index.php/Common_Mistakes#Texture_Precision
Omniscience
Omniscience
Quote:

It's just a void pointer to your raw image data.


Yeah, but OpenGL isn't going to know that I use Height x Width vectors of color classes which hold 3 values called R, G, and B...How do I have to set this data up so that OpenGL knows what it is?
------------------------------I wish I could change the world...But they won't give me the source code.
Yann L
Yann L
Quote:
Original post by Omniscience
Yeah, but OpenGL isn't going to know that I use Height x Width vectors of color classes which hold 3 values called R, G, and B...How do I have to set this data up so that OpenGL knows what it is?

Are you saying you have a vector for each single pixel in the image ?

If so, then this is an extremely inefficient way of storing image data. And there is no way to make OpenGL accept this data structure directly. OpenGL doesn't know anything about standard library containers. The only way would be to create a temporary continuous memory buffer with the actual raw image data. Then again, you'd be much better off dropping the entire one vector per pixel concept.
Omniscience
Omniscience
No, I mean I use vectors of size height x width. XD you thought I used a vector or EVERY pixel in an image? I use a vector for every image, and that vector contains HxWx3 int's stored in classes. So really, if I have 3 5x5 images, heres the list.
vector img1
25 pixel classes
each class gets 3 int's for RGB
vector img2
25 pixel classes
each class gets 3 int's for RGB
vector img3
25 pixel classes
each class gets 3 int's for RGB

I still need to know how to get these numbers to OpenGL...
------------------------------I wish I could change the world...But they won't give me the source code.
Yann L
Yann L
Your explanations are very confusing, and it's not really clear how you store your data. I would suggest you post the code that defines the structures you use.
Omniscience
Omniscience
class Color{	public:		int R,G,B;	private:};class Image{	public:		vector<vector<Color>> Pixel; //A 2D vector that will end up [Width][Height] in size when an image is loaded.	private:};Image ImportImage(const char * FileName){	Image Img;	//populate all Img.Pixel with rgb values.	return Img;}Image MyImg = ImportImage("MyImg.bmp");//MyImg will now hold all pixels of MyImg.bmp as integers stored in a 2D vector object.

Assume that this is already working. I just need to get the MyImg instance to OpenGL for use as a texture
------------------------------I wish I could change the world...But they won't give me the source code.
Yann L
Yann L
This will not work. That's not how OpenGL operates.

The main problem is the vector of vectors. As I said above, OpenGL doesn't know about vectors. Instead, you have to provide a single vector holding the entire image data, row for row. Then you supply OpenGL a pointer to the first element in the vector.

Next, using the colour class as you did will trigger undefined behaviour. OpenGL usually assumes the pixel data to be tightly packed. Your class could contain additional invisible padding added by the compiler, which will make it essentially unusable for streaming to OpenGL. You can use packing pragmas to ensure tight data packing or store the image data as a raw char stream.

Finally, current GPUs don't like 32 bit integers as per-channel data type. So OpenGL will have to convert the data to 8 bit per channel, which increases upload time. Actually, very few image file formats support 32 integer bits per component, most will be 24 or 32bit per pixel. So you should use chars instead of ints.
Omniscience
Omniscience
Yeah, I know that's not how OpenGL works, but that's how my engine is storing images. I can access any pixel of the image like this:
Img.Pixel[x][y]
So, I am at this point free to do whatever it takes to get it to OpenGL. I just don't understand how that's done...I've seen images stored as strings before, and that looks tacky and doesn't seem very reliable. You mean I can just keep a normal 1D vector with each row of the image just running right after each other? sort of like if I took the image and copied each row onto the end of the row above it until the image was 1px high, then give OpenGL the first pixel in the vector? I don't think I understand why OpenGL wants that, or am I wrong?
------------------------------I wish I could change the world...But they won't give me the source code.
Yann L
Yann L
Quote:
Original post by Omniscience
So, I am at this point free to do whatever it takes to get it to OpenGL. I just don't understand how that's done...I've seen images stored as strings before, and that looks tacky and doesn't seem very reliable.

It's just as reliable as any other method. Pixel access is done slightly differently, but it is the standard way large pixel data is stored. It's the most memory efficient layout and sequential access is fastest. You can still access each pixel individually by using a little math, eg:
const Color &GetPixelColor(int x, int y){   return( imageVector[y * imageWidth + x] );}


Quote:
Original post by Omniscience
You mean I can just keep a normal 1D vector with each row of the image just running right after each other? sort of like if I took the image and copied each row onto the end of the row above it until the image was 1px high, then give OpenGL the first pixel in the vector?

Yes, exactly. Just make sure everything is packed correctly, and that no padding is applied by the compiler.

Quote:

I don't think I understand why OpenGL wants that, or am I wrong?

Well, as I said, this is the most common way pixel data is stored.
deadstar
deadstar
Quote:
Original post by Yann L
Quote:
Original post by deadstar
glTexImage2D(GL_TEXTURE_2D, 0, 3, ImageWidth, ImageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, RAWPixeldata);

Aaargh ! Death of OpenGL due to NeHe... [wink]


Ahh Goddamn, copied the wrong line. I can only apologise.

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2   A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.