LWJGL Forum

Programming => OpenGL => Topic started by: Risa123 on March 30, 2018, 19:01:40

Title: No VAO visible
Post by: Risa123 on March 30, 2018, 19:01:40
Probably just an stupid mistake...
Code: [Select]
public class Mesh {
 private final int id;
 private final int verticesVBO;
 private final int indicesVBO;
 private final int vertexCount;
 public Mesh(float[]vertices,int[]indices){
     vertexCount = indices.length;
     id = GL30.glGenVertexArrays();
     verticesVBO = GL15.glGenBuffers();
     indicesVBO = GL15.glGenBuffers();
     GL30.glBindVertexArray(id);
     GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,verticesVBO);
     FloatBuffer vbuffer = MemoryUtil.memAllocFloat(vertices.length);
     vbuffer.put(vertices);
     vbuffer.flip();
     GL15.glBufferData(GL15.GL_ARRAY_BUFFER,vbuffer,GL15.GL_STATIC_DRAW);
     GL20.glVertexAttribPointer(0,3,GL11.GL_FLOAT,false,0,0);
     GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0);
     GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,indicesVBO);
     IntBuffer   ibuffer = MemoryUtil.memAllocInt(indices.length);
     ibuffer.put(indices);
     ibuffer.flip();
     GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER,ibuffer,GL15.GL_STATIC_DRAW);
     GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,0);
     GL30.glBindVertexArray(0);
     MemoryUtil.memFree(vbuffer);
 }
 public void render(){
     GL30.glBindVertexArray(id);
     GL20.glEnableVertexAttribArray(0);
     GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, id);
     GL11.glDrawElements(GL11.GL_TRIANGLES,vertexCount,GL11.GL_UNSIGNED_INT,0);
     GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,0);
     GL20.glDisableVertexAttribArray(0);
     GL30.glBindVertexArray(0);
 }
 public void destroy(){
     GL15.glDeleteBuffers(verticesVBO);
     GL15.glDeleteBuffers(indicesVBO);
     GL30.glDeleteVertexArrays(id);
 }
}
public  class GameEngine {
 private final String title;
 private int width;
 private int height;
 private boolean fullscreen;
 private boolean resizable;
 private boolean vsync;
 private long handle;
 private Color clearColor;
 private final ArrayList<GameObject> objects = new ArrayList<>();
 private  Shader shader;
 private final IGameLogic logic;
 public GameEngine(IGameLogic logic){
      this.logic = logic;
     WindowProperties prop =  this.logic.preInit();
     this.title = prop.getTitle();
     this.width = prop.getWidth();
     this.height = prop.getHeight();
     this.fullscreen = prop.isFullscreen();
     this.resizable = prop.isResizable();
     this.vsync = prop.isVsync();
     this.clearColor = prop.getClearColor();
   
 }
 private void init() throws GLException, ClassNotFoundException, IOException{
     GLFWErrorCallback.createThrow().set();
     if(!GLFW.glfwInit()){
         throw new GLException("cannot initialize GLFW");
     }
     long monitor = MemoryUtil.NULL;
     if(fullscreen){
         monitor = GLFW.glfwGetPrimaryMonitor();
     }
     GLFW.glfwDefaultWindowHints();
     GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR,3);
     GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR,2);
     GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE,GLFW.GLFW_OPENGL_CORE_PROFILE);
     GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT,GLFW.GLFW_TRUE);
     if(resizable){
         GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE,GLFW.GLFW_TRUE);
     }
     handle = GLFW.glfwCreateWindow(width,height,title,monitor,MemoryUtil.NULL);
     GLFW.glfwSetWindowSizeCallback(handle,new GLFWWindowSizeCallbackI() {

         @Override
         public void invoke(long window, int width, int height) {
            GL11.glViewport(0,0, width,height);
         }

         @Override
         public void callback(long args) {
         }

         @Override
         public String getSignature() {
             return null;
         }

         @Override
         public long address() {
             return MemoryUtil.NULL;
         }
     });
     GLFW.glfwMakeContextCurrent(handle);
     if(vsync){
         GLFW.glfwSwapInterval(1);
     }
     GL.createCapabilities();
     GL11.glEnable(GL11.GL_DEPTH_TEST);
     GL11.glClearColor(clearColor.getRed(),clearColor.getGreen(),clearColor.getBlue(),clearColor.getAlpha());
     shader = new Shader(Utils.loadResource("/shader/vertex.vs"),Utils.loadResource("/shader/fragment.fs"));
     logic.init();
 }
 private void destroy(){
     logic.destroy();
     shader.destroy();
     GLFW.glfwDestroyWindow(handle);
     GLFW.glfwTerminate();
 }
 private void update(){
     GLFW.glfwPollEvents();
 }
 private void render(){
     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
     GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
    shader.bind();
     logic.render();
    shader.unbind();
     if(!vsync){
         GLFW.glfwSwapBuffers(handle);
     }
 }
 public void start() throws ClassNotFoundException, IOException{
   try{
     init();
     long lastTime = System.nanoTime();
     double delta = 0.0;
     double ns = 1000000000.0 / 60.0;
     long timer = System.currentTimeMillis();
     int updates = 0;
     int frames = 0;
     while(!GLFW.glfwWindowShouldClose(handle)){
         long now = System.nanoTime();
         delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1.0) {
update();
        updates++;
delta--;
         }
         update();
         render();
         frames++;
         if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " ups, " + frames + " fps");
updates = 0;
frames = 0;
}
     }
     destroy();
   }catch(GLException ex){
       ex.printStackTrace();
   }
 }
}

