[LWJGL] Loading multiple Textures

Following a tutorial, I can load textures. how it looks:

guy = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/guy.png")));

Initialized code ^

now I draw it:


..other stuff
guy.bind();
glBegin(GL_QUADS);
            
                        glTexCoord2f(0, 0); glVertex2i(p.getx(), p.gety()); // Upper-left
                        glTexCoord2f(1, 0); glVertex2i(p.getx() + 150, p.gety()); // Upper-right
                        glTexCoord2f(1, 1); glVertex2i(p.getx() + 150, p.gety() + 150); // Bottom-right
                        glTexCoord2f(0, 1); glVertex2i(p.getx(), p.gety() + 150); // Bottom-left   
                        
            glEnd();


I tried doing the following for another image but I got the same image.(different locations ofcourse)

Are you saying that you wanted two different images in different locations, but both had the same image?

  • If so, then you need to bind the second image before you put in the second set of vertices.

       guy.bind();
glBegin(GL_QUADS);
            
                        glTexCoord2f(0, 0); glVertex2i(p.getx(), p.gety()); // Upper-left
                        glTexCoord2f(1, 0); glVertex2i(p.getx() + 150, p.gety()); // Upper-right
                        glTexCoord2f(1, 1); glVertex2i(p.getx() + 150, p.gety() + 150); // Bottom-right
                        glTexCoord2f(0, 1); glVertex2i(p.getx(), p.gety() + 150); // Bottom-left   
                        
            glEnd();

    //Would I call the same thing just differently for the image
      guy2.bind();
          glBegin(GL_QUADS);
            
                        glTexCoord2f(0, 0); glVertex2i(p.getx(), p.gety()); // Upper-left
                        glTexCoord2f(1, 0); glVertex2i(p.getx() + 150, p.gety()); // Upper-right
                        glTexCoord2f(1, 1); glVertex2i(p.getx() + 150, p.gety() + 150); // Bottom-right
                        glTexCoord2f(0, 1); glVertex2i(p.getx(), p.gety() + 150); // Bottom-left   
                        
            glEnd();

nevermind I figured it out. I called the bind methods in the wrong place :-\

Yes. But you would want to change your coordinates to be in the position of the second guy. Else, you will have two images drawn exactly on top of each other.

How it works is your glBegin() command tells the GPU that this is vertex data (texture coords, position coords, etc). The glEnd() stops this. Your GPU rasterizes this data (makes a shape out of it) and applies a texture to it based on the texture coordinates you passed. This uses the currently bound texture. So, when you bind a new texture, all the glBegin() & glEnd() calls will use that texture until you bind a new one.

I saw that you fixed this but I figured that I would just post it for you to read.

What does GPU stand for? I’ve seen you use it a few times

A quick google told me that it stands for graphic processing unit. It’s just your graphics card. If you ever hear the term “hardware acceleration” then that is referencing using the graphics card. Your graphics card is like your CPU, it processes data, and then is able to put that on your screen.