android - OpenGL ES 2.0 Android至IOS Swift 3

标签 android ios swift opengl-es-2.0

我有使用OpenGl ES 2.0的Android代码,我想将其转换为IOS Swift 3。
我花了整整一天的时间进行谷歌搜索,但是那根本行不通,
很高兴您可以帮助我...

我的着色器应该可以正常工作,在Adroid中,我可以传递要在glVertexAttribPointer函数中绘制的顶点数组,但是在IOS中,它似乎不能以相同的方式工作,因此我无法弄清楚如何加载/创建顶点数组。 ..

Swift(不起作用)

这是我的助手课:

import Foundation
import OpenGLES
import GLKit

class VertexArray : NSObject {
    private var floatBuffer = [GLfloat]()

    init(_ vertexData : Array<Float>) {
        for i in 0 ..< vertexData.count  {
            floatBuffer.append(GLfloat(vertexData[i]))
        }


    public func prepareToDraw() {
        // Create a Vector Buffer Object that will store the vertices on video memory
        var vbo : GLuint = GLuint()
        glGenBuffers(1, &vbo)

        // Allocate space and upload the data from CPU to GPU
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vbo)
        glBufferData(GLenum(GL_ARRAY_BUFFER), MemoryLayout.size(ofValue: floatBuffer), floatBuffer, GLenum(GL_STATIC_DRAW))
    }

    public func setVertexAttribPointer(_ dataOffset : Int, _ attributeLocation : GLuint, _ componentCount : Int, _ stride : Int) {
        glEnableVertexAttribArray(attributeLocation)
        glVertexAttribPointer(attributeLocation, GLint(componentCount), GLenum(GL_FLOAT), GLboolean(UInt8(GL_FALSE)), GLsizei(stride), BUFFER_OFFSET(dataOffset))
    }

    func BUFFER_OFFSET(_ i: Int) -> UnsafeRawPointer? {
        return UnsafeRawPointer(bitPattern: i)
    }
}

现在,我创建我的VertexArray,它是一个矩形:
let triangleVertices : [Float] = [
     //Order of coordinates x, y, r, g, b, a
     pos.x,                pos.y+Float(height),    r3, g3, b3, alpha,
     pos.x+Float(width),   pos.y+Float(height),    r4, g4, b4, alpha,
     pos.x,                pos.y,                  r1, g1, b1, alpha,
     pos.x+Float(width),   pos.y,                  r2, g2, b2, alpha
]    
vertexArray = VertexArray(triangleVertices)

