有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用OpenGL进行纹理颜色操作(使用LWJGL和Java)

我是OpenGL新手。我将Java与LWJGL和Slick一起使用。到目前为止,我可以绘制多个纹理,使用缓冲区,我可以将屏幕上显示的图像复制到一个纹理,以便进行后处理。 使用glColor3f()可以使屏幕具有所需的颜色(仅蓝色、仅红色、仅显示蓝色和绿色通道等)

但是glColor3f(r,g,b)所做的只是将r,g,b的值乘以当前像素。如果R、G、B是像素的当前值,则glColor对所有像素执行(R*R、G*G、B*B)

我要做的是使用当前值R、G、B,例如,我可以将红色通道交换为蓝色通道:

(B、R、G)

或者将这些值和新值用于算术运算

(R+G*R,(B+G)/5,B*0.2)

我的目的是制作一个灰度纹理,用

color = (R*0.5 + G*0.5 + B*0.5)/3
functionThatChangesColor(color, color, color);

我怎样才能做到这一点呢

谢谢


共 (1) 个答案

  1. # 1 楼答案

    使用实现组件旋转的着色器。使用更高版本的OpenGL,还可以将纹理的组件旋转设置为纹理参数。见http://www.opengl.org/wiki/Texture#Swizzle_mask

    Swizzle mask

    While GLSL shaders are perfectly capable of reordering the vec4​ value returned by a texture function, it is often more convenient to control the ordering of the data fetched from a texture from code. This is done through swizzle parameters.

    Texture objects can have swizzling parameters. This only works for textures with color image formats. Each of the four output components, RGBA, can be set to come from a particular color channel. The swizzle mask is respected by all read accesses, whether via texture samplers or Image Load Store.

    To set the output for a component, you would set the GL_TEXTURE_SWIZZLE_C texture parameter, where C is R, G, B, or A. These parameters can be set to the following values:

    • GL_RED: The value for this component comes from the red channel of the > image. All color formats have at least a red channel.
    • GL_GREEN: The value for this component comes from the green channel of > the image, or 0 if it has no green channel.
    • GL_BLUE: The value for this component comes from the blue channel of > the image, or 0 if it has no blue channel.
    • GL_ALPHA: The value for this component comes from the alpha channel of > the image, or 1 if it has no alpha channel.
    • GL_ZERO: The value for this component is always 0.
    • GL_ONE: The value for this component is always 1.

    You can also use the GL_TEXTURE_SWIZZLE_RGBA parameter to set all four at > once. This one takes an array of four values. For example:

    //Bind the texture 2D.
    GLint swizzleMask[] = {GL_ZERO, GL_ZERO, GL_ZERO, GL_RED};
    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask);
    

    This will effectively map the red channel in the image to the alpha > channel when the shader accesses it.