【发布时间】:2014-02-21 23:22:03
【问题描述】:
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?
-
因为我读到 1 点 = 2 像素。同样通过反复试验,这最接近于填满屏幕,但远不够完美。基本上,这是一个真正的菜鸟尝试,当我弄清楚这一点时,我肯定会被修改或丢弃。
-
你能张贴一张截图,说明“最接近填满屏幕”的意思吗?
-
当我具体看这个效果时,我认为这就是问题所在。如果我用纹理覆盖我的矩形,我可以看到它准确地居中(没有 *2)。当我再次执行 *2 时,现在我可以看到我的矩形似乎只覆盖了屏幕的左下象限,所以看起来这正是问题所在。不知道我之前还切换了什么,但仅隔离那部分似乎揭示了问题。请将此作为答案发布,我会接受。谢谢!
标签: ios ipad opengl-es-2.0