OpenGL纹理面临错误方向的问题

时间:2013-07-18 19:57:03

标签: java opengl lwjgl textures

我正在创建一个新游戏中的测试纹理,出于某种原因,当我将其实现到游戏中时,纹理会水平翻转,而不是垂直翻转。为什么会这样?

这是该应用的照片。

img

正如你所看到的,小家伙水平翻转,几乎就像他在睡觉一样。他应该垂直面对。为什么会发生这种情况,我该如何预防呢?我会发布两个课程。

玩家类:

   import java.io.IOException;

import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class Player {

    public Texture playerTexture;

    // Positions & speed
    public float xPos = 20.0f; // This is initial
    public float yPos = 450.0f; // Same as above.

    private float velocity = 20;

    public float gravityForce = 6;
    public float jumpVelocity = 25;

    private static int moveSpeed = 30 / 2;

    public boolean isSupported = false; // Once again, initial value. 
    boolean canJump = true;

    // movement methods

    public Texture grabTexture() {
        try {
            playerTexture = TextureLoader.
                    getTexture("PNG", ResourceLoader
                    .getResourceAsStream
                    ("resources/test_char.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return playerTexture;
    }

    public void applyGravity() {

    }

    private void printPos(String moveMethod) {
        System.out.println(moveMethod + " X: " 
    + xPos + " Y: " + yPos);
    }

    public void moveRight() {
        xPos += moveSpeed;
        printPos("Moving right!");
    }

    public void moveLeft() {
        xPos -= moveSpeed;
        printPos("Moving left!");
    }

    public void jump() {
        if (!isSupported) {

        }
    }

    public void shoot() {
        // do shooty stuff here
    }

}

Main(Launcher)类:

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class Main {

    private void display() {
        try {
            Display.setDisplayMode(new DisplayMode(1000, 550));
            Display.setTitle("Unnamed Platformer Game");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }

        // OpenGL

        while (!Display.isCloseRequested()) {
            initGL();
            player.applyGravity();
            Display.update();
            Display.sync(60); // sync to 60 fps

            if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
                player.moveRight();
            } else if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
                player.moveLeft();
            } else if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
                player.jump();
            }
        }

        Display.destroy();
    }

    private void initGL() {
        // initial OpenGL items for 2D rendering
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        GL11.glOrtho(0, 1000, 550, 0, 1, -1);


        // start rendering player image
        player.grabTexture().bind();
        GL11.glBegin(GL11.GL_QUADS);

        GL11.glVertex2f(player.xPos, player.yPos);
        GL11.glTexCoord2f(0, 0);

        GL11.glVertex2f(player.xPos + 100, player.yPos);
        GL11.glTexCoord2f(1, 0);

        GL11.glVertex2f(player.xPos + 100, player.yPos + 100);
        GL11.glTexCoord2f(1, 1);

        GL11.glVertex2f(player.xPos, player.yPos + 100);
        GL11.glTexCoord2f(0, 1);
        GL11.glEnd(); // stop rendering this image
    }

    Player player = new Player();

    public static void main(String[] args) {
        Main main = new Main();
        main.display();
    }
}

1 个答案:

答案 0 :(得分:2)

GL11.glVertex2f(player.xPos, player.yPos);
GL11.glTexCoord2f(0, 0);

问题是你正在调用这些功能。每当你调用glVertex2f时,OpenGL都将获取当前设置的任何属性并将其用于顶点。在这种情况下,您在设置顶点后设置纹理坐标,因此坐标被“延迟”一个顶点。如果您交换每对这些呼叫,它应该可以工作。

此外,您对glOrtho的调用已交换顶部和底部参数。底部值是y值应该在屏幕的底部(在这种情况下,0)。同样是最高价值。