質問

I'm trying to render a simple cube with normals. I'm using the following code to initialize the shaders.

void initShader(const char* vertexShaderPath, const char* fragmentShaderPath){
    cout<<"Initializing Shaders"<<endl<<"==="<<endl;
    cout<<"Vertex shader ";
    vs = loadShader(vertexShaderPath, GL_VERTEX_SHADER);
    cout<<"Fragment shader ";
    fs = loadShader(fragmentShaderPath, GL_FRAGMENT_SHADER);

    cout<<"Creating program: ";
    program = glCreateProgram();
    if(0==program) cout<< "Failed"<<endl; else cout<< "OK"<<endl;

    glAttachShader(program, vs);
    glAttachShader(program, fs);

    cout<<"Linking program: ";
    glLinkProgram(program); 
    GLint linkstatus = 0;
    glGetProgramiv(program, GL_LINK_STATUS, &linkstatus);
    if(GL_TRUE==linkstatus) cout<<"OK"<<endl; else cout<<"Failed" <<endl;

    cout<<endl<<endl<<"Memory status"<<endl<<"==="<<endl;

    mvpmat = glGetUniformLocation(program,"mvpMatrix");
    mvmat = glGetUniformLocation(program,"mvMatrix");
    normalmat = glGetUniformLocation(program,"normalMatrix");

    cout << "mvpMatrix: "<<mvpmat<<" mvMatrix: "<<mvmat<<" normalMatrix: " << normalmat<<endl;
}

The output of the last cout statement appears as follows (There is no error during compilation or linking).

mvpMatrix: 0 mvMatrix: -1 normalMatrix: 1

This is my vertex shader code

uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;
attribute vec3 vertex;
attribute vec3 normal;
varying vec3 outNormal;
varying vec3 outPos;

void main(){
    gl_Position = mvpMatrix*vec4(vertex,1.0);
    outPos = (mvMatrix*vec4(vertex,1.0)).xyz;
    outNormal = normalize(normalMatrix*normal);
}

I've used all mvpMatrix, mvMatrix and normalMatrix in the code. Thus i'm really confused why getUniformLocation() is returning mvMatrix = -1?

役に立ちましたか?

解決

If your fragment shader does not use the input of outPos it will be optimized away. Uniforms are defined post-link. So both fragment and vertex shader count.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top