着色器编译状态始终为0,包括零错误。如果我故意破坏着色器,它会正确显示错误,甚至无法链接,但是当着色器(我相信)是正确且有效的着色器时,它的编译状态仍为零,(GL_FALSE)但程序正确链接。当在程序中使用编译着色器失败时,没有任何渲染。
用于检查着色器的代码:
private static void checkShader(int handle,String path) {
int status = glGetProgrami(handle,GL_COMPILE_STATUS);
boolean compiled = status==GL_TRUE;
if (compiled) return; // Shader compiled and everything is fine~
String error = glGetShaderInfoLog(handle);
System.err.println("==== SHADER FAILED TO COMPILE ====");
System.err.println(path);
System.err.println(error);
System.err.println("==== ======================== ====");
}
用于读取着色器的代码:
private static String getShaderData(String path) throws IOException {
InputStream inputStream = ShaderManager.class.getResourceAsStream(path);
if (inputStream == null) throw new BadShaderException("Could not find shader: "+path);
Scanner scanner = new Scanner(inputStream);
StringBuilder builder = new StringBuilder();
while (scanner.hasNextLine())
builder.append(scanner.nextLine()).append('\n');
scanner.close();
return builder.toString();
}
加载顶点着色器(片段和几何着色器相同,但容器类已更改)
public static VertexShader loadVertexShader(String path) {
String source;
try {
source = getShaderData(path);
} catch (Exception e) {
throw new BadShaderException("Failed to load data for shader: "+path);
}
int handle = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(handle,source);
glCompileShader(handle);
checkShader(handle,path);
return new VertexShader(handle,path);
}
着色器我正在尝试使用
#version 330 core
layout(location = 0) in vec3 vpm;
uniform mat4 depthMatrix;
void main(){
gl_Position = depthMatrix * vec4(vpm,1);
}
我在搜索“passthrough”着色器时发现了一个着色器
#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
// Output data ; will be interpolated for each fragment.
out vec2 UV;
void main(){
gl_Position = vec4(vertexPosition_modelspace,1);
UV = (vertexPosition_modelspace.xy+vec2(1,1))/2.0;
}
我正在使用lwjgl 3.1.4,
OpenGL版本是(4.6.0 NVIDIA 388.13)
OpenGL供应商是(NVIDIA公司)
OpenGL Renderer是(GeForce GTX 1060 6GB / PCIe / SSE2)
编辑: 我使用的是glGetProgrami而不是glGetShaderi
答案 0 :(得分:2)
在检查我的着色器是否正确编译时,我使用了glGetProgrami而不是glGetShaderi。我没有意识到后一种方法,并认为先前用于两者。