Reputation: 711
So I decided to make a 3D Stars kinda thing in C++ with SDL and OpenGL. I created a Point class which holds x, y, and z values. I create an array of Points and fill it with random coordinates. This seems to work but when I do glTranslatef(0,0,0.1f) or something similar, the stars don't come close, they just disappear.
//OpenGL Initialization Code
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0f,640.0/480.0,0.3f,500.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_DEPTH_TEST);
//Random point generation
for(int i = 0; i < 200000; i++)
{
float randomX = (float)rand()/((float)RAND_MAX/100.0f) - 50.0f;
float randomY = (float)rand()/((float)RAND_MAX/20) - 10.0f;
float randomZ = (float)rand()/((float)RAND_MAX/20) - 20.0f;
points[i] = Point(randomX, randomY,randomZ);
}
//Render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POINTS);
for(int i = 0; i < 200000; i++)
{
glVertex3f(points[i]._x, points[i]._y, points[i]._z);
}
glEnd();
glTranslatef(0,0,0.1f);
SDL_GL_SwapBuffers();
SDL_Flip(screen);
What am I doing wrong?
Upvotes: 0
Views: 1865
Reputation: 711
I moved the OpenGL initialization code after the code where I init SDL SetVideoMode, it works as it should now.
Upvotes: 0
Reputation: 993
Is that code your render function?
If it is, then you should move the point generation elsewhere so that they get generated only once.
If it is not, then you should move the initialization code to the beginning of the render function. The matrices should be cleared every frame.
If that doesn't work:
Try temporarily disabling depth testing. If they don't disappear and you see lines, you need to adjust your view frustum. Try using gluLookAt
Also, try translating them in the negative z axis. If I remember correctly, in opengl, object-space coordinates are farther away as z decreases. (0 is closer than -1)
Upvotes: 2
Reputation: 3289
I'm not sure why they'ld be disappearing, but in your sample code it should be inert. You load a new Identity matrix at the top for the MODELVIEW matrix, and then translate it after you've rendered. Try moving the glTranslate to right before your glBegin.
Upvotes: 0