示例#1
0
void project1() {
	// Create scene
	Scene scn;
	scn.SetSkyColor(Color(0.8f, 0.9f, 1.0f));

	// Create boxes
    LambertMaterial lambert1;
    lambert1.SetDiffuseColor(Color(0.3f,0.3f,0.3f));

	MeshObject box1;
	box1.MakeBox(5.0f,0.1f,5.0f, &lambert1);
	scn.AddObject(box1);
    
    
    LambertMaterial lambert2;
    lambert2.SetDiffuseColor(Color(0.7f,0.7f,0.7f));
	MeshObject box2;
	box2.MakeBox(1.0f,1.0f,1.0f, &lambert2);
    
	InstanceObject inst1(box2);
	Matrix34 mtx;
	mtx.MakeRotateX(0.5f);
	mtx.d.y=1.0f;
	inst1.SetMatrix(mtx);
	scn.AddObject(inst1);
    
	InstanceObject inst2(box2);
	mtx.MakeRotateY(1.0f);
	mtx.d.Set(-1.0f,0.0f,1.0f);
	inst2.SetMatrix(mtx);
	scn.AddObject(inst2);
    
	// Create lights
	DirectLight sunlgt;
	sunlgt.SetBaseColor(Color(1.0f, 1.0f, 0.9f));
	sunlgt.SetIntensity(0.5f);
	sunlgt.SetDirection(Vector3(-0.5f, -1.0f, -0.5f));
	scn.AddLight(sunlgt);
    
	PointLight redlgt;
	redlgt.SetBaseColor(Color(1.0f, 0.2f, 0.2f));
	redlgt.SetIntensity(2.0f);
	redlgt.SetPosition(Vector3(2.0f, 2.0f, 0.0f));
	scn.AddLight(redlgt);
    
	// Create camera
	Camera cam;
	cam.LookAt(Vector3(2.0f,2.0f,5.0f), Vector3(0.0f,0.0f,0.0f));
	cam.SetResolution(800,600);
	cam.SetFOV(40.0f);
	cam.SetAspect(1.33f);
    
	// Render image
	cam.Render(scn);
	cam.SaveBitmap("project1.bmp");
}
示例#2
0
void setTorchLight(float x, float y, float z, float radius) {
	//PLight
	PointLight p;
	p.SetColor(fireLightColor[0], fireLightColor[1], fireLightColor[2]);
	p.SetPosition(x, y, z);
	p.SetAttenuation(0.01, 0.1, 0.1);
	//p.SetAttenuation(1.0, 0.05, 0.0);
	p.SetRadius(radius);
	p.SetGrain(10);

	plights.push_back(p);  
}
示例#3
0
文件: MainForm.cpp 项目: dgi09/D3D
void MainForm::OnAddPointLight_Click(wxMouseEvent & evt)
{
	PointLightPtr ptr = scene->AddPointLight();
	PointLight * p = ptr.Get();
	p->SetPosition(0.0f, 0.0f, 0.0f);
	p->SetDiffuse(1.0f,1.0f,1.0f,1.0f);
	p->SetRange(100.0f);

	EditorPointLight * pl = new EditorPointLight(ptr);
	EditorSceneObjectsManager::GetPtr()->AddElement(pl);

	ActiveTool::Set(new SelectTool());
}
示例#4
0
void LoadLight(TiXmlElement *element)
{
    Light *light = NULL;
    
    // name
    const char* name = element->Attribute("name");
    printf("Light [");
    if ( name ) printf("%s",name);
    printf("]");
    
    // type
    const char* type = element->Attribute("type");
    if ( type ) {
        if ( COMPARE(type,"ambient") ) {
            printf(" - Ambient\n");
            AmbientLight *l = new AmbientLight();
            light = l;
            for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) {
                if ( COMPARE( child->Value(), "intensity" ) ) {
                    Color c(1,1,1);
                    ReadColor( child, c );
                    l->SetIntensity(c);
                    printf("   intensity %f %f %f\n",c.r,c.g,c.b);
                }
            }
        } else if ( COMPARE(type,"direct") ) {
            printf(" - Direct\n");
            DirectLight *l = new DirectLight();
            light = l;
            for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) {
                if ( COMPARE( child->Value(), "intensity" ) ) {
                    Color c(1,1,1);
                    ReadColor( child, c );
                    l->SetIntensity(c);
                    printf("   intensity %f %f %f\n",c.r,c.g,c.b);
                } else if ( COMPARE( child->Value(), "direction" ) ) {
                    Point3 v(1,1,1);
                    ReadVector( child, v );
                    l->SetDirection(v);
                    printf("   direction %f %f %f\n",v.x,v.y,v.z);
                }
            }
        } else if ( COMPARE(type,"point") ) {
            printf(" - Point\n");
            PointLight *l = new PointLight();
            light = l;
            for ( TiXmlElement *child = element->FirstChildElement(); child!=NULL; child = child->NextSiblingElement() ) {
                if ( COMPARE( child->Value(), "intensity" ) ) {
                    Color c(1,1,1);
                    ReadColor( child, c );
                    l->SetIntensity(c);
                    printf("   intensity %f %f %f\n",c.r,c.g,c.b);
                } else if ( COMPARE( child->Value(), "position" ) ) {
                    Point3 v(0,0,0);
                    ReadVector( child, v );
                    l->SetPosition(v);
                    printf("   position %f %f %f\n",v.x,v.y,v.z);
                }
            }
        } else {
            printf(" - UNKNOWN\n");
        }
    }
    
    if ( light ) {
        light->SetName(name);
        lights.push_back(light);
    }
    
}
示例#5
0
文件: World.cpp 项目: aleios/3dapp
World::World(sf::RenderWindow& inWindow)
: window(inWindow), gbuffer(WINDOW_WIDTH, WINDOW_HEIGHT), hdrDownsampleTexture(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, true), hdrBlurTexture(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, true), hdrDownsample2Texture(WINDOW_WIDTH / 4, WINDOW_HEIGHT / 4, true), hdrBlur2Texture(WINDOW_WIDTH / 4, WINDOW_HEIGHT / 4, true), fullscreenQuad(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT), aaTexture(WINDOW_WIDTH, WINDOW_HEIGHT)
{
    // OpenGL State
    glBlendEquation(GL_FUNC_ADD);
    glBlendFunc(GL_ONE, GL_ONE);
    glStencilOpSeparate(GL_BACK, GL_KEEP, GL_INCR_WRAP, GL_KEEP);
    glStencilOpSeparate(GL_FRONT, GL_KEEP, GL_DECR_WRAP, GL_KEEP);

    // Seed the PRNG.
    srand(static_cast<unsigned int>(time(0)));

    // Load light meshes.
    pointLightMesh.LoadFromFile("Models/pointlightMesh.obj");
    spotLightMesh.LoadFromFile("Models/spotlightMesh.obj");
    directionalLightMesh.LoadFromFile("Models/directionalMesh.obj");

    // Lighting Shaders.
    pointLightShader.LoadFromFile("Shaders/lightShader.vert", "Shaders/pointlightShader.frag");
    spotLightShader.LoadFromFile("Shaders/lightShader.vert", "Shaders/spotlightShader.frag");
    directionalLightShader.LoadFromFile("Shaders/lightShader.vert", "Shaders/directionalShader.frag");

    // HDR Shaders.
    hdrDownsample.LoadFromFile("Shaders/fsquad.vert", "Shaders/hdrDownsample.frag");
    hdrTonemap.LoadFromFile("Shaders/fsquad.vert", "Shaders/hdrTonemap.frag");
    hdrBlur.LoadFromFile("Shaders/fsquad.vert", "Shaders/hdrBlur.frag");
    hdrBrightness.LoadFromFile("Shaders/fsquad.vert", "Shaders/hdrBrightness.frag");

    // Load Models.
    mdl2.LoadFromFile("Models/sphere.obj");
    mdl.LoadFromFile("Models/floor.obj");
    mdl3.LoadFromFile("Models/sphere.obj");
    quadMesh.LoadFromFile("Models/quad.obj");

    // Load Shaders
    basicShader.LoadFromFile("Shaders/basic.vert", "Shaders/basic.frag");
    pass1Shader.LoadFromFile("Shaders/drpass1.vert", "Shaders/drpass1.frag");
    pass2Shader.LoadFromFile("Shaders/drpass2.vert", "Shaders/drpass2.frag");
    pass2ShaderDirectional.LoadFromFile("Shaders/empty.vert", "Shaders/drpass2directional.frag");
    quadShader.LoadFromFile("Shaders/fsquad.vert", "Shaders/hdrTonemap.frag");
    terrainShader.LoadFromFile("Shaders/terrainshader.vert", "Shaders/terrainshader.frag");
    flatShader.LoadFromFile("Shaders/basic.vert", "Shaders/flatshade.frag");
    emptyShader.LoadFromFile("Shaders/empty.vert", "Shaders/empty.frag");
    sphereShader.LoadFromFile("Shaders/sphere.vert", "Shaders/sphere.frag");

    // Anti-Aliasing Shaders
#if USE_FXAA == 1
    fxaaShader.LoadFromFile("Shaders/fxaa.vert", "Shaders/fxaa.frag");
#endif
    baseAAShader.LoadFromFile("Shaders/baseaa.vert", "Shaders/baseaa.frag");

    // Load textures.
    mdlTex.loadFromFile("Textures/test.png");
    terrainTexture.loadFromFile("Textures/terraintex.png");
    terrainWater.loadFromFile("Textures/terrainwater.png");

    // Main world directional light.
    DirectionalLight worldLight;
    worldLight.SetDirection(Vector3(-1.0f, 0.0f, 0.0f));
    worldLight.SetColor(Color(0.8f, 0.8f, 0.8f));
    worldLight.SetDiffuseIntensity(0.9f);
    worldLight.SetAmbientIntensity(0.4f);
    directionalLights.push_back(worldLight);

    // Lights
    const unsigned int numLights = 40;
    const float lightRadius = 2.0f;
    PointLight l;
    l.SetPosition(Vector3(10.0f, 2.0f, 10.0f));
    l.SetScale(Vector3(3.0f, 3.0f, 3.0f));
    l.SetColor(Color(0.0f, 0.0f, 1.4f, 1.0f));
    l.SetAttenuationConstant(0.4f);
    l.SetDiffuseIntensity(0.4f);
    l.SetAmbientIntensity(0.2f);
    pointLights.push_back(l);

    const int maxXZ = 15;
    const int minXZ = -15;
    const int maxY = 5;
    const float minRad = 0.3f;
    const float maxRad = 2.0f;
    for (int i = 0; i < numLights; i++)
    {
        PointLight l;
        
        // Generate position.
        int x = (rand() % (maxXZ - minXZ)) + minXZ;
        int y = (rand() % maxY);
        int z = (rand() % (maxXZ - minXZ)) + minXZ;
        l.SetPosition(Vector3(x, y, z));

        // Generate Scale
        float frand = ((float)rand() / (float)RAND_MAX);
        float fdiff = maxRad - minRad;

        //l.SetScale(Vector3(scale, scale, scale));

        // Generate Color
        float r = float_rand(0.0f, maxCol);
        float g = float_rand(0.0f, maxCol);
        float b = float_rand(0.0f, maxCol);
        l.SetColor(Color(r, g, b, 1.0f));

        float ambient = ((float)rand() / (float)RAND_MAX);
        float diff = ((float)rand() / (float)RAND_MAX);

        l.SetAmbientIntensity(ambient);
        l.SetDiffuseIntensity(diff);

        float catt = ((float)rand() / (float)RAND_MAX);
        float latt = ((float)rand() / (float)RAND_MAX);
        float eatt = ((float)rand() / (float)RAND_MAX);

        float mc = fmax(fmax((float)r, (float)g), (float)b);
        float ret = (-latt + sqrtf(latt * latt - 4 * eatt * (eatt - 256 * mc * diff))) / 2 * eatt;

        l.SetScale(Vector3(ret, ret, ret));

        l.SetAttenuationConstant(catt);
        l.SetAttenuationLinear(latt);
        l.SetAttenuationExponential(eatt);

        pointLights.push_back(l);
    }

    // Set the initial position of the sphere
    spherePos = Vector3(10.0f, 3.0f, 10.0f);

    // Setup the help text.
    defaultFont.loadFromFile("arial.ttf");
    helpText.setFont(defaultFont);
    helpText.setString("Controls:\n" \
                       "Mouse: Camera direction.\n" \
                       "WASD: Camera movement.\n" \
                       "I: Randomize light attenuations.\n" \
                       "K: Randomize light color and intensity.\n" \
                       "Insert: Disable mouse.\n" \
                       "Delete: Switch shown render target.\n" \
                       "Home: Disable/Enable terrain movement.\n"
                       );
    helpText.setCharacterSize(14);
    helpText.setColor(sf::Color(255, 0, 255, 255));

    targetText.setFont(defaultFont);
    targetText.setCharacterSize(14);
    targetText.setPosition(sf::Vector2f(0.0, window.getSize().y - 25));
    targetText.setString("Current Buffer: Final Output");
    targetText.setColor(sf::Color(255, 0, 255, 255));

    // Sounds
    sndExplodeBuffer.loadFromFile("explode.wav");
    sndExplode.setBuffer(sndExplodeBuffer);
    sndExplode.setAttenuation(10.0f);
    sndExplode.setMinDistance(60.0f);
    sndExplode.setPosition(sf::Vector3f(0.0f, 1.0f, 0.0f));
}
示例#6
0
void Dragon_Dogs_ParticleSystem::Init(Scene * scene)
{

    StaticEntityPtr skyPtr = scene->AddStaticEntity("assets/sphere.smodel", "sky");
    StaticEntity * sky = skyPtr.Get();
    sky->Illuminate(false);
    sky->SetScale(10000, 10000, 10000);

    Material * m = sky->GetMaterial(0);
    m->SetDiffuseMap("assets/risinger.jpg");
    m->UseDiffuseMap(true);

    psPtr = scene->AddParticleSystem();
    ParticleSystem * ps = psPtr.Get();
    ps->SetColorTexture("assets/fire.png");
    ps->SetEmmiterPosition(Vector3(45.0f, 0.0f, 0.0f));
    ps->SetMinSpawnPosition(Vector3(-1.0f, 0.0f, 0.0f));
    ps->SetMaxSpawnPosition(Vector3(1.0f, 0.0f, 0.0f));
    ps->SetMaxLifeTime(70);
    ps->SetMinLifeTime(50);
    ps->SetMaxParticles(70);
    ps->SetSpawnTime(4.0);
    ps->SetMinSize(10);
    ps->SetMaxSize(14);
    ////entitiy->SetPosition(10.0f,0.0f,0.0f);


    drPtr = scene->AddParticleSystem();
    ps = drPtr.Get();

    ps->SetColorTexture("assets/fire.png");
    ps->SetEmmiterPosition(Vector3(-30.0f, 150.0, 10.0f));
    ps->SetMinSpawnPosition(Vector3(-0.2f, 0.3f, 0.0f));
    ps->SetMaxSpawnPosition(Vector3(0.2f, -0.3f, 0.0f));
    ps->SetMinSpawnVelocity(Vector3(-1.0f, -2.0f, -1.8f));
    ps->SetMaxSpawnVelocity(Vector3(1.0f, -2.0f, -1.0f));
    ps->SetMaxLifeTime(30);
    ps->SetMinLifeTime(45);
    ps->SetMaxParticles(70);
    ps->SetSpawnTime(4.0);
    ps->SetMinSize(10);
    ps->SetMaxSize(14);
    ps->AllowParticleAdd(false);

    plDragon = scene->AddPointLight();
    PointLight * plD = plDragon.Get();
    plD->SetDiffuse(1.0f, 0.4f, 0.0f, 1.0f);
    plD->SetRange(1.0f);
    plD->SetPosition(-30.0f, 150.0, 10.0f);

    dragonTime = 0;
    dragonOn = false;
    enPtr = scene->AddAnimatedEntity("assets/ninja.model", "ninja");

    AnimatedEntity * en = enPtr.Get();
    en->SetPosition(0.0f, 0.0f, 0.0f);
    en->Illuminate(true);
    en->SetScale(10.0f, 10.0f, 10.0f);
    en->SetPosZ(-100);
    //en->SetRotationY(90.0f);

    Material * mat = en->GetMaterial(0);
    mat->UseDiffuseMap(true);
    mat->SetDiffuseColor(Color::White());
    mat->SetDiffuseMap("assets/nskin.jpg");

    mat->SetEmmisivePower(1);
    mat->SetSpecularPower(0);
    mat->SetSpecularIntesity(0);

    mat = en->GetMaterial(1);
    mat->UseDiffuseMap(false);
    mat->SetEmmisivePower(1);
    mat->SetSpecularIntesity(0);
    mat->SetSpecularPower(0);
    mat->SetDiffuseColor(Color(0.3f, 0.3f, 0.3f, 1.0f));

    pointLight = scene->AddPointLight();

    PointLight * p = pointLight.Get();
    p->SetDiffuse(1.0f, 0.4f, 0.0f, 1.0f);
    p->SetRange(500.0f);
    p->SetPosition(45.0f, 4.0f, 0.0f);

    planePtr = scene->AddStaticEntity("assets/plane.model", "plane");
    StaticEntity * plane = planePtr.Get();
    plane->Illuminate(true);
    plane->SetScale(500.0f, 1.0f, 500.0f);

    Material * pMat = plane->GetMaterial(0);
    pMat->UseDiffuseMap(false);
    pMat->SetDiffuseColor(Color(0.3f, 0.3f, 0.3f, 1.0f));
    pMat->SetEmmisivePower(1);
    pMat->SetSpecularPower(0);
    pMat->SetSpecularIntesity(0);

    plane->SetPosition(0.0f, -7.0f, 0.0f);

    dogPtr = scene->AddStaticEntity("assets/dog.smodel", "dog");
    StaticEntity * dog = dogPtr.Get();
    dog->Illuminate(true);
    dog->SetPosition(80.0f, 10.0, 60.0f);
    dog->SetScale(400.0f, 400.0f, 400.0f);
    dog->SetRotation(0.0f, 60.0f, 180.0f);
    dog->SetRotationY(45.0f);

    pMat = dog->GetMaterial(0);
    pMat->SetDiffuseColor(Color(0.2f, 0.2f, 0.2f, 1.0f));

    dogPtr2 = scene->AddStaticEntity("assets/dog.smodel", "dog");
    dog = dogPtr2.Get();

    dog->Illuminate(true);
    dog->SetPosition(-100.0f, 10.0, 60.0f);
    dog->SetScale(400.0f, 400.0f, 400.0f);
    dog->SetRotation(0.0f, -60.0f, 180.0f);
    //dog->SetRotationY(45.0f);

    pMat = dog->GetMaterial(0);
    pMat->SetDiffuseColor(Color(0.2f, 0.0f, 0.0f, 1.0f));

    lightChangeTime = 0.0f;

    powerMin = 0.6f;
    powerMax = 1.0f;

    animContr = enPtr.Get()->LoadAnimationFromFile("assets/ninja.anim", "animPack");
    enPtr.Get()->SetCurrentAnimationController(animContr);
    animContr->AddAnimationState(0, 14, LOOP, "idle");
    animContr->SetCurrentAnimationState("idle");

    torchPtr = scene->AddStaticEntity("assets/smaug.smodel", "m24");
    StaticEntity * torch = torchPtr.Get();
    torch->SetPosition(-30.0f, 100.0, 70.0f);
    torch->SetScale(3.0f, 3.0f, 3.0f);
    torch->Illuminate(true);

    mat = torch->GetMaterial(0);
    mat->SetEmmisivePower(1);
    mat->SetSpecularPower(1);

    treePtr = scene->AddStaticEntity("assets/tree.smodel", "tree");
    StaticEntity * tree = treePtr.Get();

    tree->SetPosition(150.0f, 23.0f, 0.0f);
    tree->SetScale(10.0f, 10.0f, 10.0f);
    tree->Illuminate(true);


    mat = tree->GetMaterial(0);
    mat->UseDiffuseMap(true);
    mat->SetDiffuseMap("assets/w3.jpg");

    swordPtr = scene->AddStaticEntity("assets/sword.smodel", "sword");
    StaticEntity * sword = swordPtr.Get();
    sword->SetPosition(150.0f, 80.0f, 0.0f);
    sword->Illuminate(false);
    sword->SetScale(2.0f, 2.0f, 2.0f);
    sword->SetRotationZ(180.0f);
    mat = sword->GetMaterial(0);
    mat->UseDiffuseMap(false);
    mat->SetDiffuseColor(Color::White());
    mat->SetEmmisivePower(20);


}
示例#7
0
void project2() {
    // Create scene
    Scene scn;
    scn.SetSkyColor(Color(0.8f, 0.8f, 1.0f));
    
    // Create ground
    MeshObject ground;
    ground.MakeBox(5.0f,0.1f,5.0f);
    scn.AddObject(ground);
    
    // Create dragon
    MeshObject dragon;
    dragon.LoadPLY("dragon.ply");
    dragon.Smooth();
    
    std::chrono::time_point<std::chrono::system_clock> start, end;
    start = std::chrono::system_clock::now();
    
    std::cout <<  "Constructing the tree ..." << std::endl;
    
    BoxTreeObject tree;
    tree.Construct(dragon);
    scn.AddObject(tree);
    
    end = std::chrono::system_clock::now();
    std::chrono::duration<double> elapsed_seconds = end-start;
    
    std::cout << "Tree construcion done in " << elapsed_seconds.count() * 1000 << " milliseconds" <<  std::endl;

    
    // Create instance
    InstanceObject inst(tree);
    Matrix34 mtx;
    mtx.MakeRotateY(PI);
    mtx.d.Set(-0.05f,0.0f,-0.1f);
    inst.SetMatrix(mtx); scn.AddObject(inst);

    // Create lights
    DirectLight sunlgt;
    sunlgt.SetBaseColor(Color(1.0f, 1.0f, 0.9f));
    sunlgt.SetIntensity(1.0f);
    sunlgt.SetDirection(Vector3(2.0f, -3.0f, -2.0f));
    scn.AddLight(sunlgt);

    PointLight redlgt;
    redlgt.SetBaseColor(Color(1.0f, 0.2f, 0.2f));
    redlgt.SetIntensity(0.02f);
    redlgt.SetPosition(Vector3(-0.2f, 0.2f, 0.2f));
    scn.AddLight(redlgt);

    PointLight bluelgt;
    bluelgt.SetBaseColor(Color(0.2f, 0.2f, 1.0f));
    bluelgt.SetIntensity(0.02f);
    bluelgt.SetPosition(Vector3(0.1f, 0.1f, 0.3f));
    scn.AddLight(bluelgt);

    // Create camera
    Camera cam;
    cam.LookAt(Vector3(-0.1f,0.1f,0.2f),Vector3(-0.05f,0.12f,0.0f));
    cam.SetFOV(40.0f);
    cam.SetAspect(1.33f);
    cam.SetResolution(800,600);

    start = std::chrono::system_clock::now();
    std::cout <<  "Rendering the scene ..." << std::endl;
    
    // Render image
    cam.Render(scn);
    cam.SaveBitmap("project2.bmp");
    
    end = std::chrono::system_clock::now();
    elapsed_seconds = end-start;
    
    std::cout << "Scene rendering done in " << elapsed_seconds.count() * 1000 << " milliseconds" <<  std::endl;

}