OpenGL ES推荐的显示背景图像的方法 [英] OpenGL ES recommended way of displaying a background image

查看:162
本文介绍了OpenGL ES推荐的显示背景图像的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在OpenGL ES应用程序中显示背景图像的推荐方法是什么。

Whats the recommended way of displaying a background image in an OpenGL ES app.


  1. UIImage添加到glview下的视图层次结构中?

  2. 从内存中的纹理在OpenGL绘制例程中绘制的图像?

是否有任何性能/内存注意事项考虑到?

Are there any performance / memory considerations to take account of?

推荐答案

我喜欢使用Apple的Texture2D类加载纹理。创建一个四边形(两个三角形,六个顶点),映射到屏幕的角落。然后将纹理坐标映射到顶点,将其传递给glDrawArrays并转到。我实际上有一个Class,它在CG坐标中取一个帧,将其转换为OpenGL坐标,然后在帧中绘制图像。

I like to load a texture using Apple's Texture2D class. Create a quad (two triangles, six vertices) that maps to the corners of the screen. Then you map the texture coordinates to the vertices, pass that to glDrawArrays and go. I actually have a Class that takes a frame in CG Coordinates, converts that to OpenGL Coordinates and then draws the image in the frame.

假设你有一个像这样的结构:

Let's say you have a struct like this:

struct {
  GLfloat x;             // OpenGL X Coordinate
  GLfloat y;             // OpenGL Y Coordinate
  GLfloat z;             // OpenGL Z Coordinate
  GLfloat s;             // Texture S Coordinate
  Glfloat t;             // Texture T Coordinate
} vertexData;

这是一个顶点数据数组:

And an array of vertex data like this:

struct vertexData verts[6];

你有一个Texture2D对象:

And you have a Texture2D object:

 texture = [[Texture2D alloc] initWithImage:[UIImage imageNamed:image] filter:filter pixelFormat:pixelFormat];

所以,现在你必须填写顶点数组。假设x,x范围[-1,1]和y范围[-1,1],z的原点,那么你将初始化你的顶点,如下所示:

So, now you have to fill in the vertex array. Assuming 3D, x range [-1, 1] and y range [-1, 1], origin for z, then you would initialize your vertices like this:

verts[0].x = verts[1].x = verts[5].x = -1.0;
verts[2].x = verts[3].x = verts[4].x = 1.0;

verts[0].y = verts[2].y = verts[4].y = 1.0;
verts[1].y = verts[3].y = verts[5].y = -1.0;

verts[0].z = verts[1].z = verts[2].z = 0.0;
verts[3].z = verts[4].z = verts[5].z = 0.0;

verts[0].s = verts[1].s = verts[5].s = tlxf;
verts[2].s = verts[3].s = verts[4].s = trxf;

verts[0].t = verts[2].t = verts[4].t = ttyf;
verts[1].t = verts[3].t = verts[5].t = tbyf;

最后,您需要绘制:

glBindTexture(GL_TEXTURE_2D, texture.name);
glVertexPointer(3, GL_FLOAT, sizeof(struct vertexData), &verts[0].x);
glTexCoordPointer(2, GL_FLOAT, sizeof(struct vertexData), &verts[0].s);
glDrawArrays(GL_TRIANGLES, 0, 6);

我遗漏了有关设置OpenGL的任何细节。我假设你已经做到了这一点,如果你已经做到这一点了。

I'm leaving out any details about setting up OpenGL. I assume you've done that if you're getting this far.

在OpenGL工作时需要考虑大量的性能和内存限制,但我不会建议你过于担心他们。一旦你运行它,用OpenGL仪器对它进行分析,看看是否有任何性能问题,然后处理它们。

There are tons of performance and memory constraints to think about when working in OpenGL, but I wouldn't recommend getting too worried about them just yet. Once you get it working, profile it with the OpenGL Instrument to see if have any performance issues, then deal with those.

Apple有一个非常好的文档描述了最佳实践,关于SO也有一些有用的问题。我相信一旦你知道你遇到了什么(如果有的话),你就可以搜索它们。

Apple has a really good document describing best practices, and there are some useful questions on SO as well. I'm sure you will be able to search for them once you know what you're up against (if anything).

这篇关于OpenGL ES推荐的显示背景图像的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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