1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
void Map :: prepareMap()
{
this->map_list = glGenLists(1);
Ptr<SDL_Surface> grass = IMG_Load("Images/Grass.jpg"); // Surface sdl
Ptr<SDL_Surface> rock = IMG_Load("Images/Rock.jpg");
glNewList(this->map_list,GL_COMPILE); // Génération du handle de la display list de la map
int map_size = dimension.size;
float lowlands = (float)(0.33*(DataMap :: MIN_HEIGHT + DataMap :: MAX_HEIGHT));
float midlands = (float)(0.5*(lowlands + DataMap :: MAX_HEIGHT));
float highlands = (float)(0.5*(midlands + DataMap :: MAX_HEIGHT));
SDL_WM_SetCaption("Préparation de la texture",0);
for(int z=0; z<map_size-1; ++z)
{
glBegin(GL_TRIANGLE_STRIP);
for (int x=0; x<map_size; ++x)
{
float scaled_height = this->dimension.heights[z*map_size + x]; // Hauteur de la map en (x/z)
float nextz_scaled_height = this->dimension.heights[(z+1)*map_size + x]; // en x/z+1
// Récupération des couleurs de l'herbe et rock au point (x/y/z et x/y/z+1)
SDL_Color grass_col = grass.getPixel(x%grass.getWidth(),z%grass.getHeight());
SDL_Color rock_col = rock.getPixel(x%rock.getWidth(),z%rock.getHeight());
SDL_Color nextz_grass_col = grass.getPixel(x%grass.getWidth(),(z+1)%grass.getHeight());
SDL_Color nextz_rock_col = rock.getPixel(x%rock.getWidth(),(z+1)%rock.getHeight());
// Création de la future couleur en fonction de la hauteur de la map et de celle de l'herbe + du rock
SDL_Color scaled_col, nextz_scaled_col;
if (scaled_height <= lowlands ) // plaine
{
scaled_col.r = grass_col.r;
scaled_col.g = grass_col.g;
scaled_col.b = grass_col.b;
}
else if (scaled_height <= midlands ) // Faible altitude
{
scaled_col.r = static_cast<int>(scaled_height/highlands*rock_col.r +
(1-scaled_height/highlands)*grass_col.r);
scaled_col.g = static_cast<int>(scaled_height/highlands*rock_col.g +
(1-scaled_height/highlands)*grass_col.g);
scaled_col.b = static_cast<int>(scaled_height/highlands*rock_col.b +
(1-scaled_height/highlands)*grass_col.b);
}
else // Moyenne altitude
{
scaled_col.r = rock_col.r;
scaled_col.g = rock_col.g;
scaled_col.b = rock_col.b;
}
// Maintenant pour le deuxième point (x/y/z+1)
if (nextz_scaled_height <= lowlands )
{
nextz_scaled_col.r = nextz_grass_col.r;
nextz_scaled_col.g = nextz_grass_col.g;
nextz_scaled_col.b = nextz_grass_col.b;
}
else if (nextz_scaled_height <= midlands )
{
nextz_scaled_col.r = static_cast<int>(nextz_scaled_height/highlands*nextz_rock_col.r +
(1-nextz_scaled_height/highlands)*nextz_grass_col.r);
nextz_scaled_col.g = static_cast<int>(nextz_scaled_height/highlands*nextz_rock_col.g +
(1-nextz_scaled_height/highlands)*nextz_grass_col.g);
nextz_scaled_col.b = static_cast<int>(nextz_scaled_height/highlands*nextz_rock_col.b +
(1-nextz_scaled_height/highlands)*nextz_grass_col.b);
}
else
{
nextz_scaled_col.r = nextz_rock_col.r;
nextz_scaled_col.g = nextz_rock_col.g;
nextz_scaled_col.b = nextz_rock_col.b;
}
// On trace les vertices + couleurs
glColor3ub(scaled_col.r,scaled_col.g,scaled_col.b);
glVertex3f( 0.5*(float)(x-map_size/2),scaled_height,0.5*(float)(z-map_size/2) );
glColor3ub(nextz_scaled_col.r,nextz_scaled_col.g,nextz_scaled_col.b);
glVertex3f( 0.5*(float)(x-map_size/2),nextz_scaled_height,0.5*(float)(z+1-map_size/2));
}
glEnd();
}
glEndList();
}
// Dessin de la map
void Map :: draw() const
{
glDisable(GL_TEXTURE_2D); // Sert à rien ici
glCallList(this->map_list);
glEnable(GL_TEXTURE_2D); // On le remet
} |
Partager