如何使用GLSL和OpenGL ES 2.0从整数中获取一点 [英] How to get a bit from a integer using GLSL and OpenGL ES 2.0

查看:82
本文介绍了如何使用GLSL和OpenGL ES 2.0从整数中获取一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数值,想要获取放置在特殊位置的位.我正在使用GLSL for OpenGL ES 2.0

I have an integer value and want to get the Bit which is placed on a special location. I'm working with GLSL for OpenGL ES 2.0

类似这样的东西:

GetBitOnLocation(int value, int location)
{
  bit myBit = value[location];
  if (myBit == 0)
    return 0;
  if (myBit == 1)
    return 1;
}

推荐答案

以下是使用整数数学的解决方案(注意:不支持负整数)

Here is a solution using integer math (note: negative integers not supported)

// 2^x
int pow2(int x){
    int res = 1;
    for (int i=0;i<=31;i++){
        if (i<x){
            res *= 2;
        }
    }
    return res;
}

// a % n
int imod(int a, int n){
    return a - (n * (a/n));
}

// return true if the bit at index bit is set
bool bitInt(int value, int bit){
    int bitShifts = pow2(bit);
    int bitShiftetValue = value / bitShifts;
    return imod(bitShiftetValue, 2) > 0;
}

这篇关于如何使用GLSL和OpenGL ES 2.0从整数中获取一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