public  class GameEngine {
 private final String title;
 private int width;
 private int height;
 private boolean fullscreen;
 private boolean resizable;
 private boolean vsync;
 private long handle;
 private Color clearColor;
 private final ArrayList<GameObject> objects = new ArrayList<>();
 private  Shader shader;
 private final IGameLogic logic;
 public GameEngine(IGameLogic logic){
      this.logic = logic;
     WindowProperties prop =  this.logic.preInit();
     this.title = prop.getTitle();
     this.width = prop.getWidth();
     this.height = prop.getHeight();
     this.fullscreen = prop.isFullscreen();
     this.resizable = prop.isResizable();
     this.vsync = prop.isVsync();
     this.clearColor = prop.getClearColor();
   
 }
 private void init() throws GLException, ClassNotFoundException, IOException{
     GLFWErrorCallback.createThrow().set();
     if(!GLFW.glfwInit()){
         throw new GLException("cannot initialize GLFW");
     }
     long monitor = MemoryUtil.NULL;
     if(fullscreen){
         monitor = GLFW.glfwGetPrimaryMonitor();
     }
     GLFW.glfwDefaultWindowHints();
     GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR,3);
     GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR,2);
     GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE,GLFW.GLFW_OPENGL_CORE_PROFILE);
     GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT,GLFW.GLFW_TRUE);
     if(resizable){
         GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE,GLFW.GLFW_TRUE);
     }
     handle = GLFW.glfwCreateWindow(width,height,title,monitor,MemoryUtil.NULL);
     GLFW.glfwSetWindowSizeCallback(handle,new GLFWWindowSizeCallbackI() {

         @Override
         public void invoke(long window, int width, int height) {
            GL11.glViewport(0,0, width,height);
         }

         @Override
         public void callback(long args) {
         }

         @Override
         public String getSignature() {
             return null;
         }

         @Override
         public long address() {
             return MemoryUtil.NULL;
         }
     });
     GLFW.glfwMakeContextCurrent(handle);
     if(vsync){
         GLFW.glfwSwapInterval(1);
     }
     GL.createCapabilities();
     GL11.glEnable(GL11.GL_DEPTH_TEST);
     GL11.glClearColor(clearColor.getRed(),clearColor.getGreen(),clearColor.getBlue(),clearColor.getAlpha());
     shader = new Shader(Utils.loadResource("/shader/vertex.vs"),Utils.loadResource("/shader/fragment.fs"));
     logic.init();
 }
 private void destroy(){
     logic.destroy();
     shader.destroy();
     GLFW.glfwDestroyWindow(handle);
     GLFW.glfwTerminate();
 }
 private void update(){
     GLFW.glfwPollEvents();
 }
 private void render(){
     GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
     GL11.glClear(GL11.GL_DEPTH_BUFFER_BIT);
    shader.bind();
     logic.render();
    shader.unbind();
     if(!vsync){
         GLFW.glfwSwapBuffers(handle);
     }
 }
 public void start() throws ClassNotFoundException, IOException{
   try{
     init();
     long lastTime = System.nanoTime();
     double delta = 0.0;
     double ns = 1000000000.0 / 60.0;
     long timer = System.currentTimeMillis();
     int updates = 0;
     int frames = 0;
     while(!GLFW.glfwWindowShouldClose(handle)){
         long now = System.nanoTime();
         delta += (now - lastTime) / ns;
lastTime = now;
if (delta >= 1.0) {
update();
        updates++;
delta--;
         }
         update();
         render();
         frames++;
         if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " ups, " + frames + " fps");
updates = 0;
frames = 0;
}
     }
     destroy();
   }catch(GLException ex){
       ex.printStackTrace();
   }
 }
}

