void AxonNeuron::update() { Neuron::update(); std::vector<Impulse> impulses = getImpulses(); float time = app::getElapsedSeconds(); mColors.assign(LINE_SEGMENTS, ColorAf(0, 0, 0, 0)); for (int i = 0; i < impulses.size(); ++i) { Impulse impulse = impulses.at(i); if (abs(impulse.time + IMPULSE_TIME - time) < 0.167) { mNeuron->synapse(impulse); } float percentComplete = (app::getElapsedSeconds() - impulse.time) / IMPULSE_TIME; int vertex = (int) glm::round(percentComplete * LINE_SEGMENTS); for (int i = -2; i < 3; ++i) { int colorVert = vertex + i; Colorf color = impulse.color; if(colorVert < LINE_SEGMENTS && colorVert >= 0) { color = color.lerp((float)abs(colorVert - vertex) / 3.0f, Colorf(0, 0, 0)); mColors.at(colorVert) = ColorAf(color.r, color.g, color.b, 1); } } } mAxonLine->bufferAttrib(geom::Attrib::COLOR, mColors); }
Color::Color( const Colorf& source ) { setRed( convertColorFloatToByte(source.red()) ); setGreen( convertColorFloatToByte(source.green()) ); setBlue( convertColorFloatToByte(source.blue()) ); setAlpha( convertColorFloatToByte(source.alpha()) ); }
MipMap::MipMap(const std::vector<uint8_t> &texels, int width, int height, int ncomp, WRAP_MODE wrap_mode) : wrap_mode(wrap_mode), width(width), height(height), ncomp(ncomp) { if (weight_table[0] == -1){ init_weight_table(); } //We only support power of two textures at the moment //Determine if pow2 via http://www.graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2 if (!(width && !(width & (width - 1))) || !(height && !(height & (height - 1)))){ std::cout << "MipMap Error: Only powers of two are supported at the moment\n"; assert("Non power of two texture"); } int n_levels = 1 + static_cast<int>(log_2(std::max(width, height))); pyramid.reserve(n_levels); pyramid.emplace_back(width, height, texels); for (int i = 1; i < n_levels; ++i){ int w = std::max(1, pyramid[i - 1].width / 2); int h = std::max(1, pyramid[i - 1].height / 2); pyramid.emplace_back(w, h); std::vector<uint8_t> &img = pyramid.back().texels; img.resize(w * h * ncomp); for (int t = 0; t < h; ++t){ for (int s = 0; s < w; ++s){ Colorf cf = 0.25 * (texel(i - 1, 2 * s, 2 * t) + texel(i - 1, 2 * s + 1, 2 * t) + texel(i - 1, 2 * s, 2 * t + 1) + texel(i - 1, 2 * s + 1, 2 * t + 1)); cf.normalize(); Color24 c{cf}; //Write the number of components that we have only for (int j = 0; j < ncomp; ++j){ img[t * w * ncomp + s * ncomp + j] = c[j]; } } } } }
Colorf EmissionIntegrator::transmittance(const Scene &scene, const Renderer&, const RayDifferential &ray, Sampler &sampler, MemoryPool&) const { const VolumeNode *vol = scene.get_volume_root(); if (vol == nullptr){ return Colorf{1}; } Colorf tau = -vol->optical_thickness(ray, step_size, sampler.random_float()); return tau.exp(); }
BSDF* GlassMaterial::get_bsdf(const DifferentialGeometry &dg, MemoryPool &pool) const { BSDF *bsdf = pool.alloc<BSDF>(dg, refr_index); Colorf refl = reflect->sample(dg); if (!refl.is_black()){ bsdf->add(pool.alloc<SpecularReflection>(refl, pool.alloc<FresnelDielectric>(1.f, refr_index))); } Colorf trans = transmit->sample(dg); if (!trans.is_black()){ bsdf->add(pool.alloc<SpecularTransmission>(trans, pool.alloc<FresnelDielectric>(1.f, refr_index))); } return bsdf; }
ColorHSLx<float, Colorf>::ColorHSLx(const Colorf &color) { float red = color.get_red(); float green = color.get_green(); float blue = color.get_blue(); const float min_value = min(red, min(blue, green)); const float max_value = max(red, max(blue, green)); a = color.get_alpha(); l = (max_value + min_value) / 2.0f; if (min_value == max_value) { h = 0.0f; s = 0.0f; return; } if (l <= 0.5f) { s = (max_value - min_value) / (min_value + max_value); } else { s = (max_value - min_value) / (2.0f - (min_value + max_value)); } if (max_value == red) { h = (60.0f * (green - blue)) / (max_value - min_value); } else if (max_value == green) { h = 120.0f + (60.0f * (blue - red)) / (max_value - min_value); } else { h = 240.0f + (60.0f * (red - green)) / (max_value - min_value); } }
ColorHSVx<float, Colorf>::ColorHSVx(const Colorf &color) { float red = color.get_red(); float green = color.get_green(); float blue = color.get_blue(); const float min_value = min(red,min(blue, green)); const float max_value = max(red,max(blue, green)); a = color.get_alpha(); if (min_value == max_value) { h = 0.0f; s = 0.0f; v = min_value; return; } s = max_value != 0.0f ? (max_value - min_value) / max_value : 0.0f; v = max_value; float r = float(max_value - red) / (max_value - min_value); float g = float(max_value - green) / (max_value - min_value); float b = float(max_value - blue) / (max_value - min_value); if (max_value == red) { h = (60.0f * (b - g)); } else if (max_value == green) { h = (60.0f * (2.0f + r - b)); } else { h = (60.0f * (4.0f + g - r)); } }
void CollisionOutline::draw( float x, float y, const Colorf &color, Canvas &canvas) { GraphicContext &gc = canvas.get_gc(); // Draw collision outline (Contours are assumed as closed polygons, hence we use line-loop) for(unsigned int i = 0; i < impl->contours.size(); i++) { // Draw the contour unsigned int numpoints = impl->contours[i].get_points().size(); for(unsigned int s = 0; s < numpoints; s++) { const Pointf &p1 = impl->contours[i].get_points()[s]; const Pointf &p2 = impl->contours[i].get_points()[(s+1) % numpoints]; canvas.draw_line(x + p1.x + 0.5f, y + p1.y + 0.5f, x + p2.x + 0.5f, y + p2.y + 0.5f, color); } } Colorf colorinv(1.0f-color.get_red(),1.0f-color.get_green(),1.0f-color.get_blue()); for(unsigned int i = 0; i < impl->contours.size(); i++) { unsigned int numpoints = impl->contours[i].get_points().size(); // Add points (as opposite color) for(unsigned int s1 = 0; s1 < numpoints; s1++) { const Pointf &p1 = impl->contours[i].get_points()[s1]; canvas.draw_point(x + p1.x + 0.5f, y + p1.y + 0.5f, colorinv); } } }
inline Color32 coloring10B1(const double iter,long i,const long max_iter,double* xList,double* yList,const Colorf& errorColorIn,Colorf& errorColorOut){ Colorf color=errorColorIn; if (iter==max_iter){ const double x=xList[max_iter]; const double y=yList[max_iter]; double z=sqrt(x*x+y*y); color.addColor(Colorf(sinColorf(z*20*50-236),sinColorf(z*15*50+221),sinColorf(z*30*50-254))); }else{ const double x=smoothXY10B(iter,i,xList); const double y=smoothXY10B(iter,i,yList); double z=sqrt(x*x+y*y); color.addColor(Colorf(sinColorf(x*20*2-236),sinColorf(y*15*2+221),sinColorf((x*y/sqrt(z))*30*2-254))); } Color32 resultColor=color.toColor32(); errorColorOut=color; errorColorOut.subColor(resultColor); return resultColor; }
inline Color32 coloring9_s(const double iter,long i,const long max_iter,double* xList,double* yList,const Colorf& errorColorIn,Colorf& errorColorOut){ Colorf color=errorColorIn; if (iter==max_iter){ const double x=xList[max_iter]; const double y=yList[max_iter]; double z=sqrt(x*x+y*y); double zd=z-sqrt(xList[max_iter-1]*xList[max_iter-1]+yList[max_iter-1]*yList[max_iter-1]); color.addColor(Colorf(sinColorf(z*2000),sinColorf(y*x*1000),sinColorf(zd*1000))); }else{ const double x=xList[i]; const double y=yList[i]; double z=sqrt(x*x+y*y); color.addColor(Colorf(sinColorf(z*20/20-236),sinColorf(z*15/20+221),sinColorf(z*30/20-254))); } Color32 resultColor=color.toColor32(); errorColorOut=color; errorColorOut.subColor(resultColor); return resultColor; }
Colorf EmissionIntegrator::radiance(const Scene &scene, const Renderer&, const RayDifferential &ray, Sampler &sampler, MemoryPool&, Colorf &transmit) const { const VolumeNode *vol = scene.get_volume_root(); std::array<float, 2> t_range; if (vol == nullptr || !vol->intersect(ray, t_range) || t_range[0] == t_range[1]){ transmit = Colorf{1}; return Colorf{0}; } Colorf rad; //We integrate through the volume via ray marching with steps of size step_size int n_samples = std::ceil((t_range[1] - t_range[0]) / step_size); float step = (t_range[1] - t_range[0]) / n_samples; transmit = Colorf{1}; Point p = ray(t_range[0]), p_prev; Vector w_o = -ray.d; //Our first point inside the volume is offset by some random value float t = t_range[0] + sampler.random_float() * step; //Step through the volume for (int i = 0; i < n_samples; ++i, t += step, p_prev = p){ p = ray(t); //Step forward and update the transmittance to include contrubition from this segment Ray step_ray{p_prev, p - p_prev, ray, 0, 1}; Colorf step_tau = -vol->optical_thickness(step_ray, 0.5 * step_size, sampler.random_float()); transmit *= step_tau.exp(); //We consider terminating the ray if the transmittance is very low if (transmit.luminance() < 1e-3){ const float continue_prob = 0.5; if (sampler.random_float() > continue_prob){ transmit = 0; break; } transmit /= continue_prob; } //Add in emission contribution from emissive volumes rad += transmit * vol->emission(p, w_o); } return rad * step; }
void GameProjectile::setShaderParams( Primitive* /*prim*/, Shader* fx, Mesh* mesh, Camera* camera, Light* keylight ) { Vector3 cameraWPos = camera->worldTransform().translation(); Matrix4x4 worldTm = mesh->cachedWorldTransform(); Matrix4x4 worldTmInv = worldTm.inverse(); for ( int i = 0 ; i < fx->parameters() ; ++i ) { Shader::ParameterDesc desc; fx->getParameterDesc( i, &desc ); if ( desc.name == "GROUND_COLOR" ) { Vector4 groundColorMin4, groundColorMax4; fx->getVector4( "GROUND_COLOR_MIN", &groundColorMin4 ); fx->getVector4( "GROUND_COLOR_MAX", &groundColorMax4 ); Colorf groundColorMin( groundColorMin4.x, groundColorMin4.y, groundColorMin4.z, 1.f ); Colorf groundColorMax( groundColorMax4.x, groundColorMax4.y, groundColorMax4.z, 1.f ); Colorf c = ( (groundColorMax-groundColorMin) * m_groundLightmapColor + groundColorMin ).saturate(); fx->setVector4( desc.name, Vector4(c.red(),c.green(),c.blue(),1.f) ); } else if ( desc.name == "SKY_COLOR" ) { Vector4 skyColorMin4, skyColorMax4; fx->getVector4( "SKY_COLOR_MIN", &skyColorMin4 ); fx->getVector4( "SKY_COLOR_MAX", &skyColorMax4 ); Colorf skyColorMin( skyColorMin4.x, skyColorMin4.y, skyColorMin4.z, 1.f ); Colorf skyColorMax( skyColorMax4.x, skyColorMax4.y, skyColorMax4.z, 1.f ); Colorf c = ( (skyColorMax-skyColorMin) * m_groundLightmapColor + skyColorMin ).saturate(); fx->setVector4( desc.name, Vector4(c.red(),c.green(),c.blue(),1.f) ); } else if ( desc.name == "SPECULAR_COLOR" ) { Vector4 specularColorMin4, specularColorMax4; fx->getVector4( "SPECULAR_COLOR_MIN", &specularColorMin4 ); fx->getVector4( "SPECULAR_COLOR_MAX", &specularColorMax4 ); Colorf specularColorMin( specularColorMin4.x, specularColorMin4.y, specularColorMin4.z, 1.f ); Colorf specularColorMax( specularColorMax4.x, specularColorMax4.y, specularColorMax4.z, 1.f ); Colorf c = ( (specularColorMax-specularColorMin) * m_groundLightmapColor + specularColorMin ).saturate(); fx->setVector4( desc.name, Vector4(c.red(),c.green(),c.blue(),1.f) ); } else if ( desc.dataType == Shader::PT_FLOAT && desc.dataClass == Shader::PC_VECTOR4 && desc.name.length() > 0 && Character::isLowerCase(desc.name.charAt(0)) && desc.name.indexOf("Camera") == -1 ) { bool paramResolved = false; bool objSpace = ( desc.name.length() > 2 && desc.name.charAt(1) == 'o' ); int baseNameOffset = objSpace ? 2 : 1; Node* node = 0; String name = desc.name.substring( baseNameOffset ); if ( keylight && name == "Light1" ) node = keylight; if ( node ) { Char ch = desc.name.charAt(0); if ( ch == 'd' ) { Vector3 v = node->worldTransform().rotation().getColumn(2); if ( objSpace ) v = worldTmInv.rotate(v); fx->setVector4( desc.name, Vector4(v.x, v.y, v.z, 0.f) ); paramResolved = true; } if ( ch == 'p' ) { Vector3 v = node->worldTransform().translation(); if ( objSpace ) v = worldTmInv.transform(v); fx->setVector4( desc.name, Vector4(v.x, v.y, v.z, 1.f) ); paramResolved = true; } } if ( !paramResolved ) { Debug::printlnError( "Mesh {2} shader {0} parameter {1} could not be resolved", fx->name(), desc.name, mesh->name() ); } } } }