3d starfields

Hi!

I’ve written a simple 3d-starfield(like in old demos) using a perspective view.

This code renders one layer of the field (I have three layers total):

glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);

glColor3f(0.2f,0.2f,1.0f);
glBegin(GL_POINTS);
for(i=0;i<100;i++)
{
x = stars[i][0];
y = stars[i][1];
glVertex3f(x,y,x);
// Move star
stars[i][0]-=0.01f;
if (stars[i][0]<-2.0f)
{
stars[i][0]+=4.0f;
}
}
glEnd();

Setting z to 0.0 like this,
glVertex3f(x,y,0.0f);
gives me a 2d-starfield which is what I started out with.

Changing glVertex3f(x,y,0.0f) to glVertex3f(x,y,x) gave me a “3d-effect” since I am using a perspective view. Here is my resize/reshape function:

void GLCore::resizeViewport(short width, short height)
{
if (height==0)
{
height=1;
}
m_vpWidth = width;// Save new viewport width
m_vpHeight = height; // Save new viewport height

// Modify viewport aspect ratio
glViewport(0, 0, m_vpWidth, m_vpHeight);

glMatrixMode(GL_PROJECTION); // select the projection matrix
glLoadIdentity(); // reset the projection matrix

// calculate the aspect ratio of the window
gluPerspective(45.0f,(GLfloat)m_vpWidth/(GLfloat)m_vpHeight,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // select the modelview matrix
glLoadIdentity();
}

I know that my “3d-starfield” is not a “proper” 3d-starfield but that does not interest me at this point since I’m only playing around with this code to learn about OpenGL.

Using my resize function in combination with
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
and locking the x and y coordinates so that they stay between -2.0 and 2.0 gives me a starfield that stays on screen, but the starfield does not cover the entire screen…

What I want to know is how I’d get the starfield to cover the entire screen when I use a perspective view? (gluProject maybe?) I mean locking x and y to be between -2.0 and 2.0 is just plain guess work, what about a method that will guarantee that the starfield “covers” the entire screen no matter what?

By the way, is this how you more experienced OpenGL coders would render a 3d-starfield?
Would you maybe skip the perspective view for this kind of effect and use an orthoview instead?

Using an orthoview and calculating the perspective “effect” yourself is not difficult but I thought that I would try and use the 3d capabilities of OpenGL since its already there.

I’m looking forward to comments about this and maybe hints to other ways of rendering starfields.

Thank you in advance.

/D

[This message has been edited by DJ (edited 07-16-2001).]

[This message has been edited by DJ (edited 07-16-2001).]

If I wanted to do a old 2D starfield would I go for the ortho view. It would be more exciting to try to take advantage of 3D and other cool OpenGL effects.