OpenGL unter Linux : Textur
-
Hallo !
Ich probiere seit 2 Tagen verzweifelt eine Bitmap einzulesen und als Textur in OpenGl zu benutzen .
Das Programm wird erstellt , keine Fehler oder Warnungen werden ausgespuckt ,
aber es wird keine Textur angezeigt , egal was ich tu .
Es gelingt mir nicht mal eine Textur zu laden .
Könntet ihr mir bitte helfen ?Mein Makefile:
eoglp: main.cpp g++ -o eoglp main.cpp -lGL -lGLU -lglut
main.cpp :
using namespace std; #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #define WIDTH 800 #define HEIGHT 600 #define TITLE "OpenGl" bool fullscreen = 0; GLuint texture[1]; GLfloat xrot = 0.0f; GLfloat yrot = 0.0f; GLfloat zrot = 0.0f; void keyboard( unsigned char key , int x , int y ); void reshape( int w , int h ); void display( void ); bool loadtexture( char *filename ); int main( int argc , char ** argv ){ if( !loadtexture( "NeHe.bmp" ) ){ cout << "Die Textur konnte nicht geladen werden !" << endl; }else{ cout << "Die Textur konnte geladen werden !" << endl; } glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH ); glutInitWindowPosition( 0 , 0 ); glutInitWindowSize( WIDTH , HEIGHT ); glutCreateWindow( TITLE ); glutDisplayFunc( display ); glutIdleFunc( display ); glutReshapeFunc( reshape ); glutKeyboardFunc( keyboard ); glutMainLoop(); return 0; } void keyboard( unsigned char key , int x , int y ){ switch (key){ case 'f': if (fullscreen){ glutReshapeWindow( WIDTH , HEIGHT ); glutPositionWindow( 0 , 0 ); }else{ glutFullScreen(); } fullscreen = !fullscreen; break; case 27: exit(0); break; } } void reshape( int w , int h ){ glViewport( 0 , 0 , w , h ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 45.0 , (double)w / (double)h , 0.1 , 100.0 ); glMatrixMode( GL_MODELVIEW ); } void display( void ){ glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity(); glTranslatef( 0.0f ,0.0f , -5.0f ); glRotatef( xrot , 1.0f ,0.0f ,0.0f ); glRotatef( yrot , 0.0f ,1.0f ,0.0f ); glRotatef( zrot , 0.0f ,0.0f ,1.0f ); glBindTexture( GL_TEXTURE_2D , texture[0] ); glBegin( GL_QUADS ); glTexCoord2f( 0.0f , 1.0f ); glVertex3f( -1.0f , 1.0f , 0.0f ); glTexCoord2f( 0.0f , 0.0f ); glVertex3f( -1.0f , -1.0f , 0.0f ); glTexCoord2f( 1.0f , 0.0f ); glVertex3f( 1.0f , -1.0f , 0.0f ); glTexCoord2f( 1.0f , 1.0f ); glVertex3f( 1.0f , 1.0f , 0.0f ); glEnd(); xrot += 0.125f; yrot += 0.000f; zrot += 0.175f; glutSwapBuffers(); } bool loadtexture( char *filename ){ FILE *file; unsigned short int typ; long int offset; int width; int height; short int planes; short int bpp; long int size; unsigned char *data; unsigned char temp; file = fopen( filename , "rb" ); if( file == NULL ){return false;} if( !fread( &typ , sizeof( short int ) , 1 , file ) ){return false;} if( typ != 19778 ){return false;} fseek( file , 8 , SEEK_CUR ); if( !fread( &offset , sizeof( long int ) , 1 , file )){return false;} fseek( file , 4 , SEEK_CUR ); fread( &width , sizeof( int ) , 1 , file ); fread( &height , sizeof( int ) , 1 , file ); fread( &planes , sizeof( short int ) , 1 , file ); if( planes != 1 ){return false;} if( !fread( &bpp , sizeof( short int ) , 1 , file )){return false;} if( bpp != 24 ){return false;} size = width * height * 3; data = ((unsigned char *) malloc( size ) ); fseek( file , offset , SEEK_SET ); if( !fread( data , size , 1 , file )){return false;} for( int i = 0 ; i < size ; i += 3 ){ temp = data[i]; data[i] = data[i + 2]; data[i + 2] = temp; } glGenTextures( 1 , &texture[0] ); glBindTexture( GL_TEXTURE_2D , texture[0] ); glTexImage2D( GL_TEXTURE_2D , 0 , 3 , width , height , 0 , GL_RGB , GL_UNSIGNED_BYTE , data ); glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER,GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER,GL_LINEAR ); return true; }
Ich wäre dankbar für jede Antwort.
-
Formate gibts bei wotsit. Ich empfehle für den Anfang aber tga.
Aber was hat das mit Lniux zu tun? Vielleicht hat das was gegen Windows Bitmaps...
Bye, TGGC \-/
-
du musst das Bitmap auch Laden
TextureImage[0]=LoadBMP("NeHe.bmp");
-
Hallo nochmal !
Die Klasse "AUX_RGBImageRec" , verbunden mit der Funktion "LoadBMP" ,
ist doch eigentlich eine Windowsfunktion oder , jedenfalls wird sie bei mir nicht gefunden .
Habe ich irgendeine Header-Datei nicht eingebunden ?Mir ist aber noch was unklar.
Die Funktion "LoadBMP" tut doch eigentlich nur die Höhe und Breite des Bildes sowie die Bilddaten
in die Klasse "AUX_R~Rec" schreiben oder .
Im Prinzip ist das doch das gleiche , wie das was ich gemacht habe ,nur das ich die Daten nicht in eine Klasse
gespeichert werden oder ?Danke TGGC , die Seite kannte ich nicht !
Aber ich bin mir eigentlich sicher , das ich die Daten richtig aus der BMP rauslese ,
wenn ich mir die "Zwischenergebnisse" mal ausspucken lasse ,
kommt eigentlich auch was sinnvolles raus.
Er liest h , w , bpp , planes , offset und size richtig aus , eigentlich kann es dann ja nur noch im data-teil
liegen oder ?Und mir ist TGA eigentlich auch lieber , mir frustriert es nur ungemein das ich das nicht schaffe ,
wobei ich das zu Windows-Zeiten noch geschafft habe!