0

I'm currently trying to build a shader for a FogOfWar with pyglet and a glsl shader. I've now tried several things and looked at courses, but I just can't get it done. Can someone here tell me what the problem is? Here is my code:

# importing 
pyglet module import pyglet as py
# Subimports 
import pyglet 
from pyglet.graphics.shader import Shader, ShaderProgram 
from pyglet.gl import GL_TRIANGLES, glBlendFunc, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, glEnable, GL_BLEND 
from pyglet.math import Mat4, Vec3

# Create Vertex Shader 
vertex_source = """
#version 330 core layout(location = 0) in vec2 vertices;

// Import for transformation and translation
uniform mat4 vp; 
uniform mat4 model;

varying vec3 v_position;

void main(){
    v_position = vec3(vertices, 0.5);
    gl_Position = vp * model *  vec4(vec3(vertices, 1.0), 1.0); } 
"""

fragment_source = """
#version 330 core

#define PI2 6.28318530718
#define THICCNESS 30.0

varying vec3 v_position; 
out vec4 outputColor; 
vec4 outColorBuffline;

//Function for polygon 
float polygon(vec2 pt, vec2 center, float radius, int sides, float rotate, float edge_thickness){
pt -= center;

  // Angle and radius from the current pixel   
  float theta = atan(pt.y, pt.x) + rotate;
  float rad = PI2/float(sides);

  // Shaping function that modulate the distance   
  float d = cos(floor(theta/rad + 0.5)*rad-theta)*length(pt);
  return 1.0 - smoothstep(radius, radius + edge_thickness, d); 
  }

// Main Func from Frag Shader void main() {
    // Declare var for later output of color
    vec4 outColor = vec4(0.1);

    // Middle of screen
    vec2 new_pos = vec2(v_position.x, v_position.y);

    // Polygon variables and function call
    vec2 pt = new_pos;
    vec2 center = vec2(0,0);
    float radius = 300.0;
    outColor += polygon(pt, center, radius, 6, 0.0, 1.0) * vec4(1.0,1.0,1.0,0.5);

    // ** Testing for first line discard
    vec2 p1 = vec2(100.0, 100.0);
    vec2 p2 = vec2(-100.0, -100.0);
    vec2 p3 = new_pos.xy;

    vec2 p12 = p2 - p1;
    vec2 p13 = p3 - p1;

    float d = dot(p12, p13) / length(p12);
    vec2 p4 = p1 + normalize(p12) * d;
    if (length(p4 - p3) < THICCNESS
          && length(p4 - p1) <= length(p12)
          && length(p4 - p2) <= length(p12)) {
        discard;
    }
    // ** End of Line Discard

    // Output of Color
    outputColor = outColor; } """

# End of Shader glsl Programm
# Get Screens 
display = py.display.get_display() 
screens = display.get_screens()

# Initialize Shader Programm 
vert_shader = Shader(vertex_source, 'vertex') 
frag_shader = Shader(fragment_source, 'fragment') 
programm = ShaderProgram(vert_shader, frag_shader)

# Translation 
view_mat = Mat4.from_translation(Vec3(x=0, y=0, z=-1)) 
proj_mat = Mat4.orthogonal_projection(left=0, right=screens[1].width, bottom=0, top=screens[1].height, z_near=0.1, z_far=100) 
vp = proj_mat @ view_mat

# Transformation 
translate_mat = Mat4.from_translation(vector=Vec3(x=screens[1].width // 2, y=screens[1].height // 2, z=0)) 
rotate_mat = Mat4.from_rotation(angle=0, vector=Vec3(x=0, y=0, z=1)) scale_mat = Mat4.from_scale(vector=Vec3(x=1, y=1, z=0)) 
model_mat = translate_mat @ rotate_mat @ scale_mat

# creating a window 
window = py.window.Window(fullscreen=True, screen=screens[1], caption="MapViewer")

# Batches of graphis 
batch = py.graphics.Batch() 
batchtwo = py.graphics.Batch()

# Test rect for the background 
test_rect = py.shapes.Rectangle(x=500, y=500, width=500, height=500, color=(100, 100, 100, 100), batch=batchtwo)

# Activating the blending 
glEnable(GL_BLEND) 
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

# Coords: Left down, right down, right up, left up 
vertices = [coord * 500 for coord in (-0.5, -0.5, 0.5, -0.5, 0.5, 0.5, -0.5, 0.5)]

# Creating vertex for shader programm 
programm.vertex_list_indexed(4, GL_TRIANGLES,
                    indices=(0, 1, 2, 2, 3, 0),
                    batch=batch,
                    vertices=('f', vertices))

# loading variables in the shader 
programm['vp'] = vp 
programm['model'] = model_mat

# drawing label 
@window.event 
def on_draw():
    window.clear()
    batchtwo.draw()
    batch.draw()

# start running the application 
py.app.run()

Output so far

I have attached the result as a picture so far. Actually, the white one should have a transparency, right?

1 Answer 1

-1

I found the answer to the problem:

Pyglet.shapes and the shader.programm are using different shader pipelines.

This results in not foreseeable results.

Sign up to request clarification or add additional context in comments.

1 Comment

But how do you solve it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.