iOS OpenGL ES-在模拟器和设备上的不同纹理行为 [英] iOS OpenGL ES - Different texture behaviour on simulator and device

查看:85
本文介绍了iOS OpenGL ES-在模拟器和设备上的不同纹理行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用OpenGL ES加载纹理,以下是我的代码.

I'm loading textures using OpenGL ES, the below is my code.

图形由2个相同大小的纹理组成,包括停止"按钮和后面的蓝色发光.

The graphic is made up of 2 textures of the same size, the stop button and the blue glow behind.

下面的第一个图形是使用xCode中的iPad模拟器拍摄的,第二个图形是在实际设备上拍摄的.当我从Illustrator导出图形时,第一个图形是正确的输出.但是,当我在iPad上加载程序时,它会显示第二张图形.停止按钮后面的蓝色光似乎已经变小了.为什么会这样呢?

The first graphic below was taken using the iPad simulator in xCode, and the 2nd on the actual device. The 1st graphic is the correct output as I exported the graphics from Illustrator. However, when I loaded the program on the iPad, it gives me the 2nd graphic. It seems somehow that the blue light texture behind the stop button has become smaller. Why is this so?

我可以通过增大蓝色光的纹理来进行补偿,但这并不是正确的,因为它在Illustrator中的外观应该是第一幅图形.

I can compensate by making the blue light texture bigger, but it wouldn't be right as the way it is supposed to look in Illustrator is the 1st graphic.

这是我的代码.

//
//  OpenGLES_Ch3_4ViewController.m
//  OpenGLES_Ch3_4
//

#import "OpenGLES_Ch3_4ViewController.h"
#import "AGLKVertexAttribArrayBuffer.h"
#import "AGLKContext.h"

#define Y_POS 1.0
#define ASPECT_RATIO 0.75f
#define SIZE 0.8

@implementation OpenGLES_Ch3_4ViewController

@synthesize baseEffect;
@synthesize vertexBuffer;
@synthesize textureInfo0;
@synthesize textureInfo1;

/////////////////////////////////////////////////////////////////
// This data type is used to store information for each vertex
typedef struct {
   GLKVector3  positionCoords;
   GLKVector2  textureCoords;
}
SceneVertex;

/////////////////////////////////////////////////////////////////
// Define vertex data for a triangle to use in example
//static const SceneVertex vertices[] = 

static const SceneVertex vertices[] =
{
    {{-1.0f*SIZE, -ASPECT_RATIO*SIZE, 0.0f}, {0.0f, 0.0f}},  // first triangle
    {{ 0.0f*SIZE, -ASPECT_RATIO*SIZE, 0.0f}, {1.0f, 0.0f}},
    {{-1.0f*SIZE,          0.0f*SIZE, 0.0f}, {0.0f, 1.0f}},
    {{ 0.0f*SIZE, -ASPECT_RATIO*SIZE, 0.0f}, {1.0f, 0.0f}},  // second triangle
    {{-1.0f*SIZE,          0.0f*SIZE, 0.0f}, {0.0f, 1.0f}},
    {{ 0.0f*SIZE,          0.0f*SIZE, 0.0f}, {1.0f, 1.0f}},
};

