Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: extremely confused about texturing!

  1. #1
    Guest

    extremely confused about texturing!

    there are a number of tutorials detailing how to map moving bitmaps onto 3d shapes with rotation etc. All I want to do is map one 256x256 texture to a flat 2d square. Using GLUT. Loading the image from file (namely background2.bmp) Could someone please put me out of my misery and post some simple code that would do the above. Thats all it has to do. No animation or anything. Simply display the bitmap in a glut window. PLEASE! (I am actually using visual studio and the bmp is loaded as a resource named IDB_BACKGROUND2 but the code can simply load from file. C++ preferable but pure C fine! anything would be greatly appriciated. (except links to over complicated tutorials. i am a serious novice trying to do a Uni project!)

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Jun 2000
    Location
    Gastonia, NC, USA
    Posts
    2,096

    Re: extremely confused about texturing!

    nehe.gamedev.net has lot's of examples of loading a texture to a quad.

  3. #3
    Guest

    Re: extremely confused about texturing!

    they dont appear to use glut. I understand the concept of texturing the sqaure once i've loaded the bitmap. its just loading the bitmap i dont understand. The main example i have looked at is lesson 38 and tried to copy that. but there is a line which uses hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),MAKE INTRESOURCE(Texture[loop]), IMAGE_BITMAP, 0,
    0, LR_CREATEDIBSECTION); that i dont understand. It says LR_CREATEDIBSECTION creates a bitmap without the colour information. But i want the colour information...may be this is why my sqaure appears white?

  4. #4
    Intern Contributor
    Join Date
    Nov 2001
    Posts
    77

    Re: extremely confused about texturing!

    This is the basic process:

    Code :
    // .. snip ..
    void init(void)
    {
      // Set up your OpenGL settings
     
      glEnable(GL_TEXTURE_2D);
      glGenTextures(1,texName);  // texName is an usigned int
      glBindTexture(GL_TEXTURE_2D,texName);
     
      // Bilinear filtering
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
     
      // Now, load your texture
      // I'm assuming the variable name is texPtr
      glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,256,256,0,GL_RGBA,GL_UNSIGNED_BYTE,texImage);
    }
    // .. snip ..
    void display()
    {
      // Prerender stuff
      glBegin(GL_QUADS);
      glTexCoord2f(0.0,0.0); glVertex3f(-1.0,-1.0,0.0);
      glTexCoord2f(1.0,0.0); glVertex3f( 1.0,-1.0,0.0);
      glTexCoord2f(1.0,1.0); glVertex3f( 1.0, 1.0,0.0);
      glTexCoord2f(0.0,1.0); glVertex3f(-1.0, 1.0,0.0);
      glEnd();
      glFlush();
      // Finished.
    }
    [This message has been edited by Nychold (edited 02-11-2004).]

  5. #5
    Guest

    Re: extremely confused about texturing!

    the following is the code i have which includes all the stuff you wrote, but my square is still white and doesnt show the texture. any idea what could be wrong?

    (C++ info for C coders..!! Code executes in the order: constructor ie.Graphics::Graphics() (which then calls loadBackgroundTexture()) That is one off initilasation. drawBackground() is then repeatedly called)

    Code:

    //#include all the right includes!

    class Graphics{
    private:
    struct object // Create A Structure Called Object
    {
    int tex; // Integer Used To Select Texture
    float x; // X Position
    float y; // Y Position
    float z; // Z Position
    };

    GLuint texture[1]; // Storage For 1 Texture
    object background; //create object 'background that will hold' background BMP

    HBITMAP hBMP; // Handle Of The Bitmap
    BITMAP BMP; // Bitmap Structure

    public:
    Graphics(){}; //default - currently does nothing
    Graphics(int argc,char* argv[]); //overloaded constructor
    ~Graphics() {}; //destructor - currently does nothing

    void loadBackgroundTexture(void);
    void drawBackground(void);
    };


    Graphics::Graphics(int argc,char* argv[]){
    glutInit(&argc,argv); //start glut
    loadBackgroundTexture();
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); //double buffered for animation. RGB colour mode
    glutInitWindowSize(300,300); //window size
    glutInitWindowPosition(300,150); //window position
    glutCreateWindow("PicassoTalk! J.Trevan : 2k4"); //create window
    glClearColor(0.0f ,0.0f ,0.0f ,0.0f); //clear colour = black
    glMatrixMode(GL_PROJECTION); //select projection mode
    glLoadIdentity(); //reset projection view
    gluPerspective(45.0f,(GLfloat)300/(GLfloat)300,0.1f,100.0f); //perspective
    glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
    glLoadIdentity(); // Reset The Modelview Matrix
    glClearDepth (1.0f); // Depth Buffer Setup
    glDepthFunc (GL_LEQUAL); // The Type Of Depth Testing (Less Or Equal)
    glDisable(GL_DEPTH_TEST); // Disable Depth Testing
    glShadeModel (GL_SMOOTH); // Select Smooth Shading
    glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate
    glEnable(GL_TEXTURE_2D); // Enable Texture Mapping
    }


    void Graphics::loadBackgroundTexture(void){
    background.tex = 0; //define texture ie. which bitmap
    background.x = 0; //set background position in window
    background.y = 0;
    background.z = 0;

    byte Texture[]={ IDB_BACKGROUND2 }; // The ID of background bitmap to load

    glGenTextures(1, texture); // Generate Texture (sizeof(Texture)=1 ID)

    //The following loads the bitmap image. MAKEINTRESOURCE converts int to resource number
    //ie. which bitmap to load. IMAGE_BITMAP defines that image being loaded is a bitmap. The
    //next 2 zero's are the required size of the image, x and y. Want to use the default image
    //size so leave as zero. The last parameter (LR_CREATEDIBSECTION) ???!!!!
    hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),MAKE INTRESOURCE(Texture[0]), IMAGE_BITMAP, 256,
    256, LR_DEFAULTCOLOR);
    //hBMP now points to the bitmap data that is loaded by LoadImage( ).

    if (hBMP) { // Does The Bitmap Exist?
    GetObject(hBMP,sizeof(BMP), &BMP); // Get The Object
    // hBMP: Handle To Graphics Object
    // sizeof(BMP): Size Of Buffer For Object Information
    // Buffer For Object Information

    glBindTexture(GL_TEXTURE_2D, texture[0]); // Bind Our Texture

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
    DeleteObject(hBMP); // Delete The Bitmap Object
    }

    else
    cerr << "hBMP doesn't point to anything!! Error loading background bitmap!" << endl;

    }

    void Graphics::drawBackground(void){
    glLoadIdentity(); //Reset modelview matrix
    glTranslatef(0.0f,0.0f,-10.0f); //into screen

    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,256,256,0,GL_ RGBA,GL_UNSIGNED_BYTE,BMP.bmBits);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0,0.0); glVertex3f(-1.0,-1.0,0.0);
    glTexCoord2f(1.0,0.0); glVertex3f( 1.0,-1.0,0.0);
    glTexCoord2f(1.0,1.0); glVertex3f( 1.0, 1.0,0.0);
    glTexCoord2f(0.0,1.0); glVertex3f(-1.0, 1.0,0.0);
    glEnd();

    glLoadIdentity();
    glutSwapBuffers();
    }

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2004
    Posts
    3

    Re: extremely confused about texturing!

    Hi,
    I too am an extreme newbie
    I experienced the same problem with following some various pieces of tuorials I found around the web and had problems with objects just showing up white. My problem had to do with lighting. can you make your rectangle just show up some color? Make sure you can do that before you attemp textures.

    Dave

  7. #7
    Guest

    Re: extremely confused about texturing!

    hi,

    yes i can change the colour of my square, i just cant get a texture on it! confused as texturing mapping a square is just changing the colour of the square!!!?! confused!

  8. #8
    Intern Contributor
    Join Date
    Nov 2001
    Posts
    77

    Re: extremely confused about texturing!

    Code :
    if (hBMP) { // Does The Bitmap Exist?
    GetObject(hBMP,sizeof(BMP), &amp;BMP); // Get The Object
    // hBMP: Handle To Graphics Object
    // sizeof(BMP): Size Of Buffer For Object Information
    // Buffer For Object Information
     
    glBindTexture(GL_TEXTURE_2D, texture[0]); // Bind Our Texture
     
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    DeleteObject(hBMP); // Delete The Bitmap Object
    }
    Umm, okay. Do not delete the object until the program is about to shut down. So keep your HBITMAP in Graphics, and in your destructor, delete the object. See if that helps.

  9. #9
    Guest

    Re: extremely confused about texturing!

    that didnt work...i guess because the hBMP had already been used to transfer information in the line GetObject(hBMP,sizeof(BMP), &BMP); so therefore it could be deleted before the end of the program as it was no longer required?! next!!!?!!!

  10. #10
    Guest

    Re: extremely confused about texturing!

    Mr.JT, read carefully what said Nychold, especially his first post.

    Compare with yours : it not the same order at all.

    In english, for texturing to work, you have to :
    1) read the texture file using whatever you want, so that you end up with a RGB or RGBA 1-dimensionnal C array of unsigned char values.
    2) glEnable(GL_TEXTURE_2D)
    3) glGenTextures
    4) glBindTexture
    5) you can set various glTexParameter/glTexEnv (if you use mipmaps, do not forget to send each level to opengl or use gluBuildMipmaps)
    6) ** send the actual texture data to OpenGL **
    with glTexImage2D and the C array in parameter.
    7) now, you can free/delete the C array.

    Once this init is done, each time you want to use this texture you only have to glBindTexture with the 'name' (texture id number). Then set texcoords and vertices. Use glDisable/glEnable(GL_TEXTURE_2D) to switch off/on texturing.

    If you want to use several different textures, repeat the steps 3 to 6 to initialize each. And then bind to texture you need.

    I hope it is clearer for you now.

Posting Permissions

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