opengl es(iphone)从文件渲染 [英] opengl es(iphone) render from file

查看:107
本文介绍了opengl es(iphone)从文件渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我的英语水平

我要显示文件中的视频,其中每个像素4字节的帧,BRGA,1280x720?

I want to display video from a file, where the frames of 4 bytes per pixel, BRGA, 1280x720?

在Mac上,我只是取出框架并画出了这个glDrawPixels,它们在Mac上运行,但在opengl中却完全不同.

on mac I just took out the frame and drew this glDrawPixels, running on a Mac but in opengl es all differently.

这是来自Mac的代码

int pos = 0;
NSData *data = [[NSData alloc] initWithContentsOfFile:@"video.raw"];
glViewport(0,0,width,height);
glLoadIdentity();
glOrtho(0, width, 0, height, -1.0, 1.0);
glPixelZoom(1, -1);
glClear(GL_COLOR_BUFFER_BIT);
//glRasterPos2i(0, height);
glRasterPos2i(0, 0);
glDrawPixels(1280, 720, GL_BGRA, GL_UNSIGNED_BYTE, [data bytes]+pos);
glFinish();

推荐答案

使用"glTexSubImage2D"将这些数据推入纹理并渲染纹理.请注意,尽管纹理必须是2的幂,所以对于您的情况,可以将其设为(2048,1024),但可以仅更新(1280,720)部分:

Push those data to texture with "glTexSubImage2D" and render the texture. Note though that texture has to be of power of 2 so for your case you can make it (2048, 1024) but you may update only the (1280, 720) part:

CGSize videoSize;
CGSize textureSize;
GLuint dimension = 1;
while (videoSize.width > dimension) {
    dimension <<= 1;
}
textureSize = CGSizeMake(dimension, .0f);
dimension = 1;
while (videoSize.height > dimension) {
    dimension <<= 1;
}
textureSize = CGSizeMake(textureSize.width, dimension);

GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureSize.width, textureSize.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

GLfloat textureCoordinates[] = {
    .0f, .0f,
    .0f, videoSize.height/textureSize.height,
    videoSize.width/textureSize.width, .0f,
    videoSize.width/textureSize.width, videoSize.height/textureSize.height
};

要更新纹理:

void *data;
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, videoSize.width, videoSize.height, GL_RGBA, GL_UNSIGNED_BYTE, data);

然后只绘制纹理的四边形.

Then just draw your textured quad.

这篇关于opengl es(iphone)从文件渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