Swift - 如何在OpenGL ES中绘制线条?

时间:2014-12-19 18:32:37

标签: ios swift opengl-es

我尝试使用GLKitViewSwift上绘制黑色单行,但它不起作用。 我可以清除屏幕,我可以用任何颜色填充它,但我无法在其上绘制任何东西。 我知道,这个问题很多次被问过,但我找不到任何关于Swift的答案。 更重要的是,没有任何关于在Swift中使用OpenGL ES的信息,所以我来到这里。

这是我的代码:

import GLKit

class MyGLKit: GLKView {
    override func drawRect(rect: CGRect) {
        glClearColor(0.8, 0.8, 0.8, 1.0)
        glClear(GLbitfield(GL_COLOR_BUFFER_BIT))

        glColor4f(0, 0, 0, 1)

        var line: [GLfloat] = [-1, 0, 1, 0]
        var len = line.count * sizeof(GLfloat)

        // Create an handle for a buffer object array
        var bufferObjectNameArray: GLuint = 0

        // Have OpenGL generate a buffer name and store it in the buffer object array
        glGenBuffers(1, &bufferObjectNameArray);

        // Bind the buffer object array to the GL_ARRAY_BUFFER target buffer
        glBindBuffer(GLbitfield(GL_ARRAY_BUFFER), bufferObjectNameArray);

        // Send the line data over to the target buffer in GPU RAM
        glBufferData(
        GLbitfield(GL_ARRAY_BUFFER),    // the target buffer
        len,                            // the number of bytes to put into the buffer
        line,                           // a pointer to the data being copied
        GLbitfield(GL_STATIC_DRAW));    // the usage pattern of the data

        glVertexPointer(2, GLbitfield(GL_FLOAT), 0, line);
        var programHandle: GLuint = glCreateProgram()
        glLinkProgram(programHandle)

        glDrawArrays(GLbitfield(GL_LINES), 0, 2);
    }
}

我不确定这个代码在做什么,导致OpenGL ES看起来疯狂,在通常的OpenGL之后。我的意思是,你需要编写很多代码来绘制一行。它为什么这样工作?

2 个答案:

答案 0 :(得分:0)

我没有使用过Swift,但基于OpenGL EXC_BAD_ACCESS when calling glDrawElements in Swift but not in Objective-C中的代码,您的glVertexPointer()调用应如下所示:

let bufferOffset: CConstVoidPointer = COpaquePointer(UnsafePointer<Int>(0))
glVertexPointer(2, GLbitfield(GL_FLOAT), 0, bufferOffset);

看起来你还没有真正构建着色器程序:

var programHandle: GLuint = glCreateProgram()
glLinkProgram(programHandle)

如果您使用简单的OpenGL,那么需要介绍一些事情。您需要创建着色器对象,为它们提供用GLSL编写的代码,调用以编译着色器等。我相信GLKit具有使用预烘焙着色器的功能。

至于为什么OpenGL如此“疯狂”,这超出了本网站的范围。而且非常主观。恕我直言,要记住的要点是,OpenGL并不是为程序员提供方便的API。它旨在为访问图形硬件提供相对较薄的通用抽象。可比API的趋势实际上是使抽象层更薄,以减少开销(参见例如DirectX 12)。

没有什么可以阻止任何人在OpenGL之上实现更方便的更高级API。这些接口存在,人们使用它们。

答案 1 :(得分:0)

看起来您需要添加GLKBaseEffect

无论如何,这是我的工作解决方案: 要进行测试,请创建一个MyGLKit.swift文件

import Foundation
import GLKit
import OpenGLES

struct OpenGLColor {
    var r: GLubyte = 0
    var g: GLubyte = 0
    var b: GLubyte = 0
    var a: GLubyte = 0
}

class MyGLKit: GLKView {

    var lineData: [GLfloat] = []
    var colorData: [GLubyte] = []
    var linesCount:Int = 0

    func setup(){
        self.context = EAGLContext(api: .openGLES2)!
        EAGLContext.setCurrent(self.context)

        //effect
        let effect: GLKBaseEffect = GLKBaseEffect()
        let projectionMatrix: GLKMatrix4 = GLKMatrix4MakeOrtho(-1.0, 1.0, -1.0, 1.0, 1.0, -1.0)
        effect.transform.projectionMatrix = projectionMatrix
        effect.prepareToDraw()


        //OPTIONAL > disable slow GL extensions
        glDisable(GLenum(GL_DEPTH_TEST))
        glDisable(GLenum(GL_BLEND))
        glDisable(GLenum(GL_DITHER))
        glDisable(GLenum(GL_STENCIL_TEST))
        glDisable(GLenum(GL_TEXTURE_2D))
        //

        glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MAG_FILTER), GL_NEAREST)
        glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
        glEnableVertexAttribArray(GLuint(GLKVertexAttrib.position.rawValue))
        glEnableVertexAttribArray(GLuint(GLKVertexAttrib.color.rawValue))

    }

    func drawLine(x:GLfloat, y:GLfloat, x2:GLfloat, y2:GLfloat, color: OpenGLColor){

        //suppport for retina
        var x = x
        var y = y
        var x2 = x2
        var y2 = y2
        x *= 2
        x2 *= 2
        y *= 2
        y2 *= 2

        //calculate position for opengl
        let w: GLfloat = GLfloat(self.bounds.size.width)
        let h: GLfloat = GLfloat(self.bounds.size.height)

        //coordinates x/y
        lineData.append(((1.0/w)*x)-1)
        lineData.append(1-((1.0/h)*y))
        lineData.append(0)
        lineData.append(((1.0/w)*x2)-1)
        lineData.append(1-((1.0/h)*y2))
        lineData.append(0)

        //color
        colorData.append(color.r)
        colorData.append(color.g)
        colorData.append(color.b)
        colorData.append(color.a)
        colorData.append(color.r)
        colorData.append(color.g)
        colorData.append(color.b)
        colorData.append(color.a)

        linesCount += 1

    }

    override func draw(_ rect: CGRect) {

        glLineWidth(1.0)
        glClearColor(0.8, 0.8, 0.8, 1.0)
        glClear(GLbitfield(GL_COLOR_BUFFER_BIT))

        //draw layer
        if (lineData.count > 0){
            glVertexAttribPointer(GLuint(GLKVertexAttrib.position.rawValue), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), 0, lineData)
            glVertexAttribPointer(GLuint(GLKVertexAttrib.color.rawValue), 4, GLenum(GL_UNSIGNED_BYTE), GLboolean(GL_TRUE), 0, colorData)
            glDrawArrays(GLenum(GL_LINES), 0, GLsizei(linesCount*2))
        }
    }

}

然后在您的UIViewController中使用它:

let g = MyGLKit()
        g.setup()
        g.frame = self.view.bounds
        self.view.addSubview(g)
        var color = OpenGLColor()
        color.r = 20
        color.b = 30
        color.g = 40
        color.a = 255
        g.drawLine(x: 10, y: 10, x2: GLfloat(g.frame.size.width), y2: 10, color: color)
        g.drawLine(x: 10, y: 50, x2: GLfloat(g.frame.size.width), y2: 50, color: color)
        g.setNeedsDisplay()