Fragment Shader's errors said undeclared, but I did

I’m trying to use Shader.

shader.frag

#version 330
#define PI (3.14159265358979323846)
#define TWO_PI (2*PI)

uniform int ksectors = 10;
uniform float kangleRad = 0.0;
uniform vec2 kcenter = vec2(1024.0, 768.0)/2.0;
uniform vec2 screenCenter = vec2(1024.0, 768.0)/2.0;
uniform sampler2DRect inputTexture;

void main() {
    vec2 texCoord = gl_TexCoord[0].xy;
    vec2 v = texCoord - screenCenter;
    float r = length(v);
    float a = atan(v.y, v.x);
    
    float A = TWO_PI / float(ksectors);
    a = mod(a, A);
    if(a > A/2.0) a = A-a;
    a -= kangleRad;
    
    vec2 u = vec2( cos(a), sin(a) ) * r;
    u += kcenter;
    gl_FragColor = texture2DRect( inputTexture, u);
}

shader.vert

#version 330
void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
    gl_FrontColor = gl_Color;
}

ofApp.cpp

void ofApp::setup(){
    //fbo.allocate(ofGetWidth(), ofGetHeight(), GL_RGB);
    shader.load("Kaleido/shader.vert","Kaleido/shader.frag");
    gui.setup("parameters", "settings.xml");
    gui.add( kenabled.setup("kenabled", true) );
    gui.add( ksectors.setup("ksectors", 10, 1, 100) );
    gui.add( kangle.setup("kangle", 0, -180, 180) );
    gui.add( kx.setup("kx", 0.5, 0, 1) );
    gui.add( ky.setup("ky", 0.5, 0, 1) );
}

//--------------------------------------------------------------
void ofApp::draw(){
    if(kenabled) {
        shader.begin();
        shader.setUniform1i( "ksectors", ksectors);
        shader.setUniform1f( "kangle", ofDegToRad(kangle));
        shader.setUniform2f( "kcenter", kx*ofGetWidth(), ky*ofGetHeight() );
        shader.setUniform2f( "screenCenter", 0.5*ofGetWidth(), 0.5*ofGetHeight() );
        shader.end();
    }
    gui.draw();
    
}

Then, I got errors

