简体   繁体   English

如何在OpenGL / GLSL中设置特定颜色的阈值

[英]How to threshold a specific color in OpenGL/GLSL

I made a particle trail effect in OpenGL. 我在OpenGL中制作了粒子追踪效果。

The trick is to draw a semi transparent rectangle on the screen to gradually fade the previous frame: 技巧是在屏幕上绘制一个半透明的矩形以逐渐淡入前一帧:

    ofSetColor(0,0,0,255*(1.0-persistence));
    ofFill();
    ofRect(0, 0, ofGetScreenWidth(), ofGetScreenHeight());

and then draw the particles (roughly): 然后大致绘制粒子:

    ofEnableBlendMode(OF_BLENDMODE_ADD);    
    ofEnablePointSprites();
    vbo.draw(GL_POINTS, 0, (int)points.size());
    ofDisablePointSprites();

粒子痕迹效果

The problem is that the trails do not fade until being pure black, but remain grayish. 问题在于,这些迹线直到变成纯黑色才不会消失,而是保持灰色。

I could threshold the dark gray values, or fade them progressively, but what is the best way to do it? 我可以设定深灰色值的阈值,也可以逐渐淡化它们,但是最好的方法是什么? OpenGL blending functions/operations or shader? OpenGL混合功能/操作或着色器? Speed is a major concern. 速度是一个主要问题。

You need to allocate your fbo with a floating point format. 您需要以浮点格式分配fbo。

There is an example in openFrameworks examples directory that shows the difference between floating point and non floating point format and it seems to be your problem ( examples/gl/fboTrailsExample ). openFrameworks examples目录中有一个示例,该示例显示了浮点格式和非浮点格式之间的差异,这似乎是您的问题( examples/gl/fboTrailsExample )。

Allocation with a floating point format: 以浮点格式分配:

ofFbo rgbaFboFloat;
rgbaFboFloat.allocate(400, 400, GL_RGBA32F_ARB);

and then in the update method you can draw to the fbo between rgbaFboFloat.begin(); 然后在update方法中,您可以在rgbaFboFloat.begin();之间绘制到fbo rgbaFboFloat.begin(); and rgbaFboFloat.end(); rgbaFboFloat.end();

To finish, you draw your fbo in the draw method: rgbaFboFLoat.draw(0,0); 最后,您可以使用draw方法绘制fbo: rgbaFboFLoat.draw(0,0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM