Esempio n. 1
0
void ShadowsDemo::onLoadContent() {
	App::onLoadContent();

	// load shaders
	loadShaders();

	// load states
	ciri::RasterizerDesc rasterDesc;
	rasterDesc.cullMode = ciri::CullMode::Clockwise;
	//rasterDesc.fillMode = ciri::FillMode::Wireframe;
	_rasterState = graphicsDevice()->createRasterizerState(rasterDesc);
	_additiveBlendState = graphicsDevice()->getDefaultBlendAdditive();

	// create shadow map stuff
	_shadowTarget = graphicsDevice()->createRenderTarget2D(2048, 2048, ciri::TextureFormat::RGBA32_Float, ciri::DepthStencilFormat::Depth24);
	ciri::SamplerDesc shadowSamplerDesc;
	shadowSamplerDesc.filter = ciri::SamplerFilter::Point;
	shadowSamplerDesc.wrapU=shadowSamplerDesc.wrapV=shadowSamplerDesc.wrapW = ciri::SamplerWrap::Border;
	shadowSamplerDesc.borderColor[0]=shadowSamplerDesc.borderColor[1]=shadowSamplerDesc.borderColor[2]=shadowSamplerDesc.borderColor[3] = 1.0f;
	_shadowSampler = graphicsDevice()->createSamplerState(shadowSamplerDesc);

	// load some models
	_ground = std::make_shared<Model>();
	_ground->addFromObj("data/demos/shadows/ground.obj");
	_ground->build(graphicsDevice());
	if(_ground->isValid()){_models.push_back(_ground);}
	_helicopterBody = std::make_shared<Model>();
	_helicopterBody->addFromObj("data/demos/shadows/helicopter_body.obj");
	_helicopterBody->build(graphicsDevice());
	if(_helicopterBody->isValid()){_models.push_back(_helicopterBody);}
	_helicopterBlades = std::make_shared<Model>();
	_helicopterBlades->addFromObj("data/demos/shadows/helicopter_blades.obj");
	_helicopterBlades->build(graphicsDevice());
	if(_helicopterBlades->isValid()){_models.push_back(_helicopterBlades);}
	_helicopterTail = std::make_shared<Model>();
	_helicopterTail->addFromObj("data/demos/shadows/helicopter_tail.obj");
	_helicopterTail->build(graphicsDevice());
	if(_helicopterTail->isValid()){_models.push_back(_helicopterTail);}

	// add some lights
	Light light0(Light::Type::Directional);
	light0.setDirection(cc::Vec3f(0.75f, -0.8f, 0.72f));
	light0.setCastShadows(true);
	_lights.push_back(light0);
	Light light1(Light::Type::Spot);
	light1.setPosition(cc::Vec3f(0.0f, 20.0f, 0.0f));
	light1.setRange(200.0f);
	light1.setConeInnerAngle(10.0f);
	light1.setConeOuterAngle(12.0f);
	light1.setCastShadows(true);
	light1.setDiffuseIntensity(1.0f);//0.25f);
	//_lights.push_back(light1);
	_cameraLight = &_lights.back();
}
Esempio n. 2
0
void buildAndRenderScene(Scene &scene, Camera &camera, RenderTarget &renderTarget)
{
    // Build scene
    AmbientLight        ambientLight(Color::white);
    PointLight          light1(Vector3D(50.0, 70.0, 0.0));
    PointLight          light2(Vector3D(50.0, 70.0, 200.0));

    Torus               sphere1(10, 4, Vector3D(0.0, 20.0, 100.0));
    PhongMaterial       material1(Color::red);

    Sphere              sphere2(10, Vector3D(0.0, 45.0, 100.0));
    PhongMaterial       material2(Color::green);

    Sphere              sphere3(10, Vector3D(35.0, 20.0, 100.0));
    PhongMaterial       material3(Color::blue);

    Plane               plane1(Vector3D(0, 0, 0), Vector3D(0.0, 1.0, 0.0));
    PhongMaterial       material4(Color(0.0, 1.0, 1.0));

    Plane               plane2(Vector3D(-100, 0, 0), Vector3D(1.0, 0.0, 0.0));
    PhongMaterial       material5(Color(1.0, 0.0, 1.0));

    Plane               plane3(Vector3D(0, 0, 500), Vector3D(0.0, 0.0, -1.0));
    PhongMaterial       material6(Color(1.0, 1.0, 0.0));

    sphere1.setMaterial(&material1);
    sphere2.setMaterial(&material2);
    sphere3.setMaterial(&material3);
    plane1.setMaterial(&material4);
    plane2.setMaterial(&material5);
    plane3.setMaterial(&material6);
    
    scene.addObject(&sphere1);
    scene.addObject(&sphere2);
    scene.addObject(&sphere3);
    scene.addObject(&plane1);
    scene.addObject(&plane2);
    scene.addObject(&plane3);

    scene.addLight(&light1);
    scene.addLight(&light2);
    scene.setAmbientLight(&ambientLight);

    // Render scene
    camera.computeFrame();
    camera.renderScene(scene, renderTarget);
    renderTarget.update();
}
Esempio n. 3
0
int main(int argc, char** argv) {
	if(argc!=2) {
		std::cerr << "Usage: " << argv[0] << " <file-name>" << std::endl;
		return -1;
	}

	World world; //(COLOR(0.3, 0.3, 1.0));
	Light light1(&world, POINT3D(2.0, 3.0, 2.0));
	//Light light2(&world, POINT3D(-4.0, 5.0, 5.0), LIGHT(COLOR_YELLOW));
	//Light light3(&world, POINT3D(2.0, 0.0, 5.0));

	material_t blue_ball_mat = MATERIAL_DEFAULT, green_ball_mat = MATERIAL_DEFAULT;
	blue_ball_mat.color = COLOR_WHITE;
	green_ball_mat.color = COLOR(0.7,0.7,0.7);
	blue_ball_mat.specular = green_ball_mat.specular = COLOR_WHITE;
	blue_ball_mat.ka = 0.075; green_ball_mat.ka = 0.15;
	blue_ball_mat.kd = 0.075; green_ball_mat.kd = 0.25;
	blue_ball_mat.ks = 0.2; green_ball_mat.ks = 1.0;
	blue_ball_mat.ke = green_ball_mat.ke = 20.0;
	blue_ball_mat.kr = 0.05; green_ball_mat.kr = 0.75;
	blue_ball_mat.kt = 0.85;
	blue_ball_mat.n = 0.95;
	blue_ball_mat.texture = green_ball_mat.texture = 0;

	Sphere blue(&world, blue_ball_mat, 0.15, POINT3D(0.45, 0.3, 0.75));
	Sphere green(&world, green_ball_mat, 0.15, POINT3D(0.3, 0.2, 0.5));
	Object floor(&world);
	material_t floor_mat = MATERIAL_DEFAULT;
	floor_mat.color = COLOR_RED;
	floor_mat.specular = COLOR_WHITE;
	floor_mat.ka = 0.7;
	floor_mat.kd = 0.2;
	floor_mat.ks = 0.2;
	floor_mat.ke = 10.0;
	floor_mat.texture = floor_texture1;
	Triangle floor1(&floor, floor_mat, POINT3D(-0.7, 0.0, 1.0), POINT3D(1.05, 0.0, 1.0), POINT3D(1.05, 0.0, -1.5));
	Triangle floor2(&floor, floor_mat, POINT3D(-0.7, 0.0, 1.0), POINT3D(1.05, 0.0, -1.5), POINT3D(-0.7, 0.0, -1.5));
	//Cube cube(&world, MATERIAL(COLOR_YELLOW), POINT3D(0.4, 0.4, 0.4), POINT3D(1.0, 0.5, -0.5), POINT3D(M_PI/4.0, -M_PI / 4.0, M_PI/4.0));
	//Cone cone(&world, MATERIAL(COLOR_YELLOW), 1.0, 5, 0.5, 20, POINT3D(0.0, 0.5, -0.5), POINT3D(-M_PI/16.0,0.0,0.0));
	Camera cam(&world, POINT3D(0.5, 0.3, 1.5), POINT3D(0.5, 0.3, 0.0), POINT3D(0.0, 1.0, 0.0), 45.0, 0.01, 10.0, 10);
	Image img(1024,768);
	cam.snap(&img);
	Image::save(&img, argv[1]);
	return 0;
}
Esempio n. 4
0
int main(int argc, char** argv)
{
    GLFWwindow* window = initGLWindow();
    
    movement = new CameraMovement(window);
    
    initGL();
    
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    renderer.init(width, height);
    
    camera.init(width/(float)height, 60.f, .1f, 100.f);
    camera.setCameraProjection(PROJECTION_PERSPECTIVE);
    
    // Floor
    std::shared_ptr<Mesh> floorMesh = std::make_shared<Mesh>(UnitQuad::CreateUnitQuad());
    floorMesh->material.diffuse = glm::vec4(0.3f, 0.6f, 0.7f, 1.0f);
    floorMesh->material.ambient = glm::vec4(0.01f, 0.01f, 0.01f, 1.0f);
    floorMesh->material.specular = glm::vec4(0.1f, 0.1f, 0.1f, 1.0f);
    floorMesh->material.shininess = 100.0f;
    
    std::shared_ptr<SceneNode> floor(new SceneNode);
    floor->init(floorMesh);
    floor->position = glm::vec3(0.0,-1.0, 0.0);
    floor->rotation = glm::rotate(glm::mat4(1.0f),-90.0f, glm::vec3(1.0, 0.0, 0.0));
    floor->scale = glm::scale(glm::mat4(1.0), glm::vec3(100.0f));
    
    LightProperties lightProperties = LightFactory::Bright(glm::vec3(1.0, 1.0, 1.0));
    lightProperties.position = glm::vec4(-2.0f, 2.0f, -1.0f, 1.0);
    lightProperties.direction = glm::vec4(0.0, -0.1, -1.0, 0.0);
    std::shared_ptr<Light> light(new Light);
    light->properties = lightProperties;
    renderer.lights.push_back(light);
    
    LightProperties lightProperties1 = LightFactory::Bright(glm::vec3(1.0, 1.0, 1.0));
    lightProperties1.position = glm::vec4(4.0f, 2.0f, -3.0f, 1.0);
    lightProperties1.direction = glm::vec4(-1.0, -0.1, 0.0, 0.0);
    std::shared_ptr<Light> light1(new Light);
    light1->properties = lightProperties1;
    renderer.lights.push_back(light1);

    std::string path(MODEL_PATH);
    path.append("cooldragon.off");
    auto node = createSceneNode(path);
    node->position = glm::vec3(0.0f, 0.0f, -3.0f);
        
    renderer.nodes.push_back(node);
    
    renderer.nodes.push_back(floor);
    
    std::thread first (startGui, argc, argv);
    
    glfwSwapInterval(1); //0 to disable vsync, 1 to enable it
    
    while (!glfwWindowShouldClose(window))
    {
        if(shouldFlipNormals) {
            renderer.nodes[0]->mesh->flipNormals();
            shouldFlipNormals = false;
        }
        
        if(shouldLoadFile) {
            std::string path(MODEL_PATH);
            path.append(filename);
            renderer.nodes[0] = createSceneNode(path);
            renderer.nodes[0]->position = glm::vec3(-2.0f, -0.5f, -3.0f);
            
            gui->material = &renderer.nodes[0]->mesh->material;
            shouldLoadFile = false;
        }
        
        updateInput(window);
        
        glClearColor(0.0, 0.0, 0.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glClearColor(0.0, 0.0, 0.0, 0.0);
        
        light->properties.position = glm::translate(glm::mat4(1.0f), glm::vec3(-2.5f +  cosf(glfwGetTime()), 0.5f, -0.0f)) * glm::vec4(1.0f);
        
        
        camera.position = movement->position;
        camera.target = camera.position + movement->lookatDirection;
        camera.update();
        
        renderer.proj = camera.getCameraProjectionTransform();
        renderer.view = camera.getCameraViewTransform();
        renderer.renderScene();
        
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    
    delete movement;
    
    glfwDestroyWindow(window);
    glfwTerminate();
    exit(EXIT_SUCCESS);
}
Esempio n. 5
0
Raytracer::Raytracer(): texture(0)
{
    prepareTargetBuffer(128,128);

    std::shared_ptr<Clump3d> clump(new Clump3d);

    //
    // Cube
    //

    std::shared_ptr<MeshGeometry3d> geom1(new MeshGeometry3d());
    cube(geom1->getMesh());
    geom1->meshChanged();
    geom1->setColor(GAL::P4d(1.0, 0.0, 0.0, 1.0));

    std::shared_ptr<MeshGeometry3d> geom5(new MeshGeometry3d());
    cube(geom5->getMesh());
    geom5->meshChanged();
    geom5->setColor(GAL::P4d(0.0, 0.7, 1.0, 1.0));

    //
    // Manifold
    //

    std::shared_ptr<MeshGeometry<Vertex3d> > geom3(new MeshGeometry<Vertex3d>());
    manifold(geom3->getMesh());
    geom3->meshChanged();
    geom3->setColor(GAL::P4d(1.0, 1.0, 0.0, 1.0));

    //
    // Sphere
    //

    std::shared_ptr<SphereGeometry3d> geom2(new SphereGeometry3d(0.5));
    geom2->setColor(GAL::P4d(0.0, 1.0, 1.0, 1.0));

    std::shared_ptr<SphereGeometry3d> geom4(new SphereGeometry3d(0.25));
    geom4->setColor(GAL::P4d(1.0, 0.8, 0.0, 1.0));

    clump->addGeometry(geom1);
    clump->addGeometry(geom2);
    clump->addGeometry(geom3);
    clump->addGeometry(geom4);
    clump->addGeometry(geom5);

    geom1->setTranslation(GAL::P3d(-1,-1,0));
    geom2->setTranslation(GAL::P3d(0,1,0));

    geom3->setTranslation(GAL::P3d(1,0.5,0));
    geom3->setLocalTransform(GAL::EulerRotationX(90.0), 0);

    geom4->setTranslation(GAL::P3d(1.2,1,0.8));

    geom5->setTranslation(GAL::P3d(0,1,-3));
    geom5->setLocalTransform(GAL::EulerRotationY(10.0), 0);

    scene.addClump(clump);

    std::shared_ptr<Light3d> light1(new Light3d());
    light1->setPosition(GAL::P3d(1,4,-1));
    light1->setDiffuseColor(GAL::P3d(0.7, 0.7, 0.7));
    light1->setSpecularColor(GAL::P3d(1.0, 1.0, 1.0));
    light1->setShadow(true);
    light1->setSoftShadowWidth(0.05);
    scene.addLight(light1);

    std::shared_ptr<Light3d> light2(new Light3d());
    light2->setPosition(GAL::P3d(-1,4,3));
    light2->setDiffuseColor(GAL::P3d(0.7, 0.7, 0.7));
    light2->setSpecularColor(GAL::P3d(1.0, 1.0, 1.0));
    light2->setShadow(true);
    light2->setSoftShadowWidth(0.05);
    scene.addLight(light2);

    camera.setFrustum(-1, 1, -1, 1, -1, -100);

    camera.setTranslation(GAL::P3d(1,2,3));
    camera.setLocalTransform(GAL::EulerRotationX(30.0) * GAL::EulerRotationY(15.0), 0);
    camera.setFSAA(true);
    camera.setRecursionDepth(3);
}
Esempio n. 6
0
void buildSceneSphere(Camera* &cam, Film* &film,
                      std::vector<Shape*>* &objectsList,
                      std::vector<PointLightSource>* &lightSourceList)
{
    /* **************************** */
    /* Declare and place the camera */
    /* **************************** */
    // By default, this gives an ID transform
    //  which means that the camera is located at (0, 0, 0)
    //  and looking at the "+z" direction
    Matrix4x4 cameraToWorld;
    double fovDegrees = 60;
    double fovRadians = Utils::degreesToRadians(fovDegrees);
    cam = new PerspectiveCamera(cameraToWorld, fovRadians, *film);

    /* ************************** */
    /* DEFINE YOUR MATERIALS HERE */
    /* ************************** */
    // (...)
    //  EXAMPLE:  Material *green_50 = new Phong (Vector3D(0.2, 0.7, 0.3), Vector3D(0.2, 0.6, 0.2), 50);

	Material *green_50 = new Phong(Vector3D(0.2, 0.7, 0.3), Vector3D(0.2, 0.6, 0.2), 50);
	Material *red_50 = new Phong(Vector3D(0.7, 0.2, 0.3), Vector3D(0.6, 0.2, 0.2), 50);
	Material *blue_50 = new Phong(Vector3D(0.3, 0.2, 0.7), Vector3D(0.2, 0.2, 0.6), 50);

    /* ******* */
    /* Objects */
    /* ******* */
    // Create a heterogeneous list of objects of type shape
    // (some might be triangles, other spheres, plans, etc)
    objectsList = new std::vector<Shape*>;

    // Define and place a sphere
    Matrix4x4 sphereTransform1;
    sphereTransform1 = sphereTransform1.translate(Vector3D(-1.0, -0.5, 2*std::sqrt(2.0)));
    Shape *s1 = new Sphere (0.25, sphereTransform1, green_50);

    // Define and place a sphere
    Matrix4x4 sphereTransform2;
    sphereTransform2 = sphereTransform2.translate(Vector3D(1.0, 0.0, 6));
    Shape *s2 = new Sphere (1, sphereTransform2, red_50);

    // Define and place a sphere
    Matrix4x4 sphereTransform3;
    sphereTransform3 = sphereTransform3.translate(Vector3D(0.3, -0.75, 3.5));
    Shape *s3 = new Sphere (0.25, sphereTransform3, blue_50);

    // Store the objects in the object list
    objectsList->push_back(s1);
    objectsList->push_back(s2);
    objectsList->push_back(s3);


    /* ****** */
    /* Lights */
    /* ****** */
    //
    // ADD YOUR LIGHT SOURCES HERE
	PointLightSource light1(Vector3D(5, 0, 0), Vector3D(50, 50, 50));
	PointLightSource light2 (Vector3D(0, 5, 0), Vector3D(50, 50, 50));
	//PointLightSource *light3 = new PointLightSource(Vector3D(0, 0, 20), Vector3D(0, 0, 20));
    
    // DO NOT FORGET TO STORE THE LIGHT SOURCES IN THE "lightSourceList"
	lightSourceList->push_back(light1);
	lightSourceList->push_back(light2);
    
}
Esempio n. 7
0
std::shared_ptr<gx::scene> universe_bootstrap( gx::render_context* render_context, d3d11::system_context context, std::shared_ptr<fnd::universe> universe )
{

    auto scene = std::make_shared<gx::scene>();
    auto entities = std::make_shared<gx::entity_world> ();
    auto dynamic_entities = std::make_shared<fnd::typed_world> ();

    //add two worlds
    //1. with entities, created or loaded
    //2. dynamic entities which change depending on time
    //3. add a spatial structure for representing these entities
    universe->add_world(entities);
    universe->add_world(dynamic_entities);
    universe->add_world(scene);

    //room
    std::ifstream f("giroom.am", std::ios_base::in | std::ios_base::binary);

    std::future< std::shared_ptr<room_entity> > future_room = std::async( std::launch::async, [&]()
    {
        return create_room_entity(context.m_device.get(), render_context->get_shader_database(), f);
    });

    //auto room_entity = create_room_entity(context.m_device.get(), render_context->get_shader_database(), f);


    //floor
    auto floor_entity = create_floor_entity( context.m_device.get(), render_context->get_shader_database(), 20, 20, 30 );

    //directional light
    std::vector<directional_light> directional_lights;
    directional_lights.reserve(8);

    math::float4 light_position = math::set( 5.0f, 1.0f, 5.0f, 1.0f );

    directional_lights.push_back(  directional_light( gx::color::white(), math::sub(light_position, math::set(0.0f, 0.0f, 0.0f, 1.0f) ) ));

    //should be quad
    auto directional_entity = create_directional_lights_entity( context.m_device.get(), render_context->m_screen_space_render_data.m_screen_space_vertex_buffer, &directional_lights[0], &directional_lights[0] + directional_lights.size()  );

    //create point light entity and insert it into two worlds (for update and render)
    auto point_lights = create_point_lights_entity( context.m_device.get() );
    auto light_updater = create_light_entity_updater();
    dynamic_entities->add_type( 0, light_updater );


    point_light light1 ( math::set(0.3f, 0.2f, 0.0f, 0.0f), light_position, 1.0f, 0.5f);

    point_lights->add_light(light1);

    auto it = scene->begin();
    auto it_end = scene->end();

    auto pi_div_two		= 1.570796327f;

    //auto m_1 = math::rotation_x(-pi_div_two);
    //auto m_2 = math::translation(-1.0f, 0.0f, 0.0f);
    auto m_4 = math::translation(0.0f, 0.0f, 0.0f);
    
    auto root = scene->get_root();

    //auto entity_1 = gxu::create_lat_lon_sphere_entity<gx::lambert_shift_invairant_material_factory>( render_context, 1.0f, 20, gx::color::blue() ); 

    auto sphere_1 = gxu::create_lat_lon_sphere_entity<gx::blinn_phong_shift_invairant_material_factory>( render_context->get_device(), render_context->get_shader_database(), 1.0f, 20 , gx::color::green() , math::set(0.05f, 0.05f, 0.05f, gx::encode_specular_power(2 * 25.0f) )  ); 
    auto sphere_2 = gxu::create_lat_lon_sphere_entity<gx::blinn_phong_shift_invairant_material_factory>( render_context->get_device(), render_context->get_shader_database(), 1.0f, 20 , gx::color::green() , math::set(0.05f, 0.05f, 0.05f, gx::encode_specular_power(2 * 50.0f) )  ); 
    auto sphere_3 = gxu::create_lat_lon_sphere_entity<gx::blinn_phong_shift_invairant_material_factory>( render_context->get_device(), render_context->get_shader_database(), 1.0f, 20 , gx::color::green() , math::set(0.05f, 0.05f, 0.05f, gx::encode_specular_power(2 * 75.0f) )  ); 
    auto sphere_4 = gxu::create_lat_lon_sphere_entity<gx::blinn_phong_shift_invairant_material_factory>( render_context->get_device(), render_context->get_shader_database(), 1.0f, 20 , gx::color::green() , math::set(0.05f, 0.05f, 0.05f, gx::encode_specular_power(2 * 100.0f) )  ); 

    auto node_1 = std::make_shared<gx::scene::node> ( m_4, sphere_1.get() );
    auto node_2 = std::make_shared<gx::scene::node> ( m_4, point_lights.get() );
    auto node_3 = std::make_shared<gx::scene::node> ( m_4, directional_entity.get() );
    auto node_4 = std::make_shared<gx::scene::node> ( math::translation(0.0f, -2.0f, 0.0f) , floor_entity.get() );

    auto room_entity  = future_room.get();
    auto node_5 = std::make_shared<gx::scene::node> ( math::translation(0.0f, -2.0f, 0.0f), room_entity.get() );

    auto node_6 = std::make_shared<gx::scene::node> ( math::translation(2.0f, 0.0f, 0.0f), sphere_2.get() );
    auto node_7 = std::make_shared<gx::scene::node> ( math::translation(4.0f, 0.0f, 0.0f), sphere_3.get() );
    auto node_8 = std::make_shared<gx::scene::node> ( math::translation(6.0f, 0.0f, 0.0f), sphere_4.get() );


    //1. add to graphic world
    gx::add_node(root, node_1);
    gx::add_node(root, node_2);
    gx::add_node(root, node_3);
    gx::add_node(root, node_4);
    gx::add_node(root, node_5);
    gx::add_node(root, node_6);
    gx::add_node(root, node_7);
    gx::add_node(root, node_8);
    
    //2. add to entities world
    entities->add_entity( point_lights);
    entities->add_entity( directional_entity );
    entities->add_entity( floor_entity );
    entities->add_entity( room_entity );

    entities->add_entity( sphere_1);
    entities->add_entity( sphere_2);
    entities->add_entity( sphere_3);
    entities->add_entity( sphere_4);

    scene->rebuild();

    //3. add lights for updating
    light_updater->add_item(point_lights);
    
    return scene;
}