I am working on a sim of moving through a field of objects. when an object disappears behind you, another is supposed to appear in front no matter which way the camera has turned. i am using a frustum class that calculates the parameters of the viewable area but i can’t seem to figure out the math after the camera has been turned.
this is the part that generates and displays objects
for ( int i = 0; i < NUM_STARS; i++ )
{
glPushMatrix();
glTranslatef( g_xyz[i][0], g_xyz[i][1], g_xyz[i][2] );
if (g_color==GL_TRUE)
glColor3fv( g_colors[i] );
else
glColor3f( 255,255,255);
glPointSize(g_pointsize); //sets the pixel size of the stars
glBegin(GL_POINTS); // render with points
glVertex2i(0,0); //display a point at current x,y,z
glEnd();
glPopMatrix();
GLfloat lowest, highest, range;
GLfloat xp,yp,zp;
camera.GetPosition(&xp,&yp,&zp);
Vec3 star(g_xyz[i][1], g_xyz[i][0],g_xyz[i][2]);
Vec3 pos(xp, yp, zp);
Vec3 dist;
dist=pos-star;
GLfloat rPos=sqrt(xp*xp+yp*yp+zp*zp);
GLfloat xr=FIELD_WIDTH,yr=FIELD_HEIGHT;
Vec3 a( g_xyz[i][0], g_xyz[i][1], g_xyz[i][2] );
GLfloat tmp;
if (camera.pointInFrustum(a) == CCamera::OUTSIDE)
//if star is outside of frustum then recalc to somewhere inside ? ...
{
lowest=camera.ftl.x; highest=camera.ftr.x;
if (lowest> highest){tmp=lowest;lowest=highest;highest=tmp; }
range=(highest-lowest)+1;
g_xyz[i][0] = -(lowest+int(range*rand()/(RAND_MAX + 1.0)));
lowest=camera.fbl.y; highest=camera.ftl.y;
if (lowest> highest){tmp=lowest;lowest=highest;highest=tmp; }
range=(highest-lowest)+1;
g_xyz[i][1] = -(lowest+int(range*rand()/(RAND_MAX + 1.0)));
lowest=camera.ntr.z; highest=camera.ftr.z;
if (lowest> highest){tmp=lowest;lowest=highest;highest=tmp; }
range=(highest-lowest)+1;
g_xyz[i][2] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
}
}
the project files are at
http://sf.srwlab.com/