[OpenGL] Texture Mapping



  • Hi.
    Bin neu im Forum und relativ neu in der Spieleprogrammierung. Hab jetzt ein wenig was programmiert. Das läuft ganz gut.
    Aber ich hab nochmal ne Frage zum Texturmapping.
    Ich habe ein Bild welches viele Subbilder enthält, auch Spritesheet genannt. Jetzt habe ich dort folgende Daten:
    Linke obere Ecke des Bildes + Breite / Höhe des Bildes.
    Als Beispiel:
    Bild0: 100,100 und 50,50 als Größe.
    Irgendwie finde ich gerade keinen Weg zu berechnen wie man das jetzt in OpenGL als Koordinate für die Textur angibt :D.
    Einfach nur 1 / 100? Weil das ganze Bild ist ja 1.0f, durch 100 wären 0.01, aber irgendwie klappt das nicht ganz :D. Es wird eine schwarze Textur angezeigt :(. Danke für die Hilfe.

    So lade ich das SpriteSheet:

    void SpriteSheet::load( Texture* texture, const char* mapname, std::list<Texture*>& texture_list )
    {
        std::ifstream stream(mapname);
        if ( stream.is_open() )
        {
            std::string line;
            while ( !stream.eof() )
            {
                std::string name, skip;
                u32 top_x = 0, top_y = 0;
                u32 size_x = 0, size_y = 0;
                std::stringstream ss;
    
                std::getline(stream, line);
    
                ss << line;
                ss >> name;
                ss >> skip;
                ss >> top_x;
                ss >> top_y;
                ss >> size_x;
                ss >> size_y;
    
                if ( !name.empty() )
                {
                    TextureCoord c[4];
                    this->buildCoords(top_x, top_y, size_x, size_y, c);
                    Texture* sheet_part = new SpriteTexture(name.c_str(), texture->getId(), c);
                    texture_list.push_back(sheet_part);
                }
            }
        }
    }
    

    So schaut die Datendatei des Sheets aus:

    background = 0 0 640 480
    spaceship = 898 0 64 64
    rock = 0 481 512 512
    water = 641 0 256 256

    Hoffe Ihr könnt mir helfen :).


  • Mod

    falls du hilfe bei der buildCoords function brauchst, musst du uns schon noch ihren source zeigen 😉



  • rapso schrieb:

    falls du hilfe bei der buildCoords function brauchst, musst du uns schon noch ihren source zeigen 😉

    Sorry war spät, aber ich bin mir sicher der Code ist falsch :D.

    void SpriteSheet::buildCoords(const u32 pos_x, const u32 pos_y, const u32 size_x, const u32 size_y, TextureCoord* coords)
    {
        coords[0].s = 0;
        coords[0].t = 0;
    
        coords[1].s = 1/(pos_x + size_x);
        coords[1].t = 1/(pos_y*0.0001);
    
        coords[2].s = 1/(pos_x+size_y);
        coords[2].t = 0.0f;
    
        coords[3].s = 0.0f;
        coords[3].t = 0.0f;
    }
    

    Das war mein Versuch von heute nacht 😞
    Ich verstehe einfach nicht, wie ich die Korrdinaten richtig mappen kann/muss. 🤡
    lg


  • Mod

    L33TF4N schrieb:

    rapso schrieb:

    falls du hilfe bei der buildCoords function brauchst, musst du uns schon noch ihren source zeigen 😉

    Sorry war spät, aber ich bin mir sicher der Code ist falsch :D.

    void SpriteSheet::buildCoords(const u32 pos_x, const u32 pos_y, const u32 size_x, const u32 size_y, TextureCoord* coords)
    {
        coords[0].s = 0;
        coords[0].t = 0;
    
        coords[1].s = 1/(pos_x + size_x);
        coords[1].t = 1/(pos_y*0.0001);
    
        coords[2].s = 1/(pos_x+size_y);
        coords[2].t = 0.0f;
    
        coords[3].s = 0.0f;
        coords[3].t = 0.0f;
    }
    

    Das war mein Versuch von heute nacht 😞
    Ich verstehe einfach nicht, wie ich die Korrdinaten richtig mappen kann/muss. 🤡
    lg

    ich hoffe deine ausrede hat mit viel alkohol zu tun 😛

    du berechnest den skalierungsfaktor, wie du schon sagtest, neueskalierung/alteskalierung
    und multiplizierst alle koordinaten beim eintragen in dein vertex array.

    0.0f kommt dabei sicherlich nicht vor, du gibst doch selbst pos_xy und size_xy an.

    schau nochmal drueber, ist eine einfache sache 😉


Anmelden zum Antworten