Swift: glDrawElements crashing with EXC_BAD_ACCESS code=1
我正在通过本指南在 iOS 上学习 OpenGL,我想在 swift 上实现所有内容。所以,有一些代码让我崩溃了:
内存结构:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
private struct Vertex { var Position: (GLfloat, GLfloat, GLfloat) var Color: (GLfloat, GLfloat, GLfloat, GLfloat) } private static var Vertices = [ private static var Indices: [GLubyte] = [ |
创建顶点缓冲区:
1
2 3 4 5 6 7 8 9 |
var vertexBuffer = GLuint() glGenBuffers(1, &vertexBuffer) glBindBuffer(GLenum(GL_ARRAY_BUFFER), vertexBuffer) glBufferData(GLenum(GL_ARRAY_BUFFER), Vertices.size, Vertices, GLenum(GL_STATIC_DRAW)) var indexBuffer = GLuint() |
设置内存偏移:
1
2 3 4 |
var positionPtr = 0
glVertexAttribPointer(GLuint(positionSlot), 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(strideofValue(Vertex)), &positionPtr) var colorPtr = strideof(GLfloat) * 3 glVertexAttribPointer(GLuint(colorSlot), 4, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(strideofValue(Vertex)), &colorPtr) |
崩溃(尝试绘制):
1
2 3 |
var startPtr = 0
// EXC_BAD_ACCESS code=1 here! glDrawElements(GLenum(GL_TRIANGLES), GLsizei(Indices.count / 3), GLenum(GL_UNSIGNED_BYTE), &startPtr) |
所有着色器都编译没有任何错误并且
这里我如何计算数组的大小:
1
2 3 4 5 6 |
extension Array
{ var size: Int { get { return self.count * strideof(Element) } } } |
UPD:我正在使用 OpenGLES 2.0。
我在 4 个月前从你的指南中学到了金额。我试图将它从objective-c转换为swift,直到在图片下绘制相同的矩形。
现在我运行它并转换为 Swift 2.1。它仍然有效并在下面显示相同的图像。
这是我的代码(方法 setupVBO、渲染和结构)
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
// Track of all our per-vertex information (currently just color and position) struct Vertex { var Position: (CFloat, CFloat, CFloat) var Color: (CFloat, CFloat, CFloat, CFloat) } // Array with all the info for each vertex // Array that gives a list of triangles to create, by specifying the 3 vertices that make up each triangle //helper extensions to pass arguments to GL land //The best way to send data to OpenGL is through something called Vertex Buffer Objects. //There are two types of vertex buffer objects – one to keep track of the per-vertex data (like we have in the Vertices array), and one to keep track of the indices that make up triangles (like we have in the Indices array). glGenBuffers(1, &indexBuffer) func render() { //glViewport(0, 0, GLint(frame.size.width), GLint(frame.size.height)) // feed the correct values to the two input variables for the vertex shader – the Position and SourceColor attributes. // This actually ends up calling your vertex shader for every vertex you pass in, and then the fragment shader on each pixel to display on the screen. _context.presentRenderbuffer(Int(GL_RENDERBUFFER)) |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/268595.html