• Welcome to Dark Bit Factory & Gravity.
 

OGL: Loading Images From Memory

Started by Clyde, September 22, 2010

Previous topic - Next topic

0 Members and 0 Guests are viewing this topic.

Clyde

I've seen how to load bmps from a file.
However I'm having a bit of bother.

I'd like to using the method I've used previously of a gfx_buffer class and rbz's bmp2raw / bin2c, to load and create images from memory.

some code to illustrate.


#include "media\image_raw.cpp"
#include "media\image_pal.cpp"
class gfx_buffer
{
public:
int wwidth, height;

unsigned int *pixels;

};

void load_texture()
{
texture_one=create_gfx_buffer(128,128);//load_gfx_buffer( image_pal, image_raw, 128, 128 );
for ( int x=0; x<128; x++ )
{
  for ( int y=0; y<128; y++)
  {
   texture_one->pixels[ x+y*texture_one->wwidth ]=0xff00f0;
  }
}

glGenTextures(1, &texture_one->pixels[0]);     // Create The Texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtering

glBindTexture(GL_TEXTURE_2D, texture_one->pixels[0]);
}

gfx_buffer *create_gfx_buffer( int wwidth, int height, unsigned int mask_colour=0 )
{
gfx_buffer *buffer = new gfx_buffer;

buffer->pixels = new unsigned int [wwidth*height];
buffer->wwidth = wwidth;
buffer->height = height;
return buffer;
}


Thankyou.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

glGenTextures doesn't create a texture, it creates an id for a texture.
You use it like this:

int texture_id;
glGenTextures(1, &texture_id);

Then you use glTexImage2D to load the texture into video memory (and make it the current texture)

glTexImage2d(GL_TEXTURE_2D, GL_RGB8, texture_one->width, texture_one->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_one->pixels);

Remember to set the alpha in your texture to 0xff.  Remember the width and height should be powers of 2.
Then, when you want to use your texture, you can just do

glBindImage(GL_TEXTURE_2D, texture_id);


Jim
Challenge Trophies Won:

Clyde

#2
Thanks Jim :D

Is glTexImage2d apart of glaux.h or another include i need to put in or download?
As it tells me it's undefined. I've allready got gl and glu headers included.

And glGenTextures(1, &texture_id); say Im going to in total make 4 textures, does that increase to 4? or remain 1 per whenever I want to create a texture.

Thanks Dude!
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Clyde

#3
I see, the d should be a capital.

It now tells me that it doesn't take 8 arguments.
glTexImage2D(GL_TEXTURE_2D, GL_RGB8, texture_one->wwidth, texture_one->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_one->pixels );

Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

You can do

int texture_id1;
int texture_id2;
int texture_id3;
int texture_id4;
glGenTextures(1, &texture_id1);
glGenTextures(1, &texture_id2);
glGenTextures(1, &texture_id3);
glGenTextures(1, &texture_id4);

or

int texture_id[4];
glGenTextures(4, texture_id);


Sorry about this one, I'm typing it in from memory...:P  Missed out the mipmap level (0 for none)

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texture_one->wwidth, texture_one->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_one->pixels );


Jim
Challenge Trophies Won:

Jim

Challenge Trophies Won:

Clyde

Here's an update project, but the texture / image isn't being used, it's white. I've made it make a coloured textured in a for loop for the time being.

Im also using ie9beta, and I can't get the xml pages to open on that link.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

The links you want on that page are in the left-hand side bar.
Challenge Trophies Won:

Jim

You need

glEnable(GL_TEXTURE_2D);

somewhere!

Jim
Challenge Trophies Won:

Clyde

yep, it's on line 15 in the nehe - setup.h of InitGL.
perhaps it's because of different colour formats.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

#10
2 problems.

1. Between
glGenTextures()
and
glTexImage2D
you need to do a
glBindTexture(GL_TEXTURE_2D, texture_id );   

2. You need to load the texture!!
Between
CreateGLWindow
and
while(!done)
you need to call
load_texture();

Working for me now...I've attached the main cpp file with my changes.

Jim

            
Challenge Trophies Won:

Clyde

Am I right in thinking the texture needs to be loaded / reloaded each frame?
Just trying to think why in init_demo that won't work.

Cheers Jim.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

Only if you want the image to change, otherwise you load it once using glTexImage2D and it sits in video memory (usually).

If you want to change the image, don't call glGenTextures again, just do
glBindTexture(GL_TEXTURE_2D, id_of_the_texture_you_want_to_update);
then
glTexImage2D
or
glTexSubImage2D
to load the new data.

Jim
Challenge Trophies Won:

Clyde

the image i've loaded isn't as should be, the main difference from the original images is it's blue where red should be. it's a usual a 256 colour bitmap converted with bmp2raw.

can't quite get why the load_texture in init_demo wouldnt work, but putting it into winmain. maybe it's a static thing.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

To fix the colours try GL_BGRA_EXT instead of GL_RGBA in the glTexImage2D call.

It wasn't working because you never called init_demo anywhere.

Jim
Challenge Trophies Won:

Clyde

oops, I thought I had.
Thanks again!
I can now texture a cube in CPP!!
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

Challenge Trophies Won:

Clyde

Is there a way to copy the contents of a glTexture back into a buffer?

an example:


    unsigned int *colour_buffer=new unsigned int[ texture_wwidth * texture_height ];
   
    // Set Our Viewport (Match Texture Size)
    glViewport(0,0,texture_wwidth,texture_height);

    render_scene();
   
    // Bind To The Texture
    glBindTexture(GL_TEXTURE_2D, texture_id_num );
   
    // copy over texture contents to colour buffer.



Cheers!!
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won:

Jim

QuoteIs there a way to copy the contents of a glTexture back into a buffer?
Specifically, no.

But it depends what you really want to do.
You can draw it and then use glReadPixels to read the pixels off the screen. reading back from video memory is incredibly slow.
You can draw to something that isn't the screen (a video memory buffer) and use that as a texture later.



Jim
Challenge Trophies Won:

Clyde

I just wondered as it would be quite cool for trying blurring and other fx, as at the moment I can't get the hang of shaders. Most examples on google use ps 3.0 and higher, and aren't really written for novices.

when you say a video memory buffer, do you mean like for example argb_buffer[x+y]=glReadPixels(x,y,col) , similarly to blitz's read and writepixels?

Cheers.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.


Challenge Trophies Won: