OpenGL double precision issue

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

OpenGL double precision issue

alicana
Hi all,

I have a problem with double precision when drawing lines.

X axis range is from 99.9999 to 100.001. So, I set ortho as
gl.glOrtho(99.9999d, 100.001d, -5, 200, -10, 10);

Example input data:
- X-axis value is increasing 1.25e-7 for every single point

Display callback:
public void display(GLAutoDrawable drawable) {
	GL2 gl = drawable.getGL().getGL2();
	gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	gl.glBegin(GL2.GL_LINE_STRIP);
	
	int j = -5;
	double offset = (0.001 / 8000);
	for (int i = 0; i < 8000; i++) {
		int y = j++;
		double x = 100 + (offset * i);
			gl.glVertex2d( x, y);
                if (j == 200) {
			j = -5;
		}
	}

	gl.glEnd();

}

Expected output:



But I have following as a result:



Any advice ?
Reply | Threaded
Open this post in threaded view
|

Re: OpenGL double precision issue

jmaasing
alicana wrote
Any advice ?
Scale everything up by for example * 1000 and see what happens.
Reply | Threaded
Open this post in threaded view
|

Re: OpenGL double precision issue

alicana
Due to OpenGL internals prefers single precision (float I guess), still precision lost.



jmaasing wrote
Scale everything up by for example * 1000 and see what happens.
 But, I think, scaling as jmaasing said is the keyword.
Instead of multiplying the data and ortho by 1K, to protect datas' precision as much as possible, I cut off integer part before passing to glVertex2d method. (Already set projection from 0 to 1) Then I get the acceptable results.

I wonder, whether there are other approaches/techniques?
Reply | Threaded
Open this post in threaded view
|

Re: OpenGL double precision issue

elect
Use multisampling (and skip deprecated GL)