I have a simple OpenGL program that I'm trying to use Vertex Buffer Objects to render instead of the old glBegin () - glEnd (). Basically, the user clicks on the window indicating the starting point, and then presses the key to generate subsequent points that OpenGL draws as a string.
I implemented this with glBegin () and glEnd (), but did not manage to use VBO. I am wondering, the problem is that after initializing VBO, I add more vertices for which it does not have allocated memory, and therefore does not display them.
Edit: Also, I'm a little confused about how he knows exactly which values ββin the vertex structure to use for x and y, as well as for r, g, b. I could not find a clear example of this.
#include <windows.h> #include <stdio.h> #include <stdlib.h> #include <Math.h> #include <iostream> #include <vector> #include <GL/glew.h> #include <GL/glut.h> struct vertex { float x, y, u, v, r, g, b; }; const int D = 10; // distance const int A = 10; // angle const int WINDOW_WIDTH = 500, WINDOW_HEIGHT = 500; std::vector<vertex> vertices; boolean start = false; GLuint vboId; void update_line_point() { vertex temp; temp.x = vertices.back().x + D * vertices.back().u; temp.y = vertices.back().y + D * vertices.back().v; temp.u = vertices.back().u; temp.v = vertices.back().v; vertices.push_back(temp); } void update_line_angle() { float u_prime, v_prime; u_prime = vertices.back().u * cos(A) - vertices.back().v * sin(A); v_prime = vertices.back().u * sin(A) + vertices.back().v * cos(A); vertices.back().u = u_prime; vertices.back().v = v_prime; } void initVertexBuffer() { glGenBuffers(1, &vboId); glBindBuffer(GL_ARRAY_BUFFER, vboId); glBufferData(GL_ARRAY_BUFFER, sizeof(vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); } void displayCB() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT); if (start) { glBindBuffer(GL_ARRAY_BUFFER, vboId); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(2, GL_FLOAT, sizeof(vertex), &vertices[0]); glColorPointer(3, GL_FLOAT, sizeof(vertex), &vertices[0]); glDrawArrays(GL_LINE_STRIP, 0, vertices.size()); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glBindBuffer(GL_ARRAY_BUFFER, 0); } /***** this is what I'm trying to achieve glColor3f(1, 0, 0); glBegin(GL_LINE_STRIP); for (std::vector<vertex>::size_type i = 0; i < vertices.size(); i++) { glVertex2f(vertices[i].x, vertices[i].y); } glEnd(); *****/ glFlush(); glutSwapBuffers(); } void mouseCB(int button, int state, int x, int y) { if (state == GLUT_DOWN) { vertices.clear(); vertex temp = {x, WINDOW_HEIGHT - y, 1, 0, 1, 0, 0}; // default red color vertices.push_back(temp); start = true; initVertexBuffer(); } glutPostRedisplay(); } void keyboardCB(unsigned char key, int x, int y) { switch(key) { case 'f': if (start) { update_line_point(); } break; case 't': if (start) { update_line_angle(); } break; } glutPostRedisplay(); } void initCallbackFunc() { glutDisplayFunc(displayCB); glutMouseFunc(mouseCB); glutKeyboardFunc(keyboardCB); } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); glutInitWindowPosition(100, 100); glutCreateWindow("Test"); initCallbackFunc(); // initialize glew GLenum glewInitResult; glewExperimental = GL_TRUE; glewInitResult = glewInit(); if (GLEW_OK != glewInitResult) { std::cerr << "Error initializing glew." << std::endl; return 1; } glClearColor(1, 1, 1, 0); glutMainLoop(); return 0; }