2012-04-14 47 views
0

我正在研究什麼應該是一個簡單的OpenGl 4項目。我所要做的就是改變鼠標指針周圍的alpha值並混合。經過一番努力,我設法得到了一個圓來改變數值,但它是靜態的(不隨鼠標移動)並且居中於(0,512),而非鼠標的位置。我通過我的passiveMotionFunc發送鼠標值:不正確的值到GLSL碎片着色器

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只是一個簡單的浮動容器。我有打印聲明,所以我可以看到我的鼠標位置的值。

我的frag着色器很簡單:

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被搞亂了,它不會產生一個圓圈。

編輯:我在我的glutDisplay和main()中的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:這裏是我的VEC 2的定義:

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); } 
}; 
+0

你在做什麼與阿爾法?你的混合裝置在哪裏? – 2012-04-14 16:41:23

+0

你怎麼知道mouseCoord沒有正確傳輸?嘗試在mouseCoord上添加一些顏色的片段,看看它是否出現在需要的座標上。你還可以展示MouseCoord類型是什麼?它似乎不是一個陣列。 – 2012-04-14 16:50:32

+0

我在我的vec2定義中添加了。它以前沒有引起過這樣的問題。我添加: 如果(MousePos.x == gl_FragCoord.x && MousePos.y == gl_FragCoord.y){ \t \t \t fColor = vec4(0.0,1.0,0.0,1.0); \t \t \t} 我的碎片着色器,並沒有看到區別。儘管如此,即使它確實出現了,它也會在鼠標下。 – Tubbstosterone 2012-04-14 17:50:45

回答

1

你需要爲了在把它作爲一個宣佈mousePos結構爲uniform。您的片段着色器將其聲明爲「in」。

此外,您正在使用矢量統一上傳glUniform2fv從您的vec2類實例MousePos加載。您應該以2-arg的形式glUniform2f加載它。 glUniform2fv表單正在查找一個參數,該參數是指向2個浮點值塊的指針作爲其最後一個參數。你在那裏傳遞MousePos,這是一個實際的類實例,你指望你的鑄造操作員來完成這項工作。

+0

將它改爲統一。沒有一個區別。 – Tubbstosterone 2012-04-14 21:49:38

+1

glUniform2fv不適合你的vec2。使用glUniform2f(mouseposloc,MousePos.x,MousePos.y); – 2012-04-14 21:52:56