2 zwl19920410 zwl19920410 于 2013.12.19 16:38 提问

openGL读取和写入,生成的图片是空的。

我从网上找的openGL教程
教到第十课关于读图片与写,可是不管用生成的图片是空的。

#define WindowWidth 400
#define WindowHeight 400
#include
#include
#include
#define BMP_HEADER_LENGtH 54

void grab(void)
{
FILE* pDummyFile;
FILE* pWritingFile;
GLubyte* pPixelData;
GLubyte* BMP_HEAD[BMP_HEADER_LENGtH];
GLint i,j;
GLint PixelDataLength;

i=WindowWidth*3;
while(i%4!=0)
    ++i;
PixelDataLength=i*WindowHeight;


pPixelData=(GLubyte*)malloc(PixelDataLength);
if(pPixelData=0)
    exit(0);

fopen_s(&pDummyFile,"dummy.bmp", "rb");
if(pDummyFile=0)
    exit(0);

fopen_s(&pWritingFile,"grab.bmp", "wb");
if(pWritingFile=0)
    exit(0);

glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glReadPixels(0,0,WindowWidth,WindowHeight,GL_BGR_EXT,GL_UNSIGNED_BYTE,pPixelData);

fread(BMP_HEAD,sizeof(BMP_HEAD),1,pDummyFile);
fwrite(BMP_HEAD,sizeof(BMP_HEAD),1,pWritingFile);
fseek(pWritingFile,0x0012,SEEK_SET);
i=WindowWidth;
j=WindowHeight;
fwrite(&i,sizeof(i),1,pWritingFile);
//fseek(pWritingFile,0x0016,SEEK_SET);
fwrite(&j,sizeof(j),1,pWritingFile);

fseek(pWritingFile,0,SEEK_END);
fwrite(pPixelData,PixelDataLength,1,pWritingFile);

fclose(pDummyFile);
fclose(pWritingFile);
free(pPixelData);

}

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRectf(-0.5f,-0.5f,0.5f,0.5f);

glFlush();
grab();
}

int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(400,400);
glutCreateWindow("第一个OpenGL程序");
glutDisplayFunc(&myDisplay);

glutMainLoop();
return 0;
}

2个回答

wangyaninglm
wangyaninglm   Ds   Rxr 2015.01.19 11:42
xinsizhu8494
xinsizhu8494   2016.07.05 20:46

if(pDummyFile=0)
exit(0);
if(pWritingFile=0)
exit(0);

    将上面的代码去掉,
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!