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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
|
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <GL/glut.h>
//#include <GL/glx.h>
#include <GL/gl.h>
//#include <X11/extensions/xf86vmode.h>
//#include <X11/keysym.h>
#include "bmp.h"
#define MAP_SIZE 1024 /* Size Of Our .RAW Height Map */
#define STEP_SIZE 16 /* Width And Height Of Each Quad */
#define HEIGHT_RATIO 1.5f /* Ratio That The Y Is Scaled According To The X And Z */
#define MAX_NO_TEXTURES 1
// variables globales ... glut oblige !
//tableau de texture
GLuint texture_id[MAX_NO_TEXTURES];
// position et orientation du point de vue
float camera_x, camera_y, camera_z;
float camera_angle_x,camera_angle_y,camera_angle_z;
// timer pour l'animation
int timer;
int timer_start;
int timer_rate;
//BOOL bRender = True; /* Polygon Flag Set To TRUE By Default */
int bRender = 1; /* Polygon Flag Set To TRUE By Default */
unsigned char g_HeightMap[MAP_SIZE*MAP_SIZE]; /* Holds The Height Map Data */
float scaleValue = 0.15f; /* Scale Value For The Terrain */
// conversion degres vers radians
float radians(float d)
{
return d * 0.0174532925;
}
/* Loads The .RAW File And Stores It In pHeightMap */
void LoadRawFile(const char* strName, int nSize,unsigned char *pHeightMap)
{
int result;
FILE *pFile = NULL;
/* Open The File In Read / Binary Mode. */
pFile = fopen( strName, "rb" );
/* Check To See If We Found The File And Could Open It */
if ( pFile == NULL )
{
/* Display Error Message And Stop The Function */
printf("Error can't Find The Height Map!\n");
return;
}
fread( pHeightMap, 1, nSize, pFile );
/* After We Read The Data, It's A Good Idea To Check If Everything Read Fine */
result = ferror( pFile );
/* Check If We Received An Error */
if (result)
{
printf("Error failed To Get Data!\n");
}
/* Close The File. */
fclose(pFile);
}
int Height(unsigned char *pHeightMap, int X, int Y) /* This Returns The Height From A Height Map Index */
{
int x = X % MAP_SIZE; /* Error Check Our x Value */
int y = Y % MAP_SIZE; /* Error Check Our y Value */
if(!pHeightMap) return 0; /* Make Sure Our Data Is Valid */
return pHeightMap[x + (y * MAP_SIZE)]; /* Index Into Our Height Array And Return The Height */
}
void SetVertexColor(unsigned char *pHeightMap, int x, int y) /* Sets The Color Value For A Particular Index, Depending On The Height Index */
{
if(!pHeightMap)
return; /* Make Sure Our Height Data Is Valid */
else
{
float fColor = -0.15f + (Height(pHeightMap, x, y ) / 256.0f);
/* Assign This Blue Shade To The Current Vertex */
glColor3f(0, 0, fColor );
}
}
void RenderHeightMap(unsigned char pHeightMap[]) // This Renders The Height Map As Quads
{
int X = 0, Y = 0; // Create Some Variables To Walk The Array With.
int x, y, z; // Create Some Variables For Readability
//->
GLfloat Text_height = 0.0f;
GLfloat Text_width = 0.0f;
//<-
if(!pHeightMap) return; // Make Sure Our Height Data Is Valid
if(bRender) // What We Want To Render
glBegin( GL_QUADS ); // Render Polygons
else
glBegin( GL_LINES ); // Render Lines Instead
for ( X = 0; X < MAP_SIZE; X += STEP_SIZE )
for ( Y = 0; Y < MAP_SIZE; Y += STEP_SIZE )
{
//bottom left->
//->
// Get The (X, Y, Z) Value For The Bottom Left Vertex
x = X;
y = Height(pHeightMap, X, Y );
z = Y;
if(bRender)
{
if(X == 0)
Text_width = 0.0f;
else
Text_width = (GLfloat) X / MAP_SIZE;
if(Y == 0)
Text_height = 0.0f;
else
Text_height = (GLfloat) Y / MAP_SIZE;
glTexCoord2f(Text_width, Text_height);
}else{
// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
}
//<-
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered (Integer Points Are Faster)
//<-bottom left
//top left->
//->
// Get The (X, Y, Z) Value For The Top Left Vertex
x = X;
y = Height(pHeightMap, X, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
if(bRender)
{
if(X == 0)
Text_width = 0.0f;
else
Text_width = (GLfloat) X / MAP_SIZE;
if(Y == 0)
Text_height = 0.0f;
else
Text_height = (GLfloat) (Y + STEP_SIZE) / MAP_SIZE;
glTexCoord2f(Text_width, Text_height);
}else{
// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
}
//<-
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
//<-top left
//top right->
//->
// Get The (X, Y, Z) Value For The Top Right Vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
z = Y + STEP_SIZE ;
if(bRender)
{
if(X == 0)
Text_width = 0.0f;
else
Text_width = (GLfloat) (X + STEP_SIZE) / MAP_SIZE;
if(Y == 0)
Text_height = 0.0f;
else
Text_height = (GLfloat) (Y + STEP_SIZE) / MAP_SIZE;
glTexCoord2f(Text_width, Text_height);
}else{
// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
}
//<-
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
//<-top right
//bottom right->
//->
// Get The (X, Y, Z) Value For The Bottom Right Vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y );
z = Y;
if(bRender)
{
if(X == 0)
Text_width = 0.0f;
else
Text_width = (GLfloat) (X + STEP_SIZE) / MAP_SIZE;
if(Y == 0)
Text_height = 0.0f;
else
Text_height = (GLfloat) Y / MAP_SIZE;
glTexCoord2f(Text_width, Text_height);
}else{
// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);
}
//<-
glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
//<-bottom right
}
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Reset The Color
}
//Bool loadGLTextures(char *filename) /* Load Bitmaps And Convert To Textures */
int loadGLTextures(char *filename_texture,char *filename_courbe,char* ecrase_image,char* calcul_altitude) /* Load Bitmaps And Convert To Textures */
{
int status;
BMP* texture;
BMP* courbe;
int result;
status = 0;
texture = New_Bmp(filename_texture);
printf("image de la texture chargee\n");
Load_Bmp(texture->file_name, texture);
courbe = New_Bmp(filename_courbe);
printf("image des courbes chargee\n");
Load_Bmp(courbe->file_name, courbe);
Load_Curve(courbe);
printf("image des courbes chargee\n");
result = Generate_Waves(courbe);
printf("courbes reconnues\n");
Make_Median_Axis(courbe);
printf("calcul des axes medians effectue\n");
if(strcmp(calcul_altitude,"calcul")==0)
{
printf("calcul des altitudes en cours\n");
Calcul_Altitude(courbe);
printf("calcul des altitudes effectue\n");
}
if(strcmp(ecrase_image,"ecrase")==0)
{
Write_Bmp(courbe);
}
if (Get_Loaded(texture)==1)
{
//status = True;
status = 1;
glGenTextures(1, &texture_id[0]); /* create the texture */
glBindTexture(GL_TEXTURE_2D, texture_id[0]);
/* actually generate the texture */
glTexImage2D(
GL_TEXTURE_2D,
0, 3, Get_Width(texture), Get_Height(texture), 0,
GL_RGB, GL_UNSIGNED_BYTE, Get_Data(texture));
/* enable linear filtering */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
/* free the ram we used in our texture generation process */
if (Get_Loaded(texture)==1)
{
Close_Bmp(texture);
}
if (Get_Loaded(courbe)==1)
{
Close_Bmp(courbe);
}
return status;
}
// affiche la scene complete
void scene(void)
{
/* // utilise le temps courant pour modifier une caracteristique de chaque objet
glPushMatrix();
glRotatef((timer*2)%180, 0., 1., 0);
glColor3f ((float) (timer % 300) / 300., 1.0, (float) (timer % 100) / 100.);
cube(0, 10*cos(radians(timer % 360)), 0, 2);
glPopMatrix();
glColor3f (1., 0., 0.);
cube(10, 0, 0, 2+sin(radians(timer % 360)));
glColor3f(0., 0., 1.);
cube(0, 0, 10, 2+cos(radians(timer % 180)));
*/
}
// callback de glut : affichage
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // efface l'ecran et le z-buffer
glMatrixMode(GL_MODELVIEW); // selectionne la matrice de la scene
//glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // reinitialise les transformations
// positionne la scene par rapport a la camera qui est restee en 0,0,0
glRotatef(-camera_angle_x, 1., 0., 0);
glRotatef(-camera_angle_y, 0., 1., 0);
//glRotatef(-camera_angle_z, 0., 0., 1);
glTranslatef(-camera_x, -camera_y, -camera_z);
// desssine la scene
//scene();
glScalef(scaleValue, scaleValue * HEIGHT_RATIO, scaleValue);
RenderHeightMap(g_HeightMap); /* Render The Height Map */
// termine l'affichage
glFlush();
// echange le buffer de dessin et le buffer d'affichage
glutSwapBuffers();
}
// callback de glut : appellee regulierement
void anime(void)
{
int c;
#if 1
usleep(1000000. / timer_rate);
timer++;
// demande un nouvel affichage si le temps ecoule correspond a la frequence d'animation definie
glutPostRedisplay();
#else
c= clock();
if((c - timer_start)*timer_rate > CLOCKS_PER_SEC)
{
timer++;
timer_start= c;
// demande un nouvel affichage si le temps ecoule correspond a la frequence d'animation definie
glutPostRedisplay();
}
#endif
}
// callback de glut : gestion du clavier (touches speciales)
void keyboard(int key, int x, int y)
{
switch(key)
{
/*case GLUT_KEY_UP:
// deplacement de la camera dans la direction de la visee
camera_z-= cos(radians(camera_angle));
camera_x-= sin(radians(camera_angle));
break;
case GLUT_KEY_DOWN:
// deplacement de la camera dans la direction opposee de la visee
camera_z+= cos(radians(camera_angle));
camera_x+= sin(radians(camera_angle));
break;
case GLUT_KEY_LEFT:
// rotation de la camera sur Y-
camera_angle+= 4.;
if(camera_angle >= 360.)
camera_angle= 0.;
break;
case GLUT_KEY_RIGHT:
camera_angle-= 4.;
if(camera_angle < 0.)
camera_angle= 359.;
break;
case GLUT_KEY_PAGE_UP:
camera_y+=10.0;
break;
case GLUT_KEY_PAGE_DOWN:
camera_y-=10.0;
break;
default:
break;*/
case GLUT_KEY_UP:
camera_angle_x+= 4.;
if(camera_angle_x >= 360.)
camera_angle_x= 0.;
break;
case GLUT_KEY_DOWN:
camera_angle_x-= 4.;
if(camera_angle_x < 0.)
camera_angle_x= 359.;
break;
case GLUT_KEY_LEFT:
camera_angle_y+= 4.;
if(camera_angle_y >= 360.)
camera_angle_y= 0.;
break;
case GLUT_KEY_RIGHT:
camera_angle_y-= 4.;
if(camera_angle_y < 0.)
camera_angle_y= 359.;
break;
case GLUT_KEY_PAGE_UP:
// deplacement de la camera dans la direction de la visee
camera_z-= cos(radians(camera_angle_y));
camera_x-= sin(radians(camera_angle_x));
break;
case GLUT_KEY_PAGE_DOWN:
// deplacement de la camera dans la direction opposee de la visee
camera_z+= cos(radians(camera_angle_y));
camera_x+= sin(radians(camera_angle_x));
break;
case GLUT_KEY_INSERT:
camera_x= 50.;
camera_y= 100.;
camera_z= 50.;
camera_angle_x= -45.;
camera_angle_y= -180.;
camera_angle_z= 0.;
default:
break;
}
// provoque un affichage (appel du callback display)
glutPostRedisplay();
}
// mise en place des parametres d'openGL
void init (char *filename_texture,char *filename_courbe,char* ecrase_image,char* calcul_altitude)
{
// precise la couleur pour effacer l'ecran
if (!loadGLTextures(filename_texture,filename_courbe,ecrase_image,calcul_altitude))
{
return ;
}
glShadeModel (GL_SMOOTH); // Enable Smooth Shading
glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glEnable (GL_COLOR_MATERIAL );
glColorMaterial (GL_FRONT, GL_AMBIENT_AND_DIFFUSE );
glEnable (GL_TEXTURE_2D );
glPixelStorei (GL_UNPACK_ALIGNMENT, 1 );
glGenTextures (1, texture_id);
// precise la profondeur pour effacer le z-buffer
glClearDepth(1.0);
// demande a effectuer un test sur le z-buffer pendant l'affichage
glEnable(GL_DEPTH_TEST);
// precise le test
glDepthFunc(GL_LEQUAL);
// definit la projection
glMatrixMode(GL_PROJECTION); // selectionne la matrice de visualisation
glLoadIdentity(); // reintialise les transformations
#if 0
glOrtho(-1000.0, 1000.0, -1000.0, 1000.0, -1000.0, 1000.0); // definit une projection orthographique
#else
gluPerspective(50., 1., 1., 100.);
#endif
// position de la camera au debut de l'animation
camera_x= 50.;
camera_y= 100.;
camera_z= 50.;
camera_angle_x= -45.;
camera_angle_y= -180.;
camera_angle_z= 0.;
// gestion de la vitesse de l'animation
timer= 0;
timer_start= clock();
timer_rate= 50;
//LoadRawFile("../Data/Terrain.raw", MAP_SIZE * MAP_SIZE, g_HeightMap); /* Load raw data */
LoadRawFile("Terrain.raw", MAP_SIZE * MAP_SIZE, g_HeightMap); /* Load raw data */
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
// initialisation du mode d'affichage :
// GLUT_DOUBLE demande 2 buffers, un pour dessiner sans afficher, l'autre pour afficher
// GLUT_DEPTH demande 1 z-buffer
// GLUT_RGB demande un RGB 24 bits
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
glutInitWindowSize (400, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Ca bouge !!! (fleches, page up down)");
init(argv[1],argv[2],argv[3],argv[4]);
// enregistre l'action des evenements claviers
glutSpecialFunc(keyboard);
// enregistre l'action des evenements d'affichage
glutDisplayFunc(display);
// enregistre l'action a executer en manque d'evenements
glutIdleFunc(anime);
// lance la boucle de gestion des evenements
glutMainLoop();
return 0;
} |
Partager