Question

Ok so I am currently working on a 2d game in java with LWJGL. I have a fairly solid understanding of java and how it works and know the basics of how games work and LWJGL/openGL, but I am having this really weird issue with rendering textures. I determined that one of my draw methods here is the culprit ..

public static void texturedTriangleInverted(float x, float y, float width, float height, Texture texture) {
    GL11.glPushMatrix();
        {

            GL11.glTranslatef(x, y, 0);
            texture.bind();

            GL11.glBegin(GL11.GL_TRIANGLES);
                {
                    GL11.glVertex2f(width / 2, 0);
                    GL11.glTexCoord2f(width / 2, 0);

                    GL11.glVertex2f(0, height);
                    GL11.glTexCoord2f(0, 1);

                    GL11.glVertex2f(width, height);
                    GL11.glTexCoord2f(1, 1);
                }
            GL11.glEnd();
        }
    GL11.glPopMatrix();
}

so what happens is if I render anything with this method the next thing rendered looks like it literally was compressed on one end and stretched on the other, even if the next thing rendered is not rendered with this method. I am almost positive that it has to do with the argument I passed into the Gl11.glTexCoord2f(float x, float y) methods, but I cant figure out how to fix it.

here is my openGL initialization code

private void initGL() {
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, Strings.DISPLAY_WIDTH, 0, Strings.DISPLAY_HEIGHT, -1, 1);

    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glEnable(GL11.GL_TEXTURE_BINDING_2D);
    GL11.glViewport(0, 0, Strings.DISPLAY_WIDTH, Strings.DISPLAY_HEIGHT);
    GL11.glClearColor(0, 0, 1, 0);
    GL11.glDisable(GL11.GL_DEPTH_TEST);

}

my game loop code

    private void gameLoop() {
    while (!Display.isCloseRequested()) {

        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
        GL11.glLoadIdentity();
        GL11.glTranslatef(Strings.transX, Strings.transY, 0);

        this.level.update();
        this.level.render();

        Display.update();
        Display.sync(Strings.FPS_CAP);
    }

    Keyboard.destroy();
    Display.destroy();

}

my texture loading code (note I used slick to load my textures)

public static final Texture loadTexture(String location) {
    try {
        if (textureExists(location)) {
            return TextureLoader.getTexture("png", new BufferedInputStream(new FileInputStream(new File(location))), false);
        } else {
            System.err.println("txture does not exist");
        }

    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}
Was it helpful?

Solution

You need to specify your glTexCoord before the glVertex it refers to, not after. This is the same as with glColor and glNormal, glVertex uses the last attributes that you set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top