Gabriel Ortega
Gabriel Ortega

Reputation: 481

3D Scrolling Scene - OpenGL

I'm trying to draw clouds in the window and animate them towards the camera. The problem I'm having is that I want to do this continuously without anything being abruptly redrawn. Can someone tell me what I'm missing here?

Here is the code:

#include <gl/glut.h>

int width = 800, height = 600;
float theta = 0, distance1 = -600, distance2 = -600;
void drawCloud()
{
glPushMatrix();
glTranslated(1,0,-2);
glutSolidSphere(4,10,10);
glTranslated(-2,0,-5);
glutSolidSphere(4,10,10);
glTranslated(-1,0,3);
glutSolidSphere(4,10,10);
glPopMatrix();

}

void drawCloudFormation()
{
glPushMatrix();
glTranslated(-60,30,-300);
drawCloud();
glPopMatrix();

glPushMatrix();
glTranslated(15,3,-150);
drawCloud();
glPopMatrix();

glPushMatrix();
glTranslated(50,30,-200);
drawCloud();
glPopMatrix();

glPushMatrix();
glTranslated(-15,-15,-250);
drawCloud();
glPopMatrix();

glPushMatrix();
glTranslated(25,-25,-100);
drawCloud();
glPopMatrix();

glPushMatrix();
glTranslated(-30,0,-50);
drawCloud();
glPopMatrix();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glPushMatrix();
glTranslated(0,0,distance1);
drawCloudFormation();
glPopMatrix();

glPushMatrix();
glTranslated(0,0,distance1-200);
glScaled(-1,1,1);
drawCloudFormation();
glPopMatrix();

glPushMatrix();
glTranslated(0,0,distance2-400);
glScaled(1,1,-1);
drawCloudFormation();
glPopMatrix();

glPushMatrix();
glTranslated(0,0,distance2-600);
glScaled(-1,1,1);
glScaled(1,1,-1);
drawCloudFormation();
glPopMatrix();

glutSwapBuffers();
}

void idle()
{
theta += 0.2;
if (theta == 360) theta = 360;

distance1 += 1;
if (distance1 > 200) distance1 = -600;

distance2 += 1;
if (distance2 > 600) distance2 = -600;

glutPostRedisplay();
}

void main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(100, 200);
glutCreateWindow("Space Ship");
glClearColor(0, 0, 1, 1);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(120, 1, 0.1, 600);
//glOrtho(-2, 2, -2, 2, 0.1, 200);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(0, 0, -200, 0, 0, -600, 0, 1, 0);

glutDisplayFunc(display);
glutIdleFunc(idle);

glutMainLoop();
}

Upvotes: 1

Views: 334

Answers (1)

genpfault
genpfault

Reputation: 52082

Look into particle systems.

You essentially want a rectangular emitter on the far side of your scene spewing clouds (particles) with a fixed velocity toward the camera. When the clouds move behind the camera mark them as inactive and the particle system will spawn a new ones back at the emitter.

Upvotes: 2

Related Questions