解释OpenGL ES背景图像的工作方式 [英] Explain how OpenGL ES background images work

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

问题描述

有人可以解释一下如何在OpenGL ES视图上渲染背景图像吗?从设置OpenGL环境的基础知识开始.我是OpenGL的新手.

Could someone please explain how you render background images on an OpenGL ES view? From the basics of setting up the OpenGL environment. I'm a newbie to OpenGL here.

看到一个一些问题/答案关于创建背景图像的stackoverflow,但是我现在正在尝试修改现有代码(特别是Apple的GLPaint),并且不确定是否需要什么.尝试了解决方案但没有成功,我认为我应该退后一步,实际上是尝试并了解正在发生的事情.专心致志的黑客攻击并没有使我走得很远=(

I'm seeing a few questions/answers on stackoverflow about creating background images, but I'm trying to modify existing code at the moment (Apple's GLPaint in particular), and I'm not sure what is needed and what is not. Having tried the solutions, with no success, I thought I should take a step back and actually try and understand what is going on. Mindlessly hacking hasn't gotten me far =(

有没有简单的应用程序可以显示此内容?或参考手册也很有用(对于新手来说最好)-我尝试查看

Are there any simple apps that show this? Or reference manuals would be useful too (preferably ones for newbies) - I tried looking at the references for this answer, but I don't get it =(

推荐答案

您需要为opengl渲染图像(尺寸必须为2的幂).

you need to render an image (dimensions need to be powers of 2) to opengl.

您可以将图像放置在3d世界中,使其看起来像是背景(之所以这样做,是因为我的相机角度是固定的.),也可以在绘制图像之前先推入一些矩阵以使用glOrthof并弹出矩阵掉了.

you can either place the image in your 3d world so that it appears to be background (I do this because my camera angle is fixed.) or you can push some matrices to make use of glOrthof before drawing the images and pop the matrices off after.

这里有一些代码可以帮助您将图像绘制到opengl

Here is some code to help you with the image draw to opengl

glPushMatrix();

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); 
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

UIImage *image = [UIImage imageNamed:@"map1.jpg"];
if (image == nil)
    NSLog(@"Do real error checking here");

GLuint width = CGImageGetWidth(image.CGImage);
GLuint height = CGImageGetHeight(image.CGImage);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
void *imageData = malloc( height * width * 4 );
CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
CGContextTranslateCTM (context, 0, height);
CGContextScaleCTM (context, 1.0, -1.0);
CGColorSpaceRelease( colorSpace );
CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );
CGContextTranslateCTM( context, 0, height - height );
CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);

CGContextRelease(context);

free(imageData);
[image release];

static const GLfloat texCoords[] = {
    0.0, 1.0,
    1.0, 1.0,
    0.0, 0.0,
    1.0, 0.0
};

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);   


glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

static const GLfloat vertices[] = {
    -1.0,  1.0, -0.0,
    1.0,  1.0, -0.0,
    -1.0, -1.0, -0.0,
    1.0, -1.0, -0.0
};
static const GLfloat normals[] = {
    0.0, 0.0, 1.0,
    0.0, 0.0, 1.0,
    0.0, 0.0, 1.0,
    0.0, 0.0, 1.0
};

glBindTexture(GL_TEXTURE_2D, texture[0]);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glNormalPointer(GL_FLOAT, 0, normals);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glPopMatrix();

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

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