Learning shaders presents a new problem

frag

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 g_Resolution;
uniform float g_Time;

void main() {
	vec2 st = gl_FragCoord.xy/g_Resolution;
	gl_FragColor = vec4(st.x,st.y,0.0,1.0);
}

vec2 st = gl_FragCoord.xy/g_Resolution;
I guess my use of screen coordinates caused the box to display different colors in different views.
How do I pass UV coordinates to “st”?

Modify:
The above problems were solved but I was confused by the code that solved them

//the global uniform World view projection matrix
//(more on global uniforms below)
uniform mat4 g_WorldViewProjectionMatrix;
//The attribute inPosition is the Object space position of the vertex
attribute vec3 inPosition;

attribute vec2 inTexCoord;

varying vec2 texCoord1;
void main(){
    
    texCoord1=inTexCoord;
    //Transformation of the object space coordinate to projection space
    //coordinates.
    //- gl_Position is the standard GLSL variable holding projection space
    //position. It must be filled in the vertex shader
    //- To convert position we multiply the worldViewProjectionMatrix by
    //by the position vector.
    //The multiplication must be done in this order.
    
    gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1);
}

This is the vertex shader
attribute vec2 inTexCoord;
This definition is known from literature that model UV coordinates are transferred by inTexCoord Assign inTexCoord to the texCoord1 fragment shader using texCoord1 coordinates.
I don’t know if I got that right.

I was also confused about the role of attribute and varying and didn’t understand what they did. I only found some code from Google that didn’t explain the role of these two keywords
Where can I find information on how JME passes vertex parameters to shaders? Such as passing model UV coordinates

“attribute” comes from the mesh data. VertexBuffer.Type in jme’s case.

“varying” is a way to pass values from the .vert to the .frag. And the GPU will interpolate them across the triangle.

So if vertex 1 has inTexCoord(0, 0) and vertex 2 has inTexCoord(1, 0) and there is a line of 5 pixels (fragmenets) between them then the .frag shader might see (0, 0), (0.2, 0), (0.4, 0), etc… (Only non-linear because of perspective.)

3 Likes

Doesn’t IN and OUT replace Attribute and Varying?

Yes, in newer shader versions.

1 Like

I seem to have seen this warning

I think you have to use IN and OUT in glsl 1.3 and above

1 Like

Thank you again for reminding .
IN instead of Attribute and OUT instead of Varying?

Yes

To be fair, attribute/varying syntax is so out of date at this point compared to in/out that while with GLSLCompat.glsllib it still somewhat works, we really shouldn’t be pushing it as the default option to use on the wiki in my opinion. GLSL 1.3 released more than 12 years ago as of today.

2 Likes