【问题标题】:OpenGL Es 2.0 GLKit Draw PixelOpenGL Es 2.0 GLKit 绘制像素
【发布时间】:2012-04-06 14:08:04
【问题描述】:

在屏幕上绘制像素方面我需要一点帮助。我编写的代码在模拟器上运行良好,但是当我在设备上部署时它会输出垃圾。所以这是我的代码:

我有 GLKViewController 设置,这里是 viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];

    if (!self.context) {
        NSLog(@"Failed to create ES context");
    }

    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    view.drawableDepthFormat = GLKViewDrawableDepthFormat24;

    [EAGLContext setCurrentContext:self.context];

    self.effect = [[GLKBaseEffect alloc] init];


    self.effect.useConstantColor = GL_TRUE;

    self.effect.constantColor = GLKVector4Make(
                                               0.0, // Red
                                               0.0, // Green
                                               0.0, // Blue
                                               1.0f);// Alpha

    GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, 480, 0, 320, -1024, 1024);
    self.effect.transform.projectionMatrix = projectionMatrix;

}

这是我绘制点/像素的地方:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    GLfloat points[] =
    {
        110.0f, 110.0f,
        111.0f, 110.0f,
        110.0f, 111.0f,
        111.0f, 111.0f,

        112.0f, 112.0f,
        113.0f, 112.0f,
        112.0f, 113.0f,
        113.0f, 113.0f,
    };

    glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 0.0f);
    self.effect.transform.modelviewMatrix = modelViewMatrix;


    // Prepare the effect for rendering 
    [self.effect prepareToDraw];

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, 2*4, points);
    glDrawArrays(GL_POINTS, 0, 8);

}

当我在模拟器上运行时,它工作正常,即相应地绘制像素,但是当我在 iPod4 上部署时,它显示一些垃圾。我是初学者,所以需要帮助来显示简单的像素。

【问题讨论】:

    标签: ios opengl-es opengl-es-2.0 glkit


    【解决方案1】:

    我认为这里的问题是您需要在顶点着色器中设置内置的gl_PointSize 变量,因为模拟器和设备上的默认值不同。

    this question 的答案进一步解释。

    【讨论】:

    • 这似乎是一个正确的解释,但是,如果您使用的是 GLKBaseEffect,那么您将无法访问顶点着色器。 GLKit 非常适合简单的显示设置和避免缓冲区,但我强烈建议不要使用 GLKBaseEffect,而是建议您编写自己的着色器。
    • 所以只要避免 GLKBaseEffect 就可以在 GLKView 中使用自定义着色器?我无法让它工作,但这可能只是我的代码的问题 - 我正在学习。
    • 是的,我绝对会推荐它!真正的 OpenGL ES 爱好者更喜欢完全控制他们的视图和缓冲区,但我喜欢 GLKView,因为它使最终代码更精简,更少混乱——因此将注意力转移到真正重要的事情上:着色器。 “边走边学”是移动图形游戏的名称。
    • 原来我没有将着色器添加到复制的捆绑资源中,所以它们没有被编译...... 哦!现在工作得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-05
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    相关资源
    最近更新 更多