[ error ] ofShader: setupShaderFromSource(): GL_VERTEX_SHADER shader failed to compile
[ error ] ofShader: GL_VERTEX_SHADER shader reports:
ERROR: 0:3: Use of undeclared identifier 'gl_ModelViewProjectionMatrix'
ERROR: 0:3: Use of undeclared identifier 'gl_Vertex'
ERROR: 0:4: Use of undeclared identifier 'gl_TexCoord'
ERROR: 0:4: Use of undeclared identifier 'gl_MultiTexCoord0'
ERROR: 0:5: Use of undeclared identifier 'gl_FrontColor'
ERROR: 0:5: Use of undeclared identifier 'gl_Color'
[ error ] ofShader: GL_VERTEX_SHADER, offending line 3 :
	    1	#version 330
	    2	void main() {
	    3	    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
	    4	    gl_TexCoord[0] = gl_MultiTexCoord0;
	    5	    gl_FrontColor = gl_Color;

[ error ] ofShader: setupShaderFromSource(): GL_FRAGMENT_SHADER shader failed to compile
[ error ] ofShader: GL_FRAGMENT_SHADER shader reports:
ERROR: 0:12: Use of undeclared identifier 'gl_TexCoord'
ERROR: 0:13: Use of undeclared identifier 'texCoord'
ERROR: 0:14: Use of undeclared identifier 'v'
ERROR: 0:15: Use of undeclared identifier 'v'
ERROR: 0:15: Use of undeclared identifier 'v'
ERROR: 0:18: Use of undeclared identifier 'a'
ERROR: 0:18: Use of undeclared identifier 'a'
ERROR: 0:19: Use of undeclared identifier 'a'
ERROR: 0:19: Use of undeclared identifier 'a'
ERROR: 0:19: Use of undeclared identifier 'a'
ERROR: 0:20: Use of undeclared identifier 'a'
ERROR: 0:22: Use of undeclared identifier 'a'
ERROR: 0:22: Use of undeclared identifier 'a'
ERROR: 0:22: Use of undeclared identifier 'r'
ERROR: 0:23: Use of undeclared identifier 'u'
ERROR: 0:24: Use of undeclared identifier 'gl_FragColor'
ERROR: 0:24: Use of undeclared identifier 'u'

[ error ] ofShader: GL_FRAGMENT_SHADER, offending line 12 :
	   10	
	   11	void main() {
	   12	    vec2 texCoord = gl_TexCoord[0].xy;
	   13	    vec2 v = texCoord - screenCenter;
	   14	    float r = length(v);

[ error ] ofShader: checkProgramLinkStatus(): program failed to link
[ error ] ofShader: ofShader: program reports:
ERROR: One or more attached shaders not successfully compiled

I googled about errors, but I couldn’t find out the way to modify those. Could anyone give me clues to fix the problems?

Thank you so much.

Have you set the OpenGL version in main.cpp?
Maybe you can try using OpenGL 3,2 (#version 150) and check if it works

int main( ){
    ofGLFWWindowSettings settings;
    settings.setGLVersion(3, 3); //OpenGL 3,3 #version 330
    //settings.setGLVersion(3, 2); //OpenGL 3,2 #version 150
    settings.width = 1024;
    settings.height = 768;
    ofCreateWindow(settings);
    ofRunApp(new ofApp());
}

ERROR: 0:3: Use of undeclared identifier 'gl_ModelViewProjectionMatrix'

It doesn’t recognize those gl_ uniforms. See this example for non “gl_” uniforms https://github.com/openframeworks/openFrameworks/blob/master/libs/openFrameworks/gl/shaders/phong.vert

As @guardabrazo wrote, you could use an older GL version that understands gl_ModelViewProjectionMatrix, or otherwise update the uniforms in the shader code.

And you can’t use built-in variables (gl_ModelViewProjectionMatrix, gl_Vertex, gl_TexCoord etc…) on a programmable renderer. (highter 3.2)
Basically, We have to declare these ourselve, but oF pushes some variables to shader internally.

See this, a lot of info about oF’s shader is here.
http://openframeworks.cc/ofBook/chapters/shaders.html

it seems Kaleidoscope Effect. Here is an simple example of postprocessing. It will help you.

Cheers!

Hi guardabrazo,
Thank you for your comment.
Yes, I set OpenGL version in main.cpp that is the same as you wrote.

Thank you so much!

Thank you very much. I’ll check the link you put.

What you can do is this:

shader.vert

#version 330

// these come from the programmable pipeline
uniform mat4 modelViewProjectionMatrix;

in vec4 position;
in vec2 texcoord;

// texture coordinates are sent to fragment shader
out vec2 texCoordVarying;

void main()
{
    texCoordVarying = texcoord;
    gl_Position = modelViewProjectionMatrix * position;
}

shader.frag

#version 330
#extension GL_ARB_texture_rectangle: enable

#define PI (3.14159265358979323846)
#define TWO_PI (2*PI)

uniform float ksectors;
uniform float kangleRad;
uniform vec2 kcenter;
uniform sampler2DRect inputTexture;


in vec2 texCoordVarying;

out vec4 outputColor;

void main(){
	vec2 texCoord = texCoordVarying.xy;
	vec2 v = texCoord - kcenter;

	float r = length( v );
	float a = atan( v.y, v.x );

	float A = TWO_PI / ksectors;
	a = mod( a, A );
	
	if ( a > A/2.0 ) a = A - a;
	a -= kangleRad;

	vec2 u = vec2( cos(a), sin(a) ) * r;
	u += kcenter;

	outputColor = texture2DRect( inputTexture, u );
}

outputColor请问一下这个变量这样操作实现预览