我是shaders的新手,我得到了一些正确编译的GLSL代码(没有任何编译器错误),并且由于某种原因我不断得到黑屏。我正在使用LWJGL。当我尝试渲染三角形时,我不断得到一个黑色屏幕,即使它应该是白色的,根据我传递给片段着色器的颜色。我已经发布了以下代码的片段,希望能够找出问题所在。
void main()
{
gl_FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
{
void main()
{
gl_Position = ftransform();
}
vertShaderString = shaderName + ".vert";
fragShaderString = shaderName + ".frag";
shader = GL20.glCreateProgram();
vertShader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
try{
String temp;
BufferedReader reader = new BufferedReader(new FileReader(new File(vertShaderString)));
while ((temp = reader.readLine()) != null){
vertSource.append(temp).append("\n");
}
reader.close();
BufferedReader fragReader = new BufferedReader(new FileReader(new File(fragShaderString)));
String otherTemp;
while ((otherTemp = fragReader.readLine()) != null){
fragSource.append(otherTemp).append("\n");
}
fragReader.close();
}catch (Exception e){
e.printStackTrace();
}
GL20.glShaderSource(vertShader, vertSource);
GL20.glCompileShader(vertShader);
if (GL20.glGetShaderi(vertShader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE){
System.err.println("Failed to compile vertex shader");
}
GL20.glShaderSource(fragShader, fragSource);
GL20.glCompileShader(fragShader);
if (GL20.glGetShaderi(fragShader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE){
System.err.println("Failed to compile fragment shader");
}
GL20.glAttachShader(shader, vertShader);
GL20.glAttachShader(shader, fragShader);
GL20.glLinkProgram(shader);
GL20.glValidateProgram(shader);
public void begin(){
GL20.glUseProgram(shader);
}
public void end(){
GL20.glUseProgram(0);
}
public void render(){
GL11.glClearColor(0, 0, 0, 1);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glColor4f(1, 0, 0, 1);
shader.begin();
GL11.glBegin(GL11.GL_TRIANGLES);
GL11.glVertex2i(0, 0);
GL11.glVertex2i(500, 0);
GL11.glVertex2i(250, 250);
GL11.glEnd();
shader.end();
}
答案 0 :(得分:0)
由于jozxyqk,问题已经解决了。我忘了初始化片段着色器变量。
fragShader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
通过添加这行代码,问题就解决了。