Learn GLSL or Cg?

I want to learn some filter effects like those:
https://docs.panda3d.org/1.10/python/programming/render-to-texture/generalized-image-filters

Yet, there are not many tutorials for Cg. I guess Cg is out of date?
Which one should I learn?

Thank you for the reading.

Learn GLSL. Cg is deprecated by NVIDIA.

1 Like

I try GLSL on my MacBook 2019 and the GLSL version is #version 330.
Here is my code:

from direct.showbase.ShowBase import ShowBase
from panda3d.core import loadPrcFileData
from panda3d.core import Shader

configVars = """
win-size 1280 720
show-frame-rate-meter 1
"""

loadPrcFileData("", configVars)


class MyGame(ShowBase):
    def __init__(self):
        super().__init__()
        self.set_background_color(0, 0, 0, 1)
        # self.wireframeOn()
        self.cam.setPos(0, -4, 0)

        my_shader = Shader.load(Shader.SL_GLSL,
                                vertex="shaders/gradient-vert.glsl",
                                fragment="shaders/gradient-frag.glsl")

        self.plane = self.loader.loadModel("my-models/plane")
        self.plane.reparentTo(self.render)
        # self.plane.set_shader_input("resolution", (1280, 720))
        self.plane.setShader(my_shader)


game = MyGame()
game.run()

This is gradient-vert.glsl:

#version 120

uniform vec2 resolution;

void main() {
    vec2 pos = gl_FragCoord.xy / resolution.xy;

    gl_FragColor = vec4(0.3, 0.5, pos.y, 1.0);
}

This is gradient-frag.glsl:

#version 120

// Vertex inputs
in vec4 p3d_Vertex;

// Uniform inputs
uniform mat4 p3d_ModelViewProjectionMatrix;

void main() {
    gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;
}

And it pops up those errors:

:display:gsg:glgsg(error): An error occurred while compiling GLSL vertex shader shaders/gradient-vert.glsl:
ERROR: shaders/gradient-vert.glsl:1: '' :  version '330' is not supported
:display:gsg:glgsg(error): An error occurred while compiling GLSL fragment shader shaders/gradient-frag.glsl:
ERROR: shaders/gradient-frag.glsl:1: '' :  version '330' is not supported

When I change to GLSL #version 120, it pops up those errors:

ERROR: shaders/gradient-vert.glsl:4: Invalid storage qualifiers 'in' in global variable context
ERROR: shaders/gradient-vert.glsl:10: Use of undeclared identifier 'p3d_Vertex'

And I found out this post:

I’m using macOS Monterey. I’m not sure mac Monterey can run version 330. Should I change to window os?

Thank you for reading this post.

You have to add gl-version 3 2 to your configVars to enable GLSL 3.30 support on macOS.

1 Like

Thank you.
I change the config and it works. :smiley:

configVars = """
win-size 1280 720
show-frame-rate-meter 1
gl-version 3 2
"""

gl-version 3 2 is GLSL 3.30, may I ask what is the meaning about the 3 and 2?
reference:Known Shader Bugs and Limitations — Panda3D Manual

It requests an OpenGL 3.2+ profile. From OpenGL 3.2 onward, the OpenGL API is split into a “core profile” and a “compatibility profile”, with the core profile dropping some features such as the fixed-function pipeline. Some drivers only support newer OpenGL features in the core profile, though, so requesting OpenGL 3.2 tells that driver that you don’t need compatibility features and therefore “unlocks” the features only available in the core profile.

The trade-off is that you no longer have access to compatibility features such as the fixed-function pipeline, so you need to implement things like lighting in your own shaders (or use a provided shader package like panda3d-simplepbr). The shader generator also no longer works in the core profile, although this will be fixed in Panda3D 1.11.

You can pass in other values than 3 2 to the variable, but it doesn’t have any meaningful effect to do so.

1 Like

Thank you for the reply. I just read this message.
I read the panda3d-simplepbr example. It needs to install gltf. May I ask what is gltf? I run the command pip install panda3d-gltf to install gltf, is it correct?

I try the auto shader and simplepbr to try about the lighting when I use gl-version 3 2 . But not working, Here is my code:

import simplepbr
from panda3d.core import loadPrcFileData

configVars = """
win-size 1280 720
fullscreen 0
show-frame-rate-meter 1
gl-version 3 2
"""

loadPrcFileData("", configVars)

from direct.showbase.ShowBase import ShowBase
from panda3d.core import PointLight, AmbientLight, NodePath
from math import sin, cos
from direct.filter.CommonFilters import CommonFilters


