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.