c - 在c中为OpenGL读取jpg纹理

标签 c opengl

这是一张 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/

相关文章:

c - 从 c 中的 "/dev/random"读取

c - 谁在 Linux 内核上下文切换后调用 IRET?

arrays - 为什么我们在 C 中指定 size_t 数组大小而不是仅仅使用整数?这样做的好处是什么?

objective-c - 读取 9 位、10 位或 11 位(精确) block 中的数据并在 Objective-C/C 中转换为整数

c++ - 在其一侧显示 glutSolidCone

c++ - 使用影响 TEXTURE_2D 的 TEXTURE_RECTANGLE_ARB

c++ - Z 排序几何

c - 我在与 c 中的整数值进行比较时遇到问题。无法使用 '==' 运算符进行比较

c++ - 无法让 netbeans 正确链接 GLEW

objective-c - 没有 Interface Builder 的 NSOpenGLView