Thread: OpenGL, loading BMP Textures?

  1. #1
    #junkie
    Join Date
    Oct 2004
    Posts
    240

    Post OpenGL, loading BMP Textures?

    Ok, i am taking some various Glut and OpenGL tutorials. However, i am having a problem with NeHe's 6th tutorial (NeHe's Tutorial). What its focus is on is Loading Textures, and starting off, i cannot get his functions to run.

    I assume they are Glaux functions, or having to do with it. Hopefully.

    the code
    Code:
    AUX_RGBImageRec *LoadBMP(char *Filename)
    says Syntax error before '*', if i remember right (i dont have access to my compiler atm, so i am going by memory).
    Also,
    Code:
    FILE *File=NULL;
    Gives me an error on the handle "FILE" if i remember right also.

    Those, are only a couple of a whole slew of errors, basically anything having to do with loading textures in his tutorial give me problems.


    Seeing as i am so noob to all this, and his use of Glaux/Win32 api, and my not using Glaux/Win32, has been a trouble causing point. However its still seeming to be a good learning set of tutorials, just due to his explanations of the more OGL specific code.

    Anyway, Hope i gave you enough information to go by, btw i am using Dev C++, 4.9.9.0, and i downloaded the source of that program to try to run it, and obviously it didnt run due to my lack of Glaux.

    So, to recap, can someone explain to me how to load textures like the tutorial tried to do, or if the specific code i gave you is not related to Glaux and it is something i am doing, can you explain? Or give me some links?

    Any replies are much appreciated, thanks!
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    >>Gives me an error on the handle "FILE" if i remember right also.
    Is stdio.h included?

  3. #3
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    Code:
    #include    <windows.h>
    #include    <stdlib.h>
    #include    <stdio.h>
    #include    <iostream>
    #include    <gl\gl.h>
    #include    <gl\glu.h>
    #include    <gl\glut.h>
    
    GLfloat	    rquad;
    GLfloat     rtri;
    GLfloat     xrot;
    GLfloat     yrot;
    GLfloat     zrot;
    GLuint	    texture[1];
    
    void draw();
    
    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;
        AUX_RGBImageRec *TextureImage[1];
        memset(TextureImage,0,sizeof(void *)*1);
       	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
    	if (TextureImage[0]=LoadBMP("Data/NeHe.bmp"))
    	{
    		Status=TRUE;	
    		glGenTextures(1, &texture[0]);					// Create The Texture
    
    		// Typical Texture Generation Using Data From The Bitmap
    		glBindTexture(GL_TEXTURE_2D, texture[0]);
    		// Generate The Texture
    		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);	// Linear Filtering
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
    	}
    	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
    }
    
    
    // process menu option 'op'
    void menu(int op) {
    
      switch(op) {
      case 'Q':
      case 'q':
        exit(0);
      }
    }
    
    // executed when a regular key is pressed
    void keyboardDown(unsigned char key, int x, int y) {
    
      switch(key) {
      case 'Q':
      case 'q':
      case  27:   // ESC
        exit(0);
      }
    }
    
    // executed when a regular key is released
    void keyboardUp(unsigned char key, int x, int y) {
    
    }
    
    // executed when a special key is pressed
    void keyboardSpecialDown(int k, int x, int y) {
    
    }
    
    // executed when a special key is released
    void keyboardSpecialUp(int k, int x, int y) {
    
    }
    
    // reshaped window
    void reshape(int width, int height) {
    
      GLfloat fieldOfView = 90.0f;
      glViewport (0, 0, (GLsizei) width, (GLsizei) height);
    
      glMatrixMode (GL_PROJECTION);
      glLoadIdentity();
      gluPerspective(fieldOfView, (GLfloat) width/(GLfloat) height, 0.1, 500.0);
    
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
    }
    
    
    void mouseClick(int button, int state, int x, int y) {
    
    }
    
    void mouseMotion(int x, int y) {
    
    }
    
    // executed when program is idle
    void idle() { 
        draw();
    }
    
    // render the scene
    void draw() {
    
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      glTranslatef(-1.5f,0.0f,-6.0f);	
      
      glRotatef(rtri,0.0f,1.0f,0.0f);
      glBegin(GL_TRIANGLES);					// Start Drawing A Triangle
    		glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Front)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f(-1.0f,-1.0f, 1.0f);			// Left Of Triangle (Front)
    		glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f( 1.0f,-1.0f, 1.0f);			// Right Of Triangle (Front)
    
    		glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Right)
    		glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f( 1.0f,-1.0f, 1.0f);			// Left Of Triangle (Right)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f( 1.0f,-1.0f, -1.0f);			// Right Of Triangle (Right)
    
    		glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Back)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f( 1.0f,-1.0f, -1.0f);			// Left Of Triangle (Back)
    		glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f(-1.0f,-1.0f, -1.0f);			// Right Of Triangle (Back)
    
    		glColor3f(1.0f,0.0f,0.0f);			// Red
    		glVertex3f( 0.0f, 1.0f, 0.0f);			// Top Of Triangle (Left)
    		glColor3f(0.0f,0.0f,1.0f);			// Blue
    		glVertex3f(-1.0f,-1.0f,-1.0f);			// Left Of Triangle (Left)
    		glColor3f(0.0f,1.0f,0.0f);			// Green
    		glVertex3f(-1.0f,-1.0f, 1.0f);			// Right Of Triangle (Left)
      glEnd();
      
      glLoadIdentity();					// Reset The Current Modelview Matrix
      glTranslatef(1.5f,0.0f,-6.0f);				// Move Right 1.5 Units And Into The Screen 6.0
      glRotatef(rquad,1.0f,0.0f,0.0f);			// Rotate The Quad On The X axis ( NEW )
      
    	glColor3f(0.5f,0.5f,1.0f);				// Set The Color To A Nice Blue Shade
    	glBegin(GL_QUADS);					// Start Drawing A Quad
    		glColor3f(0.0f,1.0f,0.0f);			// Set The Color To Green
    		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Top)
    		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Top)
    		glVertex3f(-1.0f, 1.0f, 1.0f);			// Bottom Left Of The Quad (Top)
    		glVertex3f( 1.0f, 1.0f, 1.0f);			// Bottom Right Of The Quad (Top)
    
    		glColor3f(1.0f,0.5f,0.0f);			// Set The Color To Orange
    		glVertex3f( 1.0f,-1.0f, 1.0f);			// Top Right Of The Quad (Bottom)
    		glVertex3f(-1.0f,-1.0f, 1.0f);			// Top Left Of The Quad (Bottom)
    		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Bottom)
    		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Bottom)
    		
    		glColor3f(1.0f,0.0f,0.0f);			// Set The Color To Red
    		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Front)
    		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Front)
    		glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Front)
    		glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Right Of The Quad (Front)
    
    		glColor3f(1.0f,1.0f,0.0f);			// Set The Color To Yellow
    		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Back)
    		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Back)
    		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Back)
    		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Back)
    		
    		glColor3f(0.0f,0.0f,1.0f);			// Set The Color To Blue
    		glVertex3f(-1.0f, 1.0f, 1.0f);			// Top Right Of The Quad (Left)
    		glVertex3f(-1.0f, 1.0f,-1.0f);			// Top Left Of The Quad (Left)
    		glVertex3f(-1.0f,-1.0f,-1.0f);			// Bottom Left Of The Quad (Left)
    		glVertex3f(-1.0f,-1.0f, 1.0f);			// Bottom Right Of The Quad (Left)
    
    		glColor3f(1.0f,0.0f,1.0f);			// Set The Color To Violet
    		glVertex3f( 1.0f, 1.0f,-1.0f);			// Top Right Of The Quad (Right)
    		glVertex3f( 1.0f, 1.0f, 1.0f);			// Top Left Of The Quad (Right)
    		glVertex3f( 1.0f,-1.0f, 1.0f);			// Bottom Left Of The Quad (Right)
    		glVertex3f( 1.0f,-1.0f,-1.0f);			// Bottom Right Of The Quad (Right)
    	glEnd();						// Done Drawing The Quad
    
      
      glFlush();
      glutSwapBuffers();
      rtri+=0.2f;
      rquad-=0.15f;
    }
    
    // initialize OpenGL settings
    void initGL(int width, int height) {
    
      reshape(width, height);
    
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      glClearDepth(1.0f);
    }
    
    // initialize GLUT settings, register callbacks, enter main loop
    int main(int argc, char** argv) {
      
      glutInit(&argc, argv);
    
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
      glutInitWindowSize(800, 600);
      glutInitWindowPosition(100, 100);
      glutCreateWindow("EmptyGLUT");
    
      // register glut call backs
      glutKeyboardFunc(keyboardDown);
      glutKeyboardUpFunc(keyboardUp);
      glutSpecialFunc(keyboardSpecialDown);
      glutSpecialUpFunc(keyboardSpecialUp);
      glutMouseFunc(mouseClick);
      glutMotionFunc(mouseMotion);
      glutReshapeFunc(reshape);
      glutDisplayFunc(draw);  
      glutIdleFunc(idle);
      glutIgnoreKeyRepeat(true); // ignore keys held down
    
      // create a sub menu 
      int subMenu = glutCreateMenu(menu);
      glutAddMenuEntry("Do nothing", 0);
      glutAddMenuEntry("Really Quit", 'q');
    
      // create main "right click" menu
      glutCreateMenu(menu);
    
      glutAddSubMenu("Sub Menu", subMenu);
      glutAddMenuEntry("Quit", 'q');
      glutAttachMenu(GLUT_RIGHT_BUTTON);
    
      initGL(800, 600);
    
      glutMainLoop();
      return 0;
    }

    That is what i have, i know i am not using either of the problem causing functions in my main and whatnot, i first tried to get it to compile to make sure there were no errors, and ofcourse, there were.

    Here is the errors being reported.
    18 C:\(edited length)\main.cpp syntax error before `*' token
    25 C:\(edited length)\main.cpp ISO C++ forbids declaration of `File' with no type
    25 C:\(edited length)\main.cpp `Filename' was not declared in this scope
    26 C:\(edited length)\main.cpp syntax error before `if'
    C:\(edited length)\main.cpp In function `int LoadGLTextures()':

    36 C:\(edited length)\main.cpp `AUX_RGBImageRec' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)

    36 C:\(edited length)\main.cpp `TextureImage' undeclared (first use this function)
    39 C:\(edited length)\main.cpp `LoadBMP' undeclared (first use this function)
    C:\(edited length)\Makefile.win [Build Error] [main.o] Error 1
    Any idea what i am doing wrong? Aside from Dev C++ not liking NULL File's. :/
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    >>AUX_RGBImageRec *LoadBMP(char *Filename)
    Doesnt this need glaux.h included?
    Last edited by sand_man; 12-09-2004 at 12:22 AM.

  5. #5
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    no idea, if that is the problem, then my original question goes back in place.

    If that does require glaux, how can i load textures?

    That includes anything i need to load a texture, ie the bmp loading code, ect.
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  6. #6

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >Seeing as i am so noob to all this, and his use of Glaux/Win32 api, and my not using Glaux/Win32

    Scroll down to the bottom of the tutorial and download the linux/glut port.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Quote Originally Posted by sand_man
    >>AUX_RGBImageRec *LoadBMP(char *Filename)
    Doesnt this need glaux.h included?
    Yes it does, try including gl/glaux.h, if your compiler doesnt support that you will have to load in the data on your own.

  9. #9
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    ya it doesent support glaux if i remember right, none the less i was warned against glaux by... everyone, including the person who made the tutorial i am going off of lol. Glaux is sposed to be outdated and bug ridden. :/

    And i downloaded the GLUT code for the tutorial, however it was full of TGA this and that, and iv got no idea wtf to do with those lol, so im basically just trying to learn the best way to load textures without glaux, using like glut or ogl or GLU, ect.

    So can anyone just name something so i can take a stab at researching it?
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  10. #10
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    BTW, sandman, they use glaux in that link too. How do i do all this without using glaux lol
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    File formats, look into the fileformat you wish to load. Or create your own (I use only my own)

  12. #12
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Go to NeHe's site, scroll down to NeHe Misc, and download the "Glaux replacement" zip. Ive never used it but from the sounds of it, its a glaux replacement

  13. #13
    #junkie
    Join Date
    Oct 2004
    Posts
    240
    ya i found that, but didnt know how to use it when i first got it, i should be able to figure it out now though. I also found a bmp/texture loader via Glut, however i havent gotten it to work yet outside of the original example package it was in.

    Im almost sure lol, i can get these to work.. but none the less i like to understand what it is they are doing. So i may be posting again :/, anyway, thanks again, talk to ya latah


    Oh, and if anyone knows, this is the glaux replacement code from nehe, what does it use? I havent used it yet, and im not on my comp so i cant this second, but does it use win32 to load the bmp? or what.. :/
    Code:
    bool NeHeLoadBitmap(LPTSTR szFileName, GLuint &texid)					// Creates Texture From A Bitmap File
    {
    	HBITMAP hBMP;														// Handle Of The Bitmap
    	BITMAP	BMP;														// Bitmap Structure
    
    	glGenTextures(1, &texid);											// Create The Texture
    	hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), szFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );
    
    	if (!hBMP)															// Does The Bitmap Exist?
    		return FALSE;													// If Not Return False
    
    	GetObject(hBMP, sizeof(BMP), &BMP);									// Get The Object
    																		// hBMP:        Handle To Graphics Object
    																		// sizeof(BMP): Size Of Buffer For Object Information
    																		// &BMP:        Buffer For Object Information
    
    	glPixelStorei(GL_UNPACK_ALIGNMENT, 4);								// Pixel Storage Mode (Word Alignment / 4 Bytes)
    
    	// Typical Texture Generation Using Data From The Bitmap
    	glBindTexture(GL_TEXTURE_2D, texid);								// Bind To The Texture ID
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	// Linear Min Filter
    	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	// Linear Mag Filter
    	glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);
    
    	DeleteObject(hBMP);													// Delete The Object
    
    	return TRUE;														// Loading Was Successful
    }

    *edit
    sigh, errors again ofcourse lol. Why is this so difficult *cry .
    main.cpp: In function `bool NeHeLoadBitmap(TCHAR*, GLuint&)':
    main.cpp:85: error: `GL_BGR_EXT' undeclared (first use this function)
    main.cpp:85: error: (Each undeclared identifier is reported only once for each
    function it appears in.)

    make.exe: *** [main.o] Error 1
    and as i thought (LPTSTR gave this nub the clue lol), this requires win32. But anyway, any ideas whats wrong via the error msg? And yes i am including the win32 header
    01110111011000010110110001100100011011110010000001 11000101110101011010010111010000100000011011000110 10010110011001100101001000000111100101101111011101 0100100000011011100111010101100010

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 Again, Loading Textures onto Quads
    By Shamino in forum Game Programming
    Replies: 7
    Last Post: 05-07-2005, 08:14 PM