VS (HLSL) Problem (directx)



  • Folgender code sollte folgendes tun:

    - vector von cameraPosition zum Vertex ausrechnen --> sightvektor
    - 6 planes eines achsenparalelen würfels durch richtungsvektoren und ausgangsvektor beschreiben
    - 6 planes auf Schnittpunkte mit Line durch cameraPositions mit richtungsvector sighvektor berechnen
    - Schnittpunkte auf richtigkeit prüfen (sind sie wirklich im würfel?)

    -> Wenn (richtige) Schnittpunkte vorhanden vertex weiß färben

    Der Code funktioniert, wenn ich das ganze in c++ direkt mit den vertexdaten ausrechne, jedoch nicht, wenn ich den vertexshader dies ausrechnen lasse. Bei der shadervariante färben sich die vertices nur weiß, wenn man extrem nah an sie ranfliegt oder in einem sehr flachen winkel auf sie schaut (xz planes falsch?) oO. Daher gehe ich davon aus, dass der fehler durch die konvertierung in HLSL kommt. Hab aber mangels debugger auch keine Ahnung wo der Fehler liegt. Vielleicht kann wer helfen? thx im voraus..

    extern vector fogCubeMin;
    extern vector fogCubeMax;
    extern vector cameraPos;
    
    vector getIntersectionPoint(vector sightVector, vector planeVector1, vector planeVector2, vector planePos, inout float y)
    {
       //compute y
    
       float dif = (planeVector2.x*(planeVector1.y*sightVector.z - planeVector1.z*sightVector.y) + planeVector2.y*(planeVector1.z*sightVector.x - planeVector1.x*sightVector.z) + planeVector2.z*(planeVector1.x*sightVector.y - planeVector1.y*sightVector.x));
       if(dif != 0)
       y = - (cameraPos.x*(planeVector2.y*planeVector1.z - planeVector2.z*planeVector1.y) + cameraPos.y*(planeVector2.z*planeVector1.x - planeVector2.x*planeVector1.z) + cameraPos.z*(planeVector2.x*planeVector1.y - planeVector2.y*planeVector1.x) + planeVector2.x*(planeVector1.z*planePos.y - planeVector1.y*planePos.z) + planeVector2.y*(planeVector1.x*planePos.z - planeVector1.z*planePos.x) + planeVector2.z*(planeVector1.y*planePos.x - planeVector1.x*planePos.y))/dif;
       vector outVector = vector(y*sightVector.x+cameraPos.x,y*sightVector.y+cameraPos.y,y*sightVector.z+cameraPos.z,1);
       return outVector;
    }
    
    bool checkRealIntersection(vector intersectionPos, float y)
    {
       return (intersectionPos.x >= fogCubeMin.x-0.1f && intersectionPos.x <= fogCubeMax.x+0.1f && intersectionPos.y >= fogCubeMin.y-0.1f && intersectionPos.y <= fogCubeMax.y+0.1f && intersectionPos.z >= fogCubeMin.z-0.1f && intersectionPos.z <= fogCubeMax.z+0.1f);
    }
    
    float getFogIntensity(vector inputPosition)
    {
          vector sightVector = vector(inputPosition.x - cameraPos.x,inputPosition.y - cameraPos.y,inputPosition.z - cameraPos.z,1);
          vector intersection[6];
          for(int j = 0; j < 6; j++)
              intersection[j] = vector(0,0,0,0);
          float intensity = 0.0f;
          int intersections = 0;
    
          //first check all 6 planes for intersection with the viewVector
    
          //first plane
          vector planeVector1 = vector(fogCubeMax.x-fogCubeMin.x,0,0,1);
          vector planeVector2 = vector(0,fogCubeMax.y-fogCubeMin.y,0,1);
          float y = 0;
          intersection[intersections] = getIntersectionPoint(sightVector,planeVector1,planeVector2,fogCubeMin,y);
          if(checkRealIntersection(intersection[intersections],y))
    	  intersections++;
    
           //second plane
          planeVector1  = vector(0,0,fogCubeMax.z-fogCubeMin.z,1);
          planeVector2 = vector(0,fogCubeMax.y-fogCubeMin.y,0,1);
    
          intersection[intersections] = getIntersectionPoint(sightVector,planeVector1,planeVector2,fogCubeMin,y);
          if(checkRealIntersection(intersection[intersections],y))
    	    intersections++;
    
          //3. plane
          planeVector1 = vector(fogCubeMax.x-fogCubeMin.x,0,0,1);
          planeVector2 = vector(0,0,fogCubeMax.z-fogCubeMin.z,1);
    
          intersection[intersections] = getIntersectionPoint(sightVector,planeVector1,planeVector2,fogCubeMin,y);
          if(checkRealIntersection(intersection[intersections],y))
    	    intersections++;
    
          //4. plane
          planeVector1 = vector(0,fogCubeMin.y-fogCubeMax.y,0,1);
          planeVector2 = vector(0,0,fogCubeMin.z-fogCubeMax.z,1);
    
          intersection[intersections] = getIntersectionPoint(sightVector,planeVector1,planeVector2,fogCubeMax,y);
          if(checkRealIntersection(intersection[intersections],y))
    	    intersections++;
    
          //5. plane
          planeVector1 = vector(0,fogCubeMin.y-fogCubeMax.y,0,1);
          planeVector2 = vector(fogCubeMin.x-fogCubeMax.x,0,0,1);
    
          intersection[intersections] = getIntersectionPoint(sightVector,planeVector1,planeVector2,fogCubeMax,y);
          if(checkRealIntersection(intersection[intersections],y))
    	    intersections++;
    
          //6. plane
          planeVector1 = vector(0,0,fogCubeMin.z-fogCubeMax.z,1);
          planeVector2 = vector(fogCubeMin.x-fogCubeMax.x,0,0,1);
    
          intersection[intersections] = getIntersectionPoint(sightVector,planeVector1,planeVector2,fogCubeMax,y);
          if(checkRealIntersection(intersection[intersections],y))
    	    intersections++;
    
          if(intersections == 1)
                 intensity = 1.0f;
          if(intersections >= 2)
                 intensity = 1.0f;
    
          return intensity;
    
    }
    

Anmelden zum Antworten