t_texture const *load_texture(t_sub file_name, uint32_t flags) { t_hmap *const cache = texture_cache(); char key[sizeof(uint32_t) + file_name.length]; t_texture *texture; *(uint32_t*)key = flags; ft_memcpy(key + sizeof(uint32_t), file_name.str, file_name.length); if ((texture = ft_hmapget(cache, SUBV(key)).value) != NULL) { ft_logf(LOG_DEBUG, "Cached texture: %ts", file_name); return (texture); } texture = ft_hmapput(cache, SUBV(key), NULL, sizeof(t_texture)).value; texture->get = &texture_bilinear; if (!ft_load_img(file_name, &texture->img)) { ft_logf(LOG_VERBOSE, "Failed to load '%ts'", file_name); ft_hmaprem(cache, SUBV(key), NULL); return (NULL); } if (flags & TEXTURE_GAMMA) correct_gamma(&texture->img); ft_logf(LOG_VERBOSE, "Texture file '%ts' loaded", file_name); return (texture); }
t_texture const *load_texture1(uint32_t color, uint32_t flags) { t_hmap *const cache = texture_cache(); uint32_t key[2]; t_texture *tmp; key[0] = flags; key[1] = color; if ((tmp = ft_hmapget(cache, SUBV(key)).value) != NULL) return (tmp); tmp = ft_hmapput(cache, SUBV(key), NULL, sizeof(t_texture) + sizeof(uint32_t)).value; ft_logf(LOG_DEBUG, "Solid texture created: #%.8x", color); *tmp = (t_texture){&texture_solid, (t_img){ENDOF(tmp), 1, 1}}; *tmp->img.data = color; if (flags & TEXTURE_GAMMA) correct_gamma(&tmp->img); return (tmp); }
bool RendererServices::trace( TraceOpt& options, OSL::ShaderGlobals* sg, const OSL::Vec3& P, const OSL::Vec3& dPdx, const OSL::Vec3& dPdy, const OSL::Vec3& R, const OSL::Vec3& dRdx, const OSL::Vec3& dRdy) { assert(m_texture_store); TextureCache texture_cache(*m_texture_store); Intersector intersector(m_project.get_trace_context(), texture_cache); const ShadingPoint* parent = reinterpret_cast<const ShadingPoint*>(sg->renderstate); const ShadingPoint* origin_shading_point = 0; Vector3d pos = Vector3f(P); if (P == sg->P) { Vector3d front = Vector3f(P); Vector3d back = front; intersector.fixed_offset( parent->get_point(), parent->get_geometric_normal(), front, back); pos = sg->N.dot(R) >= 0.0f ? front : back; origin_shading_point = parent; } const Vector3d dir = Vector3f(R); const ShadingRay ray( pos, normalize(dir), options.mindist, options.maxdist, parent->get_ray().m_time, VisibilityFlags::ProbeRay, parent->get_ray().m_depth + 1); ShadingPoint shading_point; intersector.trace( ray, shading_point, origin_shading_point); ShadingPoint::OSLTraceData* trace_data = reinterpret_cast<ShadingPoint::OSLTraceData*>(sg->tracedata); trace_data->m_traced = true; if (shading_point.hit()) { trace_data->m_hit = true; trace_data->m_P = Imath::V3d(shading_point.get_point()); trace_data->m_hit_distance = static_cast<float>(shading_point.get_distance()); trace_data->m_N = Imath::V3d(shading_point.get_shading_normal()); trace_data->m_Ng = Imath::V3d(shading_point.get_geometric_normal()); const Vector2d& uv = shading_point.get_uv(0); trace_data->m_u = static_cast<float>(uv[0]); trace_data->m_v = static_cast<float>(uv[1]); return true; } return false; }