#version 450
in vec3 exColor;
out vec4 fragColor;
void main(){
 
}
#version 450

layout(location=0) in vec3 position;

void main(){
 gl_Position = vec4(position,1.0);
}
public class GameLogic implements IGameLogic{
    private Mesh mesh;
    @Override
    public void init() {
        mesh  = new Mesh(new float[]{0,0,0,1,0,0,1,1,0},new int[]{0,1,2});
    }

    @Override
    public WindowProperties preInit() {
        return new WindowProperties("Game",500,500,false,true,false,new Color(0,0,1,0));
    }

    @Override
    public void update() {
       
    }

    @Override
    public void render() {
        mesh.render();
    }

    @Override
    public void destroy() {
        mesh.destroy();
    }
}
public class Main{

    /**
     * @param args the command line arguments
     * @throws java.lang.ClassNotFoundException
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws ClassNotFoundException, IOException {
       new GameEngine(new GameLogic()).start();
    }
}
Title: Re: No VAO visible
Post by: KaiHH on March 30, 2018, 19:46:09
Download the JAR file of LWJGLX/Debug (https://github.com/LWJGLX/debug) from this direct link off its GitHub Releases page (https://github.com/LWJGLX/debug/releases/download/1.0.0-SNAPSHOT/lwjglx-debug-1.0.0-SNAPSHOT.jar) (also available on the https://www.lwjgl.org/customize site when choosing the "LWJGLX/debug v1.0.0" addon), copy it to your project's root folder (which will be the current working directory when starting your application) and run your game with the additional JVM argument -javaagent:lwjglx-debug-1.0.0-SNAPSHOT.jar. It will give you useful debugging output, which might lead to the actual cause of the error. If not, please provide an MVCE (https://stackoverflow.com/help/mcve).
Title: Re: No VAO visible
Post by: Risa123 on March 30, 2018, 20:04:59
Nothing and you dont know what else can i  give you than a complete source code.
PS: no vao visible means that i see just blue window with no triangle
Title: Re: No VAO visible
Post by: KaiHH on March 30, 2018, 20:09:10
Quote
what else can i  give you than a complete source code.
The code you provided is far from being complete and actually compilable.
There are numerous references to other classes of which you did not provide the sources. Examples: GameObject, Shader, IGameLogic, WindowProperties, GLException, Utils, Color.
Test it yourself. Create a new Java project in an IDE of your choice, copy the sources you provided here in this forum entry, and see numerous errors of unresolvable files popping up...
Title: Re: No VAO visible
Post by: Risa123 on March 30, 2018, 20:33:13
I was meaning important but I still forget to shader and utils.
And as an "excuse" I give you (hopefully) full source code
Code: [Select]
public class Shader {
 private final int id;
 private final int vertexID;
 private final int fragmentID;
 public Shader(String vertex,String fragment) throws GLException{
     id = GL20.glCreateProgram();
     if(id == 0){
         throw new GLException("cannot create shader program");
     }
     vertexID = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
     if(vertexID == 0){
         throw new GLException("cannot create vertex shader");
     }
     fragmentID = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
     if(fragmentID == 0){
         throw new GLException("cannot create fragment shader");
     }
     GL20.glShaderSource(vertexID, vertex);
     GL20.glCompileShader(vertexID);
     if(GL20.glGetShaderi(vertexID,GL20.GL_COMPILE_STATUS) == 0){
         throw new GLException("cannot compile vertex shader " + GL20.glGetShaderInfoLog(vertexID));
     }
      GL20.glShaderSource(fragmentID,fragment);
     GL20.glCompileShader(fragmentID);
     if(GL20.glGetShaderi(fragmentID,GL20.GL_COMPILE_STATUS) == 0){
         throw new GLException("cannot compile vertex shader " + GL20.glGetShaderInfoLog(fragmentID));
     }
     GL20.glAttachShader(id,vertexID);
     GL20.glAttachShader(id,fragmentID);
     GL20.glLinkProgram(id);
     if(GL20.glGetProgrami(id,GL20.GL_LINK_STATUS) == 0){
         throw new GLException("cannot link shader " + GL20.glGetProgramInfoLog(id));
     }
     GL20.glValidateProgram(id);
     if(GL20.glGetProgrami(id,GL20.GL_VALIDATE_STATUS) == 0){
         throw new GLException("cannot validate shader " + GL20.glGetProgramInfoLog(id));
     }
 }
 public void destroy(){
     GL20.glDeleteShader(vertexID);
     GL20.glDeleteShader(fragmentID);
     GL20.glDeleteProgram(id);
 }
 public void bind(){
     GL20.glUseProgram(id);
 }
 public void unbind(){
     GL20.glUseProgram(0);
 }
}
public class Utils {
 public static String loadResource(String fileName) throws ClassNotFoundException,IOException{
        String result;
        try (InputStream in = Class.forName(Utils.class.getName()).getResourceAsStream(fileName);
             Scanner scanner = new Scanner(in, "UTF-8")) {
            result = scanner.useDelimiter("\\A").next();
        }
        return result;
    }
}
public interface IGameLogic {
  WindowProperties preInit();
  void init();
  void update();
  void render();
  void destroy();
}
public class Color {
 private final float red;
 private final float green;
 private final float blue;
 private final float alpha;
 public Color(float red,float green,float blue,float alpha){
     this.red = red;
     this.green = green;
     this.blue = blue;
     this.alpha = alpha;
 }

    /**
     * @return the red
     */
    public float getRed() {
        return red;
    }

