Copy regions of a texture

Started by
15 comments, last by XTAL256 16 years, 1 month ago
I have an Image class for my 2D game, it uses SOIL to load an image from file and create a texture. I store the texture id in my Image object. My plan is to texture map a quad when i want to draw the image, this is so that i can easily change the colour by setting the vertex colours. So i can use a greyscale image and colour it. Now, what i want to do is split one image (texture) up into smaller ones. I give the function an array of structs that have the x, y, width and height of each small image. I want to use something like glCopyPixels or something. It would be good to create textures straight from it but if i end up with raw pixel data i can use SOIL to create a texture from it. thanks
[Window Detective] - Windows UI spy utility for programmers
Advertisement
glCopyTexImage2D I believe.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Oh, yes. I think i have used that before. But doesn't that copy the screen buffer to a texture? I want to copy a texture to another texture.
I was just thinking that i may store the image data in the object and draw the pixel data to the screen instead of texture mapping a quad. But i remembered that i need to either tile or stretch the image. Using a texture i can use GL_REPEAT or GL_CLAMP when i create it, that way i don't have to do it myself. But can you do that by drawing pixels (glDrawPixels is it?) I guess i could tile the image myself but can it stretch the image?

EDIT: Mmmm, i just realised that i can use glPixelZoom to stretch along x and/or y axis but now i have another problem. How to rotate!? I am planning on rotating images, i.e. if the player shoots their gun. They can aim up/down so i would draw the weapon fire (say, for a laser gun) as a quad rotated by the aim angle. But i can't easily (if at all) rotate pixel data. I think i will make another class called Sprite which has the image as a texture, and Image will contain pixel data. Does that sound good? (i've never made a game before so i am acomplete n00b at this)
[Window Detective] - Windows UI spy utility for programmers
Hi Xtal256.

You could use render-to-texture functionality. The splitting can be done by rendering parts of the large images to a framebuffer attached texture. You probably want to look at the FBO extension (see http://www.opengl.org/registry/specs/EXT/framebuffer_object.txt) if it's supported.

Else you would have to render it to your regular framebuffer first (see OpenGL Programming Guide, chapter 10), then copy back the subimages with glCopyTexImage.
Mmmm, i think i used a framebuffer once before but i couldn't get it to work. I don't think it's supported by my crappy video card.

I have just made another class, Sprite, like i described above. But i need to split Sprites as well as Images. I could easily create a texture from raw data so i think i will do it the second way ndhb said.
I have done this stuff once before where i copied the screen to a texture for a Windows Media Player visualization but it was a while ago, i will have to go back and look at it.
[Window Detective] - Windows UI spy utility for programmers
Why do you need to split your texture up? If you were thinking of using spritesheets or something similar, generally all you need to do is assign different texture-coordinates to your quads. Same thing for rotating and scaling images, just rotate and scale the quad.

[Edited by - Luctus on March 15, 2008 8:11:43 AM]
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams
Quote:Original post by Luctus
...Same thing for rotating and scaling images, just rotate and scale the quad.

Yes, that's what i'm doing. But i can't rotate pixel data, which is the problem i had. I fixed that problem by deciding to use textures for rotating, etc and just an array of GLubyte pixel data for images that don't need to be rotated (the 'map', GUI buttons, etc).

Ok, i am really confused. I don't know whether to store images as raw pixel data (GLubyte*) or as OpenGL textures and store the texture id as a GLuint. Is there a best way or not, 'cos i want to use the second method but i don't know if it will work for everything i want to do. For example, my map will be deformable so the player can blow a hole in the wall, etc (like Worms). So i will need to manipulate individual pixels which means i will need to use raw pixel data.
[Window Detective] - Windows UI spy utility for programmers
Rotate pixel data.......uh what? What are you trying to accomplish?

If you rotate the quad then you have the pixels rotated..

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

You don't seem to understand me. If i understand correctly, OpenGL allows you to work with images in two ways:
1. Create a texture from an image then draw a quad with that texture,
2. Use the raw pixel data, a pointer to GLubyte, and use glDrawPixels.
Using the second way, you cannot rotate the image. That is, you cannot call glDrawPixels with an angle, it just draws a rectangle. I don't want to use that method since i want to rotate, but i also need to manipulate individual pixels which i don't think you can do with a texture since it stores the data internally and only gives you an id. I think i have figured out what i need to do.
[Window Detective] - Windows UI spy utility for programmers
Quote:Original post by XTAL256
You don't seem to understand me.
No, I'm afraid not.
Quote:
If i understand correctly, OpenGL allows you to work with images in two ways:
1. Create a texture from an image then draw a quad with that texture,
2. Use the raw pixel data, a pointer to GLubyte, and use glDrawPixels.
Basicly, yes.
Quote:
Using the second way, you cannot rotate the image. That is, you cannot call glDrawPixels with an angle, it just draws a rectangle. I don't want to use that method since i want to rotate[...]
I really can't think of a case where you'd want to use glDrawPixels, no.
Quote:
[...] but i also need to manipulate individual pixels which i don't think you can do with a texture since it stores the data internally and only gives you an id.
If you really can't pre-generate all the picture data you need and have to modify it dynamicly, then you're in a whole different ballbark and would probably be best off using some kind of two-pass rendering method using pbuffers or something similar as have already been hinted at.

Anyway, I'd first recommend you to read through the OpenGL Red Book (newer editions are avaliable in print), especially the chapter on texturing.

Finally, to give you a hint of what I was talking about, this is almost verbatim (at least conceptually) what I'm using in my sprite engine:
typedef Texture GLuint; // introduce a new name for opengl texturesstruct Rect{  float x, y;  float width, height;};struct Frame{  Rect tc;  // The texture coordiantes of the sprite frame  Texture t; // The texture of the sprite-sheet};struct Sprite{  hashmap<String, vector<Frame&> > animations; // mapping from animation to sequence of frames  Rect r;  // the size of the sprite on screen};struct Actor{  Sprite &sprite;  Matrix transform; // Any transformation to the sprite, including scaling, translation or rotation.  String current_animation;  int current_frame;};/* ... */void render( Actor a ){  glPushMatrix( a.transform );    Frame f = a.sprite[a.current_antmation][a.current_frame];  // I don't really use glBegin/glEnd, rather a vertex buffer, but for the sake of illustration...  glBindTexture( GL_TEXTURE_2D, f.t );  glBegin( glQuads );  glTexCoord2f( f.tc.x, f.tc.y ); glVertex2f( a.sprite.r.x, a.sprite.r.y );  glTexCoord2f( f.tc.x, f.tc.y + f.tc.height ); glVertex2f( a.sprite.r.x. a.sprite.r.y + a.sprite.r.height );   glTexCoord2f( f.tc.x + f.tc. width, f.tc.y + f.tc.height ); glVertex2f( a.sprite.r.x + a.sprite.r.width, a.sprite.r.y + a.sprite.r.height );  glTexCoord2f( f.tc.x + f.tc.width. f.tc.y ); glVertex2f( a.sprite.r.x + a.sprite.r.width, a.sprite.r.y );  glEnd();  glPopMatrix();}

Pardon for any syntactical or logical errors, I don't generally code in C++ and also aren't quite awake at the moment.

Hope that helps.
-LuctusIn the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move - Douglas Adams

This topic is closed to new replies.

Advertisement