/////////////////////////////////////////////////////////////////
// Called when the view controller's view is loaded
// Perform initialization before the view is asked to draw
- (void)viewDidLoad
{
   [super viewDidLoad];

   // Verify the type of view created automatically by the
   // Interface Builder storyboard
   GLKView *view = (GLKView *)self.view;
   NSAssert([view isKindOfClass:[GLKView class]],
      @"View controller's view is not a GLKView");

   // Create an OpenGL ES 2.0 context and provide it to the
   // view
   view.context = [[AGLKContext alloc] 
      initWithAPI:kEAGLRenderingAPIOpenGLES2];

   // Make the new context current
   [AGLKContext setCurrentContext:view.context];

   // Create a base effect that provides standard OpenGL ES 2.0
   // shading language programs and set constants to be used for 
   // all subsequent rendering
   self.baseEffect = [[GLKBaseEffect alloc] init];
   self.baseEffect.useConstantColor = GL_TRUE;
   self.baseEffect.constantColor = GLKVector4Make(
      1.0f, // Red
      1.0f, // Green
      1.0f, // Blue
      1.0f);// Alpha

   // Set the background color stored in the current context 
   ((AGLKContext *)view.context).clearColor = GLKVector4Make(
      0.0f, // Red 
      0.0f, // Green 
      0.0f, // Blue 
      1.0f);// Alpha 

   // Create vertex buffer containing vertices to draw
   self.vertexBuffer = [[AGLKVertexAttribArrayBuffer alloc]
      initWithAttribStride:sizeof(SceneVertex)
      numberOfVertices:sizeof(vertices) / sizeof(SceneVertex)
      bytes:vertices
      usage:GL_STATIC_DRAW];

   // Setup texture0
   CGImageRef imageRef0 = 
      [[UIImage imageNamed:@"stoplight_full.png"] CGImage];

   self.textureInfo0 = [GLKTextureLoader 
      textureWithCGImage:imageRef0 
      options:[NSDictionary dictionaryWithObjectsAndKeys:
         [NSNumber numberWithBool:YES], 
         GLKTextureLoaderOriginBottomLeft, nil] 
      error:NULL];

    self.textureInfo0_2 = [GLKTextureLoader
                         textureWithCGImage:imageRef0
                         options:nil
                         error:NULL];

   // Setup texture1
   CGImageRef imageRef1 = 
      [[UIImage imageNamed:@"stop_button.png"] CGImage];

   self.textureInfo1 = [GLKTextureLoader 
      textureWithCGImage:imageRef1 
      options:[NSDictionary dictionaryWithObjectsAndKeys:
         [NSNumber numberWithBool:YES], 
         GLKTextureLoaderOriginBottomLeft, nil] 
      error:NULL];

    self.textureInfo1_2 = [GLKTextureLoader
                         textureWithCGImage:imageRef1
                           options:nil
                           error:NULL];

   // Enable fragment blending with Frame Buffer contents
   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}


/////////////////////////////////////////////////////////////////
// GLKView delegate method: Called by the view controller's view
// whenever Cocoa Touch asks the view controller's view to
// draw itself. (In this case, render into a frame buffer that
// shares memory with a Core Animation Layer)
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{   
   // Clear back frame buffer (erase previous drawing)
   [(AGLKContext *)view.context clear:GL_COLOR_BUFFER_BIT];

   [self.vertexBuffer prepareToDrawWithAttrib:GLKVertexAttribPosition
      numberOfCoordinates:3
      attribOffset:offsetof(SceneVertex, positionCoords)
      shouldEnable:YES];
   [self.vertexBuffer prepareToDrawWithAttrib:GLKVertexAttribTexCoord0
      numberOfCoordinates:2
      attribOffset:offsetof(SceneVertex, textureCoords)
      shouldEnable:YES];

   self.baseEffect.texture2d0.name = self.textureInfo0.name;
   self.baseEffect.texture2d0.target = self.textureInfo0.target;
   [self.baseEffect prepareToDraw];

   // Draw triangles using the vertices in the 
   // currently bound vertex buffer
   [self.vertexBuffer drawArrayWithMode:GL_TRIANGLES
      startVertexIndex:0
      numberOfVertices:sizeof(vertices) / sizeof(SceneVertex)];

   self.baseEffect.texture2d0.name = self.textureInfo1.name;
   self.baseEffect.texture2d0.target = self.textureInfo1.target;
   [self.baseEffect prepareToDraw];

   // Draw triangles using currently bound vertex buffer
   [self.vertexBuffer drawArrayWithMode:GL_TRIANGLES
      startVertexIndex:0
      numberOfVertices:sizeof(vertices) / sizeof(SceneVertex)];
 }

推荐答案

对我来说,第二个纹理看起来并不小,只是较暗. 您的笔记本电脑和iPad最有可能具有不同的伽玛值的显示器.

To me, the second texture does not look smaller, just dimmer. Your laptop and iPad most likely have displays with different gammas.

有很多方法可以校正gamma,提供了很棒的介绍

There are many ways to correct for gamma, a terrific introduction is provided here.

值得注意的是,如今,许多现代显卡也可以为您完成此操作,但据我所知,到目前为止,OpenGL ES尚不支持它:

It is worth noting many modern cards can do it for you these days too, but so far unsupported on OpenGL ES as far as I know: sRGB Color Formats.

这篇关于iOS OpenGL ES-在模拟器和设备上的不同纹理行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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