2013-10-30 67 views
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 
+1

为什么使用FRAME_HEIGHT * 2? –

+0

因为我读了一点= 2像素。另外,通过试验和错误,这是最接近填充屏幕,但它远远不够完美。基本上,这是一个真正的noob尝试,我肯定会被修改或丢弃,当我知道这一点。 – Alex

+0

您可以发布“最接近填充屏幕?”的含义吗? –

回答

0

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

0

您可以获取屏幕尺寸:

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

然而,在应用程序启动时要小心使用这个的。

为了得到当前视图控制器的方向:

[UIViewController interfaceOrientation] 

为了获得设备的方向:

[[UIDevice currentDevice] orientation] 

......或者......

[[UIApplication sharedApplication] statusBarOrientation]; 

在你例如,您可以简单地使用您拥有的EAGLView的界限:

CGSize viewSize = self.view.bounds.size 
+0

谢谢。我如何将它传递给我的顶点?我的顶点数组是全局定义的。如果我画一个矩形,我必须提前知道这些坐标。我只画一个单位平方,然后缩放它,或者如何去做这件事?再次感谢! – Alex

+0

你有没有*在全球定义你的顶点数组?如果你不这样做,它会更容易。 。 。然而,如果你必须定义一个CGSize指针并让数组引用它。 。 。然后在viewDidLoad之后更新它。 。顺便说一句:尝试了解这些重要的视图属性之间的差异:边界和框架(你可以谷歌它)。 。 。当你开始时,他们是非常重要的基础概念。 –

+0

不一定。这是我见过最频繁的方式,所以我试图按照我认为是最普遍接受的程序。感谢边界vs框架的提示。我会研究它。 – Alex