1

对 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
4

2 回答 2

0

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

于 2013-10-30T17:24:22.050 回答
0

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

CGSize screenSize = [UIScreen mainScreen].bounds.size;

但是,请注意在应用程序启动期间使用它。

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

[UIViewController interfaceOrientation] 

要获取设备方向:

[[UIDevice currentDevice] orientation]

... 或者 ...

[[UIApplication sharedApplication] statusBarOrientation];

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

CGSize viewSize = self.view.bounds.size
于 2013-10-30T17:03:26.900 回答