Ich versuche derzeit OpenGL zu lernen, allerdings stehe ich schon vor dem ersten Problem: Wie binde ich bequem mehrere Texturen ein? Meine derzeitige Lösung funktioniert nichtmal wirklich, außerdem ist sie sehr unpraktisch. Sie besteht eigentlich nur in Wiederholen der Funktionsaufrufe, allerdings funktioniert das nicht. Die Bitmaps "dice5" und "dice6" werden nämlich nicht eingebunden, obwohl es keine Fehlermeldung gibt.
Der Code ist von NeHe (GameDev) da ich deren Tutorials verwende.
Quellcode
- int LoadGLTextures() // Load Bitmaps And Convert To Textures
- {
- int Status=FALSE; // Status Indicator
- AUX_RGBImageRec *TextureImage[6]; // Create Storage Space For The Texture
- memset(TextureImage,0,sizeof(void *)*1); // Set The Pointer To NULL
- // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
- if (TextureImage[0]=LoadBMP("Data/dice1.bmp"))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(1, &texture[0]);
- // Create MipMapped Texture
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
- gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TextureImage[0]->sizeX, TextureImage[0]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); // ( NEW )
- }
- if (TextureImage[0]) // If Texture Exists
- {
- if (TextureImage[0]->data) // If Texture Image Exists
- {
- free(TextureImage[0]->data); // Free The Texture Image Memory
- }
- free(TextureImage[0]); // Free The Image Structure
- }
- if (TextureImage[1]=LoadBMP("Data/dice2.bmp"))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(1, &texture[1]);
- // Create MipMapped Texture
- glBindTexture(GL_TEXTURE_2D, texture[1]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
- gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TextureImage[1]->sizeX, TextureImage[1]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data); // ( NEW )
- }
- if (TextureImage[1]) // If Texture Exists
- {
- if (TextureImage[1]->data) // If Texture Image Exists
- {
- free(TextureImage[1]->data); // Free The Texture Image Memory
- }
- free(TextureImage[1]); // Free The Image Structure
- }
- if (TextureImage[2]=LoadBMP("Data/dice3.bmp"))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(1, &texture[2]);
- // Create MipMapped Texture
- glBindTexture(GL_TEXTURE_2D, texture[2]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
- gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TextureImage[2]->sizeX, TextureImage[2]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[2]->data); // ( NEW )
- }
- if (TextureImage[2]) // If Texture Exists
- {
- if (TextureImage[2]->data) // If Texture Image Exists
- {
- free(TextureImage[2]->data); // Free The Texture Image Memory
- }
- free(TextureImage[2]); // Free The Image Structure
- }
- if (TextureImage[3]=LoadBMP("Data/dice4.bmp"))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(1, &texture[3]);
- // Create MipMapped Texture
- glBindTexture(GL_TEXTURE_2D, texture[3]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
- gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TextureImage[3]->sizeX, TextureImage[3]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[3]->data); // ( NEW )
- }
- if (TextureImage[3]) // If Texture Exists
- {
- if (TextureImage[3]->data) // If Texture Image Exists
- {
- free(TextureImage[3]->data); // Free The Texture Image Memory
- }
- free(TextureImage[3]); // Free The Image Structure
- }
- return Status; // Return The Status
- if (TextureImage[4]=LoadBMP("Data/dice5.bmp"))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(1, &texture[4]);
- // Create MipMapped Texture
- glBindTexture(GL_TEXTURE_2D, texture[4]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
- gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TextureImage[4]->sizeX, TextureImage[4]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[4]->data); // ( NEW )
- }
- if (TextureImage[4]) // If Texture Exists
- {
- if (TextureImage[4]->data) // If Texture Image Exists
- {
- free(TextureImage[4]->data); // Free The Texture Image Memory
- }
- free(TextureImage[4]); // Free The Image Structure
- }
- if (TextureImage[5]=LoadBMP("Data/dice6.bmp"))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(1, &texture[5]);
- // Create MipMapped Texture
- glBindTexture(GL_TEXTURE_2D, texture[5]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
- gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TextureImage[5]->sizeX, TextureImage[5]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[5]->data); // ( NEW )
- }
- if (TextureImage[5]) // If Texture Exists
- {
- if (TextureImage[5]->data) // If Texture Image Exists
- {
- free(TextureImage[5]->data); // Free The Texture Image Memory
- }
- free(TextureImage[5]); // Free The Image Structure
- }
- return Status;
- }
Ich hätte drei Ideen das zu Lösen:
- Speichern der Bitmapnamen in einem Array
- Speichern der Bitmapnamen in einer Textdatei
- einbinden aller Bitmaps in einem Verzeichnis
mfg, bloodycross
Edit: Ich werde es jetzt mal mit einem string-Array oder einem zweidimensionalem char-Array versuchen, wobei ich immer den Zeiger übergebe.
Edit2:
Mittlerweile, einige Stunden Coding und vor allem Bücher lesen später, habe ich eine funktionierende Lösung gefunden.
Die Pfade sind in Strings gespeichert, welche dann in C-Strings (char-Arrays) kopiert werden. Anschließen wird auf jeden C-String eine Zeiger erstellt, dieser wird dann übergeben. Die Funktion zum einbinden der Grafiken wird in einer Schleife aufgerufen.
Vielleicht füge ich noch eine Funktion ein, die die Pfade aus einer Textdatei einliest.
Quellcode
- std::string Pfad[7];
- std::string::iterator i;
- Pfad[0] = "Data/dice1.bmp"; //Pfade zu den Grafiken
- Pfad[1] = "Data/dice2.bmp";
- Pfad[2] = "Data/dice3.bmp";
- Pfad[3] = "Data/dice4.bmp";
- Pfad[4] = "Data/dice5.bmp";
- Pfad[5] = "Data/dice6.bmp";
- char *Zeiger[7]; //Zeiger fuer das laden der Grafiken
- char charPfad[7][100];
- for(int j=0;j<6;j++) //Auf 0 setzen
- {
- for(int k=0; k<100; k++)
- {
- charPfad[j][k] = 0;
- }
- }
- for(int j=0;j<6;j++) //Strings in C-Strings (char-Arrays) kopieren
- {
- int k = 0;
- for(i=Pfad[j].begin(); i!=Pfad[j].end(); i++)
- {
- charPfad[j][k] = *i;
- k++;
- }
- }
- for(int j=0; j<6; j++) //Zeiger Array belegen
- {
- Zeiger[j] = charPfad[j];
- }
- for(int j=0; j<6; j++)
- {
- // Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
- if (TextureImage[j]=LoadBMP(Zeiger[j]))
- {
- Status=TRUE; // Set The Status To TRUE
- glGenTextures(1, &texture[j]);
- // Create MipMapped Texture
- glBindTexture(GL_TEXTURE_2D, texture[j]);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); // ( NEW )
- gluBuild2DMipmaps(GL_TEXTURE_2D, 1, TextureImage[j]->sizeX, TextureImage[j]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[j]->data); // ( NEW )
- }
- if (TextureImage[0]) // If Texture Exists
- {
- if (TextureImage[0]->data) // If Texture Image Exists
- {
- free(TextureImage[0]->data); // Free The Texture Image Memory
- }
- free(TextureImage[0]); // Free The Image Structure
- }
- }