【问题标题】:Incorrect values getting to GLSL Frag Shader获取 GLSL Frag Shader 的值不正确
【发布时间】:2012-04-14 16:37:22
【问题描述】:

我正在做一个简单的 OpenGL 4 项目。我要做的就是改变鼠标指针周围的 alpha 值并混合。经过一些工作,我设法得到一个圆圈来更改值,但它是静态的(不随鼠标移动)并且以(0,512)为中心,非常不是我的鼠标位置。我通过passiveMotionFunc 发送鼠标值:

void
motion ( int x, int y)
{
    MousePos.x = x;
    MousePos.y = y;
    printf("x=%i\ny=%i\n\n", x, y);
    glUniform2fv(glGetUniformLocation(program, "MousePos"), 2, MousePos);
    glutPostRedisplay();
}

MousePos 只是一个简单的浮动容器。我有打印语句,所以我可以看到我的鼠标位置的值。

我的片段着色器很简单:

in  vec4 color;
in  vec4 vPosition;
in vec2 MousePos;
out vec4 fColor;

void
main()
{
        //float x = gl_FragCoord.X;
        //float y = gl_FragCoord.Y;
        float distance = sqrt(pow(MousePos.x-gl_FragCoord.x, 2) + pow(MousePos.y-gl_FragCoord.y, 2));
        if(distance > 30)
            fColor = color;
        else{
            float a = .1;
            fColor = color;
            fColor.a = a;
        }
}

我完全坚持这一点。我假设碎片着色器没有得到更新的鼠标坐标,因为如果 FragCoord 被搞砸了,它就不会产生一个圆圈。

编辑:我在 main() 中的 glutDisplay 和 glutPassiveFunc 之前的 init() 函数中设置了我的混合。

void
init()
{
    wall();
    redwall();
    // Create a vertex array object
    GLuint vao;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(quad_colors), NULL, GL_STATIC_DRAW );

    glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points );

    glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(quad_colors), quad_colors );


    // Load shaders and use the resulting shader program
    program = InitShader( "p6v.glsl", "p6f.glsl" );
    glUseProgram( program );

    // set up vertex arrays
    GLuint vPosition = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( vPosition );
    glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
               BUFFER_OFFSET(0) );

    GLuint vColor = glGetAttribLocation( program, "vColor" ); 
    glEnableVertexAttribArray( vColor );
    glVertexAttribPointer( vColor, 4, GL_FLOAT, GL_FALSE, 0,
               BUFFER_OFFSET(sizeof(points)) );

    //glEnable(GL_DEPTH_TEST);
    glEnable( GL_BLEND );
    glEnable( GL_ALPHA_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glDepthMask(false);

    glClearColor( 0.0, 0.5, 1.0, 1.0 );
}

EDIT2:这是我的 vec2 定义:

struct vec2 {

    GLfloat  x;
    GLfloat  y;

    //
    //  --- Constructors and Destructors ---
    //

    vec2( GLfloat s = GLfloat(0.0) ) :
    x(s), y(s) {}

    vec2( GLfloat x, GLfloat y ) :
    x(x), y(y) {}

    vec2( const vec2& v )
    { x = v.x;  y = v.y;  }

    //
    //  --- Indexing Operator ---
    //

    GLfloat& operator [] ( int i ) { return *(&x + i); }
    const GLfloat operator [] ( int i ) const { return *(&x + i); }

    //
    //  --- (non-modifying) Arithematic Operators ---
    //

    vec2 operator - () const // unary minus operator
    { return vec2( -x, -y ); }

    vec2 operator + ( const vec2& v ) const
    { return vec2( x + v.x, y + v.y ); }

    vec2 operator - ( const vec2& v ) const
    { return vec2( x - v.x, y - v.y ); }

    vec2 operator * ( const GLfloat s ) const
    { return vec2( s*x, s*y ); }

    vec2 operator * ( const vec2& v ) const
    { return vec2( x*v.x, y*v.y ); }

    friend vec2 operator * ( const GLfloat s, const vec2& v )
    { return v * s; }

    vec2 operator / ( const GLfloat s ) const {
#ifdef DEBUG
    if ( std::fabs(s) < DivideByZeroTolerance ) {
        std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
              << "Division by zero" << std::endl;
        return vec2();
    }
#endif // DEBUG

    GLfloat r = GLfloat(1.0) / s;
    return *this * r;
    }

    //
    //  --- (modifying) Arithematic Operators ---
    //

    vec2& operator += ( const vec2& v )
    { x += v.x;  y += v.y;   return *this; }

    vec2& operator -= ( const vec2& v )
    { x -= v.x;  y -= v.y;  return *this; }

    vec2& operator *= ( const GLfloat s )
    { x *= s;  y *= s;   return *this; }

    vec2& operator *= ( const vec2& v )
    { x *= v.x;  y *= v.y; return *this; }

    vec2& operator /= ( const GLfloat s ) {
#ifdef DEBUG
    if ( std::fabs(s) < DivideByZeroTolerance ) {
        std::cerr << "[" << __FILE__ << ":" << __LINE__ << "] "
              << "Division by zero" << std::endl;
    }
#endif // DEBUG

    GLfloat r = GLfloat(1.0) / s;
    *this *= r;

    return *this;
    }

    //
    //  --- Insertion and Extraction Operators ---
    //

    friend std::ostream& operator << ( std::ostream& os, const vec2& v ) {
    return os << "( " << v.x << ", " << v.y <<  " )";
    }

    friend std::istream& operator >> ( std::istream& is, vec2& v )
    { return is >> v.x >> v.y ; }

    //
    //  --- Conversion Operators ---
    //

    operator const GLfloat* () const
    { return static_cast<const GLfloat*>( &x ); }

    operator GLfloat* ()
    { return static_cast<GLfloat*>( &x ); }
};

【问题讨论】:

  • 你对 alpha 有什么帮助吗?你的混合设置在哪里?
  • 你怎么知道mouseCoord没有正确传输?尝试在 mouseCoord 上放置一些颜色的片段,看看它是否出现在所需的坐标处。您还可以显示 MouseCoord 类型吗?它似乎不仅仅是一个数组。
  • 我在我的 vec2 定义中添加了。它以前没有引起过这样的问题。我补充说: if (MousePos.x == gl_FragCoord.x && MousePos.y == gl_FragCoord.y){ fColor = vec4(0.0, 1.0, 0.0, 1.0); } 到我的碎片着色器,并没有看到有什么不同。不过,公平地说,即使它确实出现了,它也会在鼠标下方。
  • 您是否尝试使用 gDebugger 查看它应该向您显示您传递的统一参数gremedy.com

标签: opengl glsl shader opengl-3 fragment-shader


【解决方案1】:

您需要将 MousePos 声明为 uniform 才能将其作为一个传递。您的片段着色器已将其声明为“in”。

您还使用矢量统一上传glUniform2fv 从您的 vec2 类实例 MousePos 加载。您应该以 2-arg 形式 glUniform2f 加载它。 glUniform2fv 表单正在寻找一个参数,该参数是一个指向 2 个浮点值块的指针作为其最后一个参数。您正在那里传递MousePos,这是一个实际的类实例,并且您指望您的铸造操作员来完成这项工作。

【讨论】:

  • glUniform2fv 不适合您的 vec2。使用 glUniform2f(mouseposloc, MousePos.x, MousePos.y);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多