2011-07-08 112 views
1

我試圖用一系列其他圖像填充一個圓圈,並將這些圖像蒙上圓圈。我可以看到爲什麼這不起作用,但我無法想出如何解決這個問題的解決方案。openGL紋理遮罩

我的繪製代碼(利用處理)如下:

PGraphicsOpenGL pgl = (PGraphicsOpenGL) g; // g may change 
    // This fixes the overlap issue 
    gl.glDisable(GL.GL_DEPTH_TEST); 
    // Turn on the blend mode 
    gl.glEnable(GL.GL_BLEND); 

    // Define the blend mode 
    gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA); 

    // draw the backgroud 
    fill(200,200,200); 
    rect(0, 0, width, height); 

    // cut out the circle 
    gl.glBlendFunc(GL.GL_ZERO, GL.GL_ONE_MINUS_SRC_ALPHA); 
    tint(0,0,0,255); 
    image(circle, 0, 0); 

    // draw the circle 
    gl.glBlendFunc(GL.GL_ONE_MINUS_DST_ALPHA, GL.GL_ONE); 
    tint(140,0,0,255); 
    image(circle, 0, 100); 

    gl.glBlendFunc(GL.GL_ONE_MINUS_DST_ALPHA, GL.GL_ONE); 
    tint(140,0,140,255); 
    image(circle, 0, 0); 

我一直在http://bigbucketsoftware.com/2010/10/04/how-to-blend-an-8-bit-slide-to-unlock-widget/這似乎描述了我想要的效果中的說明。我也試過這在iPhone上有類似的結果。

這是我期待的事情發生,併發生了什麼: problem image

回答

2

問題必須與你如何對待透明區域。您可以啓用GL_ALPHA_TEST。

或者如果你的照片保持簡單,你可以用三角形繪製它們。

enter image description here

+0

啓用GL_APHA_TEST似乎沒有有所作爲不幸。接下來我將嘗試模板測試。 – jonbro

+0

您還必須確保在glAlphaFunc中使用正確的設置。 – whoplisp

+0

優秀!你是對的,這是glAlphaFunc錯了。我使用的是默認的,但通過設置它爲gl.glAlphaFunc(GL.GL_GREATER,0.0);它或多或少按預期工作......在兩個圓圈之間的過渡區域存在一些噪音,但我認爲我可以混淆一些繪圖代碼...也許它只適用於像素藝術,其中alpha混合儘管如此,還是完全滿滿的。 – jonbro

0

我真的不能幫你的混合代碼,但我還有一個建議,可能簡化繪圖邏輯。

我已經使用了模板緩衝區的東西。我想繪製一個線性光柵紋理的磁盤。我不想打擾紋理座標,因爲重要的功能是能夠準確地穿過光柵的相位。

我畫了一個大矩形的紋理,然後我在模具裏畫了一張白色的磁盤。

http://www.swiftless.com/tutorials/opengl/basic_reflection.html

(let ((cnt 0d0)) 
    (defmethod display ((w window)) 
    ;; the complex number z represents amplitude and direction 
    ;; of the grating constant 
    ;; r and psi addresses different points in the back focal plane 
    ;; r=0 will result in z=w0. the system is aligned to illuminate 
    ;; the center of the back focal plane for z=w0. 
    (let* ((w0 (* 540d0 (exp (complex 0d0 (/ pi 4d0))))) 
      (r 260d0) 
      (psi 270d0) 
      (w (* r (exp (complex 0d0 (* psi (/ pi 180d0)))))) 
      (z (+ w w0))) 
     (clear-stencil 0) 
     (clear :color-buffer-bit :stencil-buffer-bit) 
     (load-identity) 
     ;; http://www.swiftless.com/tutorials/ 
     ;; opengl/basic_reflection.html 
     ;; use stencil buffer to cut a disk out of the grating 
     (color-mask :false :false :false :false) 
     (depth-mask :false) 
     (enable :stencil-test) 
     (stencil-func :always 1 #xffffff) 
     (stencil-op :replace :replace :replace) 

     (draw-disk 100d0 (* .5d0 1920) (* .5d0 1080)) 
     ;; center on camera 549,365 
     ;; 400 pixels on lcos = 276 pixels on camera (with binning 2) 
     (color-mask :true :true :true :true) 
     (depth-mask :false) 
     (stencil-func :equal 1 #xffffff) 
     (stencil-op :keep :keep :keep) 

     ;; draw the grating 
     (disable :depth-test) 
     (with-pushed-matrix 
      (translate (* .5 1920) (* .5 1080) 0) 
     (rotate (* (phase z) 180d0 (/ pi)) 0 0 1) 
     (translate (* -.5 1920) (* -.5 1080) 0) 
     (draw *bild*)) 
     (disable :stencil-test) 
     (enable :depth-test) 

     (fill-grating *grating* (abs z)) 
     (format t "~a~%" cnt) 
     (if (< cnt 360d0) 
      (incf cnt 30d0) 
      (setf cnt 0d0)) 
     (update *bild*) 
     (swap-buffers) 
     (sleep (/ 1d0)) ;; 1 frame per second 
     (post-redisplay))))