Color3d ColorPickerProxy::get_color_from_string(const string& s, const string& wavelength_range) { try { vector<float> range; tokenize(wavelength_range, Blanks, range); return Color3d(do_get_color_from_string(s, range[0], range[1])); } catch (const ExceptionStringConversionError&) { return Color3d(0.0); } }
/* Complete this function */ void init(const Vector3 & spawnPoint) { // Vectors used to create new particle Vector3 velocity( 0, 5, 0); Vector3 acceleration( 0, 0, 0); Color3d colors( 0, 0, 1); for(int i = 0; i < 1500; ++i) { // Get angle for velocity float theta = randFloat(); // Get random velocity velocity = Vector3( randFloat(-50*cos(theta), 50 * sin(theta)), randFloat(-50*cos(theta), 50 * sin(theta)), randFloat(0,10) ); // Get random colors for each particle colors = Color3d( randFloat(0,1), randFloat(0,.2), randFloat(0,1) ); // Determine acceleration in one direction acceleration.y = -10; // Push back newly created particle in vector particles.push_back( Particle( spawnPoint, velocity, acceleration, colors, 10.0) ); } }
Color3d ColorPickerProxy::get_color_from_string(const string& s) { try { vector<double> values; tokenize(s, Blanks, values); if (values.size() == 1) return Color3d(values[0]); else if (values.size() == 3) return Color3d(values[0], values[1], values[2]); else return Color3d(0.0); } catch (const ExceptionStringConversionError&) { return Color3d(0.0); } }
Color3d DirectionalLight::getDiffuse (Intersection& info) { /* * Intensity of diffuse lighting is equal to the dot product of * the normal with the vector opposite the incident light's direction */ double angleFactor = -direction.dot(info.normal); if (angleFactor<0) return Color3d(0,0,0); // light is falling on other side of surface else return color * info.material->getDiffuse(info) * angleFactor; }
void GameState::buildLevel() { m_blocks.clear(); for (int x = 0; x < 10; ++x) { for (int y = 0; y < 5; ++y) { Block bl; int x1 = x + 1; int y1 = y + 1; bl.location = Rectangle( x / 10.0, y / 50.0 + .05, x1 / 10.0, y1 / 50.0 + .05); double r = (randd() + 1) / 2; double g = (randd() + 1) / 2; double b = (randd() + 1) / 2; bl.color = Color3d(r, g, b); m_blocks.push_back(bl); } } }
Color3d DirectionalLight::getSpecular (Intersection& info) { /* * Intensity of specular lighting is the dot product of the light's * reflected ray and the ray pointing to the viewer, raised to * some power (in this case, kshine). * Note: we are using a slightly different model than opengl * so our highlights will appear more focused. */ Vector3d reflect=direction - info.normal*(2*direction.dot(info.normal)); reflect.normalize(); double angleFactor = - reflect.dot(info.theRay.getDir()); if (angleFactor<0) return Color3d(0,0,0); else { angleFactor = pow(angleFactor,info.material->getKshine()); return color * info.material->getSpecular() * angleFactor; } }
void CFireworksParticleSystem::killParticle(unsigned int nParticle) { const CParticle& oldp = (*m_pNewSystem)[nParticle]; if (oldp.type > 0) { int nChildren = MIN_CHILDREN + round(frand()*(MAX_CHILDREN-MIN_CHILDREN)); //That's actually the square root of the number of children created CParticle p; p.X = oldp.X; p.V = oldp.V; p.lifepan = m_dDefaultLifespan/3; p.span = 5.0; p.energy = 1.0; p.persistance = 1; p.type = 0; Point3d colorRand = Point3d(frand()-0.5, frand()-0.5, frand()-0.5) * m_dColorRandomness; p.color = Color3d(0.5+frand()*0.5,frand(),frand()); p.color2 = p.color + colorRand; p.shape = C_PARTICLESHAPE_DOT; p.alpha = 1.0; double headingy = 0; double headingz = 0; double expforce = 8.0 + frand()*8.0; if (oldp.type == 1) expforce *= 0.2; //expforce *= 2; if (frand()<0.04) { nChildren = MIN_CHILDREN/2; p.type = 1; p.lifepan = 1; } for (int zr=0; zr<nChildren; zr++) { for (int yr=0; yr<nChildren; yr++) { headingy = frand()*M_PI/10+(2.0*M_PI*(double)yr)/(double)nChildren; p.V = Point3d(0, 0, expforce); RotateYZ(headingy,headingz, p.V); AddParticle(p); } headingz = frand()*M_PI/10+(2.0*M_PI*(double)zr)/(double)nChildren; } p = oldp; CParticleSystem::killParticle(nParticle); if (p.type > 1) { p.type--; p.span = 0.0; p.lifepan = 0.15*(frand()*10+1); p.mass = 0; p.shape = C_PARTICLESHAPE_DOT; p.persistance = 1.0; p.color = Color3d(0.0,0.0,0.0); p.alpha = 0.0; p.V = Point3d(0,0,0); AddParticle(p); } } else CParticleSystem::killParticle(nParticle); }
Color3d ColorPickerProxy::get_color_from_string(const string& s) { return Color3d(do_get_color_from_string(s, LowWavelength, HighWavelength)); }
void Color3d(double r, double g, double b) { Color3d((float)r, (float)g, (float)b); }
void DiagnosticSurfaceShader::evaluate( SamplingContext& sampling_context, const PixelContext& pixel_context, const ShadingContext& shading_context, const ShadingPoint& shading_point, ShadingResult& shading_result) const { switch (m_shading_mode) { case Color: { shading_result.set_main_to_opaque_pink_linear_rgba(); const Material* material = shading_point.get_material(); if (material) { const Material::RenderData& material_data = material->get_render_data(); #ifdef APPLESEED_WITH_OSL // Execute the OSL shader if there is one. if (material_data.m_shader_group) { shading_context.execute_osl_shading( *material_data.m_shader_group, shading_point); } #endif if (material_data.m_bsdf) { InputEvaluator input_evaluator(shading_context.get_texture_cache()); material_data.m_bsdf->evaluate_inputs( shading_context, input_evaluator, shading_point); const Vector3d direction = -normalize(shading_point.get_ray().m_dir); material_data.m_bsdf->evaluate( input_evaluator.data(), false, false, shading_point.get_geometric_normal(), shading_point.get_shading_basis(), direction, direction, ScatteringMode::All, shading_result.m_main.m_color); shading_result.m_color_space = ColorSpaceSpectral; } } } break; case Coverage: shading_result.set_main_to_linear_rgb(Color3f(1.0f)); break; case Barycentric: shading_result.set_main_to_linear_rgb( vector2_to_color(shading_point.get_bary())); break; case UV: shading_result.set_main_to_linear_rgb( uvs_to_color(shading_point.get_uv(0))); break; case Tangent: case Bitangent: case ShadingNormal: { #ifdef APPLESEED_WITH_OSL const Material* material = shading_point.get_material(); if (material) { const Material::RenderData& material_data = material->get_render_data(); // Execute the OSL shader if there is one. if (material_data.m_shader_group) { sampling_context.split_in_place(2, 1); shading_context.execute_osl_bump( *material_data.m_shader_group, shading_point, sampling_context.next_vector2<2>()); } } #endif const Vector3d v = m_shading_mode == ShadingNormal ? shading_point.get_shading_basis().get_normal() : m_shading_mode == Tangent ? shading_point.get_shading_basis().get_tangent_u() : shading_point.get_shading_basis().get_tangent_v(); shading_result.set_main_to_linear_rgb(vector3_to_color(v)); } break; case GeometricNormal: shading_result.set_main_to_linear_rgb( vector3_to_color(shading_point.get_geometric_normal())); break; case OriginalShadingNormal: shading_result.set_main_to_linear_rgb( vector3_to_color(shading_point.get_original_shading_normal())); break; case WorldSpacePosition: { const Vector3d& p = shading_point.get_point(); shading_result.set_main_to_linear_rgb( Color3f(Color3d(p.x, p.y, p.z))); } break; case Sides: shading_result.set_main_to_linear_rgb( shading_point.get_side() == ObjectInstance::FrontSide ? Color3f(0.0f, 0.0f, 1.0f) : Color3f(1.0f, 0.0f, 0.0f)); break; case Depth: shading_result.set_main_to_linear_rgb( Color3f(static_cast<float>(shading_point.get_distance()))); break; case ScreenSpaceWireframe: { // Initialize the shading result to the background color. shading_result.set_main_to_linear_rgba(Color4f(0.0f, 0.0f, 0.8f, 0.5f)); if (shading_point.is_triangle_primitive()) { // Film space thickness of the wires. const double SquareWireThickness = square(0.00025); // Retrieve the time, the scene and the camera. const double time = shading_point.get_time().m_absolute; const Scene& scene = shading_point.get_scene(); const Camera& camera = *scene.get_camera(); // Compute the film space coordinates of the intersection point. Vector2d point_ndc; camera.project_point(time, shading_point.get_point(), point_ndc); // Loop over the triangle edges. for (size_t i = 0; i < 3; ++i) { // Retrieve the end points of this edge. const size_t j = (i + 1) % 3; const Vector3d vi = shading_point.get_vertex(i); const Vector3d vj = shading_point.get_vertex(j); // Compute the film space coordinates of the edge's end points. Vector2d vi_ndc, vj_ndc; if (!camera.project_segment(time, vi, vj, vi_ndc, vj_ndc)) continue; // Compute the film space distance from the intersection point to the edge. const double d = square_distance_point_segment(point_ndc, vi_ndc, vj_ndc); // Shade with the wire's color if the hit point is close enough to the edge. if (d < SquareWireThickness) { shading_result.set_main_to_linear_rgba(Color4f(1.0f)); break; } } } else { assert(shading_point.is_curve_primitive()); // todo: implement. } } break; case WorldSpaceWireframe: { // Initialize the shading result to the background color. shading_result.set_main_to_linear_rgba(Color4f(0.0f, 0.0f, 0.8f, 0.5f)); if (shading_point.is_triangle_primitive()) { // World space thickness of the wires. const double SquareWireThickness = square(0.0015); // Retrieve the world space intersection point. const Vector3d& point = shading_point.get_point(); // Loop over the triangle edges. for (size_t i = 0; i < 3; ++i) { // Retrieve the end points of this edge. const size_t j = (i + 1) % 3; const Vector3d& vi = shading_point.get_vertex(i); const Vector3d& vj = shading_point.get_vertex(j); // Compute the world space distance from the intersection point to the edge. const double d = square_distance_point_segment(point, vi, vj); // Shade with the wire's color if the hit point is close enough to the edge. if (d < SquareWireThickness) { shading_result.set_main_to_linear_rgba(Color4f(1.0f)); break; } } } else { assert(shading_point.is_curve_primitive()); // todo: implement. } } break; case AmbientOcclusion: { // Compute the occlusion. const double occlusion = compute_ambient_occlusion( sampling_context, sample_hemisphere_uniform<double>, shading_context.get_intersector(), shading_point, m_ao_max_distance, m_ao_samples); // Return a gray scale value proportional to the accessibility. const float accessibility = static_cast<float>(1.0 - occlusion); shading_result.set_main_to_linear_rgb(Color3f(accessibility)); } break; case AssemblyInstances: shading_result.set_main_to_linear_rgb( integer_to_color(shading_point.get_assembly_instance().get_uid())); break; case ObjectInstances: shading_result.set_main_to_linear_rgb( integer_to_color(shading_point.get_object_instance().get_uid())); break; case Regions: { const uint32 h = mix_uint32( static_cast<uint32>(shading_point.get_object_instance().get_uid()), static_cast<uint32>(shading_point.get_region_index())); shading_result.set_main_to_linear_rgb(integer_to_color(h)); } break; case Primitives: { const uint32 h = mix_uint32( static_cast<uint32>(shading_point.get_object_instance().get_uid()), static_cast<uint32>(shading_point.get_region_index()), static_cast<uint32>(shading_point.get_primitive_index())); shading_result.set_main_to_linear_rgb(integer_to_color(h)); } break; case Materials: { const Material* material = shading_point.get_material(); if (material) shading_result.set_main_to_linear_rgb(integer_to_color(material->get_uid())); else shading_result.set_main_to_opaque_pink_linear_rgba(); } break; case RaySpread: { const ShadingRay& ray = shading_point.get_ray(); if (!ray.m_has_differentials) break; const Material* material = shading_point.get_material(); if (material) { const Material::RenderData& material_data = material->get_render_data(); #ifdef APPLESEED_WITH_OSL // Execute the OSL shader if there is one. if (material_data.m_shader_group) { shading_context.execute_osl_shading( *material_data.m_shader_group, shading_point); } #endif if (material_data.m_bsdf) { const Dual3d outgoing( -ray.m_dir, ray.m_dir - ray.m_rx.m_dir, ray.m_dir - ray.m_ry.m_dir); InputEvaluator input_evaluator(shading_context.get_texture_cache()); material_data.m_bsdf->evaluate_inputs( shading_context, input_evaluator, shading_point); const void* bsdf_data = input_evaluator.data(); BSDFSample sample(shading_point, outgoing); material_data.m_bsdf->sample( sampling_context, bsdf_data, false, false, sample); if (!sample.m_incoming.has_derivatives()) break; // The 3.0 factor is chosen so that ray spread from Lambertian BRDFs is approximately 1. const double spread = max( norm(sample.m_incoming.get_dx()), norm(sample.m_incoming.get_dy())) * 3.0; shading_result.set_main_to_linear_rgb( Color3f(static_cast<float>(spread))); } } } break; case FacingRatio: { const Vector3d& normal = shading_point.get_shading_normal(); const Vector3d& view = shading_point.get_ray().m_dir; const double facing = abs(dot(normal, view)); shading_result.set_main_to_linear_rgb( Color3f(static_cast<float>(facing))); } break; default: assert(false); shading_result.set_main_to_transparent_black_linear_rgba(); break; } }