// This code example is created for educational purpose // by Thorsten Thormaehlen (contact: www.thormae.de). // It is distributed without any warranty. #include #include // we use glut here as window manager #define _USE_MATH_DEFINES #include #include #include #include #include using namespace std; class Renderer { public: float t; int mode; private: GLuint bufID; int bufSize; public: // constructor Renderer() : t(0.0), mode(0), bufID(0), bufSize(0) {} //destructor ~Renderer() { if(bufID !=0) glDeleteBuffers( 1, &bufID); } public: void init() { glEnable(GL_DEPTH_TEST); // generating VBO input data std::vector dataIn; unsigned ayimutSegs = 1000; unsigned polarSegs = 1000; float ayimutStep = 2.0f * float(M_PI) / float(ayimutSegs); float polarStep = float(M_PI) / float(polarSegs); float r = 1.0f; bufSize = 0; for(unsigned m=0; m < ayimutSegs; m++) { for(unsigned n=0; n < polarSegs; n++) { float phi = ayimutStep*m; float theta = polarStep * n; // compute xyz from spherical coordinates float x = r * sin(theta) * cos(phi); float y = r * sin(theta) * sin(phi); float z = r * cos(theta); dataIn.push_back(x); dataIn.push_back(y); dataIn.push_back(z); bufSize++; } } // generating VBO glGenBuffers(1, &bufID); glBindBuffer(GL_ARRAY_BUFFER, bufID); glBufferData(GL_ARRAY_BUFFER, dataIn.size()*sizeof(float), &dataIn[0], GL_STATIC_DRAW); } void resize(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective (30.0, (float)w/(float)h, 0.1, 50.0); } void display() { glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // set camera gluLookAt(3.5, -1.0, 3.5, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0); // draw scene glRotatef(t, 0.0f, 0.0f, 1.0f); // activating VBO glBindBuffer(GL_ARRAY_BUFFER, bufID); int stride = 0; glVertexPointer(3, GL_FLOAT, stride, NULL); glEnableClientState(GL_VERTEX_ARRAY); if(mode == 0) { glColor3f(1.0f,1.0f,1.0f); }else{ glColorPointer(3, GL_FLOAT, stride, NULL); glEnableClientState(GL_COLOR_ARRAY); } // render VBO glDrawArrays(GL_POINTS, 0, bufSize); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); } }; //this is a static pointer to a Renderer used in the glut callback functions static Renderer *renderer; //glut static callbacks start static void glutResize(int w, int h) { renderer->resize(w,h); } static void glutDisplay() { renderer->display(); glutSwapBuffers(); glutReportErrors(); } static void timer(int v) { float offset = 0.25f; renderer->t += offset; glutDisplay(); glutTimerFunc(unsigned(20), timer, ++v); } static void glutKeyboard(unsigned char key, int x, int y) { bool redraw = false; std::string modeStr; switch(key) { case '1': if(renderer->mode == 1) renderer->mode = 0; else renderer->mode = 1; redraw = true; break; } if(redraw) { glutDisplay(); } } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(320, 320); glutCreateWindow("Press 1 to use the VBO data for vertex colors as well"); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "Glew error: %s\n", glewGetErrorString(err)); } glutDisplayFunc(glutDisplay); //glutIdleFunc(glutDisplay); glutReshapeFunc(glutResize); glutKeyboardFunc(glutKeyboard); renderer = new Renderer; renderer->init(); glutTimerFunc(unsigned(20), timer, 0); glutMainLoop(); }