Reading jpg texture for OpenGL in c

copengl

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);

Best Answer

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.

Related Topic