0
我正在尋找着色器CG或HLSL,它可以計算我想要的紅色像素數或任何其他顏色。用於計算像素數的着色器
我正在尋找着色器CG或HLSL,它可以計算我想要的紅色像素數或任何其他顏色。用於計算像素數的着色器
你可以在片段着色器中使用atomic counters來做到這一點。只需測試輸出顏色以查看它是否在紅色的某個容差範圍內,如果是,則增加計數器。在繪製調用之後,您應該能夠讀取CPU上的計數器值並根據您的喜好執行任何操作。
編輯:增加了一個很簡單的例子片段着色器:
// Atomic counters require 4.2 or higher according to
// https://www.opengl.org/wiki/Atomic_Counter
#version 440
#extension GL_EXT_gpu_shader4 : enable
// Since this is a full-screen quad rendering,
// the only input we care about is texture coordinate.
in vec2 texCoord;
// Screen resolution
uniform vec2 screenRes;
// Texture info in case we use it for some reason
uniform sampler2D tex;
// Atomic counters! INCREDIBLE POWER
layout(binding = 0, offset = 0) uniform atomic_uint ac1;
// Output variable!
out vec4 colorOut;
bool isRed(vec4 c)
{
return c.r > c.g && c.r > c.b;
}
void main()
{
vec4 result = texture2D(tex, texCoord);
if (isRed(result))
{
uint cval = atomicCounterIncrement(ac1);
}
colorOut = result;
}
您還需要建立在你的代碼中的原子計數器:
GLuint acBuffer = 0;
glGenBuffers(1, &acBuffer);
glBindBuffer(GL_ATOMIC_COUNTER_BUFFER, acBuffer);
glBufferData(GL_ATOMIC_COUNTER_BUFFER, sizeof(GLuint), NULL, GL_DYNAMIC_DRAW);
您可以提供一個簡單的着色器示例嗎? – ragia 2015-04-10 07:45:54
Downvoters,請解釋一下你的恨。 @ragia,這可能會幫助http://stackoverflow.com/questions/23091370/count-pixels-by-color-in-webgl-fragment-shader – yoyo 2015-03-31 04:55:42
我希望我知道他們爲什麼downvote! – ragia 2015-03-31 13:59:29
Downvotes是StackOverflow的一部分,但downvoters的意思是在評論中指出他們爲什麼downvoting。 /聳聳肩/ – yoyo 2015-03-31 16:00:08