Hello,

I'm trying to use a shader to have some "masking effects". So basically I 
have one texture which is my image and another texture which has its 
alpha-channel set to show the shape I want.

I successfully made the shader. The only problem I have is that I have a 
ColorLayer underneath (like in test_shader_example.py) but it does not 
show. If I comment out the second texture from the code and the shader (in 
the code shown as "Try to comment out this"), I can see the ColorLayer. If 
any OpenGL expert can let me know what I'm doing wrong, this would be 
highly appreciated. :)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cocos
from cocos.director import director
from cocos import shader

import pyglet
from pyglet.gl import *

class TestLayer(cocos.layer.Layer):
    def __init__(self):
        super(TestLayer, self).__init__()
        self.texture = pyglet.image.load("image2.png").get_texture()
        self.mask = pyglet.image.load("mask.png").get_texture()
        vertex_code = '''
        void main()
        {
            gl_TexCoord[0] = gl_MultiTexCoord0;
            gl_Position = ftransform();
        }
        '''
        frag_code = '''
        uniform sampler2D texture;
        uniform sampler2D mask;

        void main()
        {
            vec4 tex = texture2D(texture, gl_TexCoord[0].xy);
            vec4 masktexture = texture2D(mask, gl_TexCoord[0].xy); // Try 
to comment out this
            tex.a *= masktexture.a; // Try to comment out this
            gl_FragColor = tex;
        }
        '''
        self.test_shader = shader.ShaderProgram.simple_program('mask', 
vertex_code, frag_code)

    def draw(self):
        x,y = director.get_window_size()
        x = x//2
        y = y//2
        d = 100
        glEnable(GL_TEXTURE_2D)
        self.test_shader.install()
        self.test_shader.usetTex('texture', 0, GL_TEXTURE_2D, 
self.texture.id)
        self.test_shader.usetTex('mask', 1, GL_TEXTURE_2D, self.mask.id) # 
Try to comment out this

        glPushMatrix()
        self.transform()
        glBegin(GL_TRIANGLES)

        # up-right triangle
        glColor4ub(255,255,255,255)
        glTexCoord2f(1.0, 1.0)
        glVertex2f(x+d, y+d)

        glColor4ub(255,255,255,255)
        glTexCoord2f(0.0, 1.0)
        glVertex2f(x-d, y+d)

        glColor4ub(255,255,255,255)
        glTexCoord2f(1.0, 0.0)
        glVertex2f(x+d, y-d)

        # down-left triangle
        glColor4ub(255,255,255,255)
        glTexCoord2f(0.0, 1.0)
        glVertex2f(x-d, y+d)

        glColor4ub(255,255,255,255)
        glTexCoord2f(0.0, 0.0)
        glVertex2f(x-d, y-d)

        glColor4ub(255,255,255,255)
        glTexCoord2f(1.0, 0.0)
        glVertex2f(x+d, y-d)

        glEnd()
        glPopMatrix()

        self.test_shader.uninstall()
        glDisable(GL_TEXTURE_2D)

def main():
    director.init()
    
    test_layer = TestLayer ()
    main_scene = cocos.scene.Scene(cocos.layer.ColorLayer(255, 214, 173, 
255),
                                   test_layer)
    director.run (main_scene)

if __name__ == '__main__':
    main()


Daniel.

-- 
You received this message because you are subscribed to the Google Groups 
"cocos2d discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/cocos-discuss.
For more options, visit https://groups.google.com/d/optout.

Reply via email to