home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / DEMO / EFEKT_14.CPP < prev    next >
C/C++ Source or Header  |  2000-08-18  |  8KB  |  323 lines

  1. /*
  2.  
  3. bazenik 3ds viewer
  4.  
  5. return:
  6.   0 - doslo k chybe
  7.   1 - este nezacal
  8.   2 - prebehol v poriadku
  9.   3 - uz skoncil
  10. */
  11.  
  12. #include <iostream>
  13. #include "api3ds.h"
  14. #include "efekt.h"
  15. #include "efekt_14.h"
  16.  
  17. extern int sync_num;
  18.  
  19. typedef struct {
  20.    GLfloat x, y, z, w;
  21.    GLfloat u1, v1;
  22.    GLfloat u2, v2;
  23. } bod;
  24.  
  25. bod body[256];
  26.  
  27. Texture3DS *obloha;
  28. Texture3DS *dno;
  29.  
  30. #define spac 40
  31.  
  32. unsigned char indices[2*16*15];
  33. void init_structures()
  34. {
  35.   bod* b = body;
  36.   for(int i=0; i<16; i++, b+=16) {
  37.     bod* b2 = b;
  38.     for(int j=0; j<16; j++, b2++)
  39.     {
  40.       b2->x = spac*j - (spac*7.5);
  41.       b2->y = (spac*i - (spac*7.5)+ 2.5)*1.005;
  42.       b2->z = 0;
  43.       b2->w = 1.0;
  44.       b2->u1 = b2->v1 = b2->u2 = b2->v2 = 0;
  45.     }
  46.   }
  47.   unsigned char* p = indices;
  48.   for(int i=0; i<15; i++)
  49.     for(int j=0; j<16; j++)
  50.     {
  51.       *p = (unsigned char)((i<<4)+j); p++;
  52.       *p = (unsigned char)(((i+1)<<4)+j); p++;
  53.     }
  54.  
  55. void vykresli_hladinu()
  56. {
  57.   glDisableClientState(GL_EDGE_FLAG_ARRAY);
  58.   glDisableClientState(GL_INDEX_ARRAY);
  59.   glDisableClientState(GL_COLOR_ARRAY);
  60.   glDisableClientState(GL_NORMAL_ARRAY);
  61.  
  62.   int stride = sizeof(bod);
  63.  
  64.   glEnableClientState(GL_VERTEX_ARRAY);
  65.   glVertexPointer(4, GL_FLOAT, stride, body);
  66.  
  67.   glLockArraysEXT(0, 256);
  68.  
  69.   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  70.  
  71.   glEnable(GL_TEXTURE_2D);
  72.   glDisable(GL_LIGHTING);
  73.   glDisable(GL_CULL_FACE);
  74.   glEnable(GL_DEPTH_TEST);
  75.   glDepthFunc(GL_ALWAYS);
  76.  
  77.   glDisable(GL_BLEND);
  78.   dno->GL();
  79.   glTexCoordPointer(2, GL_FLOAT, stride, ((char*)body)+24);
  80.   glColor3f(1.0,1.0,1.0);
  81.   for(int i=0; i<15; i++)
  82.     glDrawElements(GL_TRIANGLE_STRIP, 32, GL_UNSIGNED_BYTE,
  83.                    (void*)(indices+(i<<5)));
  84.  
  85.   glEnable(GL_BLEND);
  86.   glBlendFunc(GL_ONE, GL_ONE);
  87.   obloha->GL();
  88.   glTexCoordPointer(2, GL_FLOAT, stride, ((char*)body)+16);
  89.   glColor3f(1.0,1.0,1.0);
  90.   for(int i=0; i<15; i++)
  91.     glDrawElements(GL_TRIANGLE_STRIP, 32, GL_UNSIGNED_BYTE,
  92.                    (void*)(indices+(i<<5)));
  93.  
  94.   glUnlockArraysEXT();
  95.  
  96.   glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  97.   glDisableClientState(GL_VERTEX_ARRAY);
  98.  
  99.   glDisable(GL_BLEND);
  100.   glDisable(GL_TEXTURE_2D);
  101.   glEnable(GL_LIGHTING);
  102.   glEnable(GL_CULL_FACE);
  103.   glDepthFunc(GL_LESS);
  104.  
  105. }
  106.  
  107. double mytime = 0.0;
  108.  
  109. #define fscale 0.35
  110. #define vyska 10.0
  111. #define vyska_hladiny 140.0
  112. #define adjust (vyska*fscale/spac)
  113. #define vysobloha 1500.0
  114. #define oblsp 0.0004
  115. #define vzduch 1.0
  116. #define voda 1.3
  117. #define refr (vzduch/voda)
  118.  
  119. void updatuj_hladinu(double t, float Ox, float Oy, float Oz)
  120. {
  121.   bod* b = body;
  122.   for(int i=0; i<16; i++, b+=16) {
  123.     bod* b2 = b;
  124.     for(int j=0; j<16; j++, b2++)
  125.     {
  126.       float x=((float)i-7.5)*fscale;
  127.       float y=((float)j-7.5)*fscale;
  128.  
  129.       float f=200+vyska*(sin(x+7*t)*cos(2*y-9*t) + cos(x+4*t)*cos(y+5*t));
  130.       float u=adjust*(cos(2*y-9*t)*cos(x+7*t) - sin(x+4*t)*cos(y+5*t));
  131.       float v=adjust*(-2*sin(x+7*t)*sin(2*y-9*t) - cos(x+4*t)*sin(y+5*t));
  132.  
  133.       b2->z = (GLfloat)f-vyska_hladiny;
  134.       float len = 1.0/sqrt(1.0+u*u+v*v);
  135.       float ny = u*len;
  136.       float nz = -len;
  137.       float nx = v*len;
  138.  
  139.       float lx = b2->x - Ox;
  140.       float ly = b2->y - Oy;
  141.       float lz = b2->z - Oz;
  142.       len = 1.0/sqrt(lx*lx+ly*ly+lz*lz);
  143.       lx*=len; ly*=len; lz*=len;
  144.       float c = -2*(lx*nx+ly*ny+lz*nz);
  145.       float rx = lx + c*nx;
  146.       float ry = ly + c*ny;
  147.       float rz = lz + c*nz;
  148.       float t = (vysobloha-b2->z)/rz;
  149.       u = oblsp*(b2->x + rx*t);
  150.       v = oblsp*(b2->y + ry*t);
  151.  
  152.       b2->u1 = 0.5*u+0.5;
  153.       b2->v1 = 0.5*v+0.5;
  154.  
  155.       c = -(lx*nx + ly*ny + lz*nz);
  156.       rx = lx + c*nx;
  157.       ry = ly + c*ny;
  158.       rz = lz + c*nz;
  159.       len = 1.0/sqrt(rx*rx+ry*ry+rz*rz);
  160.       rx*=len; ry*=len; rz*=len;
  161.       float sinbeta = refr*sqrt(1.0-c*c);
  162.       float cosbeta = sqrt(1.0-sinbeta*sinbeta);
  163.       float tx = sinbeta*rx - cosbeta*nx;
  164.       float ty = sinbeta*ry - cosbeta*ny;
  165.       float tz = sinbeta*rz - cosbeta*nz;
  166.       t = (-b2->z)/tz;
  167.       u = 0.0033*(b2->x + tx*t);
  168.       v = 0.0033*(b2->y + ty*t);
  169.  
  170.       b2->u2 = 0.5*u+0.5;
  171.       b2->v2 = 0.5*v+0.5;
  172.     }
  173.   }
  174. }
  175.  
  176.  
  177.  
  178. int efekt_14::init()
  179. {
  180.   start=TRUE;
  181.   counter=ZACIATOK14*refresh;
  182.  
  183.   glClearColor (cl_r, cl_g, cl_b, 1.0);
  184.   if (lgt) 
  185.     {
  186.     glEnable(GL_LIGHTING);    
  187.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  188.     }
  189.    else 
  190.     glDisable(GL_LIGHTING);    
  191.  
  192.   if (fog) 
  193.     {
  194.     GLfloat fc[4] = {fg_r,fg_g,fg_b, 1.0};
  195.     glEnable(GL_FOG);
  196.     glFogi(GL_FOG_MODE, GL_EXP2);
  197.     glFogfv(GL_FOG_COLOR, fc);
  198.     glFogf(GL_FOG_DENSITY, fgd);
  199.     }
  200.    else glDisable(GL_FOG);    
  201. return 1;
  202. }
  203.  
  204. int efekt_14::load()
  205. {
  206.    cout << "Loading efekt14 ... ";
  207.    FILE *stream=fopen(CFGNAME14,"r");
  208.    if (stream==NULL) return 0;
  209.    fscanf(stream,"file name            %s\n",scene_name);
  210.    fscanf(stream,"camera name          %s\n",camera_name);
  211.    fscanf(stream,"begin                %f\n",&begin3d);
  212.    fscanf(stream,"end                  %f\n",&end3d);
  213.    fscanf(stream,"background color     %f,%f,%f\n",&cl_r,&cl_g,&cl_b);
  214.    fscanf(stream,"lightning            %i\n",&lgt);
  215.    fscanf(stream,"fog                  %i\n",&fog);
  216.    fscanf(stream,"fog color            %f,%f,%f\n",&fg_r,&fg_g,&fg_b);
  217.    fscanf(stream,"fog density          %f\n",&fgd);
  218.    fscanf(stream,"visibility           %f\n",&vis);
  219.    fscanf(stream,"minvisibility        %f\n",&minvis);
  220.    fscanf(stream,"hfov,vfov (PI/x)     %f,%f\n",&hfov,&vfov);
  221.    fscanf(stream,"render               %i\n",&render);
  222.    fscanf(stream,"rays                 %i\n",&rays);
  223.    fscanf(stream,"outline              %i\n",&obrysy);
  224.    fscanf(stream,"outline color        %f,%f,%f\n",&ob_r,&ob_g,&ob_b);
  225.    fscanf(stream,"outline width        %i\n",&ob_wdt);
  226.    fscanf(stream,"object name          %s\n",object_name);
  227.    fclose(stream);
  228.  
  229.    Loader3DS loader;
  230.    sce = loader.Load(scene_name,texture_library);
  231.    if (sce==NULL) return 0;
  232.  
  233.    obloha=texture_library->GetOrCreate("OBLOHA.JPG");
  234.    dno=texture_library->GetOrCreate("DNO.JPG");
  235.  
  236.    init_structures();
  237.  
  238.    cout << "ok!"<<endl;
  239. return 1;
  240. }
  241.  
  242. int efekt_14::free()
  243. {
  244. return 1;
  245. }
  246.  
  247. int efekt_14::update()
  248. {
  249. mytime+=1.0/600.0;
  250. return 1;
  251. }
  252.  
  253. int efekt_14::go(double t)
  254. {
  255. if (t<ZACIATOK14) return 1;
  256. if (t<begin3d) return 1;
  257. if (end) return 3;
  258. if (counter>=KONIEC14*refresh) return free();
  259.  
  260. if (!start) if (!init()) return 0;
  261.  
  262. int cur_frm=(int)(t*refresh);
  263. if (cur_frm>KONIEC14*refresh) cur_frm=int(KONIEC14*refresh);
  264. if (cur_frm>counter)
  265.   while (counter<cur_frm)
  266.     {
  267.     counter++;
  268.     if (counter<KONIEC14*refresh) update();
  269.     }
  270. if (counter>=KONIEC14*refresh) return free();
  271.  
  272. //tu sa kresli->
  273.  
  274.   if (lgt) 
  275.     {
  276.     glEnable(GL_LIGHTING);    
  277.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  278.     }
  279.    else 
  280.     glDisable(GL_LIGHTING);    
  281.  
  282.   if (fog) 
  283.     {
  284.     GLfloat fc[4] = {fg_r,fg_g,fg_b, 1.0};
  285.     glEnable(GL_FOG);
  286.     glFogi(GL_FOG_MODE, GL_EXP2);
  287.     glFogfv(GL_FOG_COLOR, fc);
  288.     glFogf(GL_FOG_DENSITY, fgd);
  289.     }
  290.    else glDisable(GL_FOG);    
  291.  
  292.  
  293.  
  294.   float frejm=(t-begin3d)/(end3d-begin3d);
  295.   kam = sce->GetCamera(camera_name);
  296.   kam->FarClipplane(vis);
  297.   kam->NearClipplane(minvis);
  298.   kam->HorizontalFOV(PI/hfov);
  299.   kam->VerticalFOV(PI/vfov);
  300.   kam->GL(frejm);
  301.  
  302.   Vector3f c=kam->Origin(frejm);
  303.   updatuj_hladinu(4*mytime, c.x, c.y, c.z);
  304.   vykresli_hladinu();
  305.  
  306.   if (render) {sce->Render(frejm);}
  307.  
  308.   if (obrysy||rays)
  309.     {
  310.     Object3DS* object = dynamic_cast<Object3DS*>(sce->GetObject(object_name));
  311.     if(object)
  312.       {
  313.       Vector3f S( 0.0, 200.0, 0.0);
  314.       Vector3f C=kam->Origin(frejm);
  315.       if (rays)   object->Rays(C, S, 420, GL_ONE, GL_ONE);
  316.       if (obrysy) object->Outline (C, 8, 1.0,1.0,1.0);
  317.       }
  318.     }
  319. //<-
  320. return 2;
  321. }
  322.