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 104 105 106 107 108 109 110 111 112
| #include "Map.hpp"
Map::Map()
{
_precision = 1;
loadHeightmap("data/heightmap.png");
loadTexture("data/texture.png");
compile();
}
void Map::compile()
{
// Génération d'un identifiant pour notre display lsit
_list = glGenLists( 1 );
// display list
glNewList( _list, GL_COMPILE );
{
glBegin( GL_TRIANGLES );
{
// Pour chaque ligne, avec un pas dépendant de la précision souhaitée
for( unsigned int x=0; x<(_height-_precision); x+=_precision)
{
// Pour chaque colonne, avec un pas dépendant de la précision souhaitée
for( unsigned int z=0; z<(_width-_precision); z+=_precision)
{
// Définition des coordonnées des points
// triangle 1
//v3
glTexCoord2f( (x+_precision)/_width, (1.f-(z+_precision))/_height );
glVertex3f( _precision+x, _pixels[_precision+z*_height+_precision+x], _precision+z );
//v2
glTexCoord2f( (x+_precision)/_width, 1.f-(z/_height) );
glVertex3f( _precision+x, _pixels[z*_height+_precision+x], z );
//v1
glTexCoord2f( x/_width, 1.f-(z/_height) );
glVertex3f( x, _pixels[z*_height+x], z );
// triange 2
//v4
glTexCoord2f( x/_width, (1.f-(z+_precision))/_height );
glVertex3f( x, _pixels[_precision+z*_height+x], _precision+z );
//v3
glTexCoord2f( (x+_precision)/_width, (1.f-(z+_precision))/_height );
glVertex3f( _precision+x, _pixels[_precision+z*_height+_precision+x], _precision+z );
//v1
glTexCoord2f( x/_width, 1.f-(z/_height) );
glVertex3f( x, _pixels[z*_height+x], z );
}
}
}
glEnd();
}
glEndList();
}
void Map::render( sf::RenderWindow& w )
{
_textureMain.Bind(); // Sélection de la texture principale
glCallList( _list ); // Appel de la display list
}
bool Map::loadHeightmap( const std::string& filename )
{
sf::Image image;
if ( !image.LoadFromFile(filename) )
return false;
_width = image.GetWidth ();
_height = image.GetHeight();
// Le tableau renvoyé par GetPixelsPtr() contient les 4 composantes RGBA
// sous forme de char
static const int elemSize = 4;
// La taille totale du tableau renvoyé par GetPixelsPtr()
const int size = image.GetWidth() * image.GetHeight() * 4;
// Création de notre propre tableau qui ne contiendra qu'une composante (noir et blanc)
_pixels = new unsigned char[size/elemSize];
// On stocke un pointeur vers le tableau renvoyé par GetPixelsPtr() pour y accéder plus
// simplement
const unsigned char* const px = image.GetPixelsPtr();
// On copie les pixels
for ( int i=0; i<size/elemSize; ++i )
{
_pixels[i] = px[i*elemSize];
}
return true;
}
bool Map::loadTexture( const std::string& filename )
{
if ( !_textureMain.LoadFromFile(filename) )
return false;
return true;
}
Map::~Map()
{
delete _pixels;
} |
Partager