Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <cmath>
  4. #include <vector>
  5. #include <GL/gl.h>
  6. #include <GL/glu.h>
  7. #include <SDL/SDL.h>
  8.  
  9. #define SCREEN_WIDTH  600
  10. #define SCREEN_HEIGHT 600
  11. #define SCREEN_BPP     32
  12. #define PI 3.14159265358979
  13. #define GRAVITY 0.000005
  14.  
  15. namespace move { float rotax, rotay, rotaz, zoom; }
  16. struct map {
  17.     int w, h;
  18.     int data[300][300];
  19. };
  20. struct partikkeli {
  21.     float x, y, xplus, yplus, a, v;
  22.     char r, g, b;
  23. };
  24.  
  25. int     videoFlags;
  26. int     running = true;
  27. const   SDL_VideoInfo *videoInfo;
  28. SDL_Surface *heightmap, *surface;
  29. SDL_Event event;
  30. map     world;
  31. Uint8   *napit;
  32. int     palette[255][3];
  33.  
  34.  
  35. float sina(float a) { return sin(a*PI/180); }
  36. float cosa(float a) { return cos(a*PI/180); }
  37.  
  38.  
  39. std::vector<partikkeli> partikkelit;
  40.  
  41.  
  42. void handleEvent() {
  43.     napit = SDL_GetKeyState(NULL);
  44.     if (napit[SDLK_a]){
  45.         move::rotay-=(0.5+napit[SDLK_LSHIFT]);
  46.     } else if (napit[SDLK_d]) {
  47.         move::rotay+=(0.5+napit[SDLK_LSHIFT]);
  48.     }
  49.     if (napit[SDLK_UP]){
  50.         move::rotax+=(0.5+napit[SDLK_LSHIFT]);
  51.     } else if (napit[SDLK_DOWN]) {
  52.         move::rotax-=(0.5+napit[SDLK_LSHIFT]);
  53.     }
  54.     if (napit[SDLK_LEFT]){
  55.         move::rotaz-=(0.5+napit[SDLK_LSHIFT]);
  56.     } else if (napit[SDLK_RIGHT]) {
  57.         move::rotaz+=(0.5+napit[SDLK_LSHIFT]);
  58.     }
  59.     if (napit[SDLK_w]){
  60.         move::zoom+=(0.03+napit[SDLK_LSHIFT]/2);
  61.     } else if (napit[SDLK_s]) {
  62.         move::zoom-=(0.03+napit[SDLK_LSHIFT]/2);
  63.     }
  64.  
  65.     while ( SDL_PollEvent( &event ) ) {
  66.         switch( event.type ) {
  67.         case SDL_KEYDOWN:
  68.             switch ( event.key.keysym.sym ) {
  69.             case SDLK_ESCAPE:
  70.                 exit(0);
  71.                 break;
  72.             default:
  73.                 break;
  74.             }
  75.             break;
  76.         case SDL_QUIT:
  77.             running = false;
  78.             break;
  79.         default:
  80.             break;
  81.         }
  82.     }
  83. }
  84.  
  85. int initGL() {
  86.     glShadeModel( GL_SMOOTH );
  87.     glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  88.     glClearDepth( 1.0f );
  89.     glEnable( GL_LINE_SMOOTH );
  90.     glEnable( GL_DEPTH_TEST );
  91.     glDepthFunc( GL_LEQUAL );
  92.     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
  93.     return 1;
  94. }
  95.  
  96. int runAndDraw( int frm ) {
  97.  
  98.     move::rotax = (move::rotax<-90?-90:(move::rotax>90?90:move::rotax));
  99.  
  100.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  101.     glLoadIdentity();
  102.     glTranslatef(0.0,0.0f,move::zoom);
  103.  
  104.     glRotatef(move::rotax, 1, 0, 0);
  105.     glRotatef(move::rotay, 0, 1, 0);
  106.     glRotatef(move::rotaz, 0, 0, 1);
  107.  
  108.  
  109.  
  110.     for (int y = 0; y < world.h-1; y++)
  111.     for (int x = 0; x < world.w-1; x++) {
  112.         glBegin( GL_TRIANGLES );
  113.             glColor3f(palette[world.data[x][y]][0]/255.0, palette[world.data[x][y]][1]/255.0, palette[world.data[x][y]][2]/255.0);
  114.  
  115.             glVertex3f((-(world.w/2)+x)/400.0,      world.data[x][y]/2550.0,    (-(world.h/2)+y)/400.0);
  116.             glVertex3f((-(world.w/2)+x+1)/400.0,    world.data[x+1][y]/2550.0(-(world.h/2)+y)/400.0);
  117.             glVertex3f((-(world.w/2)+x)/400.0,      world.data[x][y+1]/2550.0(-(world.h/2)+y+1)/400.0);
  118.  
  119.             glVertex3f((-(world.w/2)+x+1)/400.0,    world.data[x+1][y+1]/2550.0,(-(world.h/2)+y+1)/400.0);
  120.             glVertex3f((-(world.w/2)+x+1)/400.0,    world.data[x+1][y]/2550.0(-(world.h/2)+y)/400.0);
  121.             glVertex3f((-(world.w/2)+x)/400.0,      world.data[x][y+1]/2550.0(-(world.h/2)+y+1)/400.0);
  122.         glEnd();
  123.     }
  124.  
  125.  
  126.     SDL_GL_SwapBuffers( );
  127.  
  128.  
  129.     static GLint T0     = 0;
  130.     static GLint Frames = 0;
  131.     Frames++;
  132.     {
  133.     GLint t = SDL_GetTicks();
  134.     if (t - T0 >= 5000) {
  135.         GLfloat seconds = (t - T0) / 1000.0;
  136.         GLfloat fps = Frames / seconds;
  137.         printf("%d frame per %g second = %g FPS\n", Frames, seconds, fps);
  138.         T0 = t;
  139.         Frames = 0;
  140.     }
  141.     }
  142.  
  143.     return 1;
  144. }
  145.  
  146. int main( int argc, char **argv ) {
  147.     atexit(SDL_Quit);
  148.  
  149.     if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) exit(-1);
  150.     videoInfo = SDL_GetVideoInfo();
  151.     if ( !videoInfo ) exit(-2);
  152.  
  153.     videoFlags  = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_RESIZABLE | SDL_HWSURFACE;
  154.     SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  155.     surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags );
  156.     if ( !surface ) exit(-3);
  157.  
  158.     initGL();
  159.  
  160.     GLfloat ratio = ( GLfloat )SCREEN_WIDTH / ( GLfloat )SCREEN_HEIGHT;
  161.     glViewport( 0, 0, ( GLsizei )SCREEN_WIDTH, ( GLsizei )SCREEN_HEIGHT );
  162.  
  163.     glMatrixMode( GL_PROJECTION );
  164.     glLoadIdentity();
  165.     gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
  166.     glMatrixMode( GL_MODELVIEW );
  167.     glLoadIdentity();
  168.  
  169.     srand(SDL_GetTicks());
  170.  
  171.     SDL_Surface *palettetmp= SDL_LoadBMP("palette.bmp");
  172.     if (palettetmp == NULL) exit (-10);
  173.     palettetmp = SDL_DisplayFormat(palettetmp);
  174.     SDL_LockSurface(palettetmp);
  175.     for (int y = 0; y < 255; y++) {
  176.         Uint32 a =   ((unsigned int*)palettetmp->pixels)[y];
  177.         palette[y] = {(a >> 16) % 256, (a >> 8) % 256, a%256};
  178.     }
  179.     SDL_UnlockSurface(palettetmp);
  180.  
  181.  
  182.     /* 3047538.bmp */
  183.     // PITÄÄ OLLA KOOLTAAN PIENEMPÄÄ KUIN 300px*300px
  184.     heightmap = SDL_LoadBMP("Heightmap.bmp");
  185.     if (heightmap == NULL) exit (-11);
  186.     heightmap = SDL_DisplayFormat(heightmap);
  187.  
  188.     SDL_LockSurface(heightmap);
  189.     for (int y = 0; y < heightmap->h; y++)
  190.     for (int x = 0; x < heightmap->w; x++) {
  191.         Uint32 a =   ((unsigned int*)heightmap->pixels)[y*(heightmap->pitch/sizeof(unsigned int)) + x];
  192.         world.data[x][y] =
  193.         (.3*((a >> 16) % 256))+
  194.         (.59*((a >> 8) % 256))+
  195.         (.11*(a%256));
  196.     }
  197.  
  198.  
  199.     world.w = heightmap->w;
  200.     world.h = heightmap->h;
  201.     SDL_UnlockSurface(heightmap);
  202.  
  203.     move::rotax = 45;
  204.     move::rotay = 20;
  205.     move::zoom = -.7;
  206.  
  207.     for (int i = 0; i < 1000; i++) {
  208.         partikkeli tmp;
  209.         tmp.x = SCREEN_WIDTH/2;
  210.         tmp.y = SCREEN_HEIGHT/2;
  211.         tmp.a = rand()%360;
  212.         tmp.v = (rand()/RAND_MAX);
  213.         tmp.xplus = cosa(tmp.a)/(tmp.v);
  214.         tmp.xplus = sina(tmp.a)/(tmp.v);
  215.         tmp.r = 255;
  216.         tmp.g = 255;
  217.         tmp.b = 255;
  218.         partikkelit.push_back(tmp);
  219.     }
  220.  
  221.     for (int frm = 0; running; frm++) {
  222.         handleEvent();
  223.         runAndDraw(frm);
  224.     }
  225.     exit(0);
  226.     return( 0 );
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement