home *** CD-ROM | disk | FTP | other *** search
/ 3D Games (Spidla) / 3dhry1.iso / carterrain / src / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-17  |  13.6 KB  |  492 lines

  1. #include <allegro.h>
  2. #include <alleggl.h>
  3. #include <ode/ode.h>
  4. #include <GL/glu.h>
  5. #include <math.h>
  6.  
  7. #include "glode.h"
  8. #include "geometry.h"
  9. #include "intro.h"
  10. #include "car.h"
  11. #include "camera.h"
  12. #include "trig.h"
  13. #include "skybox.h"
  14. #include "timer.h"
  15. #include "loop.h"
  16. #include "controls.h"
  17. #include "player.h"
  18. #include "menu.h"
  19.  
  20. void initialize(void);
  21. void main_loop(void);
  22. void splash(void);
  23.  
  24. dWorldID world;
  25. dSpaceID space;
  26. dJointGroupID contactgroup;
  27.  
  28. dGeomID *groundbox;
  29. dGeomID *groundsphere;
  30. int ngroundbox;
  31. int ngroundsphere;
  32.  
  33. CPlayer player[8];
  34. int players=1;
  35.  
  36. CGeometry terrain("data/testlevel1final.trb");
  37. CGeometry wheel("data/wheel.trb");
  38. CGeometry wheelshell("data/wheelshell.trb");
  39. CGeometry carbody("data/testcar1.trb");
  40.  
  41. CLoop innerloop("data/testlevel1innerloop.cl1");
  42. CLoop outerloop("data/testlevel1outerloop.cl1");
  43.  
  44. CSkybox skybox;
  45.  
  46. GLuint carbodytex, wheeltex, leveltex;
  47.  
  48. FONT *allegro_fnt;
  49.  
  50. int main()
  51. {
  52.   splash();
  53.   
  54.     // Set up OpenGL Rendering mode
  55.     allegro_gl_begin();
  56.     glMatrixMode(GL_PROJECTION);
  57.     glLoadIdentity();
  58.     gluPerspective(45.0f, 1.3333, 0.1f, 500.0f);
  59.     glMatrixMode(GL_MODELVIEW);
  60.     glClearColor(0, 0, 0, 0);
  61.     glShadeModel(GL_SMOOTH);
  62.     glDepthFunc(GL_LEQUAL);
  63.     glClearDepth(1.0);
  64.     glEnable(GL_DEPTH_TEST);
  65.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  66.     GLfloat LightAmbient[]= { 0.02, 0.02, 0.02, 1.0 };
  67.     GLfloat LightDiffuse[]= { 0.8, 0.8, 0.8, 1.0 };
  68.     GLfloat LightPosition[]= { 3.0, 10.0, 7.0, 1.0 };
  69.     glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
  70.     glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
  71.     glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
  72.     glEnable(GL_LIGHT1);
  73.     glEnable(GL_LIGHTING);
  74.     glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  75.     glEnable(GL_COLOR_MATERIAL);
  76.     allegro_fnt = allegro_gl_convert_allegro_font(font, AGL_FONT_TYPE_TEXTURED, 16.0);
  77.     allegro_gl_end();
  78.     
  79.     timer.install(1000);
  80.     intro();
  81.  
  82.     main_menu();
  83.  
  84.     initialize();
  85.     timer.install(596590.5);
  86.     timer.reset();
  87.     main_loop();
  88.  
  89.     allegro_gl_begin();
  90.     glMatrixMode(GL_PROJECTION);
  91.     glPushMatrix();
  92.     glLoadIdentity();
  93.     glViewport(0, 0, SCREEN_W, SCREEN_H);
  94.     glOrtho(-20,20,-15,15,-1,1);
  95.     glMatrixMode(GL_MODELVIEW);
  96.     glLoadIdentity();
  97.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  98.     glDisable(GL_LIGHTING);
  99.     glEnable(GL_TEXTURE_2D);
  100.  
  101.     glClear(GL_COLOR_BUFFER_BIT);
  102.     allegro_gl_printf(allegro_fnt, -2, 10, 0, makecol(0, 255, 0), "carterrain");
  103.     allegro_gl_printf(allegro_fnt, -8, 7, 0, makecol(0, 0, 255), "Programming & Graphics:  Benny Kramek");
  104.     allegro_gl_printf(allegro_fnt, -3, -3, 0, makecol(0, 128, 0), "Uses AllegroGL game programming library");
  105.     allegro_gl_printf(allegro_fnt, -3, -5, 0, makecol(0, 128, 0), "Uses ODE real-time physics library");
  106.     allegro_gl_printf(allegro_fnt, -7, -10, 0, makecol(255, 255, 0), "I hope you enjoyed playing!");
  107.     glScalef(2, 2, 0);
  108.     allegro_gl_printf(allegro_fnt, -7, 1, 0, makecol(255, 255, 255), "http://benny.kramekweb.com");
  109.  
  110.     glFinish();
  111.     allegro_gl_flip();
  112.     allegro_gl_end();
  113.  
  114.  
  115.     rest(2000);
  116.     while(!key[KEY_ESC]);
  117.  
  118.     return 0;
  119. }
  120. END_OF_MAIN();
  121.  
  122. void initialize(void)
  123. {
  124.     world = dWorldCreate();
  125.     dWorldSetGravity(world, 0, 0, -15);
  126.     space = dHashSpaceCreate();
  127.     contactgroup = dJointGroupCreate(10000);
  128.  
  129.     int i, j;
  130.     float dummy, x, y, z, l, w, h, ax, ay, az, aa;
  131.     
  132.     FILE *levelfile;
  133.     levelfile = fopen("data/testlevel1.ct1", "r");
  134.     if (levelfile == NULL)
  135.     {
  136.         allegro_message("Cannot find Level File!\n");
  137.         exit(-1);
  138.     }
  139.  
  140.     fscanf(levelfile, "%d", &ngroundbox);
  141.     fscanf(levelfile, "%d", &ngroundsphere);
  142.     groundbox = new dGeomID[ngroundbox];
  143.     groundsphere = new dGeomID[ngroundsphere];
  144.  
  145.     dMatrix3 R;
  146.     for(i=0; i<ngroundbox; i++)
  147.     {
  148.         fscanf(levelfile, "%f", &x);
  149.         fscanf(levelfile, "%f", &y);
  150.         fscanf(levelfile, "%f", &z);
  151.         fscanf(levelfile, "%f", &ax);
  152.         fscanf(levelfile, "%f", &ay);
  153.         fscanf(levelfile, "%f", &az);
  154.         fscanf(levelfile, "%f", &aa);
  155.         fscanf(levelfile, "%f", &l);
  156.         fscanf(levelfile, "%f", &w);
  157.         fscanf(levelfile, "%f", &h);
  158.                     
  159.         groundbox[i] = dCreateBox(space, l*1.2, w*1.2, h*1.2);
  160.         
  161.         dGeomSetPosition(groundbox[i], -x/100.0*1.2, -y/100.0*1.2, z/100.0*1.2);
  162.         dRFromAxisAndAngle(R, ax, ay, -az, aa);
  163.         dGeomSetRotation(groundbox[i], R);
  164.     }
  165.     for(i=0; i<ngroundsphere; i++)
  166.     {
  167.         fscanf(levelfile, "%f", &x);
  168.         fscanf(levelfile, "%f", &y);
  169.         fscanf(levelfile, "%f", &z);
  170.         fscanf(levelfile, "%f", &l);
  171.         fscanf(levelfile, "%f", &w);
  172.         fscanf(levelfile, "%f", &h);
  173.         groundsphere[i] = dCreateSphere(space, l/2.0*1.2);
  174.         dGeomSetPosition(groundsphere[i], -x/100.0*1.2, -y/100.0*1.2, z/100.0*1.2);
  175.     }
  176.     fclose(levelfile);
  177.  
  178.     terrain.scale(1.2*0.01);
  179.            carbody.scale(0.1);
  180.     wheel.scale(0.01);
  181.     wheelshell.scale(0.01);
  182.  
  183.     BITMAP *bmp = load_tga("data/testcar1skin.tga", NULL);
  184.     carbodytex = allegro_gl_make_texture(bmp);
  185.     destroy_bitmap(bmp);
  186.     bmp = load_tga("data/testwheelskin.tga", NULL);
  187.     wheeltex = allegro_gl_make_texture(bmp);
  188.     destroy_bitmap(bmp);
  189.     bmp = load_tga("data/testlevel1skin.tga", NULL);
  190.     leveltex = allegro_gl_make_texture(bmp);
  191.     destroy_bitmap(bmp);
  192.     
  193.     if(players>=1)
  194.       player[0].car.init(world, space, -3, 0, 3);
  195.     if(players>=2)
  196.       player[1].car.init(world, space, 3, 0, 3);
  197.     if(players>=3)
  198.       player[2].car.init(world, space, -3, -5, 3);
  199.     if(players>=4)
  200.       player[3].car.init(world, space, 3, -5, 3);
  201.     if(players>=5)
  202.       player[4].car.init(world, space, -3, -10, 3);
  203.     if(players>=6)
  204.       player[5].car.init(world, space, 3, -10, 3);
  205.     if(players>=7)
  206.       player[6].car.init(world, space, -3, -15, 3);
  207.     if(players>=8)
  208.       player[7].car.init(world, space, 3, -15, 3);
  209.  
  210.     
  211.     for(i=0; i<players; i++)
  212.       player[i].camera.init(player[i].car.chassis);
  213.     
  214.     skybox.init("data/skybox/front.bmp",
  215.                 "data/skybox/back.bmp",
  216.                 "data/skybox/left.bmp",
  217.                 "data/skybox/right.bmp",
  218.                 "data/skybox/top.bmp",
  219.                 "data/skybox/bottom.bmp");
  220.  
  221.     player[0].controls.set_keys(KEY_UP,     KEY_DOWN,   KEY_LEFT,   KEY_RIGHT,  KEY_RCONTROL,  KEY_RSHIFT, KEY_ENTER);
  222.     player[1].controls.set_keys(KEY_W,      KEY_S,      KEY_A,      KEY_D,      KEY_LCONTROL,  KEY_LSHIFT, KEY_CAPSLOCK);
  223.     player[2].controls.set_keys(KEY_Y,      KEY_H,      KEY_G,      KEY_J,      KEY_B,         KEY_T,      KEY_U);
  224.     player[3].controls.set_keys(KEY_P,      KEY_COLON,  KEY_L,      KEY_QUOTE,  KEY_STOP,      KEY_O,      KEY_OPENBRACE);
  225.     player[4].controls.set_keys(KEY_8_PAD,  KEY_2_PAD,  KEY_4_PAD,  KEY_6_PAD,  KEY_1_PAD,     KEY_7_PAD,  KEY_9_PAD);
  226.     player[5].controls.set_keys(KEY_HOME,   KEY_END,    KEY_DEL,    KEY_PGDN,   KEY_BACKSPACE, KEY_INSERT, KEY_PGUP);
  227.  
  228. }
  229.  
  230. static void nearCallback (void *data, dGeomID o1, dGeomID o2)
  231. {
  232.     int i, n;
  233.  
  234.  
  235.     const int N = 10;
  236.     dContact contact[N];
  237.     n = dCollide (o1, o2, N, &contact[0].geom, sizeof(dContact));
  238.     if (n > 0)
  239.     {
  240.         for (i=0; i<n; i++)
  241.         {
  242.             contact[i].surface.mode = dContactBounce;
  243.             contact[i].surface.mu = 2000; //5000;
  244.             contact[i].surface.bounce = 0.3;
  245.             contact[i].surface.bounce_vel = 0.2;
  246.               dJointID c = dJointCreateContact (world,contactgroup,&contact[i]);
  247.             dJointAttach(c,dGeomGetBody(o1), dGeomGetBody(o2));
  248.         }
  249.     }
  250. }
  251.  
  252. void main_loop(void)
  253. {
  254.     double m;
  255.     CVector cam;
  256.  
  257.     while(!key[KEY_ESC])
  258.     {
  259.                 rest(1);
  260.         m=timer.seconds();
  261.         timer.reset();
  262.  
  263.         int i;
  264.         
  265.         for(i=0; i<players; i++)
  266.           player[i].controls.work(player[i].car, player[i].camera, m);
  267.         
  268.         dSpaceCollide (space,0,&nearCallback);
  269.         dWorldStep(world, MIN(m/2.0, 1.0/30.0));
  270.         dJointGroupEmpty(contactgroup);
  271.         dSpaceCollide (space,0,&nearCallback);
  272.         dWorldStep(world, MIN(m/2.0, 1.0/30.0));
  273.         dJointGroupEmpty(contactgroup);
  274.  
  275.         for(i=0; i<players; i++)
  276.             player[i].camera.update(m);
  277.         
  278.         allegro_gl_begin();
  279.         glClear(GL_DEPTH_BUFFER_BIT);
  280.         
  281.         int n;
  282.         for(n=0; n<players; n++)
  283.         {
  284.         glMatrixMode(GL_PROJECTION);
  285.         glLoadIdentity();
  286.         
  287.         if(players<=2)
  288.           {
  289.             glViewport(0, (players-1-n)*(SCREEN_H/players), SCREEN_W, SCREEN_H/players);
  290.             gluPerspective(45.0f, SCREEN_W/(SCREEN_H/players), 0.2, 200.0);
  291.           }
  292.         else if(players==3)
  293.           {
  294.             if(n==0)
  295.               {
  296.             glViewport(0, SCREEN_H/2, SCREEN_W, SCREEN_H/2);
  297.             gluPerspective(45.0f, SCREEN_W/(SCREEN_H/2), 0.2, 200.0);
  298.               }
  299.             else
  300.               {
  301.             glViewport(SCREEN_W/2*(n-1), 0, SCREEN_W/2, SCREEN_H/2);
  302.             gluPerspective(45.0f, SCREEN_W/(SCREEN_H), 0.2, 200.0);
  303.               }
  304.           }
  305.         else if(players==4)
  306.           {
  307.             glViewport(SCREEN_W/2*(n%2), SCREEN_H/2*((3-n)/2), SCREEN_W/2, SCREEN_H/2);
  308.             gluPerspective(45.0f, SCREEN_W/(SCREEN_H), 0.2, 200.0);
  309.           }
  310.         else if(players==5)
  311.           {
  312.             if(n<2)
  313.               {
  314.             glViewport(SCREEN_W/2*n, SCREEN_H/2, SCREEN_W/2, SCREEN_H/2);
  315.             gluPerspective(45.0f, SCREEN_W/(SCREEN_H), 0.2, 200.0);
  316.               }
  317.             else
  318.               {
  319.             glViewport(SCREEN_W/3*(n-2), 0, SCREEN_W/3, SCREEN_H/2);
  320.             gluPerspective(45.0f, (SCREEN_W)/(SCREEN_H)*(2.0/3), 0.2, 200.0);
  321.               }
  322.           }
  323.         else if(players==6)
  324.           {
  325.             glViewport(SCREEN_W/2*(n%2), SCREEN_H/3*((5-n)/2), SCREEN_W/2, SCREEN_H/3);
  326.             gluPerspective(45.0f, (SCREEN_W)/(SCREEN_H)*(3.0/2), 0.2, 200.0);
  327.           }
  328.         else if(players==7)
  329.           {
  330.             if(n<4)
  331.               {
  332.             glViewport(SCREEN_W/2*(n%2), SCREEN_H/3*((5-n)/2), SCREEN_W/2, SCREEN_H/3);
  333.             gluPerspective(45.0f, (SCREEN_W)/(SCREEN_H)*(3.0/2), 0.2, 200.0);
  334.               }
  335.             else
  336.               {
  337.             glViewport(SCREEN_W/3*(n-4), 0, SCREEN_W/3, SCREEN_H/3);
  338.             gluPerspective(45.0f, (SCREEN_W)/(SCREEN_H), 0.2, 200.0);
  339.               }
  340.           }
  341.         else
  342.           {
  343.             glViewport(SCREEN_W/2*(n%2), SCREEN_H/4*((7-n)/2), SCREEN_W/2, SCREEN_H/4);
  344.             gluPerspective(45.0f, (SCREEN_W)/(SCREEN_H)*(2), 0.2, 200.0);
  345.           }
  346.  
  347.  
  348.         glMatrixMode(GL_MODELVIEW);
  349.         glLoadIdentity();
  350.         cam.x=player[n].camera.target.x-player[n].camera.position.x;
  351.         cam.y=player[n].camera.target.y-player[n].camera.position.y;
  352.         cam.z=player[n].camera.target.z-player[n].camera.position.z;
  353.         gluLookAt(0,0,0,player[n].camera.target.x-player[n].camera.position.x,player[n].camera.target.y-player[n].camera.position.y,player[n].camera.target.z-player[n].camera.position.z,0,0,1);
  354.         skybox.draw(cam.unit());
  355.         glLoadIdentity();
  356.         gluLookAt(player[n].camera.position.x, player[n].camera.position.y, player[n].camera.position.z,   player[n].camera.target.x, player[n].camera.target.y, player[n].camera.target.z,0,0,1);
  357.         
  358.         /*        glEnable(GL_BLEND);
  359.         glDisable(GL_DEPTH_TEST);
  360.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  361.         glColor4f(0.7, 0.4, 0, 0.8);        
  362.         for(i=0; i<ngroundbox; i++)
  363.         {
  364.             dVector3 ss;
  365.             glPushMatrix();
  366.             position_gl(groundbox[i]);
  367.             dGeomBoxGetLengths(groundbox[i], ss);
  368.             draw_box(ss[0], ss[1], ss[2]);
  369.             glPopMatrix();
  370.         }
  371.         for(i=0; i<ngroundsphere; i++)
  372.         {
  373.             glPushMatrix();
  374.             position_gl(groundsphere[i]);
  375.             draw_sphere(dGeomSphereGetRadius(groundsphere[i]), 12);
  376.             glPopMatrix();
  377.         }
  378.         glEnable(GL_DEPTH_TEST);
  379.         glDisable(GL_BLEND);
  380.         //        glClear(GL_DEPTH_BUFFER_BIT);
  381.         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  382.         glColor3f(0, 1, 0);
  383.         glDisable(GL_LIGHTING);
  384.         for(i=0; i<ngroundbox; i++)
  385.         {
  386.             dVector3 ss;
  387.             glPushMatrix();
  388.             position_gl(groundbox[i]);
  389.             dGeomBoxGetLengths(groundbox[i], ss);
  390.             draw_box(ss[0], ss[1], ss[2]);
  391.             glPopMatrix();
  392.         }
  393.         for(i=0; i<ngroundsphere; i++)
  394.         {
  395.             glPushMatrix();
  396.             position_gl(groundsphere[i]);
  397.             draw_sphere(dGeomSphereGetRadius(groundsphere[i]), 12);
  398.             glPopMatrix();
  399.         }
  400.         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  401.         glEnable(GL_LIGHTING);*/
  402.                 
  403.  
  404.         glPushMatrix();
  405.         glScalef(-1, -1, 1);
  406.         glColor3f(1.0, 1.0, 1.0);
  407.         glEnable(GL_TEXTURE_2D);
  408.         glBindTexture(GL_TEXTURE_2D, leveltex);
  409.         terrain.draw();
  410.         glDisable(GL_TEXTURE_2D);
  411.         glDisable(GL_LIGHTING);
  412.         glColor3f(.5, .1, .1);
  413.         innerloop.draw();
  414.         outerloop.draw();
  415.         glEnable(GL_LIGHTING);
  416.         glPopMatrix();
  417.  
  418.         glEnable(GL_CULL_FACE);
  419.  
  420.         for(int j=0; j<players; j++)
  421.         {
  422.         glEnable(GL_TEXTURE_2D);
  423.         glBindTexture(GL_TEXTURE_2D, carbodytex);
  424.         glColor3f(1, 1, 1);        
  425.         glPushMatrix();
  426.         position_gl(player[j].car.chassis);
  427.         glScalef(-1, -1, 1);
  428.         carbody.draw();
  429.         //        draw_box(2, 3, 1.5);
  430.         glPopMatrix();
  431.  
  432.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  433.         glBindTexture(GL_TEXTURE_2D, wheeltex);
  434.         for(i=0; i<4; i++)
  435.         {
  436.             glPushMatrix();
  437.             position_gl(player[j].car.wheel[i]);
  438.             //            glRotatef(90, 0, 0, 1);
  439.             //            draw_sphere(0.8, 12);
  440.             glColor3f(1, 1, 1);
  441.             wheel.draw();
  442.             glDisable(GL_TEXTURE_2D);
  443.             glEnable(GL_BLEND);
  444.             glColor4f(0, 0.6, 1, 0.5);
  445.             wheelshell.draw();
  446.             glDisable(GL_BLEND);
  447.             glEnable(GL_TEXTURE_2D);
  448.             glPopMatrix();
  449.         }
  450.         }
  451.         glDisable(GL_CULL_FACE);
  452.  
  453.         }
  454.  
  455.         //        CVector sp;
  456.         //        const dReal *sh;
  457.         //        sh = dBodyGetLinearVel(player[0].car.chassis);
  458.         //        sp.x=sh[0];
  459.         //        sp.y=sh[1];
  460.         //        sp.z=sh[2];
  461.         //              glEnable(GL_BLEND);
  462.         //        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
  463.         //        glDisable(GL_DEPTH_TEST);
  464.         //        glLoadIdentity();
  465.         //        allegro_gl_printf(allegro_fnt, -10, 3, -20, makecol(0, 255, 0), "SPEED: %6.2f", sp.length());        
  466.         //        glDisable(GL_BLEND);
  467.         //        glEnable(GL_DEPTH_TEST);
  468.  
  469.         glMatrixMode(GL_PROJECTION);
  470.         glLoadIdentity();
  471.             glViewport(0, 0, SCREEN_W, SCREEN_H);
  472.                gluPerspective(45.0f, SCREEN_W/(SCREEN_H/2), 0.2, 200.0);
  473.         glMatrixMode(GL_MODELVIEW);
  474.               glEnable(GL_BLEND);
  475.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);
  476.         glDisable(GL_DEPTH_TEST);
  477.         glLoadIdentity();
  478.         allegro_gl_printf(allegro_fnt, -16, 8, -20, makecol(0, 255, 0), "FPS: %6.4f", 1/m);        
  479.         glDisable(GL_BLEND);
  480.         glDisable(GL_TEXTURE_2D);
  481.         glEnable(GL_DEPTH_TEST);
  482.         
  483.         glFinish();
  484.         allegro_gl_flip();
  485.         allegro_gl_end();
  486.     }
  487. }
  488.  
  489.  
  490.  
  491.  
  492.