Thread: OpenGL Again, Loading Textures onto Quads

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    OpenGL Again, Loading Textures onto Quads

    Code:
    AUX_RGBImageRec *LoadBMP(char *Filename)				// Loads A Bitmap Image
    {
    	FILE *File=NULL;									// File Handle
    
    	if (!Filename)										// Make Sure A Filename Was Given
    	{
    		return NULL;									// If Not Return NULL
    	}
    
    	File=fopen(Filename,"r");							// Check To See If The File Exists
    
    	if (File)											// Does The File Exist?
    	{
    		fclose(File);									// Close The Handle
    		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
    	}
    
    	return NULL;										// If Load Failed Return NULL
    }
    
    int LoadGLTextures()									// Load Bitmaps And Convert To Textures
    {
    	int Status=FALSE;									// Status Indicator
    
    	AUX_RGBImageRec *TextureImage[2];					// Create Storage Space For The Texture
    
    	memset(TextureImage,0,sizeof(void *)*1);           	// Set The Pointer To NULL
    
    	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    	if (TextureImage[0]=LoadBMP("Data/NeHe1.bmp"))
    	{
    		Status=TRUE;									// Set The Status To TRUE
    
    		glGenTextures(1, &texture[0]);					// Create The Texture
    
    		// Typical Texture Generation Using Data From The Bitmap
    		glBindTexture(GL_TEXTURE_2D, texture[0]);
    		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    	}
    	if (TextureImage[1]=LoadBMP("Data/NeHe.bmp"))
    	{
    		Status=TRUE;
    
    		glGenTextures(2, &texture[1]);
    
    		glBindTexture(GL_TEXTURE_2D, texture[1]);
    		glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[1]->sizeX, TextureImage[1]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[1]->data);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	
    	}
    
    	if (TextureImage[0])									// If Texture Exists
    	{
    		if (TextureImage[0]->data)							// If Texture Image Exists
    		{
    			free(TextureImage[0]->data);					// Free The Texture Image Memory
    		}
    
    		free(TextureImage[0]);								// Free The Image Structure
    	}
    
    	return Status;										// Return The Status
    }
    
    GLvoid ReSizeGLScene(GLsizei width, GLsizei height)		// Resize And Initialize The GL Window
    {
    	if (height==0)										// Prevent A Divide By Zero By
    	{
    		height=1;										// Making Height Equal One
    	}
    
    	glViewport(0,0,width,height);						// Reset The Current Viewport
    
    	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix
    	glLoadIdentity();									// Reset The Projection Matrix
    
    	// Calculate The Aspect Ratio Of The Window
    	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
    
    	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix
    	glLoadIdentity();									// Reset The Modelview Matrix
    }
    
    int InitGL(GLvoid)										// All Setup For OpenGL Goes Here
    {
    	if (!LoadGLTextures())								// Jump To Texture Loading Routine ( NEW )
    	{
    		return FALSE;									// If Texture Didn't Load Return FALSE
    	}
    
    	glEnable(GL_TEXTURE_2D);							// Enable Texture Mapping ( NEW )
    	glShadeModel(GL_SMOOTH);							// Enable Smooth Shading
    	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);				// Black Background
    	glClearDepth(1.0f);									// Depth Buffer Setup
    	glEnable(GL_DEPTH_TEST);							// Enables Depth Testing
    	glDepthFunc(GL_LEQUAL);								// The Type Of Depth Testing To Do
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	// Really Nice Perspective Calculations
    	return TRUE;										// Initialization Went OK
    }
    
    int DrawGLScene(GLvoid)									// Here's Where We Do All The Drawing
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
    	glLoadIdentity();									// Reset The View
    	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);
    		// Front Face
    		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    		// Back Face
    	glBindTexture(GL_TEXTURE_2D, texture[1]);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    		// Top Face
    	glBindTexture(GL_TEXTURE_2D, texture[0]);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    		// Bottom Face
    	glBindTexture(GL_TEXTURE_2D, texture[1]);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    		// Right face
    	glBindTexture(GL_TEXTURE_2D, texture[0]);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    		// Left Face
    	glBindTexture(GL_TEXTURE_2D, texture[1]);
    		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    		glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    		glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    	glEnd();
    
    	xrot+=0.3f;
    	yrot+=0.2f;
    	zrot+=0.4f;
    	return TRUE;										// Keep Going
    }
    I took lesson 6 of NeHe's tutorials, thought I understood the code finally, so I tried to add another texture to the equation and place it on different faces of the quad here and there, All it Shows is NeHe1.bmp..... I Probably am not Loading it correctly, All the other coding for the window is there, I just didn't put it in the post due to how long it is, Any help on this one?

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    glBindTexture() does not work in-between glBegin() and glEnd().
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Hmm, Is that all that is wrong, iff so, could I start and end every single face of the quad with a glBindTexture?

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    switching textures is slow, so select the texture and draw all the quads that use it ,then set the next texture and draw all the quads that use that one, etc...
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  5. #5
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Is there a faster way to do it? And I'm assuming you're saying theres nothing wrong with my part of the code except for that...

    WOW


    I think my code has just gone all wrong, No clue what I'm doing exactly, I've been trying to understand texture mapping n stuff for 5 days now, and I just can't grasp it....

    I can't find Jeff Molohee's email anywhere (guy who made tutorial) so this board is all I got... Help plzzz
    Last edited by Shamino; 05-07-2005 at 05:10 PM.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    OMG, I MADE IT WORK!!!!!!!

    SCOREEEEE, !!!!! sweeeeet!!!!!!

    it took me 5 days but now I understand the code and can now implement it and bend it to my WILLLLL!!!!!

    Note to all: That code up there is trash, sorry to kill your brain cells with it

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    Yeah texture mapping can be difficult to get a grasp on but its just like riding a bike

  8. #8
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Now that you've got it working, I suggest you write down some notes somewhere and keep it in a safe place.

    Because if you don't use OpenGL regularly, in 3 months, you'll forget how texture mapping works.

    Speaking from my own experience.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple textures in OpenGL...
    By yaya in forum Game Programming
    Replies: 1
    Last Post: 02-12-2008, 08:24 AM
  2. loading textures from resources (dx8/9)
    By X PaYnE X in forum Game Programming
    Replies: 1
    Last Post: 12-25-2005, 04:44 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. BMP Loading Questions, More OpenGL!!! :D
    By Shamino in forum Game Programming
    Replies: 13
    Last Post: 05-08-2005, 02:31 PM
  5. OpenGL, loading BMP Textures?
    By Zeusbwr in forum Game Programming
    Replies: 12
    Last Post: 12-09-2004, 05:16 PM