我这样画:
internal static let POSITION_COMPONENT_COUNT = 2    //x,y
internal static let COLOR_COMPONENT_COUNT = 4       //r,g,b,a
internal static let STRIDE = (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * 4

public override func draw(_ projectionMatrix : Array<GLfloat>) {
    if (visible) {
        glEnable(GLenum(GL_BLEND))
        glBlendFunc(GLenum(GL_SRC_ALPHA), GLenum(GL_ONE_MINUS_SRC_ALPHA))

        ProgramManager.useProgram(ProgramManager.colorShaderProgram)
        ProgramManager.setColorShaderUniforms(projectionMatrix)

        vertexArray.prepareToDraw()
        vertexArray.setVertexAttribPointer(
            0,
            GLuint(colorProgram.getPositionAttributeLocation()),
            GL_Colored_Shape.POSITION_COMPONENT_COUNT,
            GL_Colored_Shape.STRIDE)

        vertexArray.setVertexAttribPointer(
            GL_Colored_Shape.POSITION_COMPONENT_COUNT,
            GLuint(colorProgram.getColorAttributeLocation()),
            GL_Colored_Shape.COLOR_COMPONENT_COUNT,
            GL_Colored_Shape.STRIDE)

        glDrawArrays(GLenum(GL_TRIANGLE_STRIP), 0, 4)

        glDisable(GLenum(GL_BLEND))
    }
}

Android(有效)
import static android.opengl.GLES20.GL_FLOAT;
import static android.opengl.GLES20.glEnableVertexAttribArray;
import static android.opengl.GLES20.glVertexAttribPointer;
import static com.ldk.madquiz.Constants.BYTES_PER_FLOAT;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class VertexArray {  
    private final FloatBuffer floatBuffer;

    public VertexArray(float[] vertexData) {
        floatBuffer = ByteBuffer
            .allocateDirect(vertexData.length * BYTES_PER_FLOAT)
            .order(ByteOrder.nativeOrder())
            .asFloatBuffer()
            .put(vertexData);
    }

    public void setVertexAttribPointer(int dataOffset, int attributeLocation,
        int componentCount, int stride) {        
        floatBuffer.position(dataOffset);        
        glVertexAttribPointer(attributeLocation, componentCount, GL_FLOAT, 
            false, stride, floatBuffer);
        glEnableVertexAttribArray(attributeLocation);

        floatBuffer.position(0);
    }
}

创建VertexArray
float[] triangleVertices = {
     //Order of coordinates x, y, r, g, b, a
     pos.x,         pos.y+height,    r3, g3, b3, alpha,
     pos.x+width,   pos.y+height,    r4, g4, b4, alpha,
     pos.x,         pos.y,           r1, g1, b1, alpha,
     pos.x + width, pos.y,           r2, g2, b2, alpha
};
vertexArray = new VertexArray(triangleVertices);

画画
protected static final int POSITION_COMPONENT_COUNT = 2; //x,y
protected static final int COLOR_COMPONENT_COUNT = 4;    //r,g,b,a
protected static final int STRIDE = (POSITION_COMPONENT_COUNT
            + COLOR_COMPONENT_COUNT) * 4;

@Override
public void draw(float[] projectionMatrix) {
    if (visible) {
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        ProgramManager.useProgram(ProgramManager.colorShaderProgram);
        ProgramManager.setColorShaderUniforms(projectionMatrix);

        vertexArray.setVertexAttribPointer(
            0,
            colorProgram.getPositionAttributeLocation(),
            POSITION_COMPONENT_COUNT,
            STRIDE);

        vertexArray.setVertexAttribPointer(
            POSITION_COMPONENT_COUNT,
            colorProgram.getColorAttributeLocation(),
            COLOR_COMPONENT_COUNT,
            STRIDE);

        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        glDisable(GL_BLEND);
    }
}

在Swift中:什么也没画,也没有给我一个错误。

谢谢
狮子座

最佳答案

我知道了!

Swift(正在运行)

import Foundation
import OpenGLES
import GLKit

class VertexArray : NSObject {

    private var floatBuffer = [Vertex]()
    private var length : Int = 0

    var vertexBuffer : GLuint = 0

    init(_ vertexData : Array<Float>) {
        var i = 0
        while (i < vertexData.count) {
            floatBuffer.append(
                Vertex(
                    GLfloat(vertexData[i]),
                    GLfloat(vertexData[i+1]),
                    GLfloat(vertexData[i+2]),
                    GLfloat(vertexData[i+3]),
                    GLfloat(vertexData[i+4]),
                    GLfloat(vertexData[i+5])
                ))
            i = i + 6
        }
        self.length = vertexData.count
    }

    public func prepareToDraw() {
        if (vertexBuffer == 0) {
            glGenBuffers(1, &vertexBuffer)
            glBindBuffer(GLenum(GL_ARRAY_BUFFER), vertexBuffer)
            let count = floatBuffer.count
            let size =  MemoryLayout<Vertex>.size
            glBufferData(GLenum(GL_ARRAY_BUFFER), count * size, floatBuffer, GLenum(GL_STATIC_DRAW))

        }
        glBindBuffer(GLenum(GL_ARRAY_BUFFER), vertexBuffer)
    }

    public func setVertexAttribPointer(_ dataOffset : Int, _ attributeLocation : GLuint, _ componentCount : Int, _ stride : Int) {
        glEnableVertexAttribArray(attributeLocation)
        glVertexAttribPointer(attributeLocation, GLint(componentCount), GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(stride), BUFFER_OFFSET(dataOffset * Constants.BYTES_PER_FLOAT))
    }

    public func getLength() -> Int {
        return length;
    }

    func BUFFER_OFFSET(_ i: Int) -> UnsafeRawPointer? {
        return UnsafeRawPointer(bitPattern: i)
    }
}

通过这个额外的类:
import Foundation
import OpenGLES
import GLKit

struct Vertex {
    var x : GLfloat = 0.0
    var y : GLfloat = 0.0
    var r : GLfloat = 0.0
    var g : GLfloat = 0.0
    var b : GLfloat = 0.0
    var a : GLfloat = 0.0

    init(_ x : GLfloat, _ y : GLfloat, _ r : GLfloat, _ g : GLfloat, _ b : GLfloat, _ a : GLfloat) {
        self.x = x
        self.y = y
        self.r = r;
        self.g = g;
        self.b = b;
        self.a = a;
    }
}

创建VertexArray
let triangleVertices : [Float] = [
     //Order of coordinates x, y, r, g, b, a
     pos.x,                pos.y+Float(height),    r3, g3, b3, alpha,
     pos.x+Float(width),   pos.y+Float(height),    r4, g4, b4, alpha,
     pos.x,                pos.y,                  r1, g1, b1, alpha,
     pos.x+Float(width),   pos.y,                  r2, g2, b2, alpha
]    
vertexArray = VertexArray(triangleVertices)

画画:
internal static let POSITION_COMPONENT_COUNT = 2    //x,y
internal static let COLOR_COMPONENT_COUNT = 4       //r,g,b,a
internal static let STRIDE = (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * 4

public override func draw(_ projectionMatrix : Array<GLfloat>) {
    if (visible) {
        glEnable(GLenum(GL_BLEND))
        glBlendFunc(GLenum(GL_SRC_ALPHA), GLenum(GL_ONE_MINUS_SRC_ALPHA))

        ProgramManager.useProgram(ProgramManager.colorShaderProgram)
        ProgramManager.setColorShaderUniforms(projectionMatrix)

        vertexArray.setVertexAttribPointer(
            0,
            GLuint(colorProgram.getPositionAttributeLocation()),
            GL_Colored_Shape.POSITION_COMPONENT_COUNT,
            GL_Colored_Shape.STRIDE)
        vertexArray.setVertexAttribPointer(
            GL_Colored_Shape.POSITION_COMPONENT_COUNT,
            GLuint(colorProgram.getColorAttributeLocation()),
            GL_Colored_Shape.COLOR_COMPONENT_COUNT,
            GL_Colored_Shape.STRIDE)
        vertexArray.prepareToDraw()

        glDrawArrays(GLenum(GL_TRIANGLE_STRIP), 0, 4)

        glDisable(GLenum(GL_BLEND))
    }
}

编辑
我仍然遇到一些问题,因此代码可能并不完美,但目前确实可以使用...

关于android - OpenGL ES 2.0 Android至IOS Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43933702/

相关文章:

android - 在 Android 中,1 个 Activity 中应该只有 1 个 View 吗?

android - 移动设备上出现 404 错误,但在 PC 浏览器上有效

swift - Apple Watch Glance——使用 NSURL 加载数据

image - 通过在自定义单元格内进行修饰来制作基本的动画/翻译

ios - RxSwift - 类属性的绑定(bind)

android - 我可以发送 "SMS received intent"吗?

android - 离开 Activity 时,我应该如何处理 ImageView?

iphone - iOS 中的自动图像方向

ios - 自定义 UIView : UIButton addTarget not called

ios - 迁移后更改 Core Data 时崩溃