/ Published in: C++
Very simple openGL particle system
Expand |
Embed | Plain Text
#include <time.h> #include <cmath> #include <Windows.h> #include <glut.h> #include <GL\GLU.h> float limits = .8; float X=0, Y=0; const int MAX_PARTICLES = 1000; const int MIN_PARTICLES = 10; int currentParticle = 1; float posX[MAX_PARTICLES], posY[MAX_PARTICLES]; void moveParticles(int amount_of_particles) { srand (time (NULL) ); float myX, myY; Sleep(3); glColor3d(2, .5, 0); for (int i = 0; i < amount_of_particles; i++) { myX = rand() % 3 + 1; if(myX==1 && posX[i]<=limits ){ int mytemp = rand() % 100 + 1; int temp = rand() % 5 + 1; posX[i]+=temp*.001; posY[i]+=mytemp*.0004; } if(myX==2){posX[i]+=.00;posY[i]+=.01;} if(myX==3 && posX[i]>=-limits){ int temp = rand() % 5 + 1; int mytemp = rand() % 100 + 1; posX[i]-=temp*.001; posY[i]+=mytemp*.0004; } /////////////////////////////////////////// if(posY[i]>=limits){ posY[i]=0; posX[i]=0; } } } void Reshape(int height, int width) { glViewport(0, 0, width, height); glClearColor(0, 0, 0, 1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60, (float)height/(float)width, 1, 100); glMatrixMode(GL_MODELVIEW); } void Draw(void) { int thingy = 1; bool check = false; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* glPushMatrix(); glColor3d(.3, .1, 0); glTranslated(0, 0, -3); glutSolidCube(.3); glPopMatrix(); */ if (check == false) { float R, G, B; glPushMatrix(); glBegin(GL_TRIANGLES); for (int i = 0; i < MAX_PARTICLES; i++) { R = rand() % 100 + 1; G = rand() % 100 + 1; B = rand() % 100 + 1; glColor3d(R*.01, G*.01, B*.01); //glColor3d(10, 10, 0); //glColor3d(0, 0+posY[i], 0); glVertex3f(X-.01, Y, -2); glVertex3f(X+.01, Y, -2); glVertex3f(X, Y+.02, -2); X = posX[i]; Y = posY[i]; } glEnd(); glPopMatrix(); check = true; } switch(thingy){ case 1: Sleep(1); moveParticles(currentParticle); if (currentParticle != MAX_PARTICLES) { currentParticle++; } glutPostRedisplay(); break; } glutSwapBuffers(); } void Keyboard(unsigned char key, int x, int y) { switch(key){ default: break; glutPostRedisplay(); } } const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f }; const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f }; const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat high_shininess[] = { 100.0f }; int main(int argc, char **argv) { glutInitDisplayMode(GLUT_DOUBLE); glutInit(&argc, argv); glutInitWindowSize(500, 500); glutInitWindowPosition(100, 100); glutCreateWindow("SOME OPENGL PROJECTS"); glutReshapeFunc(Reshape); glutDisplayFunc(Draw); glutKeyboardFunc(Keyboard); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glutMainLoop(); }
You need to login to post a comment.
