我尝试按照本教程使用着色器删除纹理的背景颜色。 https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/discard.php
一切都正常运行,我直接从教程中复制着色器代码,但片段着色器会抛出几个错误。
sampler2D myTexture;
varying vec2 vTexCoord;
void main (void)
{
vec4 color = texture2D(myTexture, vTexCoord);
if (color.rgb == vec3(1.0,0.0,0.0))
discard;
gl_FragColor = color;
}
错误:0:3:' sampler2D' :采样器必须是均匀的
错误:0:7:' myTexture' :未声明的标识符
错误:0:7:' vTexCoord' :未声明的标识符
错误:0:7:' texture2D' :找不到匹配的重载函数(使用隐式转换)
错误:0:7:' =' :无法转换为' const float'浮动'
我用纹理和Vec2替换了未声明的标识符,但它们仍然未被识别。我猜他们必须以某种方式加载。
这可能就是扔掉" ...没有匹配重载..."错误。另外两个我不明白,代码直接来自教程,所以在理论上不应该存在这样的问题。
我的lwjgl版本是2.9.1
以下是相关来源:
shaderProgram = glCreateProgram();
vertexShader = glCreateShader(GL_VERTEX_SHADER);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
StringBuilder vertexShaderSource = new StringBuilder();
StringBuilder fragmentShaderSource = new StringBuilder();
try{
BufferedReader reader = new BufferedReader(new FileReader("src/shader.vert"));
String line;
line = reader.readLine();
while(line != null){
line = reader.readLine();
if(line != null){
vertexShaderSource.append(line).append('\n');
}
}
reader.close();
} catch (IOException e) {
System.err.println("Vertex no load.");
Display.destroy();
System.exit(1);
}
try{
BufferedReader reader = new BufferedReader(new FileReader("src/shader.frag"));
String line;
//line = reader.readLine();
while(true){
line = reader.readLine();
if(line == null){
break;
}
fragmentShaderSource.append(line).append('\n');
}
reader.close();
} catch (IOException e) {
System.err.println("Frag no load.");
Display.destroy();
System.exit(1);
}
System.out.println(fragmentShaderSource);
glShaderSource(vertexShader, vertexShaderSource);
glCompileShader(vertexShader);
if(glGetShader(vertexShader, GL_COMPILE_STATUS) == GL_FALSE){
System.err.println("Vertex no compile");
}
glShaderSource(fragmentShader, fragmentShaderSource);
glCompileShader(fragmentShader);
if(glGetShader(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE){
System.err.println("frag no compile");
}
System.out.println("Shader.frag Error Log\n" + glGetShaderInfoLog(fragmentShader, 1024));
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glValidateProgram(shaderProgram);
答案 0 :(得分:1)
Uniform variables in GLSL必须以uniform
关键字开头。所以要修复所有内容,用
uniform sampler2D myTexture;