Texture question

Started by u2whatmate, June 06, 2014, 01:56:40

Previous topic - Next topic


So, I am using the example code from http://www.java-gaming.org/index.php?topic=25516.0 thread on how to use a BufferedImage as a texture in OpenGL. I am completely new to LWJGL and OpenGL but not new to java. My question is: how do I set where the texture is placed on the screen? Here's my code: http://pastebin.com/B1z1bSSz (that glCoord2f call is something I tried that didn't work.)

EDIT: investigating the code more, I get to this part:
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
            glTexCoord2f(1, 0);
            glVertex2f(128, 0);
            glTexCoord2f(1, 1);
            glVertex2f(128, 128);
            glTexCoord2f(0, 1);
            glVertex2f(0, 128);

Is this what positions the texture? How does it work?



I will answer your question but you should know that these are absolute basics in LWJGL. You should read the basics tutorials here: http://lwjgl.org/wiki/index.php?title=Main_Page before you do anything else.

So in OpenGL, this is called rendering things in immediate mode. So first of all you call glBegin() passing in a mode of rendering (which tells OpenGL what kind of primitive you are drawing ie quad, triangle, polygon etc.). Then you send a list of vertices to OpenGL using the glVertex2f() function. Then you call glEnd() which tells OpenGL that you are finished sending vertices and it should draw them. So that code draws a 128 x 126 square with it's lower left corner at the origin. But there are other things to know about a vertex apart from it's position. Here you specify the texture coordinate of the vertex. Texture coords are normalized (between 0 and 1) coordinates of a texture where (0, 0) is the TOP left corner and (1, 1) the BOTTOM right corner.

So in theory, to move the square all you have to do is change the values you pass to glVertex2f(). However in the pastebin, it shows you call glTranslate(100, 100, 0) which essentially means "translate everything after this by 100, 100, 0." Which is why the square is not in the bottom corner of the screen but up and to the right a little. So an even easier way to move it is to change the values you pass to glTranslate().

I've just spoon-fed you a lot of info but, as I said, this is absolute basics and you should be reading up on it yourself.


Thank you very much for both the information and the link! I will definitely read up on this.