问题描述
我试图渲染一个简单的多维数据集与法线。我使用下面的代码来初始化着色器。
void initShader(const char * vertexShaderPath,const char * fragmentShaderPath){
cout& <<< 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<<链接程序:;
glLinkProgram(program);
GLint linkstatus = 0;
glGetProgramiv(program,GL_LINK_STATUS,& linkstatus);
if(GL_TRUE == linkstatus)cout<<OK<< endl; else cout<<Failed<< endl;
cout
mvpmat = glGetUniformLocation(program,mvpMatrix);
mvmat = glGetUniformLocation(program,mvMatrix);
normalmat = glGetUniformLocation(program,normalMatrix);
cout<< mvpMatrix:<< mvpmat<<mvMatrix:<< mvmat<<normalMatrix:< normalmat<<<
}
最后一个cout语句的输出如下所示编译或链接)。
mvpMatrix:0 mvMatrix:-1 normalMatrix:1
这是我的顶点着色器代码
uniform mat4 mvpMatrix;
uniform mat4 mvMatrix;
uniform mat3 normalMatrix;
attribute vec3 vertex;
attribute vec3 normal;
changing 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);
}
我在代码中使用了所有mvpMatrix,mvMatrix和normalMatrix。因此,我真的很困惑为什么getUniformLocation()返回mvMatrix = -1?解决方案如果你的片段着色器不使用outPos的输入将被优化掉。制服定义为后链接。因此片元和顶点着色器计数。
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.
这篇关于glGetUniformLocation()返回-1即使在顶点着色器中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!