Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <cmath>
- #include <vector>
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <SDL/SDL.h>
- #define SCREEN_WIDTH 600
- #define SCREEN_HEIGHT 600
- #define SCREEN_BPP 32
- #define PI 3.14159265358979
- #define GRAVITY 0.000005
- namespace move { float rotax, rotay, rotaz, zoom; }
- struct map {
- int w, h;
- int data[300][300];
- };
- struct partikkeli {
- float x, y, xplus, yplus, a, v;
- char r, g, b;
- };
- int videoFlags;
- int running = true;
- const SDL_VideoInfo *videoInfo;
- SDL_Surface *heightmap, *surface;
- SDL_Event event;
- map world;
- Uint8 *napit;
- int palette[255][3];
- float sina(float a) { return sin(a*PI/180); }
- float cosa(float a) { return cos(a*PI/180); }
- std::vector<partikkeli> partikkelit;
- void handleEvent() {
- napit = SDL_GetKeyState(NULL);
- if (napit[SDLK_a]){
- move::rotay-=(0.5+napit[SDLK_LSHIFT]);
- } else if (napit[SDLK_d]) {
- move::rotay+=(0.5+napit[SDLK_LSHIFT]);
- }
- if (napit[SDLK_UP]){
- move::rotax+=(0.5+napit[SDLK_LSHIFT]);
- } else if (napit[SDLK_DOWN]) {
- move::rotax-=(0.5+napit[SDLK_LSHIFT]);
- }
- if (napit[SDLK_LEFT]){
- move::rotaz-=(0.5+napit[SDLK_LSHIFT]);
- } else if (napit[SDLK_RIGHT]) {
- move::rotaz+=(0.5+napit[SDLK_LSHIFT]);
- }
- if (napit[SDLK_w]){
- move::zoom+=(0.03+napit[SDLK_LSHIFT]/2);
- } else if (napit[SDLK_s]) {
- move::zoom-=(0.03+napit[SDLK_LSHIFT]/2);
- }
- while ( SDL_PollEvent( &event ) ) {
- switch( event.type ) {
- case SDL_KEYDOWN:
- switch ( event.key.keysym.sym ) {
- case SDLK_ESCAPE:
- exit(0);
- break;
- default:
- break;
- }
- break;
- case SDL_QUIT:
- running = false;
- break;
- default:
- break;
- }
- }
- }
- int initGL() {
- glShadeModel( GL_SMOOTH );
- glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
- glClearDepth( 1.0f );
- glEnable( GL_LINE_SMOOTH );
- glEnable( GL_DEPTH_TEST );
- glDepthFunc( GL_LEQUAL );
- glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
- return 1;
- }
- int runAndDraw( int frm ) {
- move::rotax = (move::rotax<-90?-90:(move::rotax>90?90:move::rotax));
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glTranslatef(0.0,0.0f,move::zoom);
- glRotatef(move::rotax, 1, 0, 0);
- glRotatef(move::rotay, 0, 1, 0);
- glRotatef(move::rotaz, 0, 0, 1);
- for (int y = 0; y < world.h-1; y++)
- for (int x = 0; x < world.w-1; x++) {
- glBegin( GL_TRIANGLES );
- 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);
- glVertex3f((-(world.w/2)+x)/400.0, world.data[x][y]/2550.0, (-(world.h/2)+y)/400.0);
- glVertex3f((-(world.w/2)+x+1)/400.0, world.data[x+1][y]/2550.0, (-(world.h/2)+y)/400.0);
- glVertex3f((-(world.w/2)+x)/400.0, world.data[x][y+1]/2550.0, (-(world.h/2)+y+1)/400.0);
- glVertex3f((-(world.w/2)+x+1)/400.0, world.data[x+1][y+1]/2550.0,(-(world.h/2)+y+1)/400.0);
- glVertex3f((-(world.w/2)+x+1)/400.0, world.data[x+1][y]/2550.0, (-(world.h/2)+y)/400.0);
- glVertex3f((-(world.w/2)+x)/400.0, world.data[x][y+1]/2550.0, (-(world.h/2)+y+1)/400.0);
- glEnd();
- }
- SDL_GL_SwapBuffers( );
- static GLint T0 = 0;
- static GLint Frames = 0;
- Frames++;
- {
- GLint t = SDL_GetTicks();
- if (t - T0 >= 5000) {
- GLfloat seconds = (t - T0) / 1000.0;
- GLfloat fps = Frames / seconds;
- printf("%d frame per %g second = %g FPS\n", Frames, seconds, fps);
- T0 = t;
- Frames = 0;
- }
- }
- return 1;
- }
- int main( int argc, char **argv ) {
- atexit(SDL_Quit);
- if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) exit(-1);
- videoInfo = SDL_GetVideoInfo();
- if ( !videoInfo ) exit(-2);
- videoFlags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE | SDL_RESIZABLE | SDL_HWSURFACE;
- SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
- surface = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, videoFlags );
- if ( !surface ) exit(-3);
- initGL();
- GLfloat ratio = ( GLfloat )SCREEN_WIDTH / ( GLfloat )SCREEN_HEIGHT;
- glViewport( 0, 0, ( GLsizei )SCREEN_WIDTH, ( GLsizei )SCREEN_HEIGHT );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0f, ratio, 0.1f, 100.0f );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- srand(SDL_GetTicks());
- SDL_Surface *palettetmp= SDL_LoadBMP("palette.bmp");
- if (palettetmp == NULL) exit (-10);
- palettetmp = SDL_DisplayFormat(palettetmp);
- SDL_LockSurface(palettetmp);
- for (int y = 0; y < 255; y++) {
- Uint32 a = ((unsigned int*)palettetmp->pixels)[y];
- palette[y] = {(a >> 16) % 256, (a >> 8) % 256, a%256};
- }
- SDL_UnlockSurface(palettetmp);
- /* 3047538.bmp */
- // PITÄÄ OLLA KOOLTAAN PIENEMPÄÄ KUIN 300px*300px
- heightmap = SDL_LoadBMP("Heightmap.bmp");
- if (heightmap == NULL) exit (-11);
- heightmap = SDL_DisplayFormat(heightmap);
- SDL_LockSurface(heightmap);
- for (int y = 0; y < heightmap->h; y++)
- for (int x = 0; x < heightmap->w; x++) {
- Uint32 a = ((unsigned int*)heightmap->pixels)[y*(heightmap->pitch/sizeof(unsigned int)) + x];
- world.data[x][y] =
- (.3*((a >> 16) % 256))+
- (.59*((a >> 8) % 256))+
- (.11*(a%256));
- }
- world.w = heightmap->w;
- world.h = heightmap->h;
- SDL_UnlockSurface(heightmap);
- move::rotax = 45;
- move::rotay = 20;
- move::zoom = -.7;
- for (int i = 0; i < 1000; i++) {
- partikkeli tmp;
- tmp.x = SCREEN_WIDTH/2;
- tmp.y = SCREEN_HEIGHT/2;
- tmp.a = rand()%360;
- tmp.v = (rand()/RAND_MAX);
- tmp.xplus = cosa(tmp.a)/(tmp.v);
- tmp.xplus = sina(tmp.a)/(tmp.v);
- tmp.r = 255;
- tmp.g = 255;
- tmp.b = 255;
- partikkelit.push_back(tmp);
- }
- for (int frm = 0; running; frm++) {
- handleEvent();
- runAndDraw(frm);
- }
- exit(0);
- return( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement