LWJGL / lwjgl3

LWJGL is a Java library that enables cross-platform access to popular native APIs useful in the development of graphics (OpenGL, Vulkan, bgfx), audio (OpenAL, Opus), parallel computing (OpenCL, CUDA) and XR (OpenVR, LibOVR, OpenXR) applications.
https://www.lwjgl.org
BSD 3-Clause "New" or "Revised" License
4.76k stars 636 forks source link

Problem with NetBeans 13 and Eclipse (latest version) #759

Closed DeafMan1983 closed 2 years ago

DeafMan1983 commented 2 years ago

Question

Hello everyone, I have tried but NetBeans jumps exception and error:

Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#76) Syntax error: unexpected tokens following #version
ERROR: 0:1: error(#364) Invalid: unexpected token in symbol.
ERROR: error(#273) 2 compilation errors.  No code generated

It looks like weird because NetBeans compile though shader files. I don't understand.

I have written correct glsl version #version 330 core It is nothing happened: ./shaders/shader.vs

#version 330 core
layout (location=0) in vec3 position;
void main()
{
    gl_Position = vec4(position, 1.0);
}

./shaders/shader.fs

#version 330 core
out vec4 FragColor;
void main()
{
    FragColor = vec4(0.12, 0.12, 0.12, 1.0);
}

And my classes Shader.java and ShaderProgram.java are correct like other developers made with Java: I would like to show you

package Example;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.lwjgl.opengl.GL30;

public class Shader {
    private int shaderID;

    public static Shader Create(int shader_type, String shader_source)
    {
        Shader shaClass = new Shader();
        shaClass.shaderID = GL30.glCreateShader(shader_type);
        GL30.glShaderSource(shaClass.shaderID, ReadFile(shader_source));

        GL30.glCompileShader(shaClass.shaderID);
        if (GL30.glGetShaderi(shaClass.shaderID, GL30.GL_COMPILE_STATUS) != 1)
        {
            System.err.println(GL30.glGetShaderInfoLog(shaClass.shaderID));
            System.exit(1);
        }

        return shaClass;
    }

    private static String ReadFile(String filename)
    {
        StringBuilder strbdr = new StringBuilder();
        BufferedReader br;
        try
        {
            br = new BufferedReader(new FileReader(new File("./shaders/" + filename)));
            String lines;
            while ((lines = br.readLine()) != null) {
                strbdr.append(lines);
                strbdr.append("/n");
            }
        }
        catch (IOException ioe)
        {
            ioe.printStackTrace();
        }
        return strbdr.toString();
    }

    public void Delete()
    {
        GL30.glDeleteShader(this.shaderID);
    }

    public int GetShaderID()
    {
        return shaderID;
    }
}

And this

package Example;

import org.lwjgl.opengl.GL30;

public class ShaderProgram {
    private int programID;
    private int vertexID, fragmentID;

    public static ShaderProgram Attach(Shader vertex, Shader fragment)
    {
        ShaderProgram shpClass = new ShaderProgram();
        shpClass.vertexID = vertex.GetShaderID();
        shpClass.fragmentID = fragment.GetShaderID();

        shpClass.programID = GL30.glCreateProgram();
        GL30.glAttachShader(shpClass.programID, shpClass.vertexID);
        GL30.glAttachShader(shpClass.programID, shpClass.fragmentID);

        GL30.glLinkProgram(shpClass.programID);
        if (GL30.glGetProgrami(shpClass.programID, GL30.GL_LINK_STATUS) != 1)
        {
            System.err.println(GL30.glGetProgramInfoLog(shpClass.programID));
            System.exit(1);
        }

        GL30.glValidateProgram(shpClass.programID);
        if (GL30.glGetProgrami(shpClass.programID, GL30.GL_VALIDATE_STATUS) != 1)
        {
            System.err.println(GL30.glGetProgramInfoLog(shpClass.programID));
            System.exit(1);
        }

        return shpClass;
    }

    public void Detach()
    {
        GL30.glDetachShader(this.programID, this.vertexID);
        GL30.glDetachShader(this.programID, this.fragmentID);
    }

    public void Use()
    {
        GL30.glUseProgram(this.programID);
    }

    public void Delete()
    {
        GL30.glDeleteProgram(this.programID);
    }

    public void BindAttritubeLocation(int location_index, String locatiopn_name)
    {
        GL30.glBindAttribLocation(this.programID, location_index, locatiopn_name);
    }
}

And Main.java:

package Example;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL30;

public class Main
{
    private static DisplayManager dm;
    private static VertexArrayObject vao;
    private static BufferObject vbo;    
    private static float[] vertices =
    {
      //  X      Y     Z
         0.5f,  0.5f, 0.0f, // top left
         0.5f, -0.5f, 0.0f, // bottom left
        -0.5f, -0.5f, 0.0f, // bottom right
        -0.5f,  0.5f, 0.5f  // top right
    };

    private static BufferObject ebo;
    private static int[] indices =
    {
        0, 1, 3,  // First triangle
        1, 2, 3   // Secound triangle
    };

    private static Shader vertex_shader;
    private static Shader fragment_shader;
    private static ShaderProgram shader_program;

    private static Renderer render;

    public static void main(String[] args) {
        dm = DisplayManager.Create("04 Shader A - SolidColor", 1280, 800);  
        dm.Make();

        vertex_shader = Shader.Create(GL30.GL_VERTEX_SHADER, "shader.vs");
        fragment_shader = Shader.Create(GL30.GL_FRAGMENT_SHADER, "shader.fs");
        shader_program = ShaderProgram.Attach(vertex_shader, fragment_shader);

        vao = VertexArrayObject.Gen();
        vbo = BufferObject.Gen();
        ebo = BufferObject.Gen();

        vbo.Bind(GL15.GL_ARRAY_BUFFER);
        vbo.Data(vertices.length, vertices, GL15.GL_STATIC_DRAW);

        ebo.Bind(GL15.GL_ELEMENT_ARRAY_BUFFER);
        ebo.Data(indices.length, indices, GL15.GL_STATIC_DRAW);

        shader_program.BindAttritubeLocation(0, "position0");
        int stride = (int)(3 * 0.0f);
        vbo.Pointer(0, 3, stride, 0);
        vbo.Enable(0);

        while (dm.IsRunning())
        {
            dm.HandleClose();

            render = Renderer.Clear(GL11.GL_COLOR_BUFFER_BIT, 1.0f, 1.0f * 0.35f, 0.0f);
            render.HandleSwitcher(dm.GetWindow());
            shader_program.Use();
            vao.Bind();
            render.Draw(GL11.GL_TRIANGLES, indices.length, GL15.GL_UNSIGNED_INT, 0);

            dm.Poll();
        }

        shader_program.Delete();
        vertex_shader.Delete();
        fragment_shader.Delete();
        ebo.Delete();
        vbo.Delete();
        vao.Delete();
        dm.Destroy();
    }
}

Compile ( It looks successful and has nor errors ) Run exceptions :( run:

Vertex shader failed to compile with the following errors:
ERROR: 0:1: error(#76) Syntax error: unexpected tokens following #version
ERROR: 0:1: error(#364) Invalid: unexpected token in symbol.
ERROR: error(#273) 2 compilation errors.  No code generated

C:\Users\Jens\AppData\Local\NetBeans\Cache\13\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\Jens\AppData\Local\NetBeans\Cache\13\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 0 seconds)

But I don't believe that. my vertex shader is correct for glsl shader format

But It happens for Eclipse: I have tried to compile and run but It is bad classloader image But Eclipse throws common errors

How do I know? But I use JDK 11 / 18 versions Do I need downgrade Eclipse current version? But I already downloaded Latest version but It said Eclipse doesn't support latest JDK and I downgraded JDK to 11.x And It said ok but run from Java Application throws error of classloaders.

I don't understand why does NetBeans 13 not recognize if it compiles with shader files, eh? I thought Visual Studio 2019 Community ignores shader files while I created GLFW Application in C# or C++ and it works fine but why does it happen with NetBeans and Eclipse? I am surprised why LWJGL crashes with running under Eclipse and NetBeans forces and puts errors out

My display card is AMD Radeon RX 480 8 GB VRAM since December 2016 but it is really powerfully.

How do I fix? Thanks!

DeafMan1983 commented 2 years ago

Sorry NetBeans cause I found "" to '' and remove vao.Bind(); in while loop cause it restored that is why I don't need add vao.Bind() in while loop It shows up

God thanks It works fine. image

And what is about Eclipse????

DeafMan1983 commented 2 years ago

Why close??? Did you check latest Eclipse? I will prove you with video.

httpdigest commented 2 years ago

I closed this issue because it has nothing to do with LWJGL. It is related to not being aware of Java and Eclipse tooling (likely the distinction between modulepath and classpath). Questions related to "How does Java tooling work?" are better directed to more general programming problem platforms like StackOverflow, not the LWJGL repository.