Reputation: 3137
I've been working on a very simple game, and I've decided to include some basic textures. I was experimenting with the code, and for some reason, I have no idea why, I cannot get the textures to map correctly. Right now, I am simply trying to map a green rectangle (512 x 64 .png file) to the bottom of a screen of size 640 x 480. This green rectangle is supposed to represent the ground. Here is an image of my failed attempt:
Here is the relevent code:
In the Boot class I have created I call this function:
public void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, gameWidth, gameHeight, 0, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glEnable(GL11.GL_TEXTURE_2D);
}
In the ground class I have created, I call a draw function. The texture is loaded in the Boot class, and set as a protected variable which I bind and use in the ground.draw() function.
public void draw() {
this.texture.bind();
GL11.glLoadIdentity();
GL11.glTranslatef(0, 0, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(0, 480 - height);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(640, 480 - height);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(640, 480);
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(0, 480);
GL11.glEnd();
}
Also, if you'll note above I have a GL11.glTranslatef call. I can get my ground texture in the right position if I simply make the 2nd argument for this function large enough to displace it further in the y direction, however I don't wish to do it this way. I really don't know how the texture ended up in this location in the first place, and I would like to figure out why.
Upvotes: 1
Views: 222
Reputation: 11424
I think there is a problem with your matrices :
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, gameWidth, gameHeight, 0, 1, -1);
I never really used OpenGL fixed pipeline but what I see or actually DON"T SEE is where you multiply your matrices. You must multiply the model matrix of your quad with projection matrix which is glOrtho(). ( glPushMatrix/glPopMatrix )
So in your scenario you should :
Push Ortho matrix.
Push camera(view) matrix (if exists).
Push Model Matrix.
Pop Model Matrix.
Pop camera (view) matrix (if exists).
Pop Ortho matrix.
If the Ortho never changes I think you need to push it only once. Also make sure you take into account ortho coordinates changes because depending on glOrtho() setup the world center going to be different from the center of the screen.Usually top left or bottom left corner.
The bottom line is : while I don't recommend you using the old and deprecated OpenGL at all, you should take a look how the transformations are done the right way in the numerous online tutorials.Also here
Upvotes: 1