好的,我创建了一个屏幕大小的三角形,它工作得非常好。现在我想添加一些着色器 有很酷的效果。下面是代码和着色器文件,显示了我的工作方式。但问题是它没有工作,另一个大问题是任何方法都没有抛出任何异常。 意味着它已经编译,附加和链接没有问题...或者可能是我在这里错了。 我使用的是openGL 4.2.0 - Build 10.18.10.3496
// inside the Game class constructor I called
shader.addFragmentShader(ResourceLoader.getShaderResource("basicFragment.fs.txt"));
shader.addVertexShader(ResourceLoader.getShaderResource("basicVertex.vs.txt"));
shader.getReadyShader();
public static String getShaderResource(String fileName){
shaderSource=new StringBuilder();
BufferedReader shaderReader=null;
try{
File readFile=new File("./Res/Shader/"+fileName);
shaderReader=new BufferedReader(new FileReader(readFile));
String line;
try {
while((line=shaderReader.readLine())!=null){
shaderSource.append(line).append("\n");
}
shaderReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
catch(FileNotFoundException exp){
exp.printStackTrace();
System.err.println("couldn't load the shader file");
}
return shaderSource.toString();
}
public class ShaderLoader {
private static int program;
public ShaderLoader(){
program=glCreateProgram();
if(program==0){
System.out.println("Shader creation failed: could not find the valid address.");
}
}
public void addVertexShader(String text){
addProgram(text, GL_VERTEX_SHADER);
}
public void addFragmentShader(String text){
addProgram(text, GL_VERTEX_SHADER);
}
public void getReadyShader(){
glLinkProgram(program);
if(glGetProgram(program, GL_LINK_STATUS)==0){
System.out.println(glGetShaderInfoLog(program,1024));
}
glValidateProgram(program);
if(glGetProgram(program, GL_VALIDATE_STATUS)==0){
System.out.println(glGetShaderInfoLog(program,1024));
}
}
private static void addProgram(String text ,int type){
int shader=glCreateShader(type);
if(shader==0){
System.err.println("Couldn't find the valid shader source");
}
glShaderSource(shader,text);
glCompileShader(shader);
if(glGetShader(shader,GL_COMPILE_STATUS)==0){
System.err.println(glGetShaderInfoLog(shader,1024));
}
glAttachShader(program,shader);
}
public void bind(){
glUseProgram(program);
}
}
vertexShader
#version 420 (tried with #version 330 but still no effect)
layout (location=0) in vec3 position;
void main(){
gl_Position= vec4(0.25*position, 1.0);
}
fragment shader
#version 420 (tried with #version 330 but still no effect)
out vec4 fragColor;
void main(){
fragColor = vec4(0.0,1.0,1.0,1.0);
}
Why I am not having any effect? please help..
答案 0 :(得分:0)
您正在混淆着色器类型:
public void addFragmentShader(String text){
addProgram(text, GL_VERTEX_SHADER);
}
private static void addProgram(String text ,int type){
int shader = glCreateShader(type);
...
对于片段着色器,glShaderSource()
的参数必须为GL_FRAGMENT_SHADER
,而代码则传递GL_VERTEX_SHADER
。
答案 1 :(得分:-1)
哦,我得到了我的错误,它的后续教程一直到凌晨5点都有效。
public void addVertexShader(String text){
addProgram(text, GL_VERTEX_SHADER);
}
public void addFragmentShader(String text){
addProgram(text, GL_VERTEX_SHADER);(here should be GL_FRAGMENT_SHADER as a second argument)
}
sorry for such stupid question and wasting your time.....