In OpenGl Texturen anzeigen mit Hilfe von SDL



  • Hi,

    habe mich in nehe tutorial nun bis zum 6. kapitel vorgearbeitet... in diesem kapitel wird ja gezeigt, wie man texture mapping bewerkstelligt... mir ist der aufwand aber zu groß und fragte mich, ob man dies nicht mit SDL und einer SDL_Surface machen kann...
    das laden des bildes ist ja eigentlich genau so...

    ich hatte mir das so gedacht:

    GLuint texture;
    SDL_Surface* textureBMP = LoadBMP("tex.bmp");
    
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGB, GL_BITMAP, textureBMP);
    //ich wusste nun aber nicht genau, was die letzten beiden parameter sein sollen...
    glEnable(GL_TEXTURE_2D);
    

    und dann zum anzeigen:

    glBindTexture(GL_TEXTURE_2D, texture);        //warum wurde hier nochmal glBindTexture(...) aufgerufen?
    glBegin(GL_QUADS);
    		glTexCoord2f(0, 0);		glVertex2f(0,0);
    		glTexCoord2f(1, 0);		glVertex2f(1,0);
    		glTexCoord2f(1, 1);		glVertex2f(1,1);
    		glTexCoord2f(0, 1);		glVertex2f(0,1);
    	glEnd();
    

    hoffe, es ist verständlich, was ich meine...



  • Ja, interessanter Gedanke!

    Bye, TGGC (Wähle deine Helden)



  • Hi!
    Schau mal hier: http://www.libsdl.org/cgi/docwiki.cgi/SDL_5fSurface
    bei void *pixels stehen die Pixeldaten

    Zu den 2 letzten Parametern: http://www.mevis.de/~uwe/opengl/glTexImage2D.html

    Als Pixelformat gibst du RGB an. Um jetzt OpenGL klar zu machen, in welchen Format die einzelnen Farbkomponenten R, G, und B sind, musst du halt bei Type z. B. GL_UNSIGNED_BYTE angeben.

    Der letzte Parameter *pixels ist dann halt ein Pointer auf die Pixeldaten(besser gesagt Texeldaten) deiner Tetxur(also halt der *pixels Pointer vom SDL-Surface)

    mfg olli



  • okay danke vertex.. du hast mir schon ein bisschen weiter geholfen, daber es will nochimmer nicht wirklich funktionieren....
    wenn da mal jemand kurz drübergucken würde, dann wäre ich sehr dankbar...

    #include <SDL\SDL.h>
    #include <SDL\SDL_opengl.h>
    #include <iostream>
    
    using namespace std;
    
    void draw();
    
    GLfloat rtri = 0;
    GLuint texture;
    SDL_Surface* textureBMP;
    
    int main(int, char**)
    {
    	if( SDL_Init(SDL_INIT_VIDEO) < 0 )
    	{
    		cout << "SDL_Init(SDL_INIT_VIDEO) < 0!" << endl;
    		SDL_Quit();
    		exit(1);
    	}
    	textureBMP = SDL_LoadBMP("texture.bmp");
    	if(textureBMP == 0)
    	{
    		cout << "textureBMP == 0!" << endl;
    		SDL_Quit();
    		exit(1);
    	}
    
    	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
        SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
        SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
        SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
        SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    
    	SDL_Surface* screen = SDL_SetVideoMode( 1024, 768, 32, SDL_OPENGL | SDL_FULLSCREEN);
    
    	if(screen == 0)
    	{
    		cout << "screen == 0!" << endl;
    		SDL_Quit();
    		exit(1);
    	}
    
    	glGenTextures(1, &texture);
    	glBindTexture(GL_TEXTURE_2D, texture);
    	glTexImage2D(GL_TEXTURE_2D, 0, 3, 64, 64, 0, GL_RGB, GL_UNSIGNED_BYTE, textureBMP->pixels);
    	glEnable(GL_TEXTURE_2D);
    	glShadeModel(GL_SMOOTH);
    
    	while(SDL_GetTicks() <= 5000)
    	{
    		draw();
    		SDL_GL_SwapBuffers();
    	}
    	SDL_FreeSurface(textureBMP);
    	SDL_FreeSurface(screen);
    }
    
    void draw()
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	glLoadIdentity();
    
    	glRotatef(rtri, 1, 0, 0);
    
    	glBindTexture(GL_TEXTURE_2D, texture);
    
    	glBegin(GL_QUADS);
    		glTexCoord2f(0, 0);		glVertex2f(0,0);
    		glTexCoord2f(1, 0);		glVertex2f(1,0);
    		glTexCoord2f(1, 1);		glVertex2f(1,1);
    		glTexCoord2f(0, 1);		glVertex2f(0,1);
    	glEnd();
    
    	rtri += 0.1;
    }
    


  • okay.. es funktioniert nun... habe das filtering vergessen....

    aber danke für die antworten...


Anmelden zum Antworten