嗨,我正在尝试使用本教程学习 OpenGl http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ 我会说本教程不适合初学者。他们展示了很多适用于 1 个三角形的代码,但没有更多示例。Atm 我正在尝试编写绘制函数。线条或矩形,我有问题,因为这对我来说很难。我想写可重用的函数。但我不明白 VBO :/ 我想编写将从主循环执行的函数 draw。
class lines{
public: lines(){
}
static void draw(GLuint ve){
float vertices[] = {-0.5f, -0.5f, 0.5f, 0.5f};
unsigned int indices[] = {0, 1};
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glDrawElements(GL_LINES, 2, GL_UNSIGNED_INT, indices);
}
};
// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader" );
static const GLfloat g_vertex_buffer_data[] = {
-0.8f, -1.0f,0.0f,
0.8f, -1.0f, 0.0f,
-0.8f, 1.0f, 0.0f,
-0.8f, 1.0f, 0.0f,
0.8f, 1.0f, 0.0f,
0.8, -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);
do{
// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );
// Use our shader
glUseProgram(programID);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(0);
lines::draw(vertexbuffer);
// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );