简体   繁体   English

Android OpenGL ES 2.0:为具有相同片段着色器的对象设置唯一的颜色

[英]Android OpenGL ES 2.0: Setting Unique Colors for Objects with same Fragment Shader

I have 6 columns in an Android OpenGL ES View. 我在Android OpenGL ES视图中有6列。 When the user touches the screen, a column glows a certain color. 当用户触摸屏幕时,列会发出某种颜色。 Initially, all the columns used the same glow color, but i'm trying to write code to give each column a unique glow color when touched. 最初,所有列都使用相同的发光颜色,但是我试图编写代码以使每列在触摸时都具有唯一的发光颜色。

The bug i'm noticing is that each column glow is still the same color, as if my call to set the glUniform is getting stuck on one color after being set the first time. 我注意到的错误是,每列辉光仍然是相同的颜色,好像我设置glUniform的调用在第一次设置后卡在一种颜色上一样。 The last color specified in my switch statement in setUniforms() is the color that all 6 columns use when touched, the colors should instead be different for each column. 我在setUniforms()中的switch语句中指定的最后一种颜色是所有6列在触摸时都使用的颜色,因此每列的颜色应该不同。

Is there a bug in my Fragment Shader that causes all columns to incorrectly use case 5 's mGlowColorHandle value? 我的片段着色器中是否存在一个错误,该错误导致所有列均不正确使用案例5的mGlowColorHandle值? Is the bug instead in my setUniforms method? 是我的setUniforms方法中的错误吗?

OpenGL_GLRenderer.java OpenGL_GLRenderer.java

public class OpenGL_GLRenderer implements GLSurfaceView.Renderer {

    @Override
    public void onDrawFrame(GL10 glUnused) {
        GLES20.glUseProgram(neckProgramHandle);
        setUniforms(neckProgramHandle);
        ...
    }

    private void setUniforms(int programHandle)
    {
        mGlowColorHandle = GLES20.glGetUniformLocation(programHandle, "stringGlowColor");           //column glow color

        //************************String Glow code*******************************
        //if user's touching the screen, make nearest string glow
        for (int i = 0; i< 6; i++) {
            if (stringGlowEffects[i] != null) {
                float top = orthoTop + (orthoBottom-orthoTop)*stringGlowEffects[i].y + scroller.getCurrentValue();
                GLES20.glUniform2f(mGlowPosHandles[i], stringGlowEffects[i].x, top);
                float aRandomFloat = (float) (Math.random() * 100);        //generate random number from 0-99
                float glowEffectScale = 1.0f + (aRandomFloat - 50.0f) / 300.0f;
                GLES20.glUniform1f(mGlowScaleHandles[i], glowEffectScale);      //ERROR HERE: case 5's color is displayed for every column
                switch (i){
                    case 0:
                        GLES20.glUniform4f(mGlowColorHandle,.0f, .0f, 1.0f, 1.0f);
                    case 1:
                        GLES20.glUniform4f(mGlowColorHandle,.0f, 1.0f, .0f, 1.0f);
                    case 2:
                        GLES20.glUniform4f(mGlowColorHandle,1.0f, .0f, .0f, 1.0f);
                    case 3:
                        GLES20.glUniform4f(mGlowColorHandle,.0f, 1.0f, 1.0f, 1.0f);
                    case 4:
                        GLES20.glUniform4f(mGlowColorHandle,1.0f, 1.0f, .0f, 1.0f);
                    case 5:
                        GLES20.glUniform4f(mGlowColorHandle,1.0f, .0f, 1.0f, 1.0f);
                }
            }
            else{
                GLES20.glUniform1f(mGlowScaleHandles[i], 0.0f);
            }
        }
    }
}

fragment_shader_neck.glsl fragment_shader_neck.glsl

uniform sampler2D u_Texture;              // The input texture.
varying vec2 v_TexCoordinate;  // Interpolated texture coordinate per fragment.
uniform vec4 stringGlowColor;          // color of the string (column)

// The entry point for our fragment shader.
void main(){
    ...
    lowp vec4 fromTexture = texture2D(u_Texture, v_TexCoordinate);
    gl_FragColor = fromTexture + stringGlowColor*effectScale; 
}

You need a break statement after each call to GLES20.glUniform4f in your switch statement. switch语句中每次调用GLES20.glUniform4f之后,都需要一个break语句。 When your program enters a switch statement, it jumps to the appropriate case label and continues until it hits a break statement or the end of the switch , even if that takes it past a different case label. 当您的程序输入switch语句时,它会跳到适当的case标签,并继续运行,直到遇到break语句或switch的结尾为止,即使这使它超过了另一个case标签。 This is sometimes useful, but mostly just confuses people. 有时这很有用,但大多数情况下只会使人感到困惑。

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

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