公式的计算结果始终为 0,即使它不应该



我试图使用lwjgl渲染一个纹理矩形到屏幕上。我已经注册了纹理并加载了它们。我试图写一个方法,将只使用指定区域的纹理应用到矩形,这将允许使用精灵表。我用下面的代码来做:

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、宽度和高度分别是矩形的属性。

"textureX"one_answers"textureY"是图像在纹理上的左上角的坐标(我知道左下角是opengl标准)

"textureWidth"one_answers"textureHeight"是纹理部分的宽度和高度。

这就是你的问题:

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

Texture是一个整数,当你将整数除除时你得到一个整数。超过个位数的所有内容都被截断。这个表达式的求值为0。

您可以通过将1设置为浮点数来解决这个问题:

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

或者是精度更高的double类型:

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

我怀疑问题是你在做整型除法,这会截断任何小数。

让我们以这个为例:

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

假设texture.getWidth()返回100。1/100是0.01,但因为整型除以整型是整型,所以它被截断为0。然后用0乘以textureX,结果显然是0。

解决方案是将getWidth()返回的值强制转换为浮点数,或者使用1.0或1f代替int字面值1。

最新更新