2010-11-18 6 views
2

Ich versuche, eine SDL-Oberfläche in eine OpenGL-Textur mit einem Codeschnipsel zu konvertieren, die ich im Internet gefunden habe, nach vielen Stunden der Suche scheinen die gleichen Funktionen in der gleichen zu verwenden Ich gehe davon aus, dass ich die Dinge richtig mache.White Quad Konvertierung SDL_Surface zu OPENGL_Texture

Selbst Critisizing, ich bin mein Code ein wenig zu viel Aufspaltung, Ich mag Dinge organisiert halten, aber es kann etwas in einer Datei begraben sein Ich gehe nicht in viel ...

Kurz und gut, meine App soll 2 Würfel rendern, beide drehen und erlauben, dass man bewegt wird.

Würfel können mit einer Klasse erstellt werden, die ich geschrieben habe, einfach eine definieren und ihr einen Dateinamen geben, sie sollte diese Textur laden und sie auf den Würfel anwenden, wenn die Show-Funktion aufgerufen wird.

Ich hatte es teilweise mit der SOIL-Bibliothek arbeiten, aber ich habe eine Menge Code nach SDL bewegt und ich würde lieber stattdessen IMG_Load verwenden.

Hier ist der Code

GLuint loadTexture(std::string filename) 
{ 
    GLuint texture; 
    if(SDL_Surface* surfaceTex = IMG_Load(filename.c_str())) 
    { 
     glPixelStorei(GL_UNPACK_ALIGNMENT,4); 
     glGenTextures(1,&texture); 
     glBindTexture(GL_TEXTURE_2D,texture); 
     SDL_PixelFormat *format = surfaceTex->format; 
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); 
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); 
     if (format->Amask) 
     { 
      gluBuild2DMipmaps(GL_TEXTURE_2D,4,surfaceTex->w,surfaceTex->h,GL_RGBA,GL_UNSIGNED_BYTE,surfaceTex->pixels); 
     } 
     else 
     { 
      gluBuild2DMipmaps(GL_TEXTURE_2D,3,surfaceTex->w,surfaceTex->h,GL_RGBA,GL_UNSIGNED_BYTE,surfaceTex->pixels); 
     } 
     SDL_FreeSurface(surfaceTex); 
    } 
    else 
    { 
     log("Loading texture failed!",true); 
    } 
    return texture; 
} 

ich wirklich den Code wollen portable zwischen den Projekten sein, so kann ich nur sagen,

Gluint Tex = loadTexture(filename); 

und die Textur ist fertig.

UPDATE:

Hier ist die Show-Methode zur Darstellung eines Würfels

cubeTexture = loadTexture(filename); 
glLoadIdentity(); 
glTranslatef(xPos,yPos,zPos); 
xRotate = 1.0; 
yRotate = 1.0; 
zRotate = 1.0; 
glRotatef(angle,xRotate,yRotate,zRotate); 
glEnable(GL_TEXTURE_2D); 
glBindTexture(GL_TEXTURE_2D, cubeTexture); 
glBegin(GL_QUADS); 
    /* Top face */ 
    glNormal3f(0.0f,0.0f,1.0f); /* Top face normal */ 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(Width,  Height, -Depth); /* top right */ 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-Width,  Height, -Depth); /* top left */ 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-Width,  Height,  Depth); /* bottom left */ 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(Width,  Height,  Depth); /* bottom right */ 
    /* Bottom face */ 
    glNormal3f(0.0f,0.0f,-1.0f); /* Bottom face normal */ 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(Width,  -Height,  Depth); /* top right */ 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-Width,  -Height,  Depth); /* top left */ 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-Width,  -Height, -Depth); /* bottom left */ 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(Width,  -Height, -Depth); /* bottom right */ 
    /* Front face */ 
    glNormal3f(0.0f,1.0f,0.0f); /* Front face normal */ 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(Width,  Height,  Depth); /* top right */ 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-Width,  Height,  Depth); /* top left */ 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-Width,  -Height,  Depth); /* bottom left */ 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(Width,  -Height,  Depth); /* bottom right */ 
    /* Back face */ 
    glNormal3f(0.0f,-1.0f,0.0f); /* Back face normal */ 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(Width,  -Height, -Depth); /* top right */ 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-Width,  -Height, -Depth); /* top left */ 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-Width,  Height, -Depth); /* bottom left */ 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(Width,  Height, -Depth); /* bottom right */ 
    /* Left face */ 
    glNormal3f(-1.0f,0.0f,0.0f); /* Left face normal */ 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(-Width,  Height,  Depth); /* top right */ 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(-Width,  Height, -Depth); /* top left */ 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-Width,  -Height, -Depth); /* bottom left */ 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-Width,  -Height,  Depth); /* bottom right */ 
    /* Right face */ 
    glNormal3f(1.0f,0.0f,0.0f); /* Right face normal */ 
    glTexCoord2f(0.0f, 0.0f); glVertex3f(Width,  Height, -Depth); /* top right */ 
    glTexCoord2f(1.0f, 0.0f); glVertex3f(Width,  Height,  Depth); /* top left */ 
    glTexCoord2f(1.0f, 1.0f); glVertex3f(Width,  -Height,  Depth); /* bottom left */ 
    glTexCoord2f(0.0f, 1.0f); glVertex3f(Width,  -Height, -Depth); /* bottom right */ 

glEnd(); 

und meine GL_INIT Funktion

