问题描述
我试图在cocos2d的CCSprite上做一个圆形剪辑,在搜索了很多,我试过开放gl glScissor方法,我实现了一个平方剪辑我的sprite。但是,我需要使它成一个圆,这似乎是不可能做的glScissor。我一直在尝试,我发现了一些关于gl stencil,但我没有找到一个如何对此,我不熟悉opengl。
此外,我听说了多次调用glScissor的事情,你可以实现自定义形状,但我还没有找到任何东西。
预先感谢任何回答。
这是我的实际访问方法:
- (void)visit
{
glPushMatrix
glEnable(GL_SCISSOR_TEST);
glScissor(clippingRegion.origin.x,clippingRegion.origin.y,
clippingRegion.size.width,clippingRegion.size.height);
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
glScissor won做你想要的。这里是一些代码我写了一段时间的一部分。它使用光栅纹理绘制
a矩形,并使用模板缓冲区只有一个磁盘可见。
这里是我使用的链接:
http://www.swiftless.com/tutorials/opengl/basic_reflection.html
(let((cnt 0d0))
(defmethod display((w window))
;;复数z表示光栅常数的幅度和方向
;; r和psi对后焦平面中的不同点进行寻址
;; r = 0将导致z = w0。系统对齐以照亮
;对于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)
$ b ;; http://www.swiftless.com/tutorials/
;; opengl / basic_reflection.html
;;使用模板缓冲区将光栅切割出光栅
(color-mask:false:false:false:false)
(depth-mask:false)
(enable:stencil-
(stencil-func:always 1 #xffffff)
(stencil-op:replace:replace:replace)
(draw-disk 100d0(* .5d0 1920) .5d0 1080))
;;中心在相机549,365
;; lcos上的400像素=相机上的276像素(带binning 2)
(color-mask:true:true:true:true)
(depth-mask:false)
(stencil-func :equal 1 #xffffff)
(stencil-op:keep:keep:keep)
;;绘制光栅
(disable:depth-test)
(with-pushing-matrix
(translate(* .5 1920)(* .5 1080)0)
(*(相位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帧
(重新显示后))))
I'm trying to make a circle shape clipping on a CCSprite in cocos2d, after searching a lot, i tried the open gl glScissor method, i achieve an squared clipping over my sprite. But, i need to make it a circle, and it seems to be impossible to do with the glScissor.I kept trying and I found something about gl stencil, but I haven't found a how to on this and I'm not familiar with opengl.
Also I heard something about calling multiple times to the glScissor wo you can achieve a custom shape but i haven't found anything.
Thanks in advance, any answer is welcome.
this is my actual visit method:
-(void) visit
{
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(clippingRegion.origin.x , clippingRegion.origin.y ,
clippingRegion.size.width, clippingRegion.size.height);
[super visit];
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
}
glScissor won't do what you want. Here is part of some code I wrote a while ago. It drawsa rectangle with a grating texture and uses the stencil buffer to only have a disk visible.
Here is the link I used back then: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))))
这篇关于在cocos2d与opengl的圈形状剪报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!