【问题标题】:Draw Square with OpenGL ES for iOS使用适用于 iOS 的 OpenGL ES 绘制正方形
【发布时间】:2011-02-15 20:16:50
【问题描述】:

我正在尝试使用苹果提供的 GLPaint 示例项目来绘制一个矩形。我尝试修改顶点,但无法在屏幕上显示矩形。手指画效果很好。我的 renderRect 方法中是否缺少某些内容?

- (void)renderRect {

    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);

    // Replace the implementation of this method to do your own custom drawing.
    static const GLfloat squareVertices[] = {
        -0.5f, -0.33f,
        0.5f, -0.33f,
        -0.5f,  0.33f,
        0.5f,  0.33f,
    };

    static float transY = 0.0f;

    glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);


    // Render the vertex array
    glVertexPointer(2, GL_FLOAT, 0, squareVertices);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);


    // Display the buffer
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
}

项目的其余部分已设置为允许在屏幕上绘图,但仅供参考,这些是设置的 gl 设置。

// Set the view's scale factor
self.contentScaleFactor = 1.0;

// Setup OpenGL states
glMatrixMode(GL_PROJECTION);
CGRect frame = self.bounds;
CGFloat scale = self.contentScaleFactor;
// Setup the view port in Pixels
glOrthof(0, frame.size.width * scale, 0, frame.size.height * scale, -1, 1);
glViewport(0, 0, frame.size.width * scale, frame.size.height * scale);
glMatrixMode(GL_MODELVIEW);

glDisable(GL_DITHER);
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);

glEnable(GL_BLEND);
// Set a blending function appropriate for premultiplied alpha pixel data
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_POINT_SPRITE_OES);
glTexEnvf(GL_POINT_SPRITE_OES, GL_COORD_REPLACE_OES, GL_TRUE);
glPointSize(width / brushScale);   

【问题讨论】:

    标签: iphone objective-c ios opengl-es


    【解决方案1】:
    static const GLfloat squareVertices[] = {
            30.0f,  300.0f,//-0.5f, -0.33f,
            280.0f, 300.0f,//0.5f, -0.33f,
            30.0f,  170.0f,//-0.5f,  0.33f,
            280.0f, 170.0f,//0.5f,  0.33f,
        };
    

    这绝对是太多了。 OpenGL 在 [-1..1] 范围内标准化了屏幕坐标。所以你必须将设备坐标转换为标准化的。

    【讨论】:

    • 是的,我一直在使用 -1 到 1 的范围。粘贴代码时我错过了帖子。现在即使使用标准化的屏幕坐标,我仍然什么也看不到。注释坐标来自 iOS OpenGLES 模板代码中使用的正方形。我也尝试过更改颜色,但无济于事。
    • 尝试添加 glLoadIdentity(); [EAGLContext setCurrentContext:context] 之后; glBindFramebufferOES(GL_FRAMEBUFFER_OES, viewFramebuffer);
    • 不,不走运。现在代码确实为绘图设置了画笔纹理。也许这导致正方形由于纹理而无法正确绘制。这里是源代码:link 你会注意到 renderLineFromPoint 在渲染顶点数组之前使用了 glPointSize 和 glBlendFunc。有什么想法吗?
    • 您的设置代码正在更改投影矩阵,因此您的屏幕坐标应为 origin=0,0 size= frame.size.width, frame.size.height。此外,翻译是累积的,因为在您的绘图代码中没有调用 glLoadIdentity()。此外,为了更好地衡量,将绘图颜色设置为明显的颜色(我通常使用红色 (1,0,0,1))。
    【解决方案2】:

    问题是:

    (1)如下代码:

        glMatrixMode(GL_PROJECTION);
        CGRect frame = self.bounds;
        CGFloat scale = self.contentScaleFactor;
        // Setup the view port in Pixels
        glOrthof(0, frame.size.width * scale, 0, frame.size.height * scale, -1, 1);
        glViewport(0, 0, frame.size.width * scale, frame.size.height * scale);
    

    确定屏幕坐标范围从左下角的 (0, 0) 到右上角的 frame.size。换句话说,一个 OpenGL 单元就是一个 iPhone 点。所以你的数组:

    static const GLfloat squareVertices[] = {
        -0.5f, -0.33f,
        0.5f, -0.33f,
        -0.5f,  0.33f,
        0.5f,  0.33f,
    };
    

    尺寸小于 1 像素。

    (2) 您在设置中有以下内容:

        brushImage = [UIImage imageNamed:@"Particle.png"].CGImage;
    
        /* ...brushImage eventually becomes the current texture... */
    
        glEnable(GL_TEXTURE_2D);
    

    您随后未能为您的四边形提供纹理坐标。可能你想禁用GL_TEXTURE_2D

    所以如下:

    static const GLfloat squareVertices[] = {
        0.0f, 0.0f,
        0.0,  10.0f,
        90.0,  0.0f,
        90.0f, 10.0f,
    };
    
    
    
    glDisable(GL_TEXTURE_2D);
    glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
    
    // Render the vertex array
    glVertexPointer(2, GL_FLOAT, 0, squareVertices);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
    

    将在屏幕左下方生成一个 90 点宽和 10 点高的白色四边形。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多