等式总是评估为0,即使它不应该

时间:2014-06-16 17:47:34

标签: java opengl

我正在尝试使用lwjgl在屏幕上渲染纹理矩形。我已经注册了纹理并加载了它们。我正在尝试编写一个方法,只使用纹理的指定区域来应用于矩形,这将允许使用spritesheets。我有这个代码来做:

public static void drawTexturedRectangle(int x, int y, int width,
        int height, int textureX, int textureY, int textureWidth,
        int textureHeight, Texture texture) {

    System.out.println("Drawing tr:"); //console output to check input
    System.out.println("  -input:");
    System.out.println("    -x: " + x);
    System.out.println("    -y: " + y);
    System.out.println("    -width: " + width);
    System.out.println("    -height: " + height);
    System.out.println("    -tex-x: " + textureX);
    System.out.println("    -tex-y: " + textureY);
    System.out.println("    -tex-width: " + textureWidth);
    System.out.println("    -tex-height: " + textureHeight);

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId());
    GL11.glBegin(GL11.GL_QUADS);
    {

        System.out.println("  -rendered:");

        int vx = x; // define the x-coordinate for the first vertex
        int vy = y+height; // define the x-coordinate for the first vertex
        float tx = (1 / texture.getWidth()) * textureX; // define the x-coordinate of the texture section
        float ty = (1 / texture.getHeight()) * textureY;// define the y-coordinate of the texture section
        System.out.println("    -point 1:"); // more system output
        System.out.println("      -x: " + vx);
        System.out.println("      -y: " + vy);
        System.out.println("      -tex-x: " + tx); // these are always 0.0
        System.out.println("      -tex-y: " + ty); // ^^ even if textureY = texture.getHeight() which should evaluate to 1
        GL11.glTexCoord2f(tx, ty); // Actually carry out the opengl commands.
        GL11.glVertex2i(vx, vy); // ^^

        // Repeat above
        vx = x;
        vy = y;
        tx = (1 / texture.getWidth()) * textureX;
        ty = (1 / texture.getHeight()) * (textureY + textureHeight);
        System.out.println("    -point 2:");
        System.out.println("      -x: " + vx);
        System.out.println("      -y: " + vy);
        System.out.println("      -tex-x: " + tx);
        System.out.println("      -tex-y: " + ty);
        GL11.glTexCoord2f(tx,ty);
        GL11.glVertex2i(vx, vy);

        // Repeat again
        vx = x+width;
        vy = y;
        tx = (1 / texture.getWidth()) * (textureX + textureWidth);
        ty = (1 / texture.getHeight()) * (textureY - textureHeight);
        System.out.println("    -point 3:");
        System.out.println("      -x: " + vx);
        System.out.println("      -y: " + vy);
        System.out.println("      -tex-x: " + tx);
        System.out.println("      -tex-y: " + ty);
        GL11.glTexCoord2f(tx,ty);
        GL11.glVertex2i(vx, vy);

        //One more time to make a quad!
        vx = x+width;
        vy = y+height;
        tx = (1 / texture.getWidth()) * (textureX + textureWidth);
        ty = (1 / texture.getHeight()) * textureY;
        System.out.println("    -point 4:");
        System.out.println("      -x: " + vx);
        System.out.println("      -y: " + vy);
        System.out.println("      -tex-x: " + tx);
        System.out.println("      -tex-y: " + ty);
        GL11.glTexCoord2f(tx,ty);
        GL11.glVertex2i(vx, vy);
    }
    GL11.glEnd();
}

但是,运行时,tx和ty的值始终计算为0.0。

Texture是一个存储注册id和像素宽度和纹理高度的类。

x,y,width和height是矩形的相应属性。

“textureX”和“textureY”是纹理上图像左上角的坐标(我知道左下角是opengl标准)

“textureWidth”和“textureHeight”是纹理部分的宽度和高度。

2 个答案:

答案 0 :(得分:5)

这是你的问题:

float tx = (1 / texture.getWidth()) * textureX;

纹理是一个整数,当你划分整数时,你得到一个整数。超过一位数字的所有内容都会被截断。此表达式的计算结果为零。

你可以通过制作一个浮点数来解决这个问题:

float tx = (1f / texture.getWidth()) * textureX;

或者更精确的双倍:

float tx = (float)(1.0 / texture.getWidth()) * textureX;

答案 1 :(得分:1)

我怀疑问题是你正在进行int分割,这会截断任何小数。

我们以此为例:

float tx = (1 / texture.getWidth()) * textureX;

假设texture.getWidth()返回100. 1/100是.01,但由于int除以int是一个int,它被截断为0.然后你将0乘以textureX,这显然会计算为0

解决方案是将从getWidth()返回的值转换为float,或者只使用1.0或1f而不是int literal 1。