Author Topic: OGL: Loading Images From Memory  (Read 17696 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
OGL: Loading Images From Memory
« on: September 22, 2010 »
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.
 
Code: [Select]
#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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #1 on: September 22, 2010 »
glGenTextures doesn't create a texture, it creates an id for a texture.
You use it like this:
Code: [Select]
int texture_id;
glGenTextures(1, &texture_id);
Then you use glTexImage2D to load the texture into video memory (and make it the current texture)
Code: [Select]
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
Code: [Select]
glBindImage(GL_TEXTURE_2D, texture_id);

Jim
 
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #2 on: September 22, 2010 »
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!
« Last Edit: September 22, 2010 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #3 on: September 22, 2010 »
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 );

 
« Last Edit: September 22, 2010 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #4 on: September 22, 2010 »
You can do
Code: [Select]
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
Code: [Select]
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)
Code: [Select]
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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #6 on: September 23, 2010 »
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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #7 on: September 23, 2010 »
The links you want on that page are in the left-hand side bar.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #8 on: September 23, 2010 »
You need
Code: [Select]
glEnable(GL_TEXTURE_2D);
somewhere!

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #9 on: September 23, 2010 »
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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #10 on: September 23, 2010 »
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

            
« Last Edit: September 23, 2010 by Jim »
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #11 on: September 23, 2010 »
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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #12 on: September 23, 2010 »
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:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #13 on: September 23, 2010 »
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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #14 on: September 23, 2010 »
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:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #15 on: September 23, 2010 »
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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #16 on: September 23, 2010 »
Bonza!
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #17 on: April 18, 2012 »
Is there a way to copy the contents of a glTexture back into a buffer?

an example:

Code: [Select]
    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:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #18 on: April 18, 2012 »
Quote
Is 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:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: OGL: Loading Images From Memory
« Reply #19 on: October 16, 2012 »
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: