Advertisement

Packing vec4 in float (Java, OpenGL ES 2)

Started by February 24, 2018 09:30 AM
4 comments, last by Krypt0n 6 years, 11 months ago

Hi, so I'm trying to pack 4 color values into a single 32-bit float but I'm having some issues. The resulting color values which I am getting are not correct. What could be wrong here?

This is the part of code where I pack the 4 bytes into a single float in Java


                byte [] colorBytes = new byte[4];
                colorBytes[0] = (byte)(color.x*256);
                colorBytes[1] = (byte)(color.y*256);
                colorBytes[2] = (byte)(color.z*256);
                colorBytes[3] = (byte)(color.w*256);
                vertexManager.appendVertexColorData(ByteBuffer.wrap(colorBytes).order(ByteOrder.LITTLE_ENDIAN).getFloat());

I also tried this:


                bitSh.x = 1.0f/(256.0f*256.0f*256.0f);
                bitSh.y = 1.0f/(256.0f*256.0f);
                bitSh.z = 1.0f/(256.0f);
                bitSh.w = 1.0f;

                color.x = object.vertexColorData[i*4+0]*r;
                color.y = object.vertexColorData[i*4+1]*g;
                color.z = object.vertexColorData[i*4+2]*b;
                color.w = object.vertexColorData[i*4+3]*a;

                vertexManager.appendVertexColorData(color.dot(bitSh));

But it didn't work either, though it gave me different results, both are incorrect.

This is the vertex shader:


uniform mat4 MVPMatrix; // model-view-projection matrix

attribute vec4 position;
attribute vec2 textureCoords;
attribute float color;

varying vec4 outColor;
varying vec2 outTexCoords;

const vec4 bitSh = vec4(256.0*256.0*256.0, 256.0*256.0, 256.0, 1.0);
const vec4 bitMsk = vec4(0.0, 1.0/256.0, 1.0/256.0, 1.0/256.0);

vec4 unpack_float(const float value)
{
  vec4 res = fract(value * bitSh);
  res -= res.xxyz * bitMsk;
  return res;
}

void main()
{    outTexCoords = textureCoords;	
outColor = unpack_float(color);
	gl_Position = MVPMatrix * position;
}

 

And this is the fragment shader:


precision lowp float;

uniform sampler2D texture;

varying vec4 outColor;
varying vec2 outTexCoords;
varying vec3 outNormal;

void main()
{
    gl_FragColor = texture2D(texture, outTexCoords) * outColor;
}

 

Thanks in advance.

Advertisement

highp vec4 pack(highp float depth)
{
    const highp vec4 bitSh = vec4(256.0 * 256.0 * 256.0,
                       256.0 * 256.0,
                       256.0,
                      1.0);
    const highp vec4 bitMsk = vec4(0,
                         1.0 / 256.0,
                         1.0 / 256.0,
                             1.0 / 256.0);
    highp vec4 comp = fract(depth * bitSh);
   		
//    fract returns the fractional part of x. This is calculated as x - floor(x).
    comp -= comp.xxyz * bitMsk;
    return comp;
}

 

 

 


highp float getShadowFactor(highp vec2 pos)
{

    highp vec4 packedZValue = texture2D(shadow_map, pos);

    const highp vec4 bitShifts = vec4(1.0 / (256.0 * 256.0 * 256.0),
                        1.0 / (256.0 * 256.0),
                        1.0 / 256.0,
                        1);
    highp float shadow = dot(packedZValue , bitShifts);
 
    return shadow;
}

Both codes are for fragment shader only

Yeah this is pretty much exactly same code that I am using. I think the problem for me has something to do with conversion part in java. But I am not sure :/

Do you use glrgba format in gltexinage2d?

 

For me it looks like you ain't doing the same, but i could be wrong

32bit in a float does store 1 but for sign and 8 bit for the exponent, hence there are only 23 but left for your data, you should not try to fit in 32 bit into that.

try only to fit r g b, or for the beginning, r and g, into your float and recover it.

This topic is closed to new replies.

Advertisement