glEnable(GL_TEXTURE_2D);   /* Enable Texture Mapping       */ 
    glShadeModel(GL_SMOOTH);   /* Enable smooth shading       */ 
    glClearColor(0.0f,0.0f,0.0f,0.0f); /* Set the background black       */ 
    glClearDepth(1.0f);     /* Set the depth buffer up       */ 
    glEnable(GL_DEPTH_TEST);   /* Set Depth testing up        */ 
    glDepthFunc(GL_LEQUAL);    /* Sets the type of depth testing to Less or Equal */ 
    glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); /* Uses the nice perspective calcs */ 
    glLightfv(GL_LIGHT1,GL_AMBIENT,LightAmbient);  /* Sets up the ambient light  */ //test 
    glLightfv(GL_LIGHT1,GL_DIFFUSE,LightDiffuse);  /* Sets up the diffuse light  */ //test 
    glLightfv(GL_LIGHT1,GL_POSITION,LightPosition);  /* Sets up the light position  */ //test 
    glEnable(GL_LIGHT1);        /* Enable the first light   */ 

Antwort

1

Stellen Sie sicher, glEnable(GL_TEXTURE_2D).

Auch Ihre zweite gluBuild2DMipmaps() Anruf sollte GL_RGB, nicht GL_RGBA. Sonst liest es nach dem Ende surfaceTex->pixels ab.

Try this:

#include <iostream> 

#include <SDL.h> 
#include <SDL_opengl.h> 
#include <SDL_image.h> 

using namespace std; 

GLuint loadTexture(std::string filename) 
{ 
    GLuint texture; 
    if(SDL_Surface* surfaceTex = IMG_Load(filename.c_str())) 
    { 
     glPixelStorei(GL_UNPACK_ALIGNMENT,4); 
     glGenTextures(1,&texture); 
     glBindTexture(GL_TEXTURE_2D,texture); 
     SDL_PixelFormat *format = surfaceTex->format; 
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); 
     glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); 
     if (format->Amask) 
     { 
      gluBuild2DMipmaps(GL_TEXTURE_2D,4,surfaceTex->w,surfaceTex->h,GL_RGBA,GL_UNSIGNED_BYTE,surfaceTex->pixels); 
     } 
     else 
     { 
      gluBuild2DMipmaps(GL_TEXTURE_2D,3,surfaceTex->w,surfaceTex->h,GL_RGB,GL_UNSIGNED_BYTE,surfaceTex->pixels); 
     } 
     SDL_FreeSurface(surfaceTex); 
    } 
    else 
    { 
     return 0; 
    } 
    return texture; 
} 


int main(int argc, char* argv[]) 
{ 
    _putenv("SDL_VIDEO_CENTERED=1"); 

    SDL_Init(SDL_INIT_EVERYTHING); 

    int win_w = 800; 
    int win_h = 600; 
    SDL_Surface* display = SDL_SetVideoMode(win_w, win_h, 32, SDL_OPENGL); 

    GLuint tex = loadTexture("concrete.png"); 

    bool running = true; 
    while(running) 
    { 
     // handle all pending events 
     SDL_Event event; 
     while(SDL_PollEvent(&event)) 
     { 
      switch (event.type) 
      { 
       case SDL_KEYDOWN: 
        if(event.key.keysym.sym == SDLK_ESCAPE) 
         running = false; 
        break; 
       case SDL_QUIT: 
        running = false; 
        break; 
       default: 
        break; 
      } 
     } 

     glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

     glMatrixMode(GL_PROJECTION); 
     glLoadIdentity(); 
     glOrtho(0, win_w, 0, win_h, -10.0, 10.0); 
     glMatrixMode(GL_MODELVIEW); 
     glLoadIdentity(); 

     glEnable(GL_TEXTURE_2D); 
     glBindTexture(GL_TEXTURE_2D, tex); 

     glPushMatrix(); 
     glScalef(200,200,200); 
     glBegin(GL_TRIANGLES); 
     glTexCoord2f(0,0); 
     glVertex2f(0,0); 
     glTexCoord2f(1,0); 
     glVertex2f(1,0); 
     glTexCoord2f(1,1); 
     glVertex2f(1,1); 
     glEnd(); 
     glPopMatrix(); 

     SDL_GL_SwapBuffers(); 
     SDL_Delay(1); 
    } 

    SDL_Quit(); 

    return 0; 
} 

concrete.png:

concrete.png

+0

wieder, kein Glück, ich habe ein paar mehr von meinem Code hochgeladen, so dass Sie ein besseres Gefühl von dem, was ich tue, bekommen können. Versuchen, diesen Code ein bisschen zu modular zu machen, denke ich ... – sudorossy

+0

@Ashrossy: Das Programm und das Image, das ich gepostet habe, funktionieren nicht, wenn Sie es auf Ihrem System erstellen? – genpfault

+0

Nein, Segfault. Ich bin nicht großartig mit dem Debugger, aber es ist offensichtlich die zweite gluBuild2DMipmaps. – sudorossy

0
+0

wo genau soll ich das in meinem Code zu setzen? Vielen Dank im Voraus. – sudorossy

+0

Genau dort, wo Sie 'glPixelStorei (GL_UNPACK_ALIGNMENT, 4)' haben. – genpfault

+0

Ich ersetzte gluBuild2DMipmaps durch glTexImage2D und kein Glück. Ich habe sogar die Funktion durch eine von einem SDL-Port von Nehes Tutorials ersetzt. Kein Glück. Ich gehe jetzt davon aus, dass dieses Problem mehr ist als das einfache Laden von Bildern. – sudorossy