Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Textures Don't Appear on Quad: Solved

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2008
    Posts
    2

    Textures Don't Appear on Quad: Solved

    So I am in the process of porting my engine from windows to linux and seem to be having some trouble getting textures to display.

    Setting up gl using GLX:
    Code :
    		static int attributes[] = 
    		{	
    			GLX_RGBA,
    			GLX_DOUBLEBUFFER,
    			GLX_RED_SIZE, 8,
    			GLX_GREEN_SIZE, 8,
    			GLX_BLUE_SIZE, 8,
    			GLX_ALPHA_SIZE, 8,
    			GLX_DEPTH_SIZE, 16,
    			0
    		};
     
    		mpDisplay = XOpenDisplay( getenv("DISPLAY" ) );
    		mpVInfo = glXChooseVisual( mpDisplay,
    								   DefaultScreen(mpDisplay),
    								   attributes );
    		if( !mpVInfo )
    			cout << "Visual Info Empty" << endl;
     
    		//mpVInfo = XGetVisualInfo
    		mWinAttrs.event_mask = ExposureMask | VisibilityChangeMask |
    								StructureNotifyMask | ButtonPressMask |
    								ButtonReleaseMask | PointerMotionMask;
    		mWinAttrs.colormap = XCreateColormap( mpDisplay, DefaultRootWindow( mpDisplay ), mpVInfo->visual, AllocNone );    								
    		mWinAttrs.border_pixel = 0;
    		mWinAttrs.bit_gravity = StaticGravity;
    		mWinAttrs.background_pixel = 0;
    		mWinMask = CWBorderPixel | 
    					CWBitGravity | 
    					CWEventMask | 
    					CWColormap |
    					CWBackPixel;
     
     
     
    		//mWindow = XCreateSimpleWindow( mpDisplay, DefaultRootWindow( mpDisplay ), 20, 20, 400, 400, 0, 0, 0 );
     
    		mWindow = XCreateWindow( mpDisplay,
    								 DefaultRootWindow( mpDisplay ),
    								  0,
    								  0, 
    								  400, 
    								  400, 
    								  0, 
    								  mpVInfo->depth, 
    								  InputOutput, 
    								  mpVInfo->visual, 
    								  mWinMask, 
    								  &amp;mWinAttrs );							  
    		XChangeProperty( mpDisplay, mWindow, XA_WM_NAME, XA_STRING, 8, 0, (unsigned char *)(DEFCLASSNAME), strlen( DEFCLASSNAME ) );
    		XChangeProperty( mpDisplay, mWindow, XA_WM_ICON_NAME, XA_STRING, 8, 0, (unsigned char *)(DEFCLASSNAME), strlen( DEFCLASSNAME ) );
     
    		XMapWindow( mpDisplay, mWindow );
    		mContext = glXCreateContext( mpDisplay, mpVInfo, 0, True );
    		glXMakeCurrent( mpDisplay, mWindow, mContext );
    		mbMapped = false;

    Originally I was using the FreeImage library to load textures, but I took the code from Nehe on loading a tga to make sure there was no problem with this library:

    Code :
                    // yes texture exists elsewhere
                    glGenTextures( 1, &amp;texture );
    		glEnable( GL_TEXTURE_2D );
    		backTex = OpenglInterface::getSingleton().loadTexture( "background.tga" );
    		glBindTexture( GL_TEXTURE_2D, texture );
    		GLubyte TGAheader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0};
    		GLubyte TGAcompare[12];
    		GLubyte header[6];
    		GLuint bytesPerPixel;
    		GLuint imageSize;
    		GLuint temp;
    		GLuint type = GL_RGBA;
     
    		FILE *file = fopen( "background.tga", "rb" );
     
    		if(	file==NULL ||								// Does File Even Exist?
    			fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) ||	// Are There 12 Bytes To Read?
    			memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0		||	// Does The Header Match What We Want?
    			fread(header,1,sizeof(header),file)!=sizeof(header))			// If So Read Next 6 Header Bytes
    		{
    			if (file == NULL)							// Did The File Even Exist? *Added Jim Strong*
    				return 0;							// Return False
    			else
    			{
    				fclose(file);							// If Anything Failed, Close The File
    				return 0;							// Return False
    			}
    		}
     
    		GLuint width = header[1] * 256 + header[0];
    		GLuint height = header[3] * 256 + header[2];
     
    		bytesPerPixel = header[4]/8;
     
    		imageSize = width * height * bytesPerPixel;
    		GLubyte *imageData = new GLubyte[imageSize];
     
    		fread(imageData, 1, imageSize, file);
     
    		for( int i = 0; i < (int)imageSize; i++ )
    		{
    			temp = imageData[i];
    			imageData[i] = imageData[i+2];
    			imageData[i+2] = temp;
     
    		}
     
    		fclose( file );
     
    		if( bytesPerPixel == 3 )
    			type = GL_RGB;
     
    		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);		// Linear Filtered
    		glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);		// Linear Filtered
     
     
    		glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, type, GL_UNSIGNED_BYTE, imageData );
     
    		delete [] imageData;
    		imageData = 0;

    And Displaying The "Textured Quad":

    Code :
    		glEnable( GL_TEXTURE_2D );
    		glBindTexture( GL_TEXTURE_2D, 1 );
     
    		glBegin( GL_QUADS );
    		glTexCoord2f( 0, 0 );
    		glVertex2f( 20, 20 );
    		glTexCoord2f( 1, 0 );
    		glVertex2f( 200, 20 );
    		glTexCoord2f( 1, 1 );
    		glVertex2f( 200, 200 );
    		glTexCoord2f( 0, 1 );
    		glVertex2f( 20, 200 );
     
    		glEnd();


    Output:


    That should be all the pertinent code.

    Thanks.

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Feb 2006
    Location
    Sweden
    Posts
    744

    Re: Textures Don't Appear on Quad

    you can't just bind the texture like this glBindTexture( GL_TEXTURE_2D, 1 );
    you have to use glBindTexture( GL_TEXTURE_2D, texture);

    i also don't see a glGenTextures(1, &texture);

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2008
    Posts
    2

    Re: Textures Don't Appear on Quad

    Well actually you can. I've done it plenty of times for testing sake. It's not safe, but it works as long as you know where you've had textures binded, and you change it before the final product.

    In the real engine I did use the glGenTextures(...) and I just switched the code above to be safe anyway.

    Same problem. Still doesn't work.

    EDIT:
    Err... I just had one of those "I'm a complete idiot moments."

    When I created my test texture I must have been thinking of a screen resolution because I made it 1024 X 768 instead of 1024 X 1024. It didn't render because it wasn't a square texture.

    Sorry.

    My code was fine...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •