这是一个1024 x 512 jpg.
size变量返回84793.
我不明白的一件事是当1024*512 = 524288时84793作为文件的大小.
我认为这将是*3,因为每个像素3个通道.
count变量返回65.
现在,当我设置OpenGL纹理参数时,我在此行上获取访问冲突读取位置:
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,texWidth,texHeight,0,GL_RGB,GL_UNSIGNED_BYTE,texPtr) ;
其中texWidth和texHeight的宽度和高度从下面.
这是我目前的情况:
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);
Run Code Online (Sandbox Code Playgroud)
OpenGL不处理JPEG压缩,也不处理JPEG图像文件格式.你不能只是在磁盘上取一个文件,将它放入内存并将其交给OpenGL.您必须阅读文件格式,将其处理为OpenGL实际可以读取的表单,然后将其提供给OpenGL.
有一些库,你可以使用要做到这一点,各种不同的图像格式.您还应该了解OpenGL如何理解您提供的像素数据,以及了解内部图像格式参数的含义.