[SOLVED] GLSL error

Hello All,

I have this error and can’t figure out what I am doing wrong. The code looks correct…

//vert shader
uniform mat4 g_WorldViewProjectionMatrix;
attribute vec3 inPosition;
attribute vec4 inTexCoord;
attribute vec4 inTexCoord1;
uniform mat3 g_NormalMatrix;
attribute vec3 inNormal;
varying vec3 lightDir;
varying vec3 halfVector;

void main()
{
gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
gl_TexCoord[0] = inTexCoord;
vec3 n = normalize(g_NormalMatrix * inNormal);
vec3 t = normalize(g_NormalMatrix * inTexCoord1.xyz);
vec3 b = cross(n, t) * inTexCoord1.w;
mat3 tbnMatrix = mat3(t.x, b.x, n.x, t.y, b.y, n.y, t.z, b.z, n.z);
lightDir = gl_LightSource[0].position.xyz;
lightDir = tbnMatrix * lightDir;
halfVector = gl_LightSource[0].halfVector.xyz;
halfVector = tbnMatrix * halfVector;
}
//frag shader
uniform sampler2D m_ColorMap;
uniform sampler2D m_NormalMap;
uniform sampler2D m_HeightMap;
uniform float m_Bias;
uniform bool m_EnableParallax;
uniform float m_Scale;
varying vec3 lightDir;
varying vec3 halfVector;

void main()
{
vec2 newTexCoord;
vec3 h = normalize(halfVector);
if (m_EnableParallax == true)
{
float height = texture2D(m_HeightMap, gl_TexCoord[0].st).r;
height = height * m_Scale + m_Bias;
newTexCoord = gl_TexCoord[0].st + (height * h.xy);
}
else
{
newTexCoord = gl_TexCoord[0].st;
}
vec3 n = normalize(texture2D(m_NormalMap, newTexCoord).rgb * 2.0 - 1.0);
vec3 l = normalize(lightDir);
float nDotL = max(0.0, dot(n, l));
float nDotH = max(0.0, dot(n, h));
float power = (nDotL == 0.0) ? 0.0 : pow(nDotH, gl_FrontMaterial.shininess);
vec4 ambient = gl_FrontLightProduct[0].ambient;
vec4 diffuse = gl_FrontLightProduct[0].diffuse * nDotL;
vec4 specular = gl_FrontLightProduct[0].specular * power;
vec4 color = gl_FrontLightModelProduct.sceneColor + ambient + diffuse + specular;
gl_FragColor = color * texture2D(m_ColorMap, newTexCoord);
}
//jMonkeyCode
 Box b = new Box(1, 1, 1);
        geom = new Geometry("Box", b);
        //Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
        //mat.setColor("Color", ColorRGBA.Blue);
        //geom.setMaterial(mat);
        TangentBinormalGenerator.generate(geom);
        Material bumpMat = new Material(assetManager, "MatDefs/ParallaxMapping.j3md");
        bumpMat.setTexture("HeightMap", assetManager.loadTexture("Textures/BrickHM.png"));
        bumpMat.setTexture("NormalMap", assetManager.loadTexture("Textures/BrickNM.png"));
        bumpMat.setTexture("ColorMap", assetManager.loadTexture("Textures/BrickDM.png"));
        bumpMat.setBoolean("EnableParallax", true);
        bumpMat.setFloat("Scale", 1.0f);
        bumpMat.setFloat("Bias", 1.0f);
        bumpMat.setVector3("LightDir", new Vector3f(1.0f,-1.0f,-2.0f).normalize());
        bumpMat.setVector3("HalfVector", new Vector3f(1.0f,-1.0f,-2.0f).normalize());
        geom.setMaterial(bumpMat);       
        rootNode.attachChild(geom);

https://hub.jmonkeyengine.org/t/how-to-type-code-blocks/31155/23

Also, the full console dump should have dumped the entire expanded shader so we could see what the real line 25 was.

