Reputation: 1035
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);
Upvotes: 3
Views: 6886
Reputation: 162164
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:
Upvotes: 2
Reputation: 473457
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.
Upvotes: 6