Hi I am trying to learn OpenGl using this tutorial http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ I would say that this tutorial is not good for person who start. They show a lot of code which works for 1 triangle but there is no more examples. Atm I am trying to write function which draw. Lines or Rectangles and I have problem because it is hard to hard for me. I want to wrote reusable function. But I don't understand VBO :/ I want to write function draw which will be executed from main loop.
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 ) );