Just from counting and knowing the JME will insert at least one line, I guess this:

vec3 l = normalize(lightDir);

And I don’t immediately see anything wrong with that.

…but we’d have to see the real output to be sure.

That is all I got for an error dump in that window.

//matdef

MaterialDef ParallaxMapping
{
    MaterialParameters
    {
        Texture2D ColorMap
        Texture2D NormalMap
        Texture2D HeightMap
        Boolean EnableParallax;
        Float Scale;
        Float Bias;
        Vector3 LightDir;
        Vector3 HalfVector;
    }

    Technique
    {
        VertexShader GLSL110: Shaders/ParallaxMapping.vert
        FragmentShader GLSL110: Shaders/ParallaxMapping.frag
    
        WorldParameters
        {
            WorldViewProjectionMatrix
        }

        Defines
        {
            colorMap : ColorMap
            normalMap : NormalMap
            heightMap : HeightMap
            scale : Scale
            bias : Bias
            lightDir : LightDir
            halfVector : HalfVector
        }
    }
}
//simple VS
void main()
{	
	// the following three lines provide the same result
//	gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
//	gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
        gl_TexCoord[0] = gl_MultiTexCoord0;
	gl_Position = ftransform();
}
//simple fs
uniform sampler2D ColorMap;
void main()
{
    vec4 color = texture2D(ColorMap, gl_TexCoord[0].st);
    gl_FragColor = color;
    //gl_FragColor = vec4(1.0,1.0,1.0,1.0);
}

I used this above code to just do a simple example and it compiles and runs but No cube shows up… no errors…

It would be great to find a example of Jmonkey with just a simple VS/FS and MaterialDef that works to look over. I can’t find any exmaples to download from the tutorial page that would help show the correct layout.

I don’t think there are any particuarily good resources for shader examples in the tutorials or wiki (hopefully someone will correct me if I’m wrong on this)

But you could find some basic shader examples with the frag, vert, and j3md files in this area of the engine’s source code in Common/MatDefs/Misc/

The Unshaded.j3md (and corresponding frag and vert files) are the most basic stock JME shader that JME games without dynamic lighting will use, so that would be a good one to look at. If you want something shorter or simpler then I think the one called ColoredTexture.j3md is also a good example / template.

And if you’d want to investigate something slightly more complicated that has lighting (since Unshaded has no lighting), then I would recommend looking at Lighting.j3md thats located in Common/MatDefs/Light.j3md

ie: not that window. But the actual command line console dump. If you are going to do any shader development at all then that is super critical.

That line is effectively nonsense in JME because the “fixed function pipeline” variables are not setup.

As yaRnMcDonuts suggests, the best way to start with JME shaders is to fork Unshaded.j3md and then break and fix it until you get what you want.

From there you will also get a handle on how to make materials that will still work with JME skin/bone animation, JME instancing, etc…

i might be wrong, but as i remember the console debug did print ALL shader merged code where you could find line 0:25 #132 where it is

i can just guess that its about

vec3 n = normalize(texture2D(m_NormalMap, newTexCoord).rgb * 2.0 - 1.0);
vec3 l = normalize(lightDir);

where at the end of first line you might have some special character or something. Based on pasted code, it looks fine, but maybe not in file.

There is also possibility that IDE had some function like compile-on-safe similar for shader and broken file.(while you see content proper)

In your place to be sure i would just remove this file, and create it new fresh

Did you try –
Looks possibly kind of old and I’m at work so I can’t mess w/ it but might be something

MaterialDef Solid Color {
    //This is the complete list of user defined uniforms to be used in the
    //shaders
    MaterialParameters {
        Vector4 Color
    }
    Technique {
        //This is where the vertex and fragment shader files are
        //specified
        VertexShader GLSL100:   Common/MatDefs/Misc/SolidColor.vert
        FragmentShader GLSL100: Common/MatDefs/Misc/SolidColor.frag
        //This is where you specify which global uniform you need for your
        //shaders
        WorldParameters {
            WorldViewProjectionMatrix
        }
    }
    Technique FixedFunc {
    }
}

