Java Opengl:丢弃带有着色器的纹理背景 [英] Java Opengl: Discarding Texture Background with Shaders
问题描述
我正在尝试按照本教程操作,以使用着色器删除纹理的背景色. https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders /discard.php
I'm trying to follow this tutorial to remove the background color of texture with a shader. https://www.opengl.org/sdk/docs/tutorials/ClockworkCoders/discard.php
一切运行正常,我直接从教程中复制了着色器代码,但是片段着色器引发了多个错误.
Everything is running correctly, I copied the shader code directly from the tutorial, however the fragment shader is throwing several errors.
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:"=":无法从常量浮点数"转换为浮点数的4分量矢量"
ERROR: 0:3: 'sampler2D' : samplers must be uniform
ERROR: 0:7: 'myTexture' : undeclared identifier
ERROR: 0:7: 'vTexCoord' : undeclared identifier
ERROR: 0:7: 'texture2D' : no matching overloaded function found (using implicit conversion)
ERROR: 0:7: '=' : cannot convert from 'const float' to '4-component vector of float'
我用纹理和Vec2替换了未声明的标识符,但是它们仍然未被识别.我猜他们必须以某种方式加载.
I replaced the undeclared identifiers with my texture and Vec2, but they remain unidentified. I'm guessing they have to be loaded in somehow.
这可能是引发"...没有匹配的过载..."错误的原因.我不理解的另外两个,代码直接来自教程,因此从理论上讲应该不会出现此类问题.
This is probably what is then throwing the "...no matching overloaded..." error. The other two I don't understand, the code is directly from the tutorial so there shouldn't be such problems, in theory.
我的lwjgl版本是2.9.1
My lwjgl version is 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);
推荐答案
GLSL中的统一变量必须以uniform
关键字开头.因此,要修复所有问题,请将第一行替换为
Uniform variables in GLSL have to start with the uniform
keyword. So to fix everything, replace the first line with
uniform sampler2D myTexture;
这篇关于Java Opengl:丢弃带有着色器的纹理背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!