    /**
     * @return the green
     */
    public float getGreen() {
        return green;
    }

    /**
     * @return the blue
     */
    public float getBlue() {
        return blue;
    }

    /**
     * @return the alpha
     */
    public float getAlpha() {
        return alpha;
    }
}
public class GLException extends Exception{
    private static final long serialVersionUID = -7748320568487422697L;
    public GLException(String msg){
        super(msg);
    }
}
public abstract class GameObject {

}
Title: Re: No VAO visible
Post by: KaiHH on March 30, 2018, 20:44:28
The error is two-fold.
One is in your Mesh.render() method where you wanted to bind your indicesVBO but you bind the id of the VAO (which happens to be the integer value of the verticesVBO):
Code: [Select]
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, id);
This should read:
Code: [Select]
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesVBO);
Another is that you never assign to fragColor in your fragment shader.
Title: Re: No VAO visible
Post by: Risa123 on March 30, 2018, 20:52:50
Stil nothing but ,thank this little advice.
Title: Re: No VAO visible
Post by: KaiHH on March 30, 2018, 21:03:26
Works for me with these two changes. I see a triangle.
And you still did not provide a complete source example by trying to create a new Java project with only the sources you provided here in your posts. WindowProperties is still missing.
Title: Re: No VAO visible
Post by: Risa123 on March 30, 2018, 21:06:10
I miss that think with shader ...
So sorry for start thank for advice and patiens with me...