我尝试将本教程与 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 没有给出任何错误