Reputation: 185
So I'm following "theBennyBox's" Youtube series on 3d game engines and at about episode 9/10 something goes wrong for me.
i get the error
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
here is all my stuff:
my resource loader (to read the file)
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();
}
}
my Shader class:
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);
}
}
my Game class where I initialise the shader to a single polygon:
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();
}
}
and my shaders: (the error is showing up for the vertex shader, but if I comment out the vertex shader the fragment looks like it does nothing.)
Vertex: (line 5, the void main(), is the troublemaker apparently)
#version 330
layout (location = 0) in vec3 position
void main()
{
gl_Position = vec4(0.25 * position, 1.0);
}
Fragment:
#version 330
out vec3 fragColour;
void main()
{
fragColour = vec4(0.0, 1.0, 1.0, 1.0);
}
I appreciate that this is all very long but if you know anything please help me!
Upvotes: 2
Views: 3301
Reputation: 6597
Unless it is a typo from transcribing the code, you are missing a semicolon in your vertex shader. Line #3 is:
layout (location = 0) in vec3 position
should be
layout (location = 0) in vec3 position;
And in regards to
but if I comment out the vertex shader the fragment looks like it does nothing
Every shader program requires at minimum both a vertex and fragment/pixel shader. Without the vertex shader (when you comment it out) no vertices are passed on, which means no primitives are generated, and thus no primitives to fill with your fragment shader.
Upvotes: 3