glGetUniformLocation 和 glUseProgram 给出错误操作无效 (1282)。任何人都可以帮助解决问题吗?
glGetUniformLocation and glUseProgram give the error invalid operation (1282). Can anyone help resolve the issue?
现在我正在构建一个 3d 游戏引擎,以便更轻松地编写 3d 游戏。在我得到一台新电脑之前,一切都运行良好。在旧的 windows vista PC 上它工作正常,现在我使用的是 windows 8 PC 并且用于为我的着色器获取统一位置的方法给出错误无效操作(1282)。
org.lwjgl.opengl.OpenGLException: Invalid operation (1282)
at org.lwjgl.opengl.Util.checkGLError(Util.java:59)
at org.lwjgl.opengl.GL20.glGetUniformLocation(GL20.java:664)
at lib.ogl.graphicEffects.shaders.shaderProgram.getUniformLocation(shaderProgram.java:50)
at lib.ogl.graphicEffects.shaders.StaticShader.getAllUniformLocations(StaticShader.java:45)
at lib.ogl.graphicEffects.shaders.shaderProgram.<init>(shaderProgram.java:44)
at lib.ogl.graphicEffects.shaders.StaticShader.<init>(StaticShader.java:32)
at lib.org.Engine.engine.run(engine.java:72)
at lib.org.Game.MainClass.main(MainClass.java:37)
我认为 link 着色器有问题,但我不知道如何测试或修复它。
代码如下所示:
public shaderProgram(String vertexFile, String fragmentFile){
vertexShaderID = loadShader(vertexFile, GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentFile, GL_FRAGMENT_SHADER);
programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
bindAttributes();
glLinkProgram(programID);
if(glGetProgrami(programID, GL_LINK_STATUS)==GL_FALSE){
Debug.Errorlog("failure in linking the shader program");
System.exit(-1);
}
glValidateProgram(programID);
if(glGetProgrami(programID, GL_VALIDATE_STATUS)==GL_FALSE){
Debug.Errorlog("failure in validating the shader program");
System.exit(-1);
}
getAllUniformLocations();
}
loadShader class 正确加载着色器。
当程序尝试 link 我将在加载着色器的代码中添加的着色器时出现问题,但它似乎不是问题所在:
*public static int loadShader(String file, int type){
StringBuilder shaderSource = new StringBuilder();
try{
BufferedReader reader = new BufferedReader (new FileReader(file));
String line;
while((line = reader.readLine())!= null){
shaderSource.append(line).append("\n");
}
reader.close();
}catch(Exception e){
Debug.log("could not load the shader file : " + file);
}
int shaderID = glCreateShader(type);
glShaderSource(shaderID, shaderSource);
glCompileShader(shaderID);
if(glGetShaderi(shaderID,GL_COMPILE_STATUS)==GL_FALSE){
glGetShaderInfoLog(shaderID,500);//TODO DEBUG.log?
Debug.Errorlog("Could not compile the shader");
System.exit(-1);
}
return shaderID;
}
getAllUniformLocations 方法如下所示:
protected void getAllUniformLocations() {
try{
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
location_projectionMatrix = super.getUniformLocation("projectionMatrix");
location_viewMatrix = super.getUniformLocation("viewMatrix");
location_shineDamper = super.getUniformLocation("shineDamper");
location_reflectivity = super.getUniformLocation("reflectivity");
location_skyColor = super.getUniformLocation("skyColor");
location_lightPosition = new int[MAX_LIGHTS];
location_lightcolor = new int[MAX_LIGHTS];
location_attenuation = new int [MAX_LIGHTS];
for(int x = 0; x < MAX_LIGHTS; x ++){
location_lightPosition[x] = super.getUniformLocation("lightPosition["+x+"]");
location_lightcolor[x] = super.getUniformLocation("lightColor["+x+"]");
location_attenuation[x] = super.getUniformLocation("attenuation["+x+"]");
}
}catch(Exception e){
e.printStackTrace();
}
}
每次它尝试加载第一个制服时,它都会捕获错误并打印我在上面显示的堆栈跟踪。
会不会是我的旧显卡忽略了问题?
原来真正的问题是编译正确后,着色器无法link到程序!
好吧,问题是我的愚蠢!
在发现着色器没有 link 正确后,我打印了信息日志,发现一个着色器中的一些变量与另一个着色器中的变量不匹配!
The fragment shader uses varying toCameraVector, but previous shader does not write to it.
Type mismatch: Type of toLightVector different between shaders.
很抱歉浪费了大家的时间,但你确实帮助我弄清楚了问题所在,为此,我感谢你,我终于可以解决这个问题了。
现在我正在构建一个 3d 游戏引擎,以便更轻松地编写 3d 游戏。在我得到一台新电脑之前,一切都运行良好。在旧的 windows vista PC 上它工作正常,现在我使用的是 windows 8 PC 并且用于为我的着色器获取统一位置的方法给出错误无效操作(1282)。
org.lwjgl.opengl.OpenGLException: Invalid operation (1282)
at org.lwjgl.opengl.Util.checkGLError(Util.java:59)
at org.lwjgl.opengl.GL20.glGetUniformLocation(GL20.java:664)
at lib.ogl.graphicEffects.shaders.shaderProgram.getUniformLocation(shaderProgram.java:50)
at lib.ogl.graphicEffects.shaders.StaticShader.getAllUniformLocations(StaticShader.java:45)
at lib.ogl.graphicEffects.shaders.shaderProgram.<init>(shaderProgram.java:44)
at lib.ogl.graphicEffects.shaders.StaticShader.<init>(StaticShader.java:32)
at lib.org.Engine.engine.run(engine.java:72)
at lib.org.Game.MainClass.main(MainClass.java:37)
我认为 link 着色器有问题,但我不知道如何测试或修复它。
代码如下所示:
public shaderProgram(String vertexFile, String fragmentFile){
vertexShaderID = loadShader(vertexFile, GL_VERTEX_SHADER);
fragmentShaderID = loadShader(fragmentFile, GL_FRAGMENT_SHADER);
programID = glCreateProgram();
glAttachShader(programID, vertexShaderID);
glAttachShader(programID, fragmentShaderID);
bindAttributes();
glLinkProgram(programID);
if(glGetProgrami(programID, GL_LINK_STATUS)==GL_FALSE){
Debug.Errorlog("failure in linking the shader program");
System.exit(-1);
}
glValidateProgram(programID);
if(glGetProgrami(programID, GL_VALIDATE_STATUS)==GL_FALSE){
Debug.Errorlog("failure in validating the shader program");
System.exit(-1);
}
getAllUniformLocations();
}
loadShader class 正确加载着色器。 当程序尝试 link 我将在加载着色器的代码中添加的着色器时出现问题,但它似乎不是问题所在:
*public static int loadShader(String file, int type){
StringBuilder shaderSource = new StringBuilder();
try{
BufferedReader reader = new BufferedReader (new FileReader(file));
String line;
while((line = reader.readLine())!= null){
shaderSource.append(line).append("\n");
}
reader.close();
}catch(Exception e){
Debug.log("could not load the shader file : " + file);
}
int shaderID = glCreateShader(type);
glShaderSource(shaderID, shaderSource);
glCompileShader(shaderID);
if(glGetShaderi(shaderID,GL_COMPILE_STATUS)==GL_FALSE){
glGetShaderInfoLog(shaderID,500);//TODO DEBUG.log?
Debug.Errorlog("Could not compile the shader");
System.exit(-1);
}
return shaderID;
}
getAllUniformLocations 方法如下所示:
protected void getAllUniformLocations() {
try{
location_transformationMatrix = super.getUniformLocation("transformationMatrix");
location_projectionMatrix = super.getUniformLocation("projectionMatrix");
location_viewMatrix = super.getUniformLocation("viewMatrix");
location_shineDamper = super.getUniformLocation("shineDamper");
location_reflectivity = super.getUniformLocation("reflectivity");
location_skyColor = super.getUniformLocation("skyColor");
location_lightPosition = new int[MAX_LIGHTS];
location_lightcolor = new int[MAX_LIGHTS];
location_attenuation = new int [MAX_LIGHTS];
for(int x = 0; x < MAX_LIGHTS; x ++){
location_lightPosition[x] = super.getUniformLocation("lightPosition["+x+"]");
location_lightcolor[x] = super.getUniformLocation("lightColor["+x+"]");
location_attenuation[x] = super.getUniformLocation("attenuation["+x+"]");
}
}catch(Exception e){
e.printStackTrace();
}
}
每次它尝试加载第一个制服时,它都会捕获错误并打印我在上面显示的堆栈跟踪。
会不会是我的旧显卡忽略了问题?
原来真正的问题是编译正确后,着色器无法link到程序!
好吧,问题是我的愚蠢! 在发现着色器没有 link 正确后,我打印了信息日志,发现一个着色器中的一些变量与另一个着色器中的变量不匹配!
The fragment shader uses varying toCameraVector, but previous shader does not write to it.
Type mismatch: Type of toLightVector different between shaders.
很抱歉浪费了大家的时间,但你确实帮助我弄清楚了问题所在,为此,我感谢你,我终于可以解决这个问题了。