java - 为什么 float 用 1 除以 2 或 4 时返回 0?

标签 java android opengl-es

这是我用来飞过 Sprite 表的方法。在对象类中,我跟踪信息并完成所有循环,这基本上只需要分配纹理映射坐标。只是它似乎不起作用。但我记录了我翻转行/列的方式,并且它正在正确循环。但就纹理而言我什么也没看到。根本不。我错过了什么吗?

    public void SetToAnimatedTexture(int NumFramesWide, int NumFramesTall, int CurrentRow, int CurrentColumn) {
    /***************************************************************************************** 
     * This Assembles our Vertices in the correct spots by disassembling
     * our custom rectangles corners, and adds animation. Call this after creating the meshRect
     * *****************************************************************************************/

    float frameWidth = 1/NumFramesWide;
    float frameHeight = 1/NumFramesTall;

    float u1 = frameWidth*CurrentColumn;
    float u2 = (frameWidth*CurrentColumn) + frameWidth;
    float u3 = (frameWidth*CurrentColumn) + frameWidth;
    float u4  = frameWidth*CurrentColumn;

    float v1 = (frameHeight*CurrentRow) + frameHeight;
    float v2 = (frameHeight*CurrentRow) + frameHeight;
    float v3 = frameHeight*CurrentRow;
    float v4 = frameHeight*CurrentRow;

    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * VERTEX_SIZE);
    byteBuffer.order(ByteOrder.nativeOrder());
    vertices = byteBuffer.asFloatBuffer();

    float x1 = (float) rect.BottomLeftCorner.x;
    float x2 = (float) rect.BottomRightCorner.x;
    float x3 = (float) rect.TopRightCorner.x;
    float x4 = (float) rect.TopLeftCorner.x;

    float y1 = (float) rect.BottomLeftCorner.y;
    float y2 = (float) rect.BottomRightCorner.y;
    float y3 = (float) rect.TopRightCorner.y;
    float y4 = (float) rect.TopLeftCorner.y;

    vertices.put(new float[] { x1, y1, 1, 1, 1, 1, u1, v1,
                                x2, y2, 1, 1, 1, 1, u2, v2,
                                x3, y3, 1, 1, 1, 1, u3, v3,
                                x4, y4, 1, 1, 1, 1, u4, v4 });
                            //  x   y   r  g  b  A   u|s  v|t
                            //  x,y are coordinates
                            //  rgba = color red green blue alpha
                            //  u|s v|t = UV or ST texture coordinates
    vertices.flip();

    byteBuffer = ByteBuffer.allocateDirect(6 * 2);
    byteBuffer.order(ByteOrder.nativeOrder());
    indices = byteBuffer.asShortBuffer();
    indices.put(new short[] { 0, 1, 2,
                                2, 3, 0 });
    indices.flip();

}

这是我为自己建立的框架的一部分。

好的,通过进行一些日志记录解决了问题。这就是问题所在

    float frameWidth = 1/NumFramesWide;
    float frameHeight = 1/NumFramesTall;

它返回的两个值都是 0。 它说

    1/2=0 , 1/4=0

我检查了所有变量的值,这就是问题所在。

最佳答案

先进行整数除法,再进行赋值。由于 1 和 NumFrames* 都是整数,因此最终得到 0。

您可以通过将代码更改为来解决此问题

float frameWidth = 1.f/NumFramesWide;
float frameHeight = 1.f/NumFramesTall;

关于java - 为什么 float 用 1 除以 2 或 4 时返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11910833/

相关文章:

java - 在没有编译/部署过程的情况下在应用程序中实现逻辑/业务规则?

android - Realm + 改造 + Rxjava

android - 从xamarin,android中的另一个应用程序接收数据

java - 为要绘制的每个项目调用 gluLookAt()?

java - 创建可滚动区域有哪些技术?

java - 如何在 api 18 中使用 keystore 时修复 "java.security.InvalidKeyException: Unsupported key algorithm: EC. Only RSA supported"

java - 从标记创建语法树

android - Android 中的大 IndexBuffer

java - 如何使用 Spring Boot 摄取 Json 字符串数组

java - Android应用程序在M中置于 "standby"模式需要多长时间?