/* Copyright (c) 2005-2019 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "video.h" #include using namespace std; #ifdef _MSC_VER // warning C4068: unknown pragma #pragma warning(disable: 4068) // warning C4351: new behavior: elements of array 'array' will be default initialized #pragma warning(disable: 4351) #endif #include "universe.h" const colorcomp_t MaterialColor[4][3] = { // BGR {96,0,0}, // WATER {0,48,48}, // SANDSTONE {32,32,23} // SHALE }; void Universe::InitializeUniverse(video const& colorizer) { pulseCounter = pulseTime = 100; pulseX = UniverseWidth/3; pulseY = UniverseHeight/4; // Initialize V, S, and T to slightly non-zero values, in order to avoid denormal waves. for( int i=0; i0 ? t : 0; ValueType b = t<0 ? -t : 0; ValueType g = 0.5f*fabs(t); std::memcpy(c, MaterialColor[k], sizeof(c)); c[2] = colorcomp_t(r*(255-c[2])+c[2]); c[1] = colorcomp_t(g*(255-c[1])+c[1]); c[0] = colorcomp_t(b*(255-c[0])+c[0]); ColorMap[k][i] = colorizer.get_color(c[2], c[1], c[0]); } } // Set damping coefficients around border to reduce reflections from boundaries. ValueType d = 1.0; for( int k=DamperSize-1; k>0; --k ) { d *= 1-1.0f/(DamperSize*DamperSize); for( int j=1; j0 ) { ValueType t = (pulseCounter-pulseTime/2)*0.05f; V[pulseY][pulseX] += 64*sqrt(M[pulseY][pulseX])*exp(-t*t); --pulseCounter; } } void Universe::UpdateStress(Rectangle const& r ) { drawing_area drawing(r.StartX(),r.StartY(),r.Width(),r.Height(),drawingMemory); for( int i=r.StartY(); i=ColorMapSize ) index = ColorMapSize-1; color_t* c = ColorMap[material[i][j]]; drawing.put_pixel(c[index]); } } } void Universe::SerialUpdateStress() { Rectangle area(0, 0, UniverseWidth-1, UniverseHeight-1); UpdateStress(area); } //void Universe::ParallelUpdateStress(tbb::affinity_partitioner &affinity) { // tbb::parallel_for( tbb::blocked_range( 0, UniverseHeight-1 ), // Index space for loop // UpdateStressBody(*this), // Body of loop // affinity ); // Affinity hint //} void Universe::UpdateVelocity(Rectangle const& r) { for( int i=r.StartY(); i( 1, UniverseHeight ), // Index space for loop // UpdateVelocityBody(*this), // Body of loop // affinity ); // Affinity hint //} void Universe::SerialUpdateUniverse() { UpdatePulse(); SerialUpdateStress(); SerialUpdateVelocity(); } //void Universe::ParallelUpdateUniverse() { // /** Affinity is an argument to parallel_for to hint that an iteration of a loop // is best replayed on the same processor for each execution of the loop. // It is a static object because it must remember where the iterations happened // in previous executions. */ // static tbb::affinity_partitioner affinity; // UpdatePulse(); // ParallelUpdateStress(affinity); // ParallelUpdateVelocity(affinity); //} bool Universe::TryPutNewPulseSource(int x, int y){ if(pulseCounter == 0) { pulseCounter = pulseTime; pulseX = x; pulseY = y; return true; } return false; } void Universe::SetDrawingMemory(const drawing_memory &dmem) { drawingMemory = dmem; }