我试图向进入着色器的每个顶点发送一种颜色,但仅使用一个浮点值。我认为很奇怪,你不能发送 4 个字节作为每个顶点的属性,但感觉这是不可能的,我将尝试将 RGBA 打包在单个浮点变量中,所以这是我的代码:
Jave 代码(将值打包在一个 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;
}
着色器代码(将其打包): 顶点着色器:
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");
那么,有什么想法吗?
提前致谢!
最佳答案
我会转换
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
一些“伪代码”来扭转这个局面
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 - 在java中将4个浮点颜色转换为1个浮点,然后在openGL ES 2.0着色器中再次返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12515909/