plot a function

hallo everybody
i’m a beginner and i’d like to learn how to draw a function using opengl?
let’s say the sin(x) or x^2+y^2=1 (a circle)

You need to have a single parameter.
ie with sin(x) :

double t;
glBegin(GL_LINE_LOOP);
for (t=T_MIN;t<=T_MAX;t+=T_PRECISION) {
glVertex2d(t,sin(t));
}
glEnd();

The equation will be drawn for x belonging in the [t_MIN,T_MAX] interval.
You will need a proper glViewport so that the function will fit well in it.
Decrease T_INTERVAL if you need more precision, increase it for better framerate. Left as an exercise to the reader : make this parameter dependant on the curvature of the function (math involved)…

For the circle equation, you need to parametrize it.

If you want to plot a graph (in 2-d) in the x-y plane, usually the best way to proceed is to loop through the pixels on the screen in the x-direction, look up the point that corresponds to that x-value in the REAL world (this requires the affine mapping between the screen and the real world, i.e. viewport); Then the function is evaluated for the y value; map this back to screen coordinates, and plot that point. This way evaluate the function for exactly the number of points you need to; no fewer, no more…
If you are plotting a curve in 3-d, this wont work. You need to figure out some other way of sampling your function (sampling the parameterization), and then draw lines between the resulting function values. The sampling is mostly figured out by experimenting and tuning… :cool: