Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw a graph in opengl

Tags:

c++

graph

opengl

I want to create a graph (2d graph) with my code, How can I make a simple 2d graph in opengl? I'm new in opengl, so maybe you can explain me how is the code works, please. by the way here is my code :

    #include <iostream>
using namespace std;

int main ()
{
      double dt = 0.10;    //(it is constant)
      double t = 0.00;

      double dx = 0.10;    //(it is constant)
      double x = 0.00;

      double ddy = 1.00;   //(it is constant)
      double dy = 0.00;
      double y = 1.00;

      cout<<"t   = "<<t<<endl;
      cout<<"dx  = "<<dx<<endl;
      cout<<"x   = "<<x<<endl;
      cout<<"dy  = "<<dy<<endl;
      cout<<"y   = "<<y<<endl;
      cout<<endl;

      while(t<=5) 
      {
                  x = x + dx*dt;
                  dy = dy - ddy * dt * dt;
                  y = y + dy * dt;

                  if (y<=-1)
                  {
                             y = -y;
                             dy = -dy * 0.70;
                  }
      t  = t + dt;

      cout<<"t   = "<<t<<endl;
      cout<<"dx  = "<<dx<<endl;
      cout<<"x   = "<<x<<endl;
      cout<<"dy  = "<<dy<<endl;
      cout<<"y   = "<<y<<endl;
      cout<<endl;

      }
      system("pause");
}
like image 239
Black Cat Avatar asked May 22 '15 06:05

Black Cat


Video Answer


1 Answers

May be you should take a look here http://en.wikibooks.org/wiki/OpenGL_Programming/Scientific_OpenGL_Tutorial_01

There is good information to start drawing curves. Because you have a parametrized curve, just store your x,y in a vertex buffer object (VBO) with a size of the number of points you want on your curve and draw that VBO like that :

glDrawArrays(GL_LINE_STRIP, 0, nbPoints);

This will make a continuous broken line connecting all your points.

like image 164
TrapII Avatar answered Oct 01 '22 00:10

TrapII