在Java中将4浮点颜色变为一种浮点,然后在openGL ES 2.0着色器中再次返回 [英] 4 float color to one float in java, and back again in openGL ES 2.0 shader
问题描述
我正在尝试向着色器发送的每个顶点都发送一种颜色,但是仅使用一个浮点值.我觉得很奇怪,您不能在每个顶点上发送4个字节作为属性,但是感觉我不可能尝试将RGBA打包到单个float变量中,所以这是我的代码:
I am trying to send one color with every vertex that goes in to the shader, but in only one float value. I think is weird that you cannot send 4 bytes as attributes with every vertex, but sense it's not possible I am going to try to pack RGBA in a single float variable, so this is my code:
Jave代码(将值打包为一个浮点数):
Jave code (that packs the values in one float):
private float fourfColor2One(float r, float g, float b, float a)
{
long temp = (byte) (r * 255);
float res = temp << 24;
temp = (byte) (g * 255);
res += temp << 16;
temp = (byte) (b * 255);
res += temp << 8;
temp = (byte) (a * 255);
res += temp;
return res;
}
着色器代码(将其打包): 顶点着色器:
Shadercode (that packs it up): Vertex shader:
attribute vec4 vPosition;
attribute float fColor;
uniform vec2 vCamPos;
uniform float fZoom;
uniform mat4 m_projectionMat;
varying float v_Color;
void main() {
vec4 position = vec4(vPosition.x - vCamPos.x - 16., vPosition.y - vCamPos.y - ((32. / "+ GraphicsRenderer.mScnR +") / 2.), -1., 1.);
position = position * m_projectionMat;
gl_Position = vec4(position.xy * fZoom, -1, 1);
v_Color = fColor;
}
片段着色器:
precision mediump float;
varying float v_Color;
void main() {
uint temp = uint(v_Color);
float r = temp & 0xf000;
float g = temp & 0x0f00;
float b = temp & 0x00f0;
float a = temp & 0x000f;
gl_FragColor = vec4(r, g, b, a);
}
所以我有三个问题:
-
这个概念甚至可能吗?
Is this concept even possible?
转换不正确,对吧?我假设当我从float转换为float时,转换不会保留位的顺序,而是保留它在该数据类型中表示的值,对吧?
The conversions is not correct, right? I assume that when I convert from and to float, the conversion does not retain the order of the bit, but rather the values it represent in that datatype, right?
现在,当我运行时,着色器使我返回-1: glGetAttribLocation(mShaderHandle,"fColor"); 但是,这一行只适用于以下一行: glGetAttribLocation(mShaderHandle,"vPosition");
Right now the shader gives me -1 back when i run: glGetAttribLocation(mShaderHandle, "fColor"); But this one works one line before: glGetAttribLocation(mShaderHandle, "vPosition");
那么,有什么想法吗?
提前谢谢!
推荐答案
我会转换
private static int fourfColor2One(float r, float g, float b, float a) {
return (Math.round(r * 255) << 24) +
(Math.round(g * 255) << 16) +
(Math.round(b * 255) << 8) +
Math.round(a * 255);
}
这假设您的着色器"的缩放比例为0.0到1.0,而您假设缩放比例为0.0到255.0
This assumes your scale is 0.0 to 1.0 in your "shader" you assume the scale is 0.0 to 255.0
一些伪代码"可以逆转这种情况
Some "pseudo code" to reverse this
float r = (temp & 0xf000)/255.0;
float g = (temp & 0x0f00)/255.0;
float b = (temp & 0x00f0)/255.0;
float a = (temp & 0x000f)/255.0;
这篇关于在Java中将4浮点颜色变为一种浮点,然后在openGL ES 2.0着色器中再次返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!