DirectionalLight::DirectionalLight(const Vector& dir, const Spectrum& radiance): Light(LIGHT_DELTA_DIR, 1), direction(normalize(dir)), radiance(radiance) { coordinate_system(this->direction, this->s, this->t); }
Point SphereShape::sample_point_eye(const Point& eye, float u1, float u2, Normal& out_n) const { float dist_squared = length_squared(eye.v); if(dist_squared - this->radius * this->radius < 1e-3) { float ray_epsilon; return this->sample_point(u1, u2, out_n, ray_epsilon); } float dist = sqrt(dist_squared); float inv_dist = 1.f / dist; float cos_theta_max = sqrt(dist_squared - this->radius * this->radius) * inv_dist; Vec3 cone_vec = uniform_cone_sample(cos_theta_max, u1, u2); Vector cone_z = -Vector(eye.v) * inv_dist; Vector cone_x, cone_y; coordinate_system(cone_z, cone_x, cone_y); Vector ray_dir = cone_x * cone_vec.x + cone_y * cone_vec.y + cone_z * cone_vec.z; Ray ray(eye, ray_dir); float t_hit; if(!this->solve_hit_t(ray, t_hit)) { t_hit = dist; } Point pt = ray.point_t(t_hit); out_n = Normal(pt.v) / this->radius; return pt; }
void init(void) { shader = boost::shared_ptr<shaders::Surface>(new shaders::Surface); boost::shared_ptr<shade::shaders::Plastic> specular(new shade::shaders::Plastic(0.4, 0.6)); boost::shared_ptr<shade::shaders::TangentSpace> coordinate_system(new shade::shaders::TangentSpace); boost::shared_ptr<shade::shaders::Texture2D> tex_access(new shade::shaders::Texture2D); boost::shared_ptr<shade::GLSLTexture> texture(example::make_texture("examples/heightbump.dds")); tex_access->texture_unit.set(texture); boost::shared_ptr<shade::shaders::UVCoord> uvcoord(new shade::shaders::UVCoord); tex_access->uv = uvcoord; specular->color.set_value(shade::vec4<>(1., 0.4, 0.4, 1.)); specular->coordinate_system = coordinate_system; coordinate_system->normal_map = tex_access; shader->material = specular; { shaders::IlluminatedMaterial::LightList::Accessor accessor(specular->lights); boost::shared_ptr<shaders::PointLight> light(new shaders::PointLight); light->position.set_value(shade::vec3<>(30., 15., 10.)); light->color.set_value(shade::vec3<>(1., 1., 1.)); accessor->push_back(light); boost::shared_ptr<shaders::PointLight> light2(new shaders::PointLight); light2->position.set_value(shade::vec3<>(-15., -3, 0.)); light2->color.set_value(shade::vec3<>(1., 1., 1.)); accessor->push_back(light2); } boost::shared_ptr<Displacement> displacement = boost::shared_ptr<Displacement>(new Displacement); shader->geometry = displacement; displacement->coordinate_system = coordinate_system.get(); displacement->texture_unit.set(texture); state = shade::create_GLSL_wrapper(); state->init(); state->set_geometry_paramters(GL_TRIANGLES, GL_TRIANGLE_STRIP, 64); program = boost::shared_ptr<shade::Program>(new shade::Program(shader, state)); if (std::getenv("DISPLACE_WIREFRAME")) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); }
Spectrum PhongBrdf::sample_f(const Vector& wo, Vector& out_wi, float& out_pdf, float u1, float u2) const { if(u1 < this->glossy_pdf) { u1 = u1 / this->glossy_pdf; Vector refl_wo = Vector(-wo.v.x, -wo.v.y, wo.v.z); Vector refl_s, refl_t; coordinate_system(refl_wo, refl_s, refl_t); Vec3 relative_wi = power_cosine_hemisphere_sample(u1, u2, this->exponent); out_wi = refl_wo * relative_wi.z + refl_s * relative_wi.x + refl_t * relative_wi.y; out_pdf = this->glossy_pdf * power_cosine_hemisphere_pdf( relative_wi.z, this->exponent); } else { u1 = (u1 - this->glossy_pdf) / (1.f - this->glossy_pdf); out_wi = Vector(cosine_hemisphere_sample(u1, u2)); out_pdf = (1.f - this->glossy_pdf) * cosine_hemisphere_pdf(out_wi.v.z); } if(wo.v.z < 0.f) { out_wi.v.z = -out_wi.v.z; } return this->f(wo, out_wi); }
bool Triangle::hit(const Ray& r, const real_t t0, const real_t t1, hitRecord& hR, bool fullRecord) const { Ray tRay = r.transform(invMat); real_t mult[3]; Vector3 a_minus_b = vertices[0].position - vertices[1].position; Vector3 a_minus_c = vertices[0].position - vertices[2].position; Vector3 a_minus_e = vertices[0].position - tRay.e; real_t a = a_minus_b.x; real_t b = a_minus_b.y; real_t c = a_minus_b.z; real_t d = a_minus_c.x; real_t e = a_minus_c.y; real_t f = a_minus_c.z; real_t g = tRay.d.x; real_t h = tRay.d.y; real_t i = tRay.d.z; real_t j = a_minus_e.x; real_t k = a_minus_e.y; real_t l = a_minus_e.z; real_t ei_minus_hf = e * i - h * f; real_t gf_minus_di = g * f - d * i; real_t dh_minus_eg = d * h - e * g; real_t ak_minus_jb = a * k - j * b; real_t jc_minus_al = j * c - a * l; real_t bl_minus_kc = b * l - k * c; real_t M = a * ei_minus_hf + b * gf_minus_di + c * dh_minus_eg; real_t time = (f * ak_minus_jb + e * jc_minus_al + d * bl_minus_kc) / -M; if (time <= t0 || time >= t1) { return false; } real_t beta = (j * ei_minus_hf + k * gf_minus_di + l * dh_minus_eg) / M; if (beta < 0 || beta > 1) return false; real_t gamma = (i * ak_minus_jb + h * jc_minus_al + g * bl_minus_kc) / M; if (gamma < 0 || gamma > 1 - beta) return false; hR.t = time; hR.shape_ptr = (Geometry*)this; if (!fullRecord) return true; mult[0] = 1-beta-gamma; mult[1] = beta; mult[2] = gamma; Vector2 texCoord(0,0); for(int i=0;i<3;i++) texCoord += mult[i]*vertices[i].tex_coord; texCoord[0] = fmod(texCoord[0],1.0); texCoord[1] = fmod(texCoord[1],1.0); if(texCoord[0]<0) texCoord[0]+=1; if(texCoord[1]<0) texCoord[0]+=1; const Material* materials[] = { vertices[0].material,vertices[1].material,vertices[2].material }; if(!simple) getMaterialProperties(hR.mp, mult, texCoord, materials); else getMaterialProperties(hR.mp,texCoord, materials[0]); hR.n = Vector3(0,0,0); for(int i=0;i<3;i++) hR.n += mult[i]*vertices[i].normal; hR.n = normalize( normMat*hR.n); if (materials[0]) hR.bsdf_ptr = (BSDF*)&(materials[0]->bsdf); Vector3 x, y, z = hR.n; coordinate_system(z, &x, &y); hR.shading_trans = Matrix3(x, y, z); inverse(&hR.inv_shading_trans, hR.shading_trans); hR.p = r.d * time + r.e; return true; }
void Triangle::hitPacket(const Packet& packet, int start, int end, real_t t0, real_t *t1Ptr, std::vector<hitRecord>& hs, bool fullRecord) const { /*Matrix4 nm = this->invMat; nm(3,0)=0; nm(3,1)=0; nm(3,2)=0; Ray rays[256]; for(int i=start;i<end;i++) { rays[i] = packet.rays[i]; packet.rays[i].transform(invMat); }*/ // TODO: static float *texCoord_x, *texCoord_y, *norm_x, *norm_y, *norm_z; texCoord_x = new float[end - start]; texCoord_y = new float[end - start]; norm_x = new float[end - start]; norm_y = new float[end - start]; norm_z = new float[end - start]; int *hit_flag = new int[end - start]; ispc::hit_triangle(packet.e_x, packet.e_y, packet.e_z, packet.d_x, packet.d_y, packet.d_z, t0, t1Ptr, (double*)&vertices[0],(double*)&vertices[1],(double*)&vertices[2], (this->invMat._m), start, end, (int)fullRecord, hit_flag, texCoord_x, texCoord_y, norm_x, norm_y, norm_z); const Material* materials[] = { vertices[0].material,vertices[1].material,vertices[2].material }; for (int i = start; i < end; i++) { if (hit_flag[i - start]) { hs[i].t = t1Ptr[i]; Vector2 texCoord(texCoord_x[i - start], texCoord_y[i - start]); // TODO: interpolation //if(!simple) //getMaterialProperties(hs[i].mp, mult, texCoord, materials); //else getMaterialProperties(hs[i].mp,texCoord, materials[0]); hs[i].n = Vector3(norm_x[i - start], norm_y[i - start], norm_z[i - start]); if (materials[0]) hs[i].bsdf_ptr = (BSDF*)&(materials[0]->bsdf); Vector3 x, y, z = hs[i].n; coordinate_system(z, &x, &y); hs[i].shading_trans = Matrix3(x, y, z); inverse(&hs[i].inv_shading_trans, hs[i].shading_trans); hs[i].shape_ptr = (Geometry*)this; hs[i].p = packet.rays[i].d * hs[i].t + packet.rays[i].e; } } /*for(int i=start;i<end;i++) packet.rays[i]= rays[i];*/ delete[] texCoord_x; delete[] texCoord_y; delete[] norm_x; delete[] norm_y; delete[] norm_z; }