OpenGL如何利用glDrawPixels来读取glreadPixels保存的文件

代码例子:

void drawPixels() {
    FILE *pPixelsFile = 0;
    pPixelsFile = fopen("d:\\grab.bmp", "rb");
    if(pPixelsFile == 0) {
        exit(0);
    }

    fseek(pPixelsFile, 0x0012, SEEK_SET);
    fread(&gImageWidth, sizeof(gImageWidth), 1, pPixelsFile);
    fread(&gImageHeigh, sizeof(gImageHeigh), 1, pPixelsFile);

    gImageLength = gImageWidth * 3;
    while( gImageLength % 4 != 0 ) {       // 补充数据,直到i是的倍数
        ++gImageLength;   
    }

    gImageLength = gImageLength * gImageHeigh;
    

    g_spPixelsBuffer = (GLubyte *)malloc(gImageLength);

    if(g_spPixelsBuffer == 0) {
        fclose(pPixelsFile);
        exit(0);
    }
    memset(g_spPixelsBuffer, 0, gImageLength);
    fseek(pPixelsFile, 54, SEEK_SET);//文件的头部信息,不是图的pixels
    fread(g_spPixelsBuffer, gImageLength, 1, pPixelsFile);
    
    fclose(pPixelsFile);
}


void displayShape() {

    glDrawPixels(gImageWidth, gImageHeigh,  GL_BGR_EXT, GL_UNSIGNED_BYTE, g_spPixelsBuffer);
    glutSwapBuffers();
}

int main(int argc, char *argv[]) {
    
    drawPixels();//首先读取文件中的pixels

    glutInit(&argc, argv); 
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Five-Pointed Star");

    glutDisplayFunc(displayShape);
    glutSpecialUpFunc(handleMessage);
    glutReshapeFunc(reShape);
    glutIdleFunc(idleFunc);
    glutMainLoop();//后面free(g_spPixelsBuffer)
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值