如何在片段着色器中获取原始片段颜色

时间:2015-08-05 11:15:54

标签: opengl glsl jogl fragment-shader

我正在使用JOGL为我的应用程序编写基于图块的图像查看器。图像在图块网格中划分。我正在根据当前的缩放比例为每个图块渲染纹理。 我的每个图块渲染代码如下所示:

double projectedX = ... //will calculate the X location of tile on the screen
double projectedY = ... //will calculate the Y location of tile on the screen
double projectedWidth = ... //calculate width of the tile based on scale
double projectedHeight= ... //calculate height of the tile based on scale

gl.glTranslated(projectedX, projectedY, 0.0);
gl.glScaled( scale, scale, 1.0);

texture.bing(gl);  //here, the texture is of tile image which is created in a shared context in background. There are several texture already created one for each tile.
texture.enable(gl);

double s2 = iw * (1.0 / texture.getWidth());
double t2 = ih * ( 1.0 / texture.getHeight());

//draw the texture
gl.glBegin( GL2.GL_QUADS );
gl.glTexCoord2d( 0, 0 );
gl.glVertex2d( 0, 0 );
gl.glTexCoord2d( 0, t2 );
gl.glVertex2d( 0, ih );
gl.glTexCoord2d( s2, t2 );
gl.glVertex2d( iw, ih );
gl.glTexCoord2d( s2, 0 );
gl.glVertex2d( iw, 0 );
gl.glEnd();
texture.disable(gl);

现在,我想在查看器中添加片段着色器以实现亮度,白平衡功能。为此,在片段着色器中,首先我需要获得纹理的原始像素颜色,然后应用颜色校正算法。但我不知道该怎么做。如何在片段着色器中获取当前正在渲染的纹理及其当前像素坐标?

1 个答案:

答案 0 :(得分:1)

你使用效率低且完全过时的立即模式(glBegin,glEnd,glVertex,...)。甚至Quake 2也使用保留模式。

即使您只针对OpenGL 1.2,使用立即模式也不是一个好主意。此外,您可能会遇到一些仅在将着色器与此渲染模式混合时出现的未修复和非常旧的错误。许多驱动程序使用动态VBO模拟它,或多或少成功:s取决于您的目标硬件但请至少使用VBO。

使用一些GLSL代码在片段着色器中获取来自纹理的像素的“颜色”:texture2D(mySampler2D_1,gl_TexCoord [0] .st);

texture2D是一个内置函数,用于从采样器中检索特定的纹素。声明每个纹理单元的均匀sample2D并使用glGetUniformLocation + glUniform1。不要忘记为每个单元调用glActiveTexture和glBindTexture或Texture.bind()。

如果您查找片段着色器的内置输入变量,我建议您查看此内容: https://www.opengl.org/wiki/Built-in_Variable_%28GLSL%29#Fragment_shader_inputs

gl_SampleID可能对您的情况有用。

相关问题