Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. kernel MultigradKernel : ImageComputationKernel<ePixelWise>
  2. {
  3.   Image<eWrite> dst;
  4.  
  5.   param:
  6.     int numPoints; // Number of points that will be used
  7.     float4 c0, c1, c2, c3, c4, c5, c6, c7, c8, c9;
  8.     int2 p0, p1, p2, p3, p4, p5, p6, p7, p8, p9;
  9.  
  10.  
  11.   void process(int2 pos) {
  12.     float4 colors [10] = {c0,c1,c2,c3,c4,c5,c6,c7,c8,c9};
  13.     int2 points [10] = {p0,p1,p2,p3,p4,p5,p6,p7,p8,p9};
  14.  
  15.     //--------------
  16.     // 1. FIND THE 3 CLOSEST POINTS
  17.  
  18.     int vertexId [3] = {-1,-1,-1}; // Stores the ids of the closest vertices to avoid repetition
  19.     float4 vertexColors [3]; // Will store the color of the 3 closest vertices
  20.     vertexColors[0] = colors[0]; // Initialising...
  21.     int2 vertexPos [3]; // Will store the position of the 3 closest vertices
  22.     vertexPos[0] = points[0]; // Initialising...
  23.  
  24.     float vertexDistance [3] = {10000000000.0f,10000000000.0f,10000000000.0f}; // Distance to vertices, for iterating
  25.     int vertices = min(numPoints, 3);
  26.     float dist = 0.0f;
  27.     //Point 1
  28.     for(int i = 0; i<numPoints; i++){
  29.       dist = length(float2(float(pos.x),float(pos.y))-float2(float(points[i][0]),float(points[i][1])));
  30.       if(dist < vertexDistance[0]){
  31.         vertexDistance[0] = dist;
  32.         vertexColors[0] = colors[i];
  33.         vertexPos[0] = points[i];
  34.         vertexId[0] = i;
  35.       }
  36.     }
  37.     //dst() = vertexColors[0];
  38.     //dst() = vertexId[0] ;
  39.     //dst() = float(vertexPos[0][0]);
  40.  
  41.     //Point 2
  42.     if(numPoints > 1){
  43.       for(int i = 0; i<numPoints; i++){
  44.         dist = length(float2(float(pos.x),float(pos.y))-float2(float(points[i][0]),float(points[i][1])));
  45.         if(dist < vertexDistance[1] && i != vertexId[0]){
  46.           vertexDistance[1] = dist;
  47.           vertexColors[1] = colors[i];
  48.           vertexPos[1] = points[i];
  49.           vertexId[1] = i;
  50.         }
  51.       }
  52.     }
  53.  
  54.     //Point 3  
  55.     if(numPoints > 2){
  56.       for(int i = 0; i<numPoints; i++){
  57.         dist = length(float2(float(pos.x),float(pos.y))-float2(float(points[i][0]),float(points[i][1])));
  58.         if(dist < vertexDistance[2] && i != vertexId[0] && i != vertexId[1]){
  59.           vertexDistance[2] = dist;
  60.           vertexColors[2] = colors[i];
  61.           vertexPos[2] = points[i];
  62.           vertexId[2] = i;
  63.         }
  64.       }
  65.     }
  66.    
  67.     // 0,1 and 2 are ordered in their distance. Next step is to calculate the barycentric coordinates, in order to find out
  68.     // if the pixel lays on the current triangle or not? Or otherwise we can simply take the ones with positive weights...
  69.    
  70.     //--------------
  71.     // 2. GET THE VERTEX WEIGHTS
  72.    
  73.     float x1,x2,x3,y1,y2,y3, Px, Py;
  74.     x1 = float(vertexPos[0][0]);
  75.     x2 = float(vertexPos[1][0]);
  76.     x3 = float(vertexPos[2][0]);
  77.     y1 = float(vertexPos[0][1]);
  78.     y2 = float(vertexPos[1][1]);
  79.     y3 = float(vertexPos[2][1]);
  80.     Px = float(pos.x);
  81.     Py = float(pos.y);
  82.    
  83.    
  84.     float w1, w2, w3; // Barycentric weights
  85.     w1 = ( (y2-y3)*(Px-x3) + (x3-x2)*(Py-y3) ) / ( (y2-y3)*(x1-x3) + (x3-x2)*(y1-y3) );
  86.     w2 = ( (y3-y1)*(Px-x3) + (x1-x3)*(Py-y3) ) / ( (y2-y3)*(x1-x3) + (x3-x2)*(y1-y3) );
  87.     w3 = 1.0f - w1 - w2;
  88.    
  89.     float wc1, wc2, wc3; // Weights clamped and normalized
  90.     wc1 = max(w1,0.0f);
  91.     wc2 = max(w2,0.0f);
  92.     wc3 = max(w3,0.0f);
  93.    
  94.     wc1 = wc1/(wc1+wc2+wc3);
  95.     wc2 = wc2/(wc1+wc2+wc3);
  96.     wc3 = wc3/(wc1+wc2+wc3);
  97.    
  98.     //--------------
  99.     // 2. CALCULATE THE RESULTING COLOR
  100.    
  101.     float4 result = vertexColors[0];
  102.    
  103.     /*  Maybe clamp the weights to 0... and then normalize the resulting  */
  104.     if(numPoints <= 1){
  105.       result = vertexColors[0];
  106.     }else if(numPoints == 2){
  107.       result = vertexColors[0]; //TODO: DO THIS WITH A LINEAR GRADIENT!!!!!!!!
  108.     }else{
  109.       result = vertexColors[0] * wc1 + vertexColors[1] * wc2 + vertexColors[2] * wc3;
  110.     }
  111.    
  112.     // Write the result to the output image
  113.     dst() = result;
  114.   }
  115. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement