我尝试将本教程与Golang一起使用:http : //www.opengl- tutorial.org/beginners-tutorials/tutorial-2-the-first- triangle/ go-version打开窗口并使背景变蓝,但是不显示三角形。C版本确实显示了它。这是Go中的代码:
err := glfw.Init() if err != nil { log.Fatal("Failed to init GLFW: " + err.Error()) } err = glfw.OpenWindow(1024, 768, 0,0,0,0, 32,0, glfw.Windowed) if err != nil { log.Fatal("Failed to open GLFW window: " + err.Error()) } if gl.Init() != 0 { log.Fatal("Failed to init GL") } gl.ClearColor(0.0, 0.0, 0.3, 0.0) // create vertexbuffer gVertexBufferData := []float32{-1.0,-1.0,0.0, 1.0,-1.0,0.0, 0.0,1.0,0.0} vertexBuffer := gl.GenBuffer() vertexBuffer.Bind(gl.ARRAY_BUFFER) gl.BufferData(gl.ARRAY_BUFFER, len(gVertexBufferData), gVertexBufferData, gl.STATIC_DRAW) for { // clear screen gl.Clear(gl.COLOR_BUFFER_BIT) // first attribute buffer: vertices var vertexAttrib gl.AttribLocation = 0 vertexAttrib.EnableArray() vertexBuffer.Bind(gl.ARRAY_BUFFER) var f float32 = 0.0 vertexAttrib.AttribPointer( 3, // size false, // normalized? 0, // stride &f) // array buffer offset // draw the triangle gl.DrawArrays(gl.TRIANGLES, 0, 3) vertexAttrib.DisableArray() glfw.SwapBuffers() }
这是c中有效的代码:
if(!glfwInit()) return -1; if(!glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW )) return -1; if(glewInit() != GLEW_OK) return -1; glClearColor(0.0f, 0.0f, 0.3f, 0.0f); GLuint VertexArrayID; glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); static const GLfloat g_vertex_buffer_data[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, }; GLuint vertexbuffer; glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); while(1) { glClear( GL_COLOR_BUFFER_BIT ); // 1rst attribute buffer : vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glVertexAttribPointer( 0, 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); // Draw the triangle ! glDrawArrays(GL_TRIANGLES, 0, 3); // From index 0 to 3 -> 1 triangle glDisableVertexAttribArray(0); // Swap buffers glfwSwapBuffers(); }
也许我给了vertexAttrib.AttribPointer()错误的参数,因为我不确定要给它什么而不是(void *)0。我尝试了nil,但这导致应用程序崩溃。&gVertexBufferData [0]也不起作用。
我正在使用github.com/banthar/gl作为glew-wrapper,转到1.0.2和ubuntu 12.04 amd64。
编辑更新:
glGetError没有给出任何错误
我遇到了同样的问题,在查看您的帖子后设法解决了这个问题,因此首先非常感谢。
我设法通过使用Banthar绑定的工作分支来显示三角形AttribPointer:
AttribPointer
vertexAttrib.AttribPointer( 3, // size gl.FLOAT, //type false, // normalized? 0, // stride nil) // array buffer offset
并将字节大小传递给BufferData。
[...] data := []float32{0, 1, 0, -1, -1, 0, 1, -1, 0} [...] gl.BufferData(gl.ARRAY_BUFFER, len(data)*4, data, gl.STATIC_DRAW) [...]
传递正确的长度可能是更好的方法。