Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <allegro5/allegro.h>
- #include <allegro5/allegro_opengl.h>
- #include <allegro5/allegro_image.h>
- #include <GL/glu.h>
- #include <vector>
- using namespace std;
- // GENERAL COMMENTS
- //---------------------------------------------------------------------
- /* linker configuration
- `pkg-config --libs allegro-5.1 allegro_image-5.1`
- -lGL
- -lGLU
- */
- // GLOBALS
- //---------------------------------------------------------------------
- const int SCREEN_X = 800;
- const int SCREEN_Y = 600;
- // FUNCTIONS
- //---------------------------------------------------------------------
- void setup_allegro5( ALLEGRO_DISPLAY **Disp,
- ALLEGRO_EVENT_QUEUE **Event_Q,
- ALLEGRO_TIMER **FPS_Tm,
- const float fps)
- {
- al_init();
- al_init_image_addon();
- al_set_new_display_flags(ALLEGRO_OPENGL);
- al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_SUGGEST);
- *Disp = al_create_display(SCREEN_X, SCREEN_Y);
- *Event_Q = al_create_event_queue();
- *FPS_Tm = al_create_timer(1.0f / fps);
- al_register_event_source(*Event_Q, al_get_display_event_source(*Disp));
- al_register_event_source(*Event_Q, al_get_timer_event_source(*FPS_Tm));
- }
- //---------------------------------------------------------------------
- void exit_allegro5( ALLEGRO_DISPLAY **Disp,
- ALLEGRO_EVENT_QUEUE **Event_Q,
- ALLEGRO_TIMER **FPS_Tm)
- {
- al_destroy_display(*Disp);
- al_destroy_event_queue(*Event_Q);
- al_destroy_timer(*FPS_Tm);
- }
- //---------------------------------------------------------------------
- void load_ht_map( int &BMP_HT,
- int &BMP_WD,
- vector<GLfloat> &verts)
- {
- ALLEGRO_BITMAP *ht_bmp = NULL;
- ALLEGRO_COLOR ht_pixel;
- int bmp_x = 0;
- int bmp_z = 0;
- GLfloat land_scale = 5.0f;
- GLfloat height_scale = 200.0f;
- GLfloat height_true = 0.0f;
- unsigned char r, g, b;
- ht_bmp = al_load_bitmap("landtile.jpg");
- // locking the bitmap makes the reading of it noticably faster...
- al_lock_bitmap(ht_bmp, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY);
- BMP_HT = al_get_bitmap_height(ht_bmp);
- BMP_WD = al_get_bitmap_width(ht_bmp);
- // autocentering variables
- GLfloat cent_wd = GLfloat((BMP_WD) / 2.0f) * land_scale;
- GLfloat cent_ht = GLfloat((BMP_HT) / 2.0f) * land_scale;
- verts.reserve(BMP_HT * BMP_WD * 3);
- for(bmp_z = 0; bmp_z < BMP_HT; bmp_z++)
- {
- for(bmp_x = 0; bmp_x < BMP_WD; bmp_x++)
- {
- verts.push_back(GLfloat((bmp_x * land_scale) - cent_wd));
- ht_pixel = al_get_pixel(ht_bmp, bmp_x, bmp_z); // get pixel color
- al_unmap_rgb(ht_pixel, &r, &g, &b);
- height_true = GLfloat(float(r) / 255.0f) * height_scale;
- verts.push_back(height_true);
- verts.push_back(GLfloat((bmp_z * land_scale) - cent_ht));
- }
- }
- al_unlock_bitmap(ht_bmp);
- al_destroy_bitmap(ht_bmp);
- }
- //---------------------------------------------------------------------
- void make_point_connections( int BMP_HT,
- int BMP_WD,
- vector<GLuint> &points)
- {
- GLuint i = 0;
- size_t point_count = (BMP_WD - 1) * (BMP_HT - 1) * 6;
- GLuint triangle_count = ((BMP_WD - 1) * (BMP_HT - 1)) + (BMP_HT - 1);
- points.reserve(point_count);
- int width_cnt = 0;
- for(i = 0; i < triangle_count; i++)
- {
- if(width_cnt == (BMP_WD - 1))
- {
- width_cnt = 0;
- continue;
- }
- else
- {
- // Triangle 1
- points.push_back(i);
- points.push_back(i + 1);
- points.push_back(i + BMP_WD);
- // Triangle 2
- points.push_back(i + 1);
- points.push_back(i + BMP_WD + 1);
- points.push_back(i + BMP_WD);
- // Mod: both triangles go around counter clockwise now...
- width_cnt++;
- }
- }
- }
- //---------------------------------------------------------------------
- void camera_3D_setup()
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(35.0, (GLdouble)SCREEN_X / (GLdouble)SCREEN_Y, 1.0, 1000.0);
- glRotatef(15.0f, 1.0f, 0.0f, 0.0f);
- glTranslatef(0.0, -150.0f, -550.0f);
- }
- //---------------------------------------------------------------------
- void draw_scene( vector<GLfloat> verts,
- vector<GLuint> points,
- GLsizei count,
- GLfloat _ang)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- al_clear_to_color(al_map_rgb(0,0,30));
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(_ang, 0.0f, 1.0f, 0.0f);
- // again I resort to using gl_ClientState, but this time employing glDrawElements
- glEnableClientState(GL_VERTEX_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, &verts.at(0));
- glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, &points.at(0));
- glDisableClientState(GL_VERTEX_ARRAY);
- glFlush();
- }
- // MAIN()
- //---------------------------------------------------------------------
- int main(int argc, char *argv[])
- {
- ALLEGRO_DISPLAY *display = NULL;
- ALLEGRO_EVENT_QUEUE *event_queue = NULL;
- ALLEGRO_TIMER *FPS_timer = NULL;
- const float FPS = 60.0f;
- GLfloat angle = 0.0f;
- int BMP_HEIGHT = 0;
- int BMP_WIDTH = 0;
- bool draw = false;
- bool loop = true;
- vector<GLfloat> land_vertices; // float
- vector<GLuint> connect_points; // unsigned int
- setup_allegro5(&display, &event_queue, &FPS_timer, FPS);
- load_ht_map(BMP_HEIGHT, BMP_WIDTH, land_vertices);
- make_point_connections(BMP_HEIGHT, BMP_WIDTH, connect_points);
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- al_start_timer(FPS_timer);
- camera_3D_setup();
- while(loop)
- {
- ALLEGRO_EVENT event;
- al_wait_for_event(event_queue, &event);
- switch(event.type)
- {
- case ALLEGRO_EVENT_DISPLAY_CLOSE:
- loop = false;
- break;
- case ALLEGRO_EVENT_TIMER:
- draw = true;
- break;
- default:
- break;
- }
- if(draw == true && al_event_queue_is_empty(event_queue))
- {
- draw = false;
- angle += 0.5f;
- if(angle > 360.0f)
- angle -= 360.0f;
- draw_scene(land_vertices, connect_points, GLsizei(connect_points.size()), angle);
- al_flip_display();
- }
- }
- al_stop_timer(FPS_timer);
- exit_allegro5(&display, &event_queue, &FPS_timer);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement