Advertisement
HK47

c++ 3d game test

May 9th, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | None | 0 0
  1. #include <glut.h>
  2. #include <vector>
  3. #include <conio.h>
  4. #include <cmath>
  5.  
  6. #define RANGE 0
  7.  
  8. using namespace std;
  9.  
  10. class Star
  11. {
  12. public:
  13.     float x, y, z;
  14. };
  15.  
  16. vector<Star> starList;
  17.  
  18. void display()
  19. {
  20.     glClear(GL_COLOR_BUFFER_BIT);
  21.  
  22.     for (vector<Star>::iterator i = starList.begin(); i != starList.end(); ++i)
  23.     {
  24.         const float L = 70 / abs(i->z - (70 + 50));
  25.         glPointSize(1.0f + 1.0f - (50 - i->z) / 550.0f);
  26.         glColor3f(L, L, L);
  27.         glBegin(GL_POINTS);
  28.         glVertex3f(i->x, i->y, i->z);
  29.         glEnd();
  30.     }
  31.  
  32.     glutSwapBuffers();
  33. }
  34.  
  35. void timer(int = 0)
  36. {
  37.     glutPostRedisplay();
  38.     glutTimerFunc(1, timer, 0);
  39. }
  40.  
  41. void Keyboard(unsigned char key, int x, int y)
  42. {
  43.     if(key == 'w')
  44.     {
  45.         for (vector<Star>::iterator i = starList.begin(); i != starList.end(); ++i)
  46.         {
  47.             i->z += 10;
  48.         }
  49.     }
  50.  
  51.     if(key == 's')
  52.     {
  53.         for (vector<Star>::iterator i = starList.begin(); i != starList.end(); ++i)
  54.         {
  55.             i->z -= 10;
  56.         }
  57.     }
  58.  
  59.     if(key == 'd')
  60.     {
  61.         for (vector<Star>::iterator i = starList.begin(); i != starList.end(); ++i)
  62.         {
  63.             i->x -= 10;
  64.         }
  65.     }
  66.  
  67.     if(key == 'a')
  68.     {
  69.         for (vector<Star>::iterator i = starList.begin(); i != starList.end(); ++i)
  70.         {
  71.             i->x += 10;
  72.         }
  73.     }
  74. }
  75.  
  76. int main(int argc, char **argv)
  77. {
  78.     for (int i = 0; i < 10000; i++)
  79.     {
  80.         Star star = {
  81.             1.0f * (rand() % (2 * RANGE) - RANGE),  
  82.             1.0f * (rand() % (2 * RANGE) - RANGE),
  83.             1.0f * (rand() % 2000)
  84.         };
  85.  
  86.         starList.push_back(star);
  87.     }
  88.  
  89.     glutInit(&argc, argv);
  90.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  91.     glutInitWindowSize(852, 480);
  92.     glutInitWindowPosition(0, 86);
  93.     glutCreateWindow("Stars");
  94.     glClearColor(0, 0, 0, 1.0);
  95.  
  96.     glMatrixMode(GL_PROJECTION);
  97.     glLoadIdentity();
  98.     gluPerspective(45, 1, 0, 100);
  99.     glutDisplayFunc(display);
  100.     glutKeyboardFunc(Keyboard);
  101.     timer();
  102.     glutMainLoop();
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement