I got it to work but the textures are being rendered incorrectly. I have to open them using mspaint, rotate them 180 degrees and flip them horizontally, in order for them to render correctly. I noticed the same thing happened when I tried to load png files. After searching google, I found out I have to swap the rows of the texture buffer before loading it in to opengl. However, I couldn't to find any code (or pseudo code) on it. If someone could tell me how to do this, it would be great.
This is the function I'm using to load the texture:
unsigned char* LoadJPG(const string& filename, int& width,int& height, int& nChannel)
{
unsigned char * big_buff;
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE * infile;
JSAMPARRAY buffer;
int row_stride;
if ((infile = fopen(filename.data(), "rb")) == NULL)
return NULL;
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer))
{
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return NULL;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
(void) jpeg_read_header(&cinfo, TRUE);
(void) jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
big_buff = (unsigned char*)malloc(cinfo.output_height * cinfo.output_width * cinfo.output_components);
while (cinfo.output_scanline < cinfo.output_height)
{
JDIMENSION read_now = jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(&big_buff[(cinfo.output_scanline - read_now) * cinfo.output_width * cinfo.output_components], buffer[0], row_stride);
}
width = cinfo.output_width;
height = cinfo.output_height;
nChannel = cinfo.num_components;
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return big_buff;
}