2016-05-09 17 views
0

Ich versuche Terrain zu folgen, und ich bekomme eine negative Kameraposition in der XZ-Ebene. Jetzt bekomme ich eine Ausnahme außerhalb der Grenzen, weil die Zeile oder das Col negativ ist. Wie würde ich die Zelle des Gitters korrekt in den Ursprung transformieren und negative Kamerakoordinaten geben? HierNegative Zeile und Spalte im Gelände folgenden Algorithmus

sind die beiden Funktionen

int cGrid::getHeightmapEntry(int row, int col) 
{ 
    return m_heightmap[row * 300 + col]; 
} 

float cGrid::getHeight(float x, float z, float _width, float _depth, int _cellSpacing) 
{ 
    // Translate on xz-plane by the transformation that takes 
    // the terrain START point to the origin. 
    x = ((float)_width/2.0f) + x; 
    z = ((float)_depth/2.0f) - z; 

    // Scale down by the transformation that makes the 
    // cellspacing equal to one. This is given by 
    // 1/cellspacing since; cellspacing * 1/cellspacing = 1. 
    x /= (float)_cellSpacing; 
    z /= (float)_cellSpacing; 

    // From now on, we will interpret our positive z-axis as 
    // going in the 'down' direction, rather than the 'up' direction. 
    // This allows to extract the row and column simply by 'flooring' 
    // x and z: 

    float col = ::floorf(x); 
    float row = ::floorf(z); 

    if (row < 0 || col<0) 
    { 
     row = 0; 
    } 
    // get the heights of the quad we're in: 
    // 
    // A B 
    // *---* 
    // |/| 
    // *---* 
    // C D 

    float A = getHeightmapEntry(row, col); 
    float B = getHeightmapEntry(row, col + 1); 
    float C = getHeightmapEntry(row + 1, col); 
    float D = getHeightmapEntry(row + 1, col + 1); 

    // 
    // Find the triangle we are in: 
    // 

    // Translate by the transformation that takes the upper-left 
    // corner of the cell we are in to the origin. Recall that our 
    // cellspacing was nomalized to 1. Thus we have a unit square 
    // at the origin of our +x -> 'right' and +z -> 'down' system. 
    float dx = x - col; 
    float dz = z - row; 

    // Note the below compuations of u and v are unneccessary, we really 
    // only need the height, but we compute the entire vector to emphasis 
    // the books discussion. 
    float height = 0.0f; 
    if (dz < 1.0f - dx) // upper triangle ABC 
    { 
     float uy = B - A; // A->B 
     float vy = C - A; // A->C 

     // Linearly interpolate on each vector. The height is the vertex 
     // height the vectors u and v originate from {A}, plus the heights 
     // found by interpolating on each vector u and v. 
     height = A + Lerp(0.0f, uy, dx) + Lerp(0.0f, vy, dz); 
    } 
    else // lower triangle DCB 
    { 
     float uy = C - D; // D->C 
     float vy = B - D; // D->B 

     // Linearly interpolate on each vector. The height is the vertex 
     // height the vectors u and v originate from {D}, plus the heights 
     // found by interpolating on each vector u and v. 
     height = D + Lerp(0.0f, uy, 1.0f - dx) + Lerp(0.0f, vy, 1.0f - dz); 
    } 

    return height; 
} 

enter image description here

float height = m_Grid.getHeight(position.x, position.y, 49 * 300, 49 * 300, 6.1224489795918367f); 
    if (height != 0) 
    { 
     position.y = height + 10.0f; 
    } 

    m_Camera.SetPosition(position.x, position.y, position.z); 


bool cGrid::readRawFile(std::string fileName, int m, int n) 
{ 

    // A height for each vertex 
    std::vector<BYTE> in(m*n); 
    std::ifstream inFile(fileName.c_str(), std::ios_base::binary); 
    if (!inFile) 
     return false; 
    inFile.read(
     (char*)&in[0], // buffer 
     in.size());// number of bytes to read into buffer 
    inFile.close(); 
    // copy BYTE vector to int vector 
    m_heightmap.resize(n*m); 
    for (int i = 0; i < in.size(); i++) 
     m_heightmap[i] = (float)((in[i])/255)*50.0f; 
    return true; 
} 

m_Grid.readRawFile("castlehm257.raw", 50, 50); 
+0

Ich würde empfehlen, den DX11-Tag und das Hinzufügen des Grafik-Tages .. oder etwas anderes mehr Germane zu entfernen - Sie Ich muss für diese Frage ein breiteres Publikum treffen. Versuchen Sie auch, auf gamedev.stackexchange.com zu posten – xaxxon

+0

danke haben Ihre Vorschläge –

+0

oh, aus irgendeinem Grund dachte ich, Sie könnten nur 4 Tags haben. – xaxxon

Antwort

1

I ableiten, dass Sie sich in einem 300 x 300-Matrix eine 50 mal 50-Matrix sind speichern, ein Raster von 49 darstellen durch 49 Zellen. Ich folge auch, dass m_Grid ein Objekt vom Typ cGrid ist. Ihr Code scheint die folgenden Fehler zu enthalten:

Argument (2) von Anruf m_Grid.getHeight ist kein z Wert.

Argument (3) des Aufrufs m_Grid.getHeight stimmt nicht mit Argument (5) überein.

Das Argument (4) des Aufrufs m_Grid.getHeight stimmt nicht mit Argument (5) überein.

Implizite Umwandlung von Literal float zu int in Argument (5) von Aufruf m_Grid.getHeight - der Wert wird abgeschnitten.

Versuchen Sie, Ihre Funktion aufrufen, um dies zu ändern:

float height = m_Grid.getHeight(position.x, position.z, 49 * cellspacing, 49 * cellspacing, cellspacing); 

- wo cellspacing wie im Diagramm definiert ist.

Versuchen Sie auch, den Parameter (5) von cGrid::getHeight von int _cellSpacing zu float _cellSpacing zu ändern.

(Ich habe das ein paar Mal als mein Verständnis des Codes entwickelt hat, beantworten bearbeitet.)

+0

vielen Dank, aber das Gelände folgt jetzt, aber es ist nicht genau, in einigen Hügeln folgt es überhaupt nicht, und das folgende ist nicht genau, was soll ich tun, um es zu verbessern –

+0

Ich schlage vor, Sie stellen sicher, dass Sie ALLE gefolgt sind von meinen Vorschlägen, einschließlich des einen über das Ändern des 'int' zu' float'. Ich schlage vor, dass Sie Ihren Code auch auf andere inkorrekte Verwendungen von "int" anstelle von "float" überprüfen. Ich schlage vor, dass Sie 'm_heightmap [i] = (float) ((in [i])/255) * 50.0f ändern;' zu 'm_heightmap [i] = ((float) in [i]) * 50.0f/255.0 f; '. – Entropy

+0

Und wenn diese Vorschläge Ihr Problem nicht lösen, schlage ich vor, dass Sie mehr von Ihrem Code (möglicherweise beginnend mit dem, der das Bild des Geländes darstellt) und mehr Informationen über die Art Ihres Problems veröffentlichen. Erwägen Sie, dies als eine separate Frage zu veröffentlichen und dann den Link dazu in einem Kommentar hier zu veröffentlichen.Ich empfehle auch, dass Sie die Deklarationen von Variablen und Objektinstanzen anzeigen, wenn Sie Hilfe suchen, um zu verdeutlichen, um welchen Typ es sich bei jedem Objekt handelt. – Entropy