2013-10-18 97 views
2

因此,我正在關注「theBennyBox」的Youtube系列關於3D遊戲引擎,並且在第9/10集中有些事情對我來說有些不對勁。openGL GLSL頂點着色器上的「void」解析錯誤

我得到的錯誤

Vertex shader failed to compile with the following errors: ERROR: 0:5: error(#132) Syntax error: "void" parse error ERROR: error(#273) 1 compilation errors. No code generated

這裏是我所有的東西:

我的資源加載器(讀取文件)

package com.base.engine; 

import java.io.BufferedReader; 
import java.io.FileReader; 

public class ResourceLoader { 
    public static String loadShader(String fileName) { 
     StringBuilder shaderSource = new StringBuilder(); 
     BufferedReader shaderReader = null; 

     try { 
      shaderReader = new BufferedReader(new FileReader("./res/shaders/" + fileName)); 
      String line; 
      while ((line = shaderReader.readLine()) != null) { 
       shaderSource.append(line).append("\n"); 
      } 

      shaderReader.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      System.exit(1); 
     } 

     return shaderSource.toString(); 
    } 
} 

我Shader類:

package com.base.engine; 

import static org.lwjgl.opengl.GL20.*; 
import static org.lwjgl.opengl.GL32.*; 

public class Shader { 
    private int program; 

    public Shader() { 
     program = glCreateProgram(); 

     if (program == 0) { 
      System.err.println("Shader Creation failed: could not find valid memory location in constructor"); 
      System.exit(1); 
     } 
    } 

    public void bind(){ 
     glUseProgram(program); 
    } 

    public void addVertexShader(String text) { 
     addProgram(text, GL_VERTEX_SHADER); 
    } 

    public void addGeometryShader(String text) { 
     addProgram(text, GL_GEOMETRY_SHADER); 
    } 

    public void addFragmentShader(String text) { 
     addProgram(text, GL_FRAGMENT_SHADER); 
    } 

    public void compileShader() { 
     glLinkProgram(program); 

     if (glGetProgrami(program, GL_LINK_STATUS) == 0) { 
      System.err.println(glGetShaderInfoLog(program, 1024)); 
      System.exit(1); 
     } 

     glValidateProgram(program); 
     if(glGetProgrami(program, GL_VALIDATE_STATUS) == 0){ 
      System.err.println(glGetShaderInfoLog(program, 1024)); 
      System.exit(1); 
     } 
    } 

    private void addProgram(String text, int type) { 
     int shader = glCreateShader(type); 

     if (shader == 0) { 
      System.err.println("Shader Creation failed: could not find valid memory location when adding shader"); 
      System.exit(1); 
     } 

     glShaderSource(shader, text); 
     glCompileShader(shader); 

     if (glGetShaderi(shader, GL_COMPILE_STATUS) == 0) { 
      System.err.println(glGetShaderInfoLog(shader, 1024)); 
      System.exit(1); 
     } 

     glAttachShader(program, shader); 
    } 
} 

我的遊戲課在哪裏我初始化着色器一個多邊形:

package com.base.engine; 

import org.lwjgl.input.Keyboard; 

public class Game { 
    private Mesh mesh; 
    private Shader shader; 

    public Game() { 
     mesh = new Mesh(); 
     shader = new Shader(); 

     Vertex[] data = new Vertex[] {new Vertex(new Vector3f(-1, -1, 0)), 
             new Vertex(new Vector3f(0, 1, 0)), 
             new Vertex(new Vector3f(1, -1, 0))}; 
     mesh.addVertices(data); 
     shader.addVertexShader(ResourceLoader.loadShader("basicVertex.vs")); 
     shader.addFragmentShader(ResourceLoader.loadShader("basicFragment.fs")); 

    } 

    public void input() { 
     if (Input.getKeyDown(Keyboard.KEY_UP)) { 
      System.out.println("We've just pressed up"); 
     } 
     if (Input.getKeyUp(Keyboard.KEY_UP)) { 
      System.out.println("We've just released up"); 
     } 

     if (Input.getMouseDown(1)) { 
      System.out.println("We've just pressed right-mouse at " 
        + Input.getMousePosition().toString()); 
     } 
     if (Input.getMouseUp(1)) { 
      System.out.println("We've just released right-mouse"); 
     } 
    } 

    public void update() { 

    } 

    public void render() { 
     shader.bind(); 
     mesh.draw(); 
    } 
} 

和我着色器:(錯誤是否會顯示頂點着色器,但如果我註釋掉頂點着色器的片段看起來像它什麼都不做。)

頂點:(第5行中,空隙的main(),顯然是麻煩製造者)

#version 330 

layout (location = 0) in vec3 position 

void main() 
{ 
    gl_Position = vec4(0.25 * position, 1.0); 
} 

片段:

#version 330 

out vec3 fragColour; 

void main() 
{ 
    fragColour = vec4(0.0, 1.0, 1.0, 1.0); 
} 

我明白這一切都很長,但如果你知道任何事情,請幫助我!

回答

3

除非是轉錄代碼的錯字,否則您在頂點着色器中缺少分號。 3號線是:​​

layout (location = 0) in vec3 position

應該

layout (location = 0) in vec3 position;

並且關於

but if I comment out the vertex shader the fragment looks like it does nothing

每個着色器程序,至少需要兩個頂點和片段/像素着色器。如果沒有頂點着色器(當您將其註釋掉時),則不會傳遞頂點,這意味着不會生成任何基元,因此不會使用您的片段着色器填充基元。

+0

謝謝,這將是有道理的,如果它仍然繼續(可能不會)後回來。另外,對不起。我一直跳到結論沒有檢查分號等小事。 –

+0

不用擔心。它發生在我們最好的人身上! – ssell