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 */
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
@Ashrossy: Das Programm und das Image, das ich gepostet habe, funktionieren nicht, wenn Sie es auf Ihrem System erstellen? – genpfault
Nein, Segfault. Ich bin nicht großartig mit dem Debugger, aber es ist offensichtlich die zweite gluBuild2DMipmaps. – sudorossy