int DirXParticle::AddNewEmitter(vec3 pPosition, vec3 pColor, float pLifeTime, float pDensity, vec3 pVelocity, float pParticleLifeTime, float pSpeedMulti, float pSpread, float pStartSize, float pTextureID) { for (size_t i = 0; i < mEmitters.size(); i++) { if (mEmitters[i].LifeTime < 0) { mEmitters[i] = Emitter(pPosition, pColor, pLifeTime, pDensity, pVelocity, pParticleLifeTime, pSpeedMulti, pSpread, pStartSize, pTextureID); return i; } } mEmitters.push_back(Emitter(pPosition, pColor, pLifeTime, pDensity, pVelocity, pParticleLifeTime, pSpeedMulti, pSpread, pStartSize, pTextureID)); return mEmitters.size() - 1; }
//Setters void Bullet::setHit(bool hit) { hit_ = hit; if (rocket_) { rocketEmitter_.setParticlesToEmit(0); bullet_.setFillColor(sf::Color::Transparent); explosionEmitter_ = Emitter(true, positionGlobal_, true, 300, 3000, sf::Vector2f(5.0f, 5.0f), sf::Vector2f(50.0f, 50.0f), 0.0f, 360.0f, 0.0f, 850.0f, -3000.0f, 0.75f, 1.25f, sf::Color(255, 225, 50, 255), sf::Color(250, 250, 250, 255)); } }
Doe::Doe(float x, float y) { m_health = 30; m_position.x = x; m_position.y = y; m_bodyTexture.loadFromFile("Assets/Graphics/NPC/bodyDoeNPC.png"); m_bodyTexture.setSmooth(true); m_bodySprite.setTexture(m_bodyTexture); m_bodySprite.setOrigin(m_bodySprite.getLocalBounds().width / 2, m_bodySprite.getLocalBounds().height / 2); m_bodySprite.setRotation(0); m_bodySprite.setPosition(m_position.x, m_position.y); m_headTexture.loadFromFile("Assets/Graphics/NPC/headDoeNPC.png"); m_headTexture.setSmooth(true); m_headSprite.setTexture(m_headTexture); m_headSprite.setOrigin(25.0f, 34.0f); m_headSprite.setPosition(m_position.x, m_position.y - DistanceOfNeck); m_tailTexture.loadFromFile("Assets/Graphics/NPC/tailDoeNPC.png"); m_tailTexture.setSmooth(true); m_tailSprite.setTexture(m_tailTexture); m_tailSprite.setOrigin(7.0f, 3.0f); m_tailSprite.setPosition(m_position.x, m_position.y - DistanceOfTail); m_selectedTex.loadFromFile("Assets/Graphics/NPC/selectedDoe.png"); m_selectedTex.setSmooth(true); m_selectedSprite.setTexture(m_selectedTex); m_selectedSprite.setOrigin(m_selectedSprite.getLocalBounds().width / 2, m_selectedSprite.getLocalBounds().height / 2); m_selectedSprite.setPosition(m_position.x, m_position.y); m_colour = sf::Color::Yellow; m_emitter = Emitter(m_position.x, m_position.y, m_colour); //Sounds m_injuredBuffer.loadFromFile("Assets/Audio/NPC/Doe/doeSound.wav"); m_injuredSound.setBuffer(m_injuredBuffer); m_injuredSound.setRelativeToListener(false); //m_injuredSound.setPosition(200, 200, 0); m_injuredSound.setAttenuation(5); m_deathBuffer.loadFromFile("Assets/Audio/NPC/Doe/doeDead.wav"); m_deathSound.setBuffer(m_deathBuffer); m_deathSound.setRelativeToListener(false); m_deathSound.setAttenuation(10); }
Bullet::Bullet(const bool rocket, const sf::Vector2f& startPos, const sf::Vector2f& velocity, const int damage) :rocket_(rocket), velocity_(velocity), damage_(damage) { //Bullet visual description positionGlobal_ = startPos; lastPosition_ = positionGlobal_; bullet_.setRotation((float)atan2(velocity_.y, velocity_.x) / 3.14159265358f * 180); if (!rocket_) { bullet_.setSize(sf::Vector2f(15.0f, 3.3f)); bullet_.setFillColor(sf::Color(255, 204, 49, 255)); bullet_.setOrigin(bullet_.getLocalBounds().width / 2.0f, bullet_.getLocalBounds().height / 2.0f); //Critical if (std::rand() % 100 == 0) { damage_ *= 5; bullet_.setFillColor(sf::Color(255, 50, 50, 255)); } } else { shake_ = 30.0f; bullet_.setSize(sf::Vector2f(20.0f, 6.0f)); bullet_.setOrigin(10.0f, 3.0f); bullet_.setFillColor(sf::Color(50, 125, 50, 255)); rocketEmitter_ = Emitter(false, positionGlobal_, true, 1000, 100, sf::Vector2f(5.0f, 5.0f), sf::Vector2f(25.0f, 25.0f), -10.0f, 10.0f, 0.0f, 50.0f, -25.0f, 1.0f, 3.0f, sf::Color(200, 200, 155, 255), sf::Color(155, 155, 155, 255)); } }
// llvm_emit_code_for_current_function - Top level interface for emitting a // function to the .s file. void llvm_emit_code_for_current_function(tree fndecl) { if (cfun->nonlocal_goto_save_area) sorry("%Jnon-local gotos not supported by LLVM", fndecl); if (errorcount || sorrycount) { TREE_ASM_WRITTEN(fndecl) = 1; return; // Do not process broken code. } timevar_push(TV_LLVM_FUNCS); // Convert the AST to raw/ugly LLVM code. Function *Fn; { TreeToLLVM Emitter(fndecl); // Set up parameters and prepare for return, for the function. Emitter.StartFunctionBody(); // Emit the body of the function. Emitter.Emit(DECL_SAVED_TREE(fndecl), 0); // Wrap things up. Fn = Emitter.FinishFunctionBody(); } #if 0 if (dump_file) { fprintf (dump_file, "\n\n;;\n;; Full LLVM generated for this function:\n;;\n"); Fn->dump(); } #endif if (PerFunctionPasses) PerFunctionPasses->run(*Fn); // TODO: Nuke the .ll code for the function at -O[01] if we don't want to // inline it or something else. // There's no need to defer outputting this function any more; we // know we want to output it. DECL_DEFER_OUTPUT(fndecl) = 0; // Finally, we have written out this function! TREE_ASM_WRITTEN(fndecl) = 1; timevar_pop(TV_LLVM_FUNCS); }
Output::Output(Sass_Output_Options& opt) : Inspect(Emitter(opt)), charset(""), top_nodes(0) {}
bool basic_builder::BuildScene( string file_name, Camera &camera, Scene &scene ) const { int line_num = 0; char input_line[512]; Plugin *dat = NULL; // A container for arbitrary data. Object *obj = NULL; // The current object, which was the last object created. Shader *shd = NULL; // The current shader. Aggregate *agg = NULL; // Current aggregate object, to which all objects are now added. Envmap *env = NULL; // Current environment map. Material *mat = NULL; // Current material pointer. Material material; // Current material. scene.object = NULL; scene.envmap = NULL; scene.rasterize = NULL; // Attempt to open the input file. file_name += ".sdf"; std::ifstream fin; fin.open( file_name.c_str() ); if( fin.fail() ) { cerr << "Error: Could not open file " << file_name << endl; return false; // Report failure. } cout << "Reading " << file_name << "... "; cout.flush(); // Set some defaults. material.diffuse = White; material.emission = Black; material.specular = White; material.ambient = Black; material.reflectivity = Black; material.translucency = Black; material.ref_index = 0.0; material.Phong_exp = 0.0; camera.x_res = default_image_width; camera.y_res = default_image_height; camera.x_win = Interval( -1.0, 1.0 ); camera.y_win = Interval( -1.0, 1.0 ); // Process lines until the end of file is reached. // Print a warning for all lines that are unrecognizable. while( fin.getline( input_line, 512 ) ) { line_num++; if( Skip( input_line ) ) continue; // Ask each registered object if it recognizes the line. If it does, it will // create a new instance of the object and return it as the function value. Plugin *plg = Instance_of_Plugin( input_line ); if( plg != NULL ) { switch( plg->PluginType() ) { case data_plugin: if( obj == NULL ) cerr << "Error: data ignored. Line " << line_num << endl; else obj->AddData( obj ); break; case shader_plugin: shd = (Shader*)plg; break; case aggregate_plugin: obj = (Object*)plg; obj->shader = shd; obj->envmap = env; obj->material = Copy( mat, material ); obj->parent = agg; if( Emitter( material ) ) { cerr << "Error: An aggregate object cannot be an emitter. Line " << line_num << ": " << input_line << endl; return false; } if( agg != NULL ) // If there is alrealy an agg obj, this one is a child. { // agg->AddChild( obj ); // Do not add aggregates as children until they are complete. agg->material = Copy( mat, material ); } else if( scene.object == NULL ) scene.object = obj; agg = (Aggregate *)obj; break; case primitive_plugin: obj = (Object*)plg; obj->shader = shd; obj->envmap = env; obj->material = Copy( mat, material ); obj->parent = agg; if( Emitter( material ) ) scene.lights.push_back( obj ); if( agg != NULL ) { agg->AddChild( obj ); agg->material = Copy( mat, material ); } else if( scene.object == NULL ) scene.object = obj; break; case envmap_plugin: env = (Envmap*)plg; // If an environment map is set before any object is created, use it as // the background. if( obj == NULL ) scene.envmap = env; break; case rasterizer_plugin: if( scene.rasterize != NULL ) { cerr << "Error: More than one rasterizer specified. Line " << line_num << ": " << input_line << endl; return false; } scene.rasterize = (Rasterizer *)plg; break; } continue; } // Now look for all the other stuff... materials, camera, lights, etc. ParamReader get( input_line ); if( get["eye"] && get[camera.eye] ) continue; if( get["lookat"] && get[camera.lookat] ) continue; if( get["up"] && get[camera.up] ) continue; if( get["vpdist"] && get[camera.vpdist] ) continue; if( get["x_res"] && get[camera.x_res] ) continue; if( get["y_res"] && get[camera.y_res] ) continue; if( get["x_win"] && get[camera.x_win] ) continue; if( get["y_win"] && get[camera.y_win] ) continue; if( get["ambient"] && get[material.ambient] ) continue; if( get["diffuse"] && get[material.diffuse] ) continue; if( get["specular"] && get[material.specular] ) continue; if( get["emission"] && get[material.emission] ) continue; if( get["reflectivity"] && get[material.reflectivity] ) continue; if( get["translucency"] && get[material.translucency] ) continue; if( get["Phong_exp"] && get[material.Phong_exp] ) continue; if( get["ref_index"] && get[material.ref_index] ) continue; // If no object is defined at this point, it's an error. if( obj == NULL ) { cerr << "Error reading scene file; No object defined at line " << line_num << ": " << input_line << endl; return false; } // Look for an end statement that closes the current aggregate. This allows us to nest aggregates. if( get["end"] ) { if( agg == NULL ) { cerr << "Error: end statement found outside an aggregate object at line " << line_num << ": " << input_line << endl; return false; } // Go back to adding objects to the parent object (if there is one). agg->Close(); // Signal the aggregate that it is now complete. Object *closed_agg = agg; agg = agg->parent; if( agg != NULL ) { material = *(agg->material); mat = agg->material; shd = agg->shader; env = agg->envmap; agg->AddChild( closed_agg ); // Add the agg that just ended. } continue; } // If nothing matched, it's an error. Print a warning and continue processing. cerr << "Warning: Unrecognized or ill-formed command at line " << line_num << ": " << input_line << endl; } if( agg != NULL ) { cerr << "Error: Top-most aggregate object has no 'end' statement." << endl; return false; } cout << "done." << endl; return true; }
Stag::Stag(float x, float y) { m_health = 70; m_position.x = x; m_position.y = y; m_bodyTexture.loadFromFile("Assets/Graphics/NPC/bodyStagNPC.png"); m_bodyTexture.setSmooth(true); m_bodySprite.setTexture(m_bodyTexture); m_bodySprite.setOrigin(m_bodySprite.getLocalBounds().width / 2, m_bodySprite.getLocalBounds().height / 2); m_bodySprite.setRotation(0); m_bodySprite.setPosition(m_position.x, m_position.y); m_headTexture.loadFromFile("Assets/Graphics/NPC/headStagNPC.png"); m_headTexture.setSmooth(true); m_headSprite.setTexture(m_headTexture); m_headSprite.setOrigin(60.0f, 45.0f); m_headSprite.setPosition(m_position.x, m_position.y - DistanceOfNeck); m_tailTexture.loadFromFile("Assets/Graphics/NPC/tailStagNPC.png"); m_tailTexture.setSmooth(true); m_tailSprite.setTexture(m_tailTexture); m_tailSprite.setOrigin(9.0f, 3.0f); m_tailSprite.setPosition(m_position.x, m_position.y - DistanceOfTail); m_selectedTex.loadFromFile("Assets/Graphics/NPC/selectedStag.png"); m_selectedTex.setSmooth(true); m_selectedSprite.setTexture(m_selectedTex); m_selectedSprite.setOrigin(m_selectedSprite.getLocalBounds().width / 2, m_selectedSprite.getLocalBounds().height / 2); m_selectedSprite.setPosition(m_position.x, m_position.y); // Attack animations // texture m_bashTexture.loadFromFile("Assets/Graphics/Actions/bashSheet.png"); m_attackAreaTex.loadFromFile("Assets/Graphics/Actions/attackArea.png"); m_attackArea.setTexture(m_attackAreaTex); // animation m_bashAnimation.setSpriteSheet(m_bashTexture); m_bashAnimation.addFrame(sf::IntRect(0, 144, 64, 35)); m_bashAnimation.addFrame(sf::IntRect(0, 108, 64, 35)); m_bashAnimation.addFrame(sf::IntRect(0, 72, 64, 35)); m_bashAnimation.addFrame(sf::IntRect(0, 36, 64, 35)); m_bashAnimation.addFrame(sf::IntRect(0, 0, 64, 35)); m_currentAnimation = &m_bashAnimation; m_animatedSprite = AnimatedSprite(sf::seconds(0.085), true, false); m_animatedSprite.setOrigin(32, 14); m_animatedSprite.setPosition(m_position.x, m_position.y - DistanceOfAttack); m_speed = 2.5; m_colour = sf::Color(255,162,68,100); m_emitter = Emitter(m_position.x, m_position.y, m_colour); //Sounds //m_injuredBuffer.loadFromFile("Assets/Audio/NPC/Doe/doeSound.wav"); //m_injuredSound.setBuffer(m_injuredBuffer); //m_injuredSound.setRelativeToListener(false); //m_injuredSound.setPosition(200, 200, 0); //m_injuredSound.setAttenuation(5); m_deathBuffer.loadFromFile("Assets/Audio/NPC/Stag/stagDead.wav"); m_deathSound.setBuffer(m_deathBuffer); m_deathSound.setRelativeToListener(false); m_deathSound.setAttenuation(10); }
Output::Output(Context* ctx) : Inspect(Emitter(ctx)), charset(""), top_nodes(0) {}
namespace mpe { //------------------------------------------------------------------------------ // Class: Emitter // Method: constructor // Description: //------------------------------------------------------------------------------ Emitter::Emitter(EmitterID theID): mID(theID), mGenFocTOL(0, 0), mGenFocPPS(0, 0) { } //------------------------------------------------------------------------------ // Class: Emitter // Method: generateVelocity // Description: //------------------------------------------------------------------------------ Vec2 Emitter::generateVelocity(const Focus& theFocus, const Vec2& theParticlePosition) const { Vec2 anVelocity; switch (mDispersion) { case LINEAR: anVelocity = Vec2(std::cos(theFocus.getAngle()), std::sin(theFocus.getAngle())); break; case RADIAL: anVelocity = theParticlePosition - theFocus.getPosition(); anVelocity.normalize(); break; case STATIC: anVelocity = Vec2(); break; case RANDOM: anVelocity = Vec2(sRealRandom(), sRealRandom()); anVelocity.normalize(); break; case REFLECT: anVelocity = Vec2(); break; default: anVelocity = Vec2(); break; } anVelocity *= mGenParLV(); return anVelocity; } //------------------------------------------------------------------------------ // Class: Emitter // Method: generatePosition // Description: //------------------------------------------------------------------------------ Vec2 Emitter::generatePosition(const Focus& theFocus) const { Vec2 anPosition; do { anPosition.init(sRealRandom(), sRealRandom()); } while(mShape == CIRCLE && anPosition.squareLength() > 1); anPosition.scale(theFocus.getWidth(), theFocus.getHeight()); anPosition.rotate(theFocus.getAngle()); anPosition = anPosition + theFocus.getPosition(); return anPosition; } //------------------------------------------------------------------------------ // Class: Emitter // Method: createFocus // Description: //------------------------------------------------------------------------------ FocusPtr Emitter::createFocus() const { FocusPtr anFocus = Focus::create((*this)); anFocus->resetLife(getFocusTOL()); anFocus->setWidth(getFocusWidth()); anFocus->setHeight(getFocusHeight()); anFocus->resetNP(getFocusNP()); anFocus->setPPS(getFocusPPS()); anFocus->addGroups(mpe::GROUP_A); return anFocus; } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ EmitterID Emitter::getID() const { return mID; } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ System* Emitter::getSystem() const { return mSystem; } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getParticleSize() const { return mGenParSize(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getParticleAngle() const { return mGenParAngle(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getParticleLV() const { return mGenParLV(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getParticleAV() const { return mGenParAV(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getParticleTOL() const { return mGenParTOL(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getFocusTOL() const { return mGenFocTOL(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getFocusWidth() const { return mGenFocWidth(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getFocusHeight() const { return mGenFocHeight(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Real Emitter::getFocusPPS() const { return mGenFocPPS(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ Integer Emitter::getFocusNP() const { return mGenFocNP(); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setShape // Description: //------------------------------------------------------------------------------ void Emitter::setShape(Shape theShape) { mShape = theShape; } //------------------------------------------------------------------------------ // Class: Emitter // Method: getID // Description: //------------------------------------------------------------------------------ TextRect Emitter::getTextRect() const { return mRect; } //------------------------------------------------------------------------------ // Class: Emitter // Method: setDispersion // Description: //------------------------------------------------------------------------------ void Emitter::setDispersion(Dispersion theDispersion) { mDispersion = theDispersion; } //------------------------------------------------------------------------------ // Class: Emitter // Method: setTextRect // Description: //------------------------------------------------------------------------------ void Emitter::setTextRect(TextRect theTextRect) { mRect = theTextRect; } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorParticleSize // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorParticleSize(Real theMin, Real theMax) { mGenParSize = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorParticleAngle // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorParticleAngle(Real theMin, Real theMax) { mGenParAngle = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorParticleLV // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorParticleLV(Real theMin, Real theMax) { mGenParLV = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorParticleAV // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorParticleAV(Real theMin, Real theMax) { mGenParAV = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorParticleTOL // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorParticleTOL(Real theMin, Real theMax) { mGenParTOL = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorFocusWidth // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorFocusWidth(Real theMin, Real theMax) { mGenFocWidth = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorFocusHeight // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorFocusHeight(Real theMin, Real theMax) { mGenFocHeight = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorFocusTOL // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorFocusTOL(Real theMin, Real theMax) { mGenFocTOL = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorFocusPPS // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorFocusPPS(Real theMin, Real theMax) { mGenFocPPS = Generator<Real>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setGeneratorFocusNP // Description: //------------------------------------------------------------------------------ void Emitter::setGeneratorFocusNP(Integer theMin, Integer theMax) { mGenFocNP = Generator<Integer>::create(theMin,theMax); } //------------------------------------------------------------------------------ // Class: Emitter // Method: setSystem // Description: //------------------------------------------------------------------------------ void Emitter::setSystem(System* theSystem) { mSystem = theSystem; } //////////////////////////////////////////////////////////////////////////////// Emitter Emitter::DUMMY = Emitter("DUMMY"); Generator<Real> Emitter::sRealRandom {}; }
Color basic_shader::Shade( const Scene &scene, const HitInfo &hit ) const { Ray ray; Ray reflectionRay; Ray refractedRay; Ray secondaryRefractedRay; HitInfo otherhit; HitInfo refractionHit; static const double epsilon = 1.0E-6; if( Emitter( hit.object ) ) return hit.object->material->emission; Material *mat = hit.object->material; Color diffuse = mat->diffuse; Color specular = mat->specular; Color color = mat->ambient * diffuse; Vec3 O = hit.ray.origin; Vec3 P = hit.point; Vec3 N = hit.normal; Vec3 E = Unit( O - P ); Vec3 R = Unit( ( 2.0 * ( E * N ) ) * N - E ); Color r = mat->reflectivity; double e = mat->Phong_exp; double k = mat->ref_index; Color t = mat->translucency; if(hit.object->Inside(P)){ P = P + epsilon*N; } if( E * N < 0.0 ) N = -N; // Flip the normal if necessary. //get the attentuation double attenuation; double lightDistance; Vec3 lightVector; double diffuseFactor; double specularFactor; Color diffuseColor = Color(); Color specularColor = Color(); Color finalColor; Color colorWithLighting; Color reflectedColor; Color refractedColor; Vec3 currentR; double shadowFactor = 0; bool objectWasHit = false; int numGridVals = 30; float numSamples = 5; int totalNumGridVals = numGridVals*numGridVals*numSamples; int totalArrayValues = totalNumGridVals*2; float totalGridVals = numGridVals; float currentXvalue; float currentYvalue; float interval = 2.0f/totalGridVals; float currentDist; bool posZinHemisphere,negZinHemisphere; int currentInd1,currentInd2; float randomX,randomY; float currentPosZvalue,currentNegZvalue; //Vec3 currentNormal = Vec3(0.0f,0.0f,1.0f); Vec3 currentNormal = N; float dotProdPosZ,dotProdNegZ,dotProdXYpart; Vec3 positiveZvector; Vec3 negativeZvector; int numLightsHit = 0; /* Surface Area of sphere from each patch P is approximately area(P)*(1/z') where z' is taken at the center This comes from the fact that the surface area is integral_P (1/z) */ float minZvalue=sqrt(interval)*sqrt(2-interval);; //used to calculate surface area float centerXvalue,centerYvalue,centerZvalue,approxSurfaceArea; //temp variable. default emission of the light blocks Color defaultEmission = Color(1.0,1.0,1.0); double emissionFactor = 30.0; Color posZpatchValue = Color(); Color negZpatchValue = Color(); Color posZpatchSpecValue = Color(); Color negZpatchSpecValue = Color(); double radius,patchFormFactor; Vec3 otherNormal; float currentEnergy = 0; for(int xInd = 0; xInd < numGridVals; xInd++){ for(int yInd = 0; yInd < numGridVals; yInd++){ centerXvalue = -1 + interval*xInd + interval*0.5; centerYvalue = -1 + interval*yInd + interval*0.5; currentDist = centerXvalue*centerXvalue + centerYvalue*centerYvalue; centerZvalue = sqrt(1-currentDist); if(centerZvalue < minZvalue){ centerZvalue = minZvalue; } approxSurfaceArea = interval*interval*(1.0f/centerZvalue); posZpatchValue = Color(); negZpatchValue = Color(); for(int sampleInd = 0; sampleInd < numSamples; sampleInd++){ randomX = (double)rand() / RAND_MAX; randomY = (double)rand() / RAND_MAX; currentXvalue = -1+interval*xInd + interval*randomX; currentYvalue = -1+interval*yInd + interval*randomY; currentDist = currentXvalue*currentXvalue + currentYvalue*currentYvalue; posZinHemisphere = false; negZinHemisphere = false; currentInd1 = (xInd*numGridVals + yInd)*numSamples + sampleInd; currentInd2 = currentInd1 + totalNumGridVals; //makes sure it is inside the unit disk if(currentDist <= 1){ currentPosZvalue = sqrt(1-currentDist); currentNegZvalue = -currentPosZvalue; dotProdXYpart = currentNormal.x*currentXvalue + currentNormal.y*currentYvalue; dotProdPosZ = currentNormal.z*currentPosZvalue + dotProdXYpart; dotProdNegZ = currentNormal.z*currentNegZvalue + dotProdXYpart; posZinHemisphere = (dotProdPosZ >=0); negZinHemisphere = (dotProdNegZ >=0); } if(posZinHemisphere){ positiveZvector = Vec3(currentXvalue,currentYvalue,currentPosZvalue); //light ray to case to determine occulsion ray.origin = P; ray.direction = positiveZvector; HitInfo objectHit; objectHit.distance = Infinity; shadowFactor = 1; if(scene.Cast(ray,objectHit) ){ if(objectHit.object != NULL){ Vec3 LightPos = objectHit.point; if(LightPos.z > 12.0){ lightVector = LightPos - P; numLightsHit = numLightsHit + 1; radius = Length(lightVector); otherNormal = Unit(objectHit.normal); lightVector = Unit(lightVector); //this is the current patches approximation of F_ij //patchFormFactor = ((abs(lightVector*currentNormal))*(abs(-1*lightVector*otherNormal)))/(Pi*radius*radius); patchFormFactor = ((lightVector*currentNormal)*(-1*lightVector*otherNormal))/(Pi*radius*radius); patchFormFactor = max(0,patchFormFactor); //printf("radius: %f\n",radius); currentEnergy = currentEnergy + emissionFactor*patchFormFactor; diffuseColor = diffuseColor + GetDiffuseColor(lightVector,N,defaultEmission,diffuse); } } } } if(negZinHemisphere){ negativeZvector = Vec3(currentXvalue,currentYvalue,currentNegZvalue); //printf("(x,y,z)=(%f,%f,%f)\n",gridXvalues[currentInd2],gridYvalues[currentInd2],gridZvalues[currentInd2]); //light ray to case to determine occulsion ray.origin = P; ray.direction = negativeZvector; HitInfo objectHit; objectHit.distance = Infinity; shadowFactor = 1; if(scene.Cast(ray,objectHit) ){ if(objectHit.object != NULL){ Vec3 LightPos = objectHit.point; if(LightPos.z > 12.0){ lightVector = LightPos - P; numLightsHit = numLightsHit + 1; radius = Length(lightVector); otherNormal = Unit(objectHit.normal); lightVector = Unit(lightVector); //this is the current patches approximation of F_ij //patchFormFactor = ((abs(lightVector*currentNormal))*(abs(-1*lightVector*otherNormal)))/(Pi*radius*radius); patchFormFactor = ((lightVector*currentNormal)*(-1*lightVector*otherNormal))/(Pi*radius*radius); patchFormFactor = max(0,patchFormFactor); currentEnergy = currentEnergy + emissionFactor*patchFormFactor; diffuseColor = diffuseColor + GetDiffuseColor(lightVector,N,defaultEmission,diffuse); //negZpatchValue = negZpatchValue + GetDiffuseColor(lightVector,N,defaultEmission,diffuse); } } } } } //diffuseColor = diffuseColor + (posZpatchValue/numSamples)*approxSurfaceArea; //diffuseColor = diffuseColor + (negZpatchValue/numSamples)*approxSurfaceArea; //printf("Approx Surface Area:%f\n",approxSurfaceArea); //diffuseColor = diffuseColor + posZpatchValue; //diffuseColor = diffuseColor + negZpatchValue; if(diffuseColor.blue > 0.5 || diffuseColor.green > 0.5 || diffuseColor.red > 0.5){ //printf("Current Diffuse Color: (%f,%f,%f)\n",diffuseColor.blue,diffuseColor.green,diffuseColor.red); } } } //makes sure to include the light vectors in the calculations for( unsigned i = 0; i < scene.NumLights(); i++ ) { const Object *light = scene.GetLight(i); Color emission = light->material->emission; AABB box = GetBox( *light ); Vec3 LightPos( Center( box ) ); //gets the light Vector lightVector = LightPos - P; lightDistance = Length(lightVector); Vec3 unitLightVector = Unit(lightVector); //gets the attenuation factor attenuation = 1/(attenuation_a + attenuation_b*lightDistance + attenuation_c*lightDistance*lightDistance); float dotProd = currentNormal.x*unitLightVector.x + currentNormal.y*unitLightVector.y + currentNormal.z*unitLightVector.z; //light vector in unit hemipshere if(dotProd >= 0){ //light ray to case to determine occulsion ray.origin = P; ray.direction = unitLightVector; HitInfo objectHit; objectHit.distance = Infinity; if(scene.Cast(ray,objectHit) ){ if(objectHit.object != NULL){ Vec3 LightPos = objectHit.point; if(LightPos.z > 12.0){ numLightsHit = numLightsHit + 1; lightVector = LightPos - P; //diffuseColor = diffuseColor + GetDiffuseColor(lightVector,N,defaultEmission,diffuse); radius = Length(lightVector); otherNormal = Unit(objectHit.normal); lightVector = Unit(lightVector); //this is the current patches approximation of F_ij //patchFormFactor = ((abs(lightVector*currentNormal))*(abs(-1*lightVector*otherNormal)))/(Pi*radius*radius); patchFormFactor = ((lightVector*currentNormal)*(-1*lightVector*otherNormal))/(Pi*radius*radius); patchFormFactor = max(0,patchFormFactor); currentEnergy = currentEnergy + emissionFactor*patchFormFactor; diffuseColor = diffuseColor + GetDiffuseColor(lightVector,N,defaultEmission,diffuse); } } } } } diffuseColor = currentEnergy*defaultEmission; //diffuseColor = diffuseColor/12.0; float numLights = numLightsHit; //diffuseColor = diffuseColor/(numLights*Pi); if(numLightsHit > 1){ //printf("Number of Lights Hit:%d\n",numLightsHit); //printf("Original Color: (%f,%f,%f)\n",diffuse.blue,diffuse.green,diffuse.red); //printf("Diffuse Color: (%f,%f,%f)\n\n",diffuseColor.blue,diffuseColor.green,diffuseColor.red); } //colorWithLighting = color + diffuseColor*diffuse + specularColor*specular; //colorWithLighting = diffuseColor*diffuse; colorWithLighting = diffuseColor*diffuse + diffuse*0.4; //set variables for reflection reflectionRay.origin = P; reflectionRay.direction = R; reflectionRay.generation = hit.ray.generation + 1; reflectedColor = Color(); //set variables for refraction refractedRay.origin = P-epsilon*N; Vec3 refractionDir = RefractionDirection(1.0,k,E,N); refractedRay.direction = refractionDir; //for refraction refractedRay.generation = hit.ray.generation + 1; refractedColor = Color(); refractionHit.distance = Infinity; //only do refraction if the transluency is greater than zero // this is an optimization so unnecessary refractions are not calculated if( (t.red + t.green + t.blue) > epsilon){ if(scene.Cast(refractedRay,refractionHit)){ bool insideMaterial = false; Vec3 currentNormal; Vec3 previousRefractedDirection; do{ currentNormal = Unit(refractionHit.normal); previousRefractedDirection = refractedRay.direction; refractedRay.direction = RefractionDirection(k,1.0,-1*refractedRay.direction,-1*currentNormal); if(Length(refractedRay.direction) < epsilon){ insideMaterial = true; refractionHit.distance = Infinity; refractedRay.origin = refractionHit.point - epsilon*currentNormal; refractedRay.direction = Unit( ( 2.0 * ( previousRefractedDirection * currentNormal ) ) * (-1*currentNormal) + previousRefractedDirection ); if(!scene.Cast(refractedRay,refractionHit)){ insideMaterial = false; } }else{ refractedRay.origin = refractionHit.point + epsilon*currentNormal; insideMaterial = false; } }while(insideMaterial); refractedRay.generation = hit.ray.generation + 1; //now do refraction refractedColor = scene.Trace(refractedRay); } } //do the reflection //only do reflection if the reflectance is greater than zero // this is an optimization so unnecessary reflections are not calculated if( (r.red + r.green + r.blue) > epsilon){ reflectedColor = scene.Trace(reflectionRay); } //printf("diffuseColor: (%f,%f,%f)\n",colorWithLighting.red,colorWithLighting.green,colorWithLighting.blue); //now combine calculated color with reflected color //finalColor = (-1*t + Color(1.0,1.0,1.0))*(colorWithLighting + r*reflectedColor) + t*refractedColor; return colorWithLighting; //return finalColor; }