这是一张 1024 x 512 的 jpg。
大小变量返回 84793。
我不明白的一件事是 84793 作为 1024 * 512 = 524288 时的文件大小。
我认为这将是 * 3因为每个像素 3 个 channel 。
计数变量返回 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);
最佳答案
OpenGL 不处理 JPEG 压缩,也不处理 JPEG 图像文件格式。你不能只是把一个文件放在磁盘上,把它放到内存中然后交给 OpenGL。你要读取文件格式,处理成OpenGL真正可以读取的形式,然后交给OpenGL。
有a number of libraries对于各种不同的图像格式,您可以使用它来执行此操作。您还应该阅读有关 how OpenGL understands the pixel data you give it 的内容,以及了解 internal image format parameter 是什么意味着。
关于c - 在c中为OpenGL读取jpg纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7681890/