完全是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
爲什麼使用FRAME_HEIGHT * 2? –
因爲我讀了一點= 2像素。另外,通過試驗和錯誤,這是最接近填充屏幕,但它遠遠不夠完美。基本上,這是一個真正的noob嘗試,我肯定會被修改或丟棄,當我知道這一點。 – Alex
您可以發佈「最接近填充屏幕?」的含義嗎? –