PDA

View Full Version : drawing hyperbola in openGL



NarutoUzumakis
01-04-2011, 07:17 PM
hi there,ive been struggling with this for a couple of days,i just can't draw a good approximation of a hyperbola,ive tried some stuff,but it didnt work out..i did manage to draw an ellipse and a circle,but i have no idea how to draw a hyperbola..does anyone know,any ideas?pls help me if you got something,it is very very urgent,any solid idea is welcomed,i'll try everything possible..i do know it's not the hardest problem,it's far from it,but i don't have any ideas,just can't come up with one..
thanks
niko

MaxH
01-04-2011, 07:54 PM
I assume you mean you can't write an OpenGL program that plots a hyperbola? Otherwise you wouldn't be posting in this forum. I also assume that you've written an OpenGL program that draws an ellipse? If so, post the code and the resulting graphics. If you can do that, I think you'll get a lot of help. Doing a hyperbola would be very similar to doing an ellipse. Are we talking 2D or 3D graphics?

NarutoUzumakis
01-05-2011, 08:19 AM
hey thanks for the reply,this is the code for the ellipse,and it draws some axes needed to draw hyperbola.



const float RADIAN = 3.14159/180;
void drawEllipse()
{
float i,c;
i=0.0;
glColor3f(0.0,0.0,0.0);


glBegin(GL_POINTS);

while(i<360)
{

float degInRad = i*RADIAN;
glVertex2f(cos(degInRad)*a,sin(degInRad)*b);
i+=0.1;
}

glEnd();

}

void drawHyperbola()
{
float k=b1/a1;
float x,y,i;

glColor3f(0.0,0.0,0.0);






float c;//foci
c=sqrt(a1*a1+b1*b1);
glBegin(GL_POINTS);
glVertex3f(c,0.0,0.0);
glVertex3f(-c,0.0,0.0);
glEnd();



glColor3f(0.0,0.6,0.0);//axes
glPushMatrix();
glBegin(GL_LINES);
glVertex3f(-1000,-k*1000,0);
glVertex3f(1000,k*1000,0);
glEnd();
glPopMatrix();

glPushMatrix();//axes
glBegin(GL_LINES);
glVertex3f(1000,-k*1000,0);
glVertex3f(-1000,k*1000,0);
glEnd();
glPopMatrix();


glPushMatrix();
glBegin(GL_LINES);
glVertex3f(a1,-b1,0.0);
glVertex3f(a1,b1,0.0);
glVertex3f(-a1,-b1,0.0);
glVertex3f(-a1,b1,0.0);
glEnd();
glPopMatrix();
}

p.s. a,b and a1,b1 come from the user input,they specify the "size" of an ellipse and a hyperbola

so,yeah,no the only thing left is to draw a hyperbola,an approximation of it in openGL

thanks

MaxH
01-05-2011, 04:23 PM
Try the stuff below inside your drawHyperbola routine.
This will draw both the vertical and horizontal hyperbolas using coefficients a1 and b1.
This is not quite the way I'd do this if I was coding it up from scratch.
But I tried to stay close to your coding style.
One general comment. You don't any of those Push and PopMatrix commands.
They are only necessary when you want to isolate transformations from other parts of your code
(i.e. glTranslate, glRotate, glScale, glMultMatrix, etc.) Try commenting them out.
Let me know how it goes. Good luck.



glColor3f (1, 1, 0);

glBegin (GL_POINTS);
for (x = -10; x < 10; x += 0.01) { // Horizontal axis hyperbola
y = a1 * sqrt (1 + (x*x)/(b1*b1));
glVertex2f (x, y);
glVertex2f (x, -y);
}
glEnd ();

glColor3f (1, 0.4, 0.4); // Vertical axis hyperbola

glBegin (GL_POINTS);
for (y = -10; y < 10; y += 0.01) {
x = a1 * sqrt (1 + (y*y)/(b1*b1));
glVertex2f ( x, y);
glVertex2f (-x, y);
}
glEnd();

NarutoUzumakis
01-06-2011, 02:59 AM
it works perfectly,thanks a lot,as for the Push and Pop matrix commands,i know they're only used to isolate transformations,but my code was around 300-350 lines long,i had a few transformations of the coordinate system,and something was messed up,so in order to isolate everything,i used them in my complete code:).but thanks again,the code is perfect,
cheers

kRogue
01-08-2011, 03:29 PM
There are better ways to better ways to draw various implicit functions via GL.. without tesselation.

Consider the following GLSL fragment shader:


uniform float tolerance;
uniform float color;
in vec2 coord;

out vec4 pretty;

void
main(void)
{
float v, f;
//want to "draw" x*x - y*y = 1

v= coord.x*coord.x - coord.y*coord.y - 1;

#ifdef BLENDING
//can do alpha blending with f
f=smoothstep(-tolerance, tolerance, v);
pretty=vec4(color, f)

#else
// or just do a brutal discard
//
if(abs(f)>tolerance)
{
discard;
}
pretty=vec4(color, 1.0);
#endif
}


Thus drawing a hyperbole becomes drawing a quad.

Alfonse Reinheart
01-08-2011, 04:14 PM
There are better ways to better ways to draw various implicit functions via GL.. without tesselation.

Better is of course a relative term. This will produce mathematically perfect results, but it is difficult to say if this would be faster than CPU or GPU tessellation.

And of course, without the ability to write the sample mask (hopefully coming soon), it would be difficult to make this work correctly with multisampling.

ugluk
01-08-2011, 07:54 PM
Hehe, another ingenious idea.