2

我正在使用 OpenGL(固定管道,我需要支持 GL1.1)试图将图像变成类似棕褐色的颜色。

到目前为止,我已经能够(有点,它最终太暗)对图像进行灰度化,但是当我尝试对其进行调制或添加另一种颜色以使其呈现棕褐色外观时,所有出现的都是恒定颜色。

到目前为止,代码是

float weights_vector[] = { 0.2126f, 0.7152f, 0.0722f, 1.0f };
float color_vector[] = {0.1f, 0.2f, 0.05f, 0.0f};

gl.glActiveTexture(GL11.GL_TEXTURE0); // Select texture unit 0
gl.glBindTexture(GL11.GL_TEXTURE_2D, _texturePointer); // Bind the texture
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_COMBINE); // Set to combine
if (grayscale) { // Gives better results, but can't understand yet why
    gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
else {
    gl.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
}

/* Grayscale */
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_DOT3_RGB);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC0_RGB, GL11.GL_TEXTURE);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND0_RGB, GL11.GL_ONE_MINUS_SRC_COLOR);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_CONSTANT);
gl.glTexEnvfv(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_COLOR, weights_vector, 0);

/* Give color */
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_ADD);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC0_RGB, GL11.GL_PREVIOUS);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_CONSTANT);
gl.glTexEnvfv(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_COLOR, color_vector, 0);

编辑1:

感谢@Tim,我终于让它工作了,他断然指出我的代码永远不会工作。我把工作代码留在这里以防万一有人需要它。

最终的代码看起来像这样,工作起来就像一个魅力,虽然在我的移动设备上有点慢,屏幕上有很多精灵。

gl.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

// STAGE 0
gl.glActiveTexture(GL11.GL_TEXTURE0);
gl.glEnable(GL11.GL_TEXTURE_2D);
gl.glBindTexture(GL11.GL_TEXTURE_2D, _texturePointer);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_COMBINE);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_DOT3_RGB);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_SRC0_RGB, GL11.GL_TEXTURE);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_CONSTANT);
//gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND0_RGB, GL11.GL_SRC_COLOR);
gl.glTexEnvf(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND0_RGB, GL11.GL_ONE_MINUS_SRC_COLOR);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_RGB, GL11.GL_SRC_COLOR);
gl.glTexEnvfv(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_COLOR, weights_vector, 0);

// STAGE 2
gl.glActiveTexture(GL11.GL_TEXTURE1);
gl.glEnable(GL11.GL_TEXTURE_2D);
gl.glBindTexture(GL11.GL_TEXTURE_2D, 100); // Dummy texture - use any that's not your current
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_TEXTURE_ENV_MODE, GL11.GL_COMBINE);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_COMBINE_RGB, GL11.GL_ADD);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_SRC0_RGB, GL11.GL_PREVIOUS);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_SRC1_RGB, GL11.GL_CONSTANT);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND0_RGB, GL11.GL_SRC_COLOR);
gl.glTexEnvi(GL11.GL_TEXTURE_ENV, GL11.GL_OPERAND1_RGB, GL11.GL_SRC_COLOR);
4

1 回答 1

3

我认为您对状态机的工作方式有误解。

If you're expecting that code to convert the color to grayscale, and then convert the grayscale to sepia, it doesn't work that way.

The code under Give color isn't being appended to the end of the Grayscale stage, it's just overwriting all of it's settings. So you're going directly from the texture sample to the GL_ADD part, because you've overwritten all of the grayscale settings. You need to switch to another texture unit to setup the grayscale to sepia transform.

There's lots of examples here if you want an example:

http://www.opengl.org/wiki/Texture_Combiners

于 2012-07-14T22:43:37.307 回答