| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- #include <stdio.h>
- #include <iostream>
- #include <fstream>
- #include <typeinfo>
- #include "common.h"
- #include "input.h"
- #include "camera.h"
- #include "gmath.h"
- #include "renderer.h"
- #include "ui.h"
- #include "app.h"
- #include "particles.h"
- #include "texture.h"
- #include "mesh.h"
- #include "light.h"
- #include "collision.h"
- #include "material.h"
- #include "resource.h"
- #include "scene.h"
- #include "Scanner.h"
- #include "skybox.h"
- #include "map.h"
- #include "mesh_node.h"
- #include "skel_model_node.h"
- #include "mesh_node.h"
- #include "skel_anim.h"
- #include "mesh_skel_ctrl.h"
- #include "skel_anim_ctrl.h"
- #include "skel_node.h"
- #include "light_props.h"
- #include "btBulletCollisionCommon.h"
- #include "btBulletDynamicsCommon.h"
- #include "BulletDebuger.h"
- // map (hard coded)
- camera_t* main_cam;
- mesh_node_t* floor__,* sarge,* horse;
- skel_model_node_t* imp;
- point_light_t* point_lights[10];
- spot_light_t* spot_lights[2];
- class floor_t: public camera_t
- {
- public:
- void Render()
- {
- r::dbg::RenderCube( true, 1.0 );
- }
- void RenderDepth()
- {
- r::dbg::RenderCube( true, 1.0 );
- }
- }* floor_;
- // Physics
- btDefaultCollisionConfiguration* collisionConfiguration;
- btCollisionDispatcher* dispatcher;
- btDbvtBroadphase* broadphase;
- btSequentialImpulseConstraintSolver* sol;
- btDiscreteDynamicsWorld* dynamicsWorld;
- BulletDebuger debugDrawer;
- #define ARRAY_SIZE_X 5
- #define ARRAY_SIZE_Y 5
- #define ARRAY_SIZE_Z 5
- #define MAX_PROXIES (ARRAY_SIZE_X*ARRAY_SIZE_Y*ARRAY_SIZE_Z + 1024)
- #define SCALING 1.
- #define START_POS_X -5
- #define START_POS_Y -5
- #define START_POS_Z -3
- void initPhysics()
- {
- collisionConfiguration = new btDefaultCollisionConfiguration();
- dispatcher = new btCollisionDispatcher(collisionConfiguration);
- broadphase = new btDbvtBroadphase();
- sol = new btSequentialImpulseConstraintSolver;
- dynamicsWorld = new btDiscreteDynamicsWorld( dispatcher, broadphase, sol, collisionConfiguration );
- dynamicsWorld->setGravity(btVector3(0,-10,0));
- btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
- btTransform groundTransform;
- groundTransform.setIdentity();
- groundTransform.setOrigin(btVector3(0,-50,0));
- //We can also use DemoApplication::localCreateRigidBody, but for clarity it is provided here:
- {
- btScalar mass(0.);
- //rigidbody is dynamic if and only if mass is non zero, otherwise static
- bool isDynamic = (mass != 0.f);
- btVector3 localInertia(0,0,0);
- if (isDynamic)
- groundShape->calculateLocalInertia(mass,localInertia);
- //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
- btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
- btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
- btRigidBody* body = new btRigidBody(rbInfo);
- //add the body to the dynamics world
- dynamicsWorld->addRigidBody(body);
- }
- {
- //create a few dynamic rigidbodies
- // Re-using the same collision is better for memory usage and performance
- btCollisionShape* colShape = new btBoxShape(btVector3(SCALING*1,SCALING*1,SCALING*1));
- //btCollisionShape* colShape = new btSphereShape(btScalar(1.));
- /// Create Dynamic Objects
- btTransform startTransform;
- startTransform.setIdentity();
- btScalar mass(1.f);
- //rigidbody is dynamic if and only if mass is non zero, otherwise static
- bool isDynamic = (mass != 0.f);
- btVector3 localInertia(0,0,0);
- if (isDynamic)
- colShape->calculateLocalInertia(mass,localInertia);
- float start_x = START_POS_X - ARRAY_SIZE_X/2;
- float start_y = START_POS_Y;
- float start_z = START_POS_Z - ARRAY_SIZE_Z/2;
- for (int k=0;k<ARRAY_SIZE_Y;k++)
- {
- for (int i=0;i<ARRAY_SIZE_X;i++)
- {
- for(int j = 0;j<ARRAY_SIZE_Z;j++)
- {
- startTransform.setOrigin(SCALING*btVector3(
- btScalar(2.0*i + start_x),
- btScalar(20+2.0*k + start_y),
- btScalar(2.0*j + start_z)));
- //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
- btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
- btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
- btRigidBody* body = new btRigidBody(rbInfo);
- body->setActivationState(ISLAND_SLEEPING);
- dynamicsWorld->addRigidBody(body);
- body->setActivationState(ISLAND_SLEEPING);
- }
- }
- }
- }
- dynamicsWorld->setDebugDrawer(&debugDrawer);
- }
- //=====================================================================================================================================
- // Init =
- //=====================================================================================================================================
- void Init()
- {
- PRINT( "Engine initializing..." );
- initPhysics();
- srand( unsigned(time(NULL)) );
- MathSanityChecks();
- app::InitWindow();
- uint ticks = app::GetTicks();
- r::Init();
- ui::Init();
- // camera
- main_cam = new camera_t( r::aspect_ratio*ToRad(60.0), ToRad(60.0), 0.5, 100.0 );
- main_cam->MoveLocalY( 3.0 );
- main_cam->MoveLocalZ( 5.7 );
- main_cam->MoveLocalX( -0.3 );
- // lights
- point_lights[0] = new point_light_t();
- point_lights[0]->Init( "maps/temple/light0.light" );
- point_lights[0]->SetLocalTransformation( vec3_t( -1.0, 2.4, 1.0 ), mat3_t::GetIdentity(), 1.0 );
- point_lights[1] = new point_light_t();
- point_lights[1]->Init( "maps/temple/light1.light" );
- point_lights[1]->SetLocalTransformation( vec3_t( 2.5, 1.4, 1.0 ), mat3_t::GetIdentity(), 1.0 );
- spot_lights[0] = new spot_light_t();
- spot_lights[0]->Init( "maps/temple/light2.light" );
- spot_lights[0]->SetLocalTransformation( vec3_t( 1.3, 4.3, 3.0 ), mat3_t( euler_t(ToRad(-20), ToRad(20), 0.0) ), 1.0 );
- spot_lights[1] = new spot_light_t();
- spot_lights[1]->Init( "maps/temple/light3.light" );
- spot_lights[1]->SetLocalTransformation( vec3_t( -2.3, 6.3, 2.9 ), mat3_t( euler_t(ToRad(-70), ToRad(-20), 0.0) ), 1.0 );
- // horse
- horse = new mesh_node_t();
- horse->Init( "meshes/horse/horse.mesh" );
- horse->SetLocalTransformation( vec3_t( -2, 0, 1 ), mat3_t( euler_t(-m::PI/2, 0.0, 0.0) ), 0.5 );
-
- // sarge
- sarge = new mesh_node_t();
- sarge->Init( "meshes/sphere/sphere16.mesh" );
- //sarge->SetLocalTransformation( vec3_t( 0, -2.8, 1.0 ), mat3_t( euler_t(-m::PI/2, 0.0, 0.0) ), 1.1 );
- sarge->SetLocalTransformation( vec3_t( 0, 2.0, 2.0 ), mat3_t::GetIdentity(), 0.4 );
-
- // floor
- floor__ = new mesh_node_t();
- floor__->Init( "maps/temple/Cube.019.mesh" );
- floor__->SetLocalTransformation( vec3_t(0.0, -0.19, 0.0), mat3_t( euler_t(-m::PI/2, 0.0, 0.0) ), 0.8 );
- // imp
- imp = new skel_model_node_t();
- imp->Init( "models/imp/imp.smdl" );
- imp->SetLocalTransformation( vec3_t( 0.0, 2.11, 0.0 ), mat3_t( euler_t(-m::PI/2, 0.0, 0.0) ), 0.7 );
- imp->mesh_nodes[0]->mesh_skel_ctrl->skel_node->skel_anim_ctrl->skel_anim = rsrc::skel_anims.Load( "models/imp/walk.imp.anim" );
- imp->mesh_nodes[0]->mesh_skel_ctrl->skel_node->skel_anim_ctrl->step = 0.8;
- //
- floor_ = new floor_t;
- //floor_->material = rsrc::materials.Load( "materials/default.mtl" );
- const char* skybox_fnames [] = { "textures/env/hellsky4_forward.tga", "textures/env/hellsky4_back.tga", "textures/env/hellsky4_left.tga",
- "textures/env/hellsky4_right.tga", "textures/env/hellsky4_up.tga", "textures/env/hellsky4_down.tga" };
- scene::skybox.Load( skybox_fnames );
- PRINT( "Engine initialization ends (" << app::GetTicks()-ticks << ")" );
- cerr.flush();
- }
- //=====================================================================================================================================
- // main =
- //=====================================================================================================================================
- int main( int /*argc*/, char* /*argv*/[] )
- {
- app::PrintAppInfo();
- Init();
- PRINT( "Entering main loop" );
- int ticks = app::GetTicks();
- do
- {
- int ticks_ = app::GetTicks();
- i::HandleEvents();
- r::PrepareNextFrame();
- float dist = 0.2;
- float ang = ToRad(3.0);
- float scale = 0.01;
- // move the camera
- static node_t* mover = main_cam;
- if( i::keys[ SDLK_1 ] ) mover = main_cam;
- if( i::keys[ SDLK_2 ] ) mover = point_lights[0];
- if( i::keys[ SDLK_3 ] ) mover = spot_lights[0];
- if( i::keys[ SDLK_4 ] ) mover = point_lights[1];
- if( i::keys[ SDLK_5 ] ) mover = spot_lights[1];
- if( i::keys[ SDLK_m ] == 1 ) i::warp_mouse = !i::warp_mouse;
- if( i::keys[SDLK_a] ) mover->MoveLocalX( -dist );
- if( i::keys[SDLK_d] ) mover->MoveLocalX( dist );
- if( i::keys[SDLK_LSHIFT] ) mover->MoveLocalY( dist );
- if( i::keys[SDLK_SPACE] ) mover->MoveLocalY( -dist );
- if( i::keys[SDLK_w] ) mover->MoveLocalZ( -dist );
- if( i::keys[SDLK_s] ) mover->MoveLocalZ( dist );
- if( !i::warp_mouse )
- {
- if( i::keys[SDLK_UP] ) mover->RotateLocalX( ang );
- if( i::keys[SDLK_DOWN] ) mover->RotateLocalX( -ang );
- if( i::keys[SDLK_LEFT] ) mover->RotateLocalY( ang );
- if( i::keys[SDLK_RIGHT] ) mover->RotateLocalY( -ang );
- }
- else
- {
- float accel = 44.0;
- mover->RotateLocalX( ang * i::mouse_velocity.y * accel );
- mover->RotateLocalY( -ang * i::mouse_velocity.x * accel );
- }
- if( i::keys[SDLK_q] ) mover->RotateLocalZ( ang );
- if( i::keys[SDLK_e] ) mover->RotateLocalZ( -ang );
- if( i::keys[SDLK_PAGEUP] ) mover->scale_lspace += scale ;
- if( i::keys[SDLK_PAGEDOWN] ) mover->scale_lspace -= scale ;
- if( i::keys[SDLK_k] ) main_cam->LookAtPoint( point_lights[0]->translation_wspace );
- mover->rotation_lspace.Reorthogonalize();
- scene::UpdateAllControllers();
- scene::UpdateAllWorldStuff();
- dynamicsWorld->stepSimulation( 1 );
- r::Render( *main_cam );
- //map.octree.root->bounding_box.Render();
- // print some debug stuff
- ui::SetColor( vec4_t(1.0, 1.0, 1.0, 1.0) );
- ui::SetPos( -0.98, 0.95 );
- ui::SetFontWidth( 0.03 );
- ui::Printf( "frame:%d time:%dms\n", r::frames_num, app::GetTicks()-ticks_ );
- //ui::Print( "Movement keys: arrows,w,a,s,d,q,e,shift,space\nSelect objects: keys 1 to 5\n" );
- ui::Printf( "Mover: Pos(%.2f %.2f %.2f) Angs(%.2f %.2f %.2f)", mover->translation_wspace.x, mover->translation_wspace.y, mover->translation_wspace.z,
- ToDegrees(euler_t(mover->rotation_wspace).x), ToDegrees(euler_t(mover->rotation_wspace).y), ToDegrees(euler_t(mover->rotation_wspace).z) );
- if( i::keys[SDLK_ESCAPE] ) break;
- if( i::keys[SDLK_F11] ) app::TogleFullScreen();
- if( i::keys[SDLK_F12] == 1 ) r::TakeScreenshot("gfx/screenshot.jpg");
- /*char str[128];
- if( r::frames_num < 1000 )
- sprintf( str, "capt/%06d.jpg", r::frames_num );
- else
- sprintf( str, "capt2/%06d.jpg", r::frames_num );
- r::TakeScreenshot(str);*/
- // std stuff follow
- SDL_GL_SwapBuffers();
- r::PrintLastError();
- if( 1 )
- {
- //if( r::frames_num == 10 ) r::TakeScreenshot("gfx/screenshot.tga");
- app::WaitForNextFrame();
- }
- else
- if( r::frames_num == 5000 ) break;
- }while( true );
- PRINT( "Exiting main loop (" << app::GetTicks()-ticks << ")" );
- PRINT( "Exiting..." );
- app::QuitApp( EXIT_SUCCESS );
- return 0;
- }
|