COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Help! I have possible precision errors in a simple OpenGL scene

• Follow

```I am drawing a simple scene of a sphere of radius 1 and a plane
of 10000 x 10000.

The sphere at (9000, 1, -9000) looks embedded in the plane
although it shouldn't, and rotating the camera causes all kinds of
flickering.

However if the sphere is placed at (90, 1, -90) the scene looks fine.
Can anyone explain this? Thanks!

Here is the C++ code:

//////////////////////////////////////////////////////////////
HDC m_hdc;
int m_width = 800;
int m_height = 600;

....

void drawScene()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// set up the viewport and projection matrix

glViewport( 0, 0, m_width, m_height );

glMatrixMode( GL_PROJECTION );
gluPerspective(
60,					// FOV angle
(double)m_width / (double)m_height,	// aspect ratio
.001,					// near
10000	);					// far

const double ballX = 9000;
const double ballY = 1;
const double ballZ = -9000;

// rotate the camera

glMatrixMode( GL_MODELVIEW );

static double cameraAngle = 0;
cameraAngle += .001;
glRotated( cameraAngle, 0, 1, 0 );
glTranslated( -ballX, -(ballY + 1), -(ballZ + 10) );

// draw a red plane

glColor3ub( 200, 100, 100 );
glVertex3d( 0, 0, 0 );
glVertex3d( 10000, 0, 0 );
glVertex3d( 10000, 0, -10000 );
glVertex3d( 0, 0, -10000 );
glEnd();

// draw a green ball

glTranslated( ballX, ballY, ballZ );
glColor3ub( 100, 200, 100 );
gluSphere( m_quadricObj, 1, 10, 10 );

SwapBuffers( m_hdc );
}

//////////////////////////////////////////////////////////////
```
 0

```fungus wrote:

>
> 10000/0.001 is 10000000, a big number - much too
> big to expect a sphere of radius 1 to draw
> perfectly. Try adjusting these numbers, especially
> the 0.001.

Yep, tried .1 and it works fine now. Thank you!
```
 0

```MPB wrote:

> fungus wrote:
>
>
>>10000/0.001 is 10000000, a big number - much too
>>big to expect a sphere of radius 1 to draw
>>perfectly. Try adjusting these numbers, especially
>>the 0.001.
>
>
> Yep, tried .1 and it works fine now. Thank you!

FYI, in a real application 'near' would be slightly smaller than the collision
distance. For example, in a FPS where the collision distance is 5, you might use
a near of 4.5.

--
Phil Frisbie, Jr.
Hawk Software
http://www.hawksoft.com

```
 0

2 Replies
288 Views

Similiar Articles:

7/22/2012 6:46:38 PM