Ios OpenGL ES 2.0中绘制渐变背景

Ios OpenGL ES 2.0中绘制渐变背景,ios,graphics,opengl-es,opengl-es-2.0,Ios,Graphics,Opengl Es,Opengl Es 2.0,我想使我的场景背景渐变,而不是单一的颜色。有可能这样做吗?我在iOS上使用OpenGL ES 2.0。好的,现在再次作为答案 您可以绘制一个填充整个屏幕的矩形,并对其应用渐变。为此,请使用矩形顶点的纹理坐标计算顶点着色器中的相应颜色: // ... attribute vec2 textureCoordinate; varying highp vec4 colorGradient; // other attributes, uniforms, etc... void main() {

我想使我的场景背景渐变,而不是单一的颜色。有可能这样做吗?我在iOS上使用OpenGL ES 2.0。

好的,现在再次作为答案


您可以绘制一个填充整个屏幕的矩形,并对其应用渐变。为此,请使用矩形顶点的纹理坐标计算顶点着色器中的相应颜色:

// ...
attribute vec2 textureCoordinate;
varying highp vec4 colorGradient;
// other attributes, uniforms, etc...

void main() {
    // This defines the colors of the 4 corners of your rectangle.
    // The gradient is then interpolated by the GPU between the vertices.
    colorGradient = vec4(textureCoordinate, 0.0f, 1.0f); // adjust as needed

    // ... set gl_Position, etc.
}
现在可以在片段着色器中使用颜色:

// ...
varying highp vec4 colorGradient;
// other uniforms, etc...

void main() {
    // do additional shading if needed

    // colorGradient is already interpolated between the vertices by the GPU
    gl_FragColor = colorGradient;
}

您也可以使用
variable vec3
并在以后添加alpha通道,但使用
vec4
更干净。

您可以绘制一个填充整个屏幕的矩形,并对其应用渐变。您可以使用矩形四个顶点的纹理坐标,计算顶点着色器中顶点的相应颜色,将其作为
variable vec3
传递给片段着色器(因此它由GPU自动插值),然后在其中将颜色设置为
gl\u FragColor