簡體   English   中英

Swift - 如何在OpenGL ES中繪制線條?

[英]Swift - How to draw line in OpenGL ES?

我試圖在GLKitView使用Swift繪制黑色單行,但它不起作用。 我可以清除屏幕,我可以用任何顏色填充它,但我不能在它上面畫任何東西。 我知道,這個問題很多次被問過,但我找不到任何關於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之后。 我的意思是,你需要編寫很多代碼來繪制一行。 它為什么這樣工作?

我沒有使用Swift,但是基於OpenGL EXC_BAD_ACCESS中的代碼在Swift中調用glDrawElements而不是在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。 這些接口存在,人們使用它們。

看起來你需要添加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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM