我试图为进入着色器的每个顶点发送一种颜色,但只有一个浮点值。我认为您不能将 4 个字节作为每个顶点的属性发送,这很奇怪,但感觉不可能我将尝试将 RGBA 打包在一个浮点变量中,所以这是我的代码:
Jave 代码(将值打包在一个浮点数中):
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;
}
着色器代码(打包):顶点着色器:
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);
}
所以我有三个问题:
这个概念甚至可能吗?
转换不正确,对吧?我假设当我从浮点数转换为浮点数时,转换不会保留位的顺序,而是保留它在该数据类型中表示的值,对吗?
现在,当我运行时,着色器返回 -1:glGetAttribLocation(mShaderHandle, "fColor"); 但是这一行之前可以使用: glGetAttribLocation(mShaderHandle, "vPosition");
那么,有什么想法吗?
提前致谢!