简体   繁体   English

如何在片段着色器中轻松更改纹理的像素颜色? (OpenGL)

[英]How to easily change pixel color of a texture in the fragment shader? (OpenGL)

My goal is to make simple sand simulation using c++ and OpenGL. Right now my plan is to have a 2d array of pixel colors and a texture the same size.我的目标是使用 c++ 和 OpenGL 进行简单的沙子模拟。现在我的计划是拥有像素 colors 的二维数组和相同大小的纹理 To simulate sand I will update the array accordingly to the sand coordinates and where it has to travel.为了模拟沙子,我将根据沙子坐标及其必须移动的位置相应地更新数组。 I'm thinking of sending the 2d array of pixels to the fragment shader and update the texture there with colors on the array.我正在考虑将二维像素数组发送片段着色器并使用数组上的 colors 更新那里的纹理。 The problem is that I can't find a way to change the pixel color on the texture.问题是我找不到改变纹理像素颜色的方法。

So how do I change the color of the pixel at certain coordinates on texture?那么如何改变纹理上特定坐标处像素的颜色呢? Is doing this even practical?这样做是否实用? If, no what are the other ways?如果,不,还有什么其他方法?

Did u mean for texture coordinates?你是说纹理坐标吗? You can change only fragment color output. You have also the texture color but you can use only for frgament output.您只能更改片段颜色 output。您也有纹理颜色,但只能用于片段 output。

gl_FragColor = tex + vec4(0.5, 0, 0, 1)*tex.a;

for blending operation用于混合操作

Anyway here's more info about the frag https://www.khronos.org/opengl/wiki/Fragment_Shader#Other_outputs无论如何,这里有关于碎片的更多信息https://www.khronos.org/opengl/wiki/Fragment_Shader#Other_outputs

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

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