对 CGL 中的内容进行快照?

发布于 2024-12-01 16:47:43 字数 1338 浏览 2 评论 0原文

我想在 Core OpenGL 上下文之外创建一个图像。

我使用了以下代码,但它创建了一个黑色图像。所以我想我不能在那里使用 glReadPixles 吗?请问还有其他建议吗?

int myDataLength = 480 * 480 * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    // gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < 480; y++)
{
    for(int x = 0; x < 320 * 4; x++)
    {
        buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
    }
}

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGImageRef image= CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, false, renderingIntent);

//PRINT image... Its black!!!!!!

CGDataProviderRelease(provider);
free(buffer);
free(buffer2);

I want to create a image out of Core OpenGL context.

I used following code but it creates a black image. So I guess I cannot use glReadPixles there? Any other suggestions please?

int myDataLength = 480 * 480 * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    // gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y < 480; y++)
{
    for(int x = 0; x < 320 * 4; x++)
    {
        buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x];
    }
}

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 320;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGImageRef image= CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, false, renderingIntent);

//PRINT image... Its black!!!!!!

CGDataProviderRelease(provider);
free(buffer);
free(buffer2);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光下的微笑 2024-12-08 16:47:43

在执行 glReadPixels 调用之前,您必须

  • 设置正确的打包(请参阅 glPixelStorei 参考页),
  • 选择要使用 glReadBuffer 读取的正确缓冲区(交换后前部,交换前后部,我建议交换并从前面阅读)

Before you do a glReadPixels call you must

  • set proper packing (see glPixelStorei reference page)
  • select the right buffer to read from with glReadBuffer (front after swapping, back before swapping, I recommend swap and read from front)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文