2017-05-12 6 views
1

OpenGl ES 2.0을 사용하는 안드로이드 코드가 있고 IOS 스위프트 3으로 변환하고 싶습니다. 나는 하루 종일 인터넷 검색을하고 있지만 작동하지 않을 것입니다. hofefully you 나를 도울 수있다 ...IOS 스위프트 3에 대한 OpenGL ES 2.0 안드로이드

내 Shader는 잘 작동해야한다. Adroid에서 glVertexAttribPointer 함수로 그려지는 정점 배열을 전달할 수 있지만 IOS에서는 같은 방식으로 작동하지 않는 것처럼 보이고 그림을 그릴 수 없다. 밖으로로드하는 방법/정점 배열 (작동하지 않는)

스위프트

을 만들

이 내 도우미 클래스입니다 :

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)) 
    } 
} 

안드로이드 (작동 중임)

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); 
    } 
} 
스위프트에서

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); 
    } 
} 

그리기하지 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); 

만들기

: 아무 것도 그려지고 그것은 나에게 오류를 제공하지 않습니다.

감사 레오

+1

결과가 정확히 무엇입니까? 아무것도 그려져 있습니까? Constants.BYTES_PER_FLOAT이란 무엇입니까? prepareToDraw에서는 매번 새로운 버퍼를 생성하지만 결코 삭제하지 않습니다. 이것은 메모리 누출입니다. 전반적으로 iOS에서 Objective-C를 사용하는 것이 좋습니다. Swift에서 사용할 수도 있습니다. 프로젝트에서 둘 다 가질 수 있습니다. –

+0

아무 것도 그려지지 않지만 오류를주지 않습니다. BYTES_PER_FLOAT = 4 Im은 지금 objective-c를 시도하고 있지만 동일한 문제가있는 것으로 보입니다. 안드로이드에서 glVertexAttribPointer 함수의 끝에서 배열을 전달합니다. int swift glVertexAttribPointer는 배열을 전달하지 않습니다 ... – LDK

+1

색상을 출력 할 때 버퍼를 지우시겠습니까? 매우 단순한 그림을 그리려고 했습니까? 예를 들어 셰이더가 단단한 하드 코딩 된 색상을 출력하는 정적 삼각형 하나만? –

답변

1

나는 그것을 알아 냈어요!

스위프트이 별도의 클래스로

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) 
    } 
} 

(현재 작업) :

다음 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) 

도면을 작성

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; 
    } 
} 

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)) 
    } 
}