寻找精确的屏幕尺寸

时间:2013-10-30 16:41:07

标签: ios ipad opengl-es-2.0

iOS OpenGL ES 2.0全新。我试图通过绘制一个简单的矩形来精确识别我的屏幕(iPad)的边界。根据我的发现,它看起来像iPad屏幕的尺寸是768 x 1024.但是,我的屏幕没有被正确覆盖(请注意,如果重要的话,我正在横向模式绘图)。

我不确定顶点之间的相互作用以及我如何使用投影矩阵命令。

'self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeOrtho(0,FRAME_WIDTH * 2,0,FRAME_HEIGHT * 2,0,0);'

如果我删除此行,我的矩形会在原点左下方呈现。但如果我把它留在里面,它似乎从左下角渲染,但尺寸太大,我似乎无法弄清楚如何以可预测的方式改变它们。

正如你所看到的,我很困惑。获得精确屏幕尺寸的最佳方法是什么。我需要这个来正确地将其他对象放在屏幕上。谢谢!

#import "ViewController.h"

typedef struct {
    GLKVector3 positionCoordinates;
    GLKVector2 textureCoordinates;
} VertexData;

#define FRAME_HEIGHT 768.0f
#define FRAME_WIDTH 1024.0f

VertexData vertices[] = {
    { {       0.0f,         0.0f, 0.0f}, {0.0f, 0.0f} },   // bottom left
    { {FRAME_WIDTH,         0.0f, 0.0f}, {1.0f, 0.0f} },   // bottom right
    { {       0.0f, FRAME_HEIGHT, 0.0f}, {0.0f, 1.0f} },   // top left
    { {       0.0f, FRAME_HEIGHT, 0.0f}, {0.0f, 1.0f} },   // top left
    { {FRAME_WIDTH,         0.0f, 0.0f}, {1.0f, 0.0f} },   // bottom right
    { {FRAME_WIDTH, FRAME_HEIGHT, 0.0f}, {1.0f, 1.0f} }    // top right
};

@interface ViewController ()

@property (nonatomic, strong) EAGLContext *context;
@property (nonatomic, strong) GLKBaseEffect *baseEffect;

@end

@implementation ViewController {
    GLuint _vertexBufferID;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    GLKView *view = (GLKView *) self.view;
    view.context = self.context;
    [EAGLContext setCurrentContext:self.context];

    self.baseEffect = [[GLKBaseEffect alloc] init];
    self.baseEffect.useConstantColor = YES;
    self.baseEffect.constantColor = GLKVector4Make(1.0f, 0.0f, 0.0f, 1.0f);

    self.baseEffect.transform.projectionMatrix = GLKMatrix4MakeOrtho(0, FRAME_WIDTH*2, 0, FRAME_HEIGHT*2, 0, 0);

    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

    glGenBuffers(1, &_vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), offsetof(VertexData, positionCoordinates));

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - GKLView delegate methods

- (void) glkView: (GLKView *) view drawInRect:(CGRect)rect{
    glClear(GL_COLOR_BUFFER_BIT);

    [self.baseEffect prepareToDraw];

    glDrawArrays(GL_TRIANGLES, 0, 6);
}

- (void) update {

}

@end

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式获取屏幕尺寸:

CGSize screenSize = [UIScreen mainScreen].bounds.size;
但是,在应用程序启动期间要小心使用它。

获取当前视图控制器的方向:

[UIViewController interfaceOrientation] 

获取设备方向:

[[UIDevice currentDevice] orientation]

......或......

[[UIApplication sharedApplication] statusBarOrientation];

在您的情况下,您可以简单地使用您拥有的EAGLView边界:

CGSize viewSize = self.view.bounds.size

答案 1 :(得分:0)

你应该使用FRAME_HEIGHT和FRAME_WIDTH,而不是两倍。