3
votes

This is a 1024 x 512 jpg.
The size variable returns 84793.
One thing that I don't understand is the 84793 as the size of the file when 1024 * 512 = 524288.
I would think this would be * 3 since 3 channels per pixel.
The count variable is returning 65.
Right now I'm getting a access violation reading location on this line when I'm setting OpenGL texture parameters:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, texPtr);

Where texWidth and texHeight where the width and height from below.

Here's what I have currently:

int width = 1024;
int height = 512;
long size  = 0;
GLubyte * data;
FILE * file;
char name[100];
int count;

int test;

strcpy(name, filename);
// open texture data
file = fopen( name, "r" );
if ( file == NULL )
{
fputs ("File error",stderr); 
exit (1);
 }
 fseek (file , 0 , SEEK_END);
 size = ftell( file );
 rewind(file);
// allocate buffer
data = (unsigned char *)malloc( sizeof(char)*size );

count = (int) fread (data,sizeof(unsigned char) ,size,file);
fclose( file );

// allocate a texture name
glGenTextures( 1, &dayGLTexture );

initTexture(dayGLTexture, width, height, data);
2
I think you'll have to decode the JPEG before you can use it with OpenGL, which only supports certain compressed image formats. Your size difference also comes from this - JPEG is compressed.li.davidm
thank you so much. this led me to converting to bmp and I'm starting to get results. If you answer this question, I will accept.Oscar

2 Answers

6
votes

OpenGL does not process JPEG compression, and it also doesn't process JPEG image file formats. You cannot just take a file on the disk, blast it into memory and hand it off to OpenGL. You have to read the file format, process it into a form that OpenGL can actually read, and then give it to OpenGL.

There are a number of libraries you can use to do this, for various different image formats. You should also read up about how OpenGL understands the pixel data you give it, as well as understand what the internal image format parameter means.

2
votes

You can't pass image files directly to OpenGL directly. Image files are containers. The data they contain must be unpacked and decoded first. There are some file formats which may contain the data in a form directly digestable by OpenGL (flat data), among them TGA and BMP.

However you should NEVER pass data of a file directly to some API (except if you're building a file transfer or copy program).

Reading an image file always requires the following steps:

  • Reading and interpreting the metadata (either a header, or a tail, or both).
  • Preparing intermediary buffers and the reading/decoding process based on the metadata information.
  • Read the data from the file and decode it (for flat files this may still require some field swizzling, bitmasking and color correction). Compressed data must be decompressed first. JPEG is a lossy compression scheme
  • Pass the data to OpenGL, or just keep it around in a buffer for later use.