编辑:Apple的工作编码示例在此链接中找到:http: //developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/TechniquesforWorkingwithVertexData/TechniquesforWorkingwithVertexData.html
在为200 x 200个正方形的固定网格创建顶点之后,我通过使用VBO将顶点发送到GPU来渲染它.我怎样才能更新顶点的颜色?数百个顶点的颜色会频繁变化 - 每隔几帧.
我以前解决了不使用VBO的问题.我创建了顶点和颜色数组,并使用以下代码渲染顶点.我通过更新数组改变了顶点的颜色triangleColours:
-(void)render {
glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
glClear(GL_COLOR_BUFFER_BIT);
[effect prepareToDraw];
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 2,
GL_FLOAT, GL_FALSE, 0, triangleVertices);
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor, 4,
GL_FLOAT, GL_FALSE, 0, triangleColours);
glDrawArrays(GL_TRIANGLES, 0, numTriangleVertices);
glDisableVertexAttribArray(GLKVertexAttribPosition);
glDisableVertexAttribArray(GLKVertexAttribColor);
}
Run Code Online (Sandbox Code Playgroud)
虽然上面的代码有用,但它吃了很多电池,所以现在我正试着用VBO做.顶点不会改变位置,所以我理解VBO是一个很好的选择,但是如何改变顶点的颜色呢?
这是我目前的代码简化为只绘制一个方块:
编辑:我如何更新下面的代码,以便它们更改时只上传顶点颜色?VBO设置代码和绘图代码应该如何变化?如何更新我的verticesColours颜色数组然后调用glBufferSubData()?
// Setup vertices and VBO
// Vertices for a single square
const Vertex Vertices[] = {
// position and colour
{{-20, -20}, {1, 1, 1, 1}},
{{-20, -20}, {1, 1, 1, 1}},
{{-20, -20}, {1, …Run Code Online (Sandbox Code Playgroud) 我正在使用计算属性来获取我的books数组中的最后一本书.但是,我似乎无法使用此属性直接设置图书的position属性,因为我的下面的尝试显示:
struct Book {
var position: CGPoint?
}
class ViewController: UIViewController {
var books = [Book]()
var currentBook: Book {
return books[books.count - 1]
}
func setup() {
// Compiler Error: Cannot assign to property: 'currentBook' is a get-only property
currentBook.position = CGPoint.zero
}
}
Run Code Online (Sandbox Code Playgroud)
以下工作,但我希望它更具可读性和单行.
books[books.count - 1].position = CGPoint.zero
Run Code Online (Sandbox Code Playgroud)
我可以使用函数返回当前的书,但使用属性会更干净.还有另外一种方法吗?
在 Javascript 中,如何创建一个在 0 - 580 之间乘以 20 的随机偶数?
例如:220、360、180、0(最小)、400、200、580(最大)