Source:Génération de heightmaps en OpenGL/heightmaps.h
#include <GL/gl.h> #include <GL/glu.h> #include <stdlib.h> #include <math.h> #include <qstring.h> // Defines #define NB_SUBDIV_INIT 128 #define NB_SUBDIV_MAX 256 #define ECHELLE_VERT_INIT 0.2 #define ECHELLE_VERT_MAX 1.0 #define ECHELLE_NORMALES 0.1 // Model of point typedef struct tag_Point_t{ float X; float Y; float Z; }Point_t; // Model of vector typedef struct tag_Vect_t{ float x; float y; float z; }Vect_t; /* Definition of the color */ typedef struct { int r; int g; int b; } Color_t; /* Definition of the type sommet */ typedef struct { float x; float y; float z; float nx; float ny; float nz; char nb; int txtNumber; Color_t *c; } vertex; class Terrain : public GLObject { Q_OBJECT public: /** Default Constructor */ Terrain(); /** Recopy Constructor */ Terrain(QObject *parent, const char *name, const QString &mapFile ); /** Destructor */ ~Terrain(); /** Initializes the components of the terrains. */ void init(); /** Draws the terrain. */ void draw(); /** Set the number of Subdivisions of the terrain. * @param subDiv number of subDivisions */ void setSubDiv(int subDiv); /** Gets the vertical scale of the terrain * @return int */ float getVScale(); /** Sets the vertical scale of the terrain * @param vScale vertical scale */ void SetVScale(float vScale); /** Get the number of subdivisions of the terrain * @return int */ int getSubDiv(); /** Returns the index from the picture * @param i column number * @param j line number * @param composants number of composants for a pixel * @param width width of the picture * @param height height of the picture * @param nbSubdiv number of subdivision * @return int */ int Terrain::takeIndex(int i, int j, int composants, int width, int height, int nbSubdiv); /** Write property of bool afficheNormales. */ virtual void setafficheNormales( const bool& _newVal); /** Read property of bool afficheNormales. */ virtual const bool& getafficheNormales(); /** Write property of bool areteTransv. */ virtual void setareteTransv( const bool& _newVal); /** Read property of bool areteTransv. */ virtual const bool& getareteTransv(); /** Write property of float realDim. */ virtual void setrealDim( const float& _newVal); /** Read property of float realDim. */ virtual const float& getrealDim(); protected: // Protected methods /** Draws a vertex from a 2D point * @param i first coordonate of the 2D point * @param j second coordonate of the 2D point * @param vertex vertex */ void drawVertex(int i, int j, vertex *T); /** Calcules the evevation from a 2D point * @param i first coordonate of the 2D point * @param j second coordonate of the 2D point * @return Return the value set to scale of the elevation */ float elevation(int i, int j); /** Calcules the color from a 2D point * @param i first coordonate of the 2D point * @param j second coordonate of the 2D point * @return Return a reference on a color struct */ Color_t* color(int i, int j); /** Calcules the texture number from a 2D point * @param i first coordonate of the 2D point * @param j second coordonate of the 2D point * @return Return the number of the texture im the texture index */ int txtNumber(int i, int j); /** create the terrain */ void createTerrain(); /** Creates normales from a vertex * @param vertex Vertex */ void createNormales(vertex *T); private: // Private methods /** Write a textured poygone in the pipeline graphic */ void texturedPolygone(int i, int j, vertex *T); /** Draw a colored polygone in the pipelline graphic */ void colorPolygone(int i, int j, vertex *T); private: // Private attributes /** path of the map fileName. Must be in gray scale without alpha layer. */ QString fileMapName; QString fileColorName; QString fileTexName; /* l'image du terrain */ unsigned char * imageMap; unsigned char * imageColor; unsigned char * imageTex; /* pointeur sur Textures */ Texture * texture; /* Id of the Displays lists */ int terrain; int normales; /* Nombre de subdivisions du maillage */ int nbSubdiv; /* echelle verticale du relief */ float vScale; /** Draw the normales in the pipeline graphic */ bool afficheNormales; /** Draw all the vertex in the graphic pipeline */ bool areteTransv; /** Width of the reference image */ int mapWidth; /** Height of the reference image */ int mapHeight; /** Width of the reference image */ int colorWidth; /** Height of the reference image */ int colorHeight; /** Width of the reference image */ int txtWidth; /** Height of the reference image */ int txtHeight; /** true if the previous drawn polygone is textured */ bool flagTexture; /** Save of the previous texture number */ int prevTxtNumber; /** Width of the terrain in the virtual world. The scale is in meters. * The width of the terrain is equal to the height */ float realDim; }

