Esempio n. 1
0
void game_scene::start (void) {
	get_physics_world()->setGravity( btVector3(0.0F, 0.0F, 0.0F) );
	//
	std::string shader_file_name = "ver.sdr f*g.sdr";
	const unsigned int game_shader = 1;
	shader_load( shader_file_name, game_shader );
	//
	set_ambient_color( glm::vec4(0.218F, 0.218F, 0.218F, 1.0F) );
	//
	_light_base* light_one = new _light_base();
	light_one->set_diffuse_color( glm::vec3(1.0F, 1.0F, 1.0F) );
	/* light_one->set_diffuse_color( glm::vec3(0.36F, 0.05F, 0.67F) ); */
	light_one->set_specular_color( glm::vec3(0.9F, 0.9F, 0.9F) );
	light_one->set_position_in_world( glm::vec3( 0.0F, 0.0F, 0.0F) );
	/* light_one->rotate( 8.0F, vec3( 0.0F, 1.0F, 0.0F ) ); */
	light_one->translate( glm::vec3(400.0F, -100.0F, 400.0F) );
	add_light( light_one );

	_light_base* light_two = new _light_base();
	light_two->set_diffuse_color( glm::vec3(0.F, 0.F, 1.F) );
	light_two->set_specular_color( glm::vec3(0.9F, 0.9F, 0.9F) );
	light_two->set_position_in_world( glm::vec3( 0.0F, 0.0F, 0.0F) );
	/* light_two->rotate( 8.0F, vec3( 0.0F, 1.0F, 0.0F ) ); */
	light_two->translate( glm::vec3(-400.0F, 200.0F, 400.0F) );
	add_light( light_two );
	/* std::cout<<"dot: "<<glm::dot( glm::vec3(0.0F, 0.0F, -4.0), glm::vec3(0.0F, 0.0F, -1.0) )<<std::endl; */
	//
	camera_main* camera_m = new camera_main();
	camera_m->set_ID( 11 );
	camera_m->activate( true );
	camera_m->perspective( 45.0F, 1366.0F / 768.0F, 1.0F, 1000.0F );
	camera_m->set_position_in_world( glm::vec3( 0.0F, 0.0F, 0.0F ) );
	camera_m->rotate( 0.0F, glm::vec3( 0.0F, 1.0F, 0.0F ) );
	camera_m->_camera_base::translate( glm::vec3( 0.0F, 3.F, 8.0F ) );
	camera_m->_camera_base::rotate( -10.0F, glm::vec3( 1.0F, 0.F, 0.0F ) );
	add_camera( camera_m );

	//
	btCollisionDispatcher* dispatcher = static_cast<btCollisionDispatcher*>( this->get_physics_world()->getDispatcher() );
	btGImpactCollisionAlgorithm::registerAlgorithm( dispatcher );
	//
	{
		ship* ship_a = new ship();
		ship_a->activate( true );
		/* ship_a->generate_model(); */
		/* ship_a->load_model_data_from_assets( asset->get_model_data( "lowpolyspaceship.dae" ) ); */
		ship_a->set_position_in_world( glm::vec3( 0.0F, 0.0F, 0.0F ) );
		ship_a->translate( glm::vec3( .0F, 0.0F, -10.5F ) );
		ship_a->set_material_diffuse_color( glm::vec4( 0.0, 0.6, 0.6, 1.0 ) );
		ship_a->set_material_specular_color( glm::vec4( 0.0, 0.6, 0.6, 1.0 ) );

		
		/* //////////////////// */
		/* ship_a->init_rigid_body( 1.0F, btVector3( 0.6F, 0.6F, 0.6F ), tmpshape ); */
		ship_a->get_rigidbody()->setDamping( 0.05F, 0.01F );
		ship_a->get_rigidbody()->setAngularFactor( btVector3(0.4F, 0.4F, 0.4F) );
		ship_a->get_rigidbody()->setLinearFactor( btVector3(0.4F, 0.4F, 0.4F) );
		add_object( ship_a, game_shader );
	}

	{
		stone_a_l* stone_a_l_a = new stone_a_l();
		/* stone_a_l_a->generate_model(); */
		/* stone_a_l_a->load_model_data_from_assets( asset->get_model_data( "lowpolyspacestone_a_l.dae" ) ); */
		stone_a_l_a->set_position_in_world( glm::vec3( 0.0F, 0.0F, 0.0F ) );
		stone_a_l_a->translate( glm::vec3( .0F, 0.0F, -5.0F ) );
		stone_a_l_a->set_material_diffuse_color( glm::vec4( 0.0, 0.6, 0.6, 1.0 ) );
		stone_a_l_a->set_material_specular_color( glm::vec4( 0.0, 0.6, 0.6, 1.0 ) );

		
		/* //////////////////// */
		/* stone_a_l_a->init_rigid_body( 1.0F, btVector3( 0.6F, 0.6F, 0.6F ), tmpshape ); */
		stone_a_l_a->get_rigidbody()->setDamping( 0.05F, 0.01F );
		stone_a_l_a->get_rigidbody()->setAngularFactor( btVector3(0.4F, 0.4F, 0.4F) );
		stone_a_l_a->get_rigidbody()->setLinearFactor( btVector3(0.4F, 0.4F, 0.4F) );
		add_object( stone_a_l_a, game_shader );
	}

	{
		circle* circle_a = new circle();
		circle_a->catch_contrtoller();
		circle_a->catch_camera( camera_m->get_ID() );
		circle_a->set_position_in_world( glm::vec3( 0.0F, 0.0F, 0.0F ) );
		circle_a->translate( glm::vec3( .0F, 0.0F, 20.5F ) );
		circle_a->set_material_diffuse_color( glm::vec4( 0.6, 0.6, 0.6, 1.0 ) );
		circle_a->set_material_specular_color( glm::vec4( 0.6, 0.6, 0.6, 1.0 ) );
		add_object( circle_a, game_shader );
	}


}
void ray_test(glm::vec2 Mouse, glm::vec2 screen_size)
{
    //glm::vec2 Mouse = mouse_pos;
   Mouse.y = screen_size.y - Mouse.y;

    //glm::vec4 RayStart = glm::vec4(2*Mouse.x/screen_size.x - 1,2*Mouse.y/screen_size.y - 1, -1, 1);
    //glm::vec4 RayEnd = RayStart; RayEnd.z = 0;
	glm::vec4 RayStart(
		((float)Mouse.x/(float)screen_size.x - 0.5f) * 2.0f, // [0,1024] -> [-1,1]
		((float)Mouse.y/(float)screen_size.y - 0.5f) * 2.0f, // [0, 768] -> [-1,1]
		-1.0, // The near plane maps to Z=-1 in Normalized Device Coordinates
		1.0f
	);
	glm::vec4 RayEnd = RayStart; RayEnd.z = 0.0; /*lRayEnd_NDC(
		((float)mouseX/(float)screenWidth  - 0.5f) * 2.0f,
		((float)mouseY/(float)screenHeight - 0.5f) * 2.0f,
		0.0,
		1.0f
	);*/
    //RayStart = glm::inverse( get_g_projection()*get_g_view() )*RayStart; RayStart /= RayStart.w;
    //RayEnd   = glm::inverse( get_g_projection()*get_g_view() )*RayEnd;   RayEnd   /= RayEnd.w;
	// Faster way (just one inverse)
	glm::mat4 M = glm::inverse(get_g_projection() * get_g_view());
	glm::vec4 lRayStart_world = M * RayStart; lRayStart_world/=lRayStart_world.w;
	glm::vec4 lRayEnd_world   = M * RayEnd  ; lRayEnd_world  /=lRayEnd_world.w;


    //glm::vec4 RayDirection = glm::normalize(RayEnd-RayStart);
	glm::vec3 lRayDir_world(lRayEnd_world - lRayStart_world);
	lRayDir_world = 1000.0f * glm::normalize(lRayDir_world);

    //btCollisionWorld::AllHitsRayResultCallback RayCallback(btVector3(lRayStart_world.x,lRayStart_world.y,lRayStart_world.z),
	btCollisionWorld::ClosestRayResultCallback RayCallback(btVector3(lRayStart_world.x,lRayStart_world.y,lRayStart_world.z),
                                                           btVector3(lRayDir_world.x,lRayDir_world.y,lRayDir_world.z));
	btDiscreteDynamicsWorld* TheWorld = get_physics_world();
    TheWorld->rayTest(btVector3(lRayStart_world.x,lRayStart_world.y,lRayStart_world.z),
                      btVector3(lRayDir_world.x,lRayDir_world.y,lRayDir_world.z),
                      RayCallback);
	int closest = 0;
    if ( RayCallback.hasHit() )
    {
        
        //for ( int i = 0; i < RayCallback.m_collisionObjects.size(); i++ )
       // {
           /* std::cout<<i<<": "<<glm::ToVec3(RayCallback.m_hitPointWorld[i])<<" "
                <<RayCallback.m_hitFractions[i]<<" "
                <<RayCallback.m_collisionObjects[i]->getCollisionShape()->getShapeType()<<std::endl;
				*/
			//printf("collisions %i distance %f ID: %i\n", i, RayCallback.m_hitPointWorld[i].getZ(), RayCallback.m_collisionObjects[i]->getUserIndex());
           // if (RayCallback.m_hitPointWorld[i].getZ() >
           //     RayCallback.m_hitPointWorld[closest].getZ() )
            //    closest = i;
      //  }
		//printf("closest %i\n", closest);
    } else{
		printf("no hits!!\n");
		return;
	}

	//struct d3_object * clicked_object = find_object_by_id(RayCallback.m_collisionObjects[closest]->getUserIndex());
	struct d3_object * clicked_object = find_object_by_id(RayCallback.m_collisionObject->getUserIndex());
	//printf("id trying to find: %i, ID GOT: %i\n", RayCallback.m_collisionObjects[closest]->getUserIndex(), clicked_object->id);
	//clicked_object->flags = clicked_object->flags | D3_OBJECT_NORENDER;
	printf("physics state: %i\n", clicked_object->physics_obj->fallRigidBody->getActivationState());
	//delete_object(clicked_object);
}