class LightsAndShadows(ShowBase):
    def __init__(self):
        super().__init__()
        self.set_background_color(0, 0, 0, 1)
        self.cam.setPos(0, -12, 0)

        # simplepbr
        # simplepbr.init()


        self.tree1 = self.loader.loadModel('jack')
        self.tree1.setPos(0, 0, -2.5)
        self.tree1.reparentTo(self.render)

        self.tree2 = self.loader.loadModel('jack')
        self.tree2.setPos(4, 5, 2.5)
        self.tree2.reparentTo(self.render)

        self.tree3 = self.loader.loadModel('jack')
        self.tree3.setPos(-4, 7, 2.5)
        self.tree3.reparentTo(self.render)

        # self.trees.reparentTo(self.render)

        self.floor = self.loader.loadModel('my-models/floor')
        self.floor.setPos(0, 0, -2.5)
        self.floor.reparentTo(self.render)

        self.light_model = self.loader.loadModel('models/misc/sphere')
        self.light_model.setScale(0.2, 0.2, 0.2)
        # self.light_model.setPos(4, -4, 0)
        self.light_model.reparentTo(self.render)

        plight = PointLight("plight")
        plight.setShadowCaster(True, 1024, 1024)
        self.render.setShaderAuto()
        plnp = self.light_model.attachNewNode(plight)
        # plight.setAttenuation((1, 0, 0)) # constant, linear, and quadratic.
        self.render.setLight(plnp)

        alight = AmbientLight("alight")
        alight.setColor((0.04, 0.04, 0.04, 1))
        alnp = self.render.attachNewNode(alight)
        self.render.setLight(alnp)

        self.floor.setLight(plnp)
        self.floor.setLight(alnp)

        filters = CommonFilters(self.win, self.cam)
        filters.setBloom(size="large")

         # Auto Shader
        # self.render.setShaderAuto()

        self.taskMgr.add(self.move_light, "move-light")

    def move_light(self, task):
        ft = globalClock.getFrameTime()

        self.light_model.setPos(cos(ft)*4, sin(ft)*4, 0)

        return task.cont


game = LightsAndShadows()
game.run()

I commented the self.render.setShaderAuto() and simplepbr.init(). If you want to use it you can uncomment the commands.

  1. self.render.setShaderAuto() not working in my program.
  2. simplepbr.init() have those error:

Traceback (most recent call last):
  File "/Users//gitdir/fyp/panda3d/other/test_lighting.py", line 81, in <module>
    game = LightsAndShadows()
  File "/Users//gitdir/fyp/panda3d/other/test_lighting.py", line 25, in __init__
    simplepbr.init()
  File "/Users//opt/anaconda3/envs/py310/lib/python3.10/site-packages/simplepbr/__init__.py", line 350, in init
    return Pipeline(**kwargs)
  File "/Users//opt/anaconda3/envs/py310/lib/python3.10/site-packages/simplepbr/__init__.py", line 128, in __init__
    self._recompile_pbr()
  File "/Users//opt/anaconda3/envs/py310/lib/python3.10/site-packages/simplepbr/__init__.py", line 214, in _recompile_pbr
    pbr_vert_str = _load_shader_str('simplepbr.vert', pbr_defines)
  File "/Users//opt/anaconda3/envs/py310/lib/python3.10/site-packages/simplepbr/__init__.py", line 48, in _load_shader_str
    with open(os.path.join(shader_dir, shaderpath)) as shaderfile:
FileNotFoundError: [Errno 2] No such file or directory: './shaders/simplepbr.vert'

If you don’t understand my explanation, please feel free to tell me. And thank you for reading this.

Please update to the latest version of simplepbr that was just released, I believe it should fix this issue.

setShaderAuto, in Panda3D 1.10 and below, does not work with gl-version 3 2.

1 Like

Thank you for the reply.
I reinstall the panda-simplepbr. However it has some problem. Is there anything I need to know when I use simplepbr?

The code I didn’t change, just add the simplepbr at the beginning:

class LightsAndShadows(ShowBase):
    def __init__(self):
        super().__init__()

        # simplepbr
        simplepbr.init()

The error:



:display:gsg:glgsg(error): An error occurred while compiling GLSL fragment shader created-shader:
ERROR: created-shader:12: Invalid call of undeclared identifier 'texture2D'
ERROR: created-shader:14: Use of undeclared identifier 'color'
ERROR: created-shader:15: Use of undeclared identifier 'color'
ERROR: created-shader:15: Use of undeclared identifier 'color'
ERROR: created-shader:16: Use of undeclared identifier 'color'
ERROR: created-shader:16: Use of undeclared identifier 'color'
ERROR: created-shader:16: Use of undeclared identifier 'color'
ERROR: created-shader:16: Use of undeclared identifier 'color'
ERROR: created-shader:16: Use of undeclared identifier 'color'
ERROR: created-shader:19: Use of undeclared identifier 'color'

I have been unable to reproduce the error, but I believe my drivers are not being as strict and letting some stuff through on a GLSL 330 Core profile. Would you mind testing the latest dev/git version of panda3d-simplepbr to see if it fixes the problem on your hadware/drivers? You can use the following to have pip install from a git repo:

python -m pip install -U git+https://github.com/Moguri/panda3d-simplepbr.git
1 Like

Thank you for the replay.
I’m using conda to create the Python 3.10 environment, not sure if it affects the panda-simplepbr. I use the command pip install panda3d-simplepbr to install and run in the pyCharm CE IDE. In python interpreter, it shows the panda-simplepbr is 0.10 version. I guess I am using the latest version. Anyway, I uninstalled the panda-simplepbr and use the command python -m pip install -U git+https://github.com/Moguri/panda3d-simplepbr.git. The results are different.

Here is the result image:

Thank you for reading this post.