0
\$\begingroup\$

I am following the tutorial "3D Game Development with LWJGL3" using LWJGL 3.0.2(stable)

https://www.gitbook.com/book/lwjglgamedev/3d-game-development-with-lwjgl/details

I am following the chapter "Rendering" . Since the vertex.vs and fragment.fs did not work, I tried to compile the shaders using Strings.It was supposed to create a triangle on the screen. However, the result console said it was a problem with some newlines. I am using Eclipse and latest jre and jdk and imported the library through the Build path property.

I am NOT using Maven because there is no Apache Maven installed in my workstation and it is very tedious.

Program Directory:-

LWJGL

Engine

GameEngine.java

Window.java

Timer.java

Utils.java(unused)

IGameLogic(interface).java

Engine.Graph

ShaderProgram.java

Game

DummyGame.java

Renderer.java

Main.java

Renderer.java

package game;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;
import engine.Window;
import engine.Graph.ShaderProgram;
public class Renderer {
 public String fragmentShaderData = 
                        "#version 330                   /n"+ "/n" +   "/n" +                 
                                 "out vec4 fragColor;                                    +
                                 "void main()                    /n"+ "/n" +
                                 "{                        /n"+
                                 "fragColor = vec4(0.0, 0.5, 0.5, 1.0);                   /n"+ "/n" +
                                 "}                     /n" +"/n" ;
 public String vertexShaderData = 
 "#version 330   /n"+ "/n"+ 
        "layout (location =0) in vec3 position;      /n"+"/n"+
        "void main()                /n"+"/n"+
        "{                         /n"+"/n"+
        "gl_Position = vec4(position, 1.0);                   /n"+"/n"+
        "}                           /n"+"/n";

    private int vboId;

 private int vaoId;

 private ShaderProgram shaderProgram;

 public Renderer() {
 }

 public void init() throws Exception {
     shaderProgram = new ShaderProgram();
     shaderProgram.createVertexShader(vertexShaderData);
     shaderProgram.createFragmentShader(fragmentShaderData);
     shaderProgram.link();

     float[] vertices = new float[]{
         0.0f,  0.5f, 0.0f,
         -0.5f, -0.5f, 0.0f,
         0.5f,  -0.5f, 0.0f
     };

     FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
     verticesBuffer.put(vertices).flip();

     // Create the VAO and bind to it
     vaoId = glGenVertexArrays();
     glBindVertexArray(vaoId);

     // Create the VBO and bint to it
     vboId = glGenBuffers();
     glBindBuffer(GL_ARRAY_BUFFER, vboId);
     glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
     // Define structure of the data
     glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);

     // Unbind the VBO
     glBindBuffer(GL_ARRAY_BUFFER, 0);

     // Unbind the VAO
     glBindVertexArray(0);
 }

 public void clear() {
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 }

 public void render(Window window) {
     clear();

     if ( window.isResized() ) {
         glViewport(0, 0, window.getWidth(), window.getHeight());
         window.setResized(false);
     }

     shaderProgram.bind();

     // Bind to the VAO
     glBindVertexArray(vaoId);
     glEnableVertexAttribArray(0);

     // Draw the vertices
     glDrawArrays(GL_TRIANGLES, 0, 3);

     // Restore state
     glDisableVertexAttribArray(0);
     glBindVertexArray(0);

     shaderProgram.unbind();
    }

     public void cleanup() {
     if (shaderProgram != null) {
         shaderProgram.cleanup();
     }

     glDisableVertexAttribArray(0);

     // Delete the VBO
     glBindBuffer(GL_ARRAY_BUFFER, 0);
     glDeleteBuffers(vboId);

     // Delete the VAO
     glBindVertexArray(0);
     glDeleteVertexArrays(vaoId);
 }
}

The rest of the code is same as of the sources of this repository:-

Source code

Except that Utils.java is not used.

The result:

java.lang.Exception: Error compiling Shader code: ERROR: 0:1: '#version' :     syntax error
ERROR: 0:1: '' : syntax error: unexpected tokens following preprocessor directive - expected a newline

 at engine.Graph.ShaderProgram.createShader(ShaderProgram.java:71)
 at engine.Graph.ShaderProgram.createVertexShader(ShaderProgram.java:37)
 at game.Renderer.init(Renderer.java:64)
 at game.DummyGame.init(DummyGame.java:34)
 at engine.GameEngine.init(GameEngine.java:91)
 at engine.GameEngine.run(GameEngine.java:67)
 at java.lang.Thread.run(Unknown Source)

Please help!!!!!

There seems to be a problem with my shaders, but I don't know why.

\$\endgroup\$
  • \$\begingroup\$ since the .vs 's don't work in my Utils, I used the direct String method (See Renderer.java) \$\endgroup\$ – EnderNinja7 Dec 18 '16 at 11:40
  • \$\begingroup\$ since the .vs 's and the .fs 's don't work in my Utils, I used the direct String method (See Renderer.java) \$\endgroup\$ – EnderNinja7 Dec 18 '16 at 11:41
  • 1
    \$\begingroup\$ You are missing a quotation mark on "out vec4 fragColor; + aren't you? \$\endgroup\$ – Tyyppi_77 Dec 18 '16 at 11:46
  • 1
    \$\begingroup\$ Shouldn't the newline be with a backslash? \$\endgroup\$ – cozmic Dec 18 '16 at 19:18
  • \$\begingroup\$ +cozmic Thanks, it works. Can you post this as answer so that I can set it as resolved? \$\endgroup\$ – EnderNinja7 Dec 19 '16 at 10:59
0
\$\begingroup\$

The newline argument has to be written as \n (with backslash).

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Does he need it, at all? Isn't the shader supposed to compile without them too? \$\endgroup\$ – Djindjidj Dec 19 '16 at 12:26
  • 1
    \$\begingroup\$ They are needed because without them the compiler can't know when a #define for example ends and the next line begins. Same goes for #version or basically any preprocessor directive I think. Normal code lines however should compile as expected without newlines as they are terminated by ; unless single line comments are used (//comment). \$\endgroup\$ – cozmic Dec 19 '16 at 13:40
  • \$\begingroup\$ I would use them anyways however in case you ever need to print out the shader source to maintain readability. \$\endgroup\$ – cozmic Dec 19 '16 at 13:43
0
\$\begingroup\$
 public String fragmentShaderData = 
                         "#version 330 \n" +               
                          "out vec4 fragColor;" +         
                          "void main(){" +
                            "fragColor = vec4(0.0, 0.5, 0.5, 1.0);" +    
                          "}";

is way more readable and should work, too.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.