How do I know if a Shader is working?

Asked

Viewed 74 times

2

Is my code right? I’m using Python and Pyglet, as I know if Shader is running, why did I get Shader’s code on the internet:

import pyglet
from pyglet.gl import *
import pyshaders as ps

t = pyglet.window.Window()

v = """
#version 330 core
layout (location = 0) in vec3 aPos;

out vec4 vertexColor;

void main()
{
    gl_Position = vec4(aPos, 1.0);
    vertexColor = vec4(0.5, 0.0, 0.0, 1.0);
}
"""

f = """
#version 330 core
out vec4 FragColor;

in vec4 vertexColor;

void main()
{
    FragColor = vertexColor;
}
"""

shader = ps.from_string(v,f)

@t.event
def on_draw():
    glClearColor(1,0,0,1)
    glClear(GL_COLOR_BUFFER_BIT)
    shader.use()
    glBegin(GL_POLYGON)
    glColor3f(1,1,1)
    glVertex2f(10,10)
    glVertex2f(10,50)
    glVertex2f(60,70)
    glEnd()

def SRO(dt):
    on_draw()

pyglet.clock.schedule_interval(SRO, 1/60)

pyglet.app.run()

And yes I know Opengl but I am a beginner in GLSL, I have tested other codes of Shader on the internet but they always give the same thing.

2 answers

2


Coordinates are too high. After testing a triangle of the defined color appears.

shader = ps.from_string(v,f)
shader.use()

@t.event
def on_draw():
    glClearColor(0.5,0.5,0.5,1)
    glClear(GL_COLOR_BUFFER_BIT)
    glBegin(GL_POLYGON)
    glVertex2f(0.0, 0.95)
    glVertex2f(0.95, -0.95)
    glVertex2f(-0.95, -0.95)
    glEnd()
  • Why was there this change of dimensions that I could draw? Before Shader they worked

2

You can use GLSL linter that does syntax checking of your shader files, there are extensions for it in some text editors (I use in vscode)in your case you need to extract the Shader string in a new file and use the lib from python files to read the shaders files, apparently your shaders are correct.

Opengl works with a business called "Normalized Device Coordinates", in short, you need to pass the coordinates with values between -1.0 in each of the 3 axes. To inform a coordinate of a custom world you must use the MVP matrices (model, view, projection).

Normalized Device Coordinates https://learnopengl.com/Getting-started/Hello-Triangle

Coordinate Systems https://learnopengl.com/Getting-started/Coordinate-Systems

Browser other questions tagged

You are not signed in. Login or sign up in order to post.