example from wiki Shaders

Thanks all for helping. I need to clear up some confusion on my part. I get this LWJGL-OpenGL2 as a return value from settings.getRenderer().

GLSL is 1.10 with GL2.0

I would like to use the most current GL4.6 or at least 3.0

So writing these shaders in the newer GLSL syntax can I just drop and play with some standard GLSL code or is the attributes and uniforms in the VS / FS going to use some JME attribute names, that I will need to update the GLSL3 code to work? This chart below applies to the latest GLSL version too?

image

I assume moving to the newer GL3 or GL4 version all the rendering code will be updated and I don’t have to touch this correct? Meaning using VBO ect… no fixed function code, all the matrix math is done by JME and sent to the shaders?

I did get the shader to work eventually but was only for color, or a single texture, and it was on texture unit 0. How do you assign the texture unit 1,2, to take the textures you load in. I assumed when you do this

mat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/BrickDM.png"));
        mat.setTexture("NormalMap", assetManager.loadTexture("Textures/BrickNM.png"));
        mat.setTexture("ParallaxMap", assetManager.loadTexture("Textures/BrickHM.png"));

that JME would put these in their own texture units…

Anyone have a good tutorial with code to walk through on using the newer shaders and GL3/4?

Thanks!

i dont quite understand.

you put for example GLSL400 in Matdef technique, isnt this what you need?

JME m_ params are just passed to shader, and you can pass anything from Matdef and it will be m_something in shader. Not sure how gl_ things are passed and how related to glsl 1.2, but are you sure you cant use GLSL4 when passed GLSL400 into matdef?

im not shader specialist here, but used Texture Arrays and as i remember it required higher GLSL(3+?)

Also i would suggest change to

settings.setRenderer(AppSettings.LWJGL_OPENGL45);

maybe this is missing?

I’m also not sure why they are messing with texture units. That’s why I didn’t respond at first.

…things seem very strange.

Thanks oxplay2

I put the call to switch to GL4.5 and that works now. I will try messing with the shaders again to see what I come up with.

Thanks!

After looking through the source files in JME at looking at the files here is what I came up with for a simple texture mapped example for GL4.5 and GLSL4.5 if anyone needed to use something simple to work from…

java

	Box b = new Box(1, 1, 1);
        geom = new Geometry("Box", b);
        Material mat = new Material(assetManager, "MatDefs/SimpleTextureMapping.j3md");
        mat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Brick.png"));
        geom.setMaterial(mat);     
        geom.setLocalTranslation(0.0f, 3.0f, 0.0f);
        rootNode.attachChild(geom);

VertShader

#import "Common/ShaderLib/Instancing.glsllib"

varying vec2 texCoord;
attribute vec3 inPosition;
attribute vec2 inTexCoord;

void main()
{
    vec4 modelSpacePos = vec4(inPosition, 1.0);
    texCoord = inTexCoord;
    gl_Position = TransformWorldViewProjection(modelSpacePos);
}

FragShader

varying vec2 texCoord;
uniform sampler2D m_DiffuseMap;

void main(){
    vec2 newTexCoord;
    newTexCoord = texCoord;    
    vec4 diffuseColor = texture2D(m_DiffuseMap, newTexCoord);
    gl_FragColor = diffuseColor;
}

MaterialDef

MaterialDef SimpleTextureMapping {

    MaterialParameters {

        // Diffuse map
        Texture2D DiffuseMap
   }

    Technique {
        VertexShader GLSL450 : Shaders/SimpleTextureMapping.vert
        FragmentShader GLSL450 : Shaders/SimpleTextureMapping.frag

       WorldParameters {
            WorldViewProjectionMatrix
            NormalMatrix
            WorldViewMatrix
            ViewMatrix
            CameraPosition
            WorldMatrix
            ViewProjectionMatrix            
        }

        Defines {
        }
    }
}
2 Likes