[LWJGL3] Nothing is rendering to screen

Hi All

I have been looking at this problem for hours now, so I’m taking it to you and maybe someone can help me.

I have been porting my 2d graphics library from LWJGL2 to LWJGL3 the past few days.
I have set up the screen and everything, but nothing is rendering.

The function

glClear(GL_COLOR_BUFFER_BIT);

is working as intended and clears the screen to the desired color.

I have not changed any of my gl code when I ported and I receive no errors in Eclipse.
Neither do I get any errors from

glGetError()

or from the openGL debug mode.

I am using

glDrawElements(GL_TRIANGLES)

for my rendering where I update the buffers every frame.
This worked perfectly before but not now.

If i disable my shaders and use

glBegin(GL_TRIANGLES);
            glColor3f(1f, 0f, 0f);
            glVertex3f(-0.6f, -0.4f, 0f);
            glColor3f(0f, 1f, 0f);
            glVertex3f(0.6f, -0.4f, 0f);
            glColor3f(0f, 0f, 1f);
            glVertex3f(0f, 0.6f, 0f);
            glEnd();

it works…

My render code:


		if(numberOfVerticies == 0)
			return;
		
		glUseProgram(shaderProgramID);
		
		int numberOfIndecies = indexBuffer.position();
		
		glBindVertexArray(vaoID);
		
		buffer.flip();
		glBindBuffer(GL_ARRAY_BUFFER, vboID);
		glBufferSubData(GL_ARRAY_BUFFER, 0, buffer);
		
		indexBuffer.flip();
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ivboID);
		glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, indexBuffer);
		
		glDrawElements(GL_TRIANGLES, numberOfIndecies, GL_UNSIGNED_SHORT, 0);
		
		buffer.clear();
		indexBuffer.clear();
		numberOfVerticies = 0;

My shaders:

#version 150

uniform mat4 projMatrix;

in vec3 position;
in vec4 color;

out vec4 pass_color;

void main()
{	
	gl_Position = projMatrix * vec4(position, 1.0);
	
	pass_color = color;
}


#version 150

in vec4 pass_color;

out vec4 out_color;

void main()
{	
	out_color = pass_color;
}

I also tried setting my projection matrix to the identity matrix just to see if that would make any difference, but no.

If anyone has any ideas to what might be the problem I would be so grateful for your help!
I have checked every single line of code that I thought might be the problem but nothing is working for me :frowning:

Download LWJGLX/debug and run your application with the additional JVM argument -javaagent:/path/to/lwjglx-debug-1.0.0.jar and see whether it outputs any errors or warnings.
If not, please provide the complete code showing:

  • how you setup and enable the vertex attributes
  • how you load shaders
  • how you set uniforms in the shaders

Thank you for the suggestion.

I get the following output in the console:

[LWJGL] Version: 3.1.6 build 14
[LWJGL] 	 OS: Windows 10 v10.0
[LWJGL] 	JRE: 1.8.0_181 amd64
[LWJGL] 	JVM: Java HotSpot(TM) 64-Bit Server VM v25.181-b13 by Oracle Corporation
[LWJGL] Loading library (system): lwjgl
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\Mathias\AppData\Local\Temp\lwjglMathias\3.1.6-build-14\lwjgl.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\Mathias\AppData\Local\Temp\lwjglMathias\3.1.6-build-14\lwjgl.dll
[LWJGL] MemoryUtil accessor: MemoryAccessorUnsafe
[LWJGL] MemoryUtil allocator: DebugAllocator
[LWJGL] Loading library: glfw
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\Mathias\AppData\Local\Temp\lwjglMathias\3.1.6-build-14\glfw.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\Mathias\AppData\Local\Temp\lwjglMathias\3.1.6-build-14\glfw.dll
[LWJGL] Loading library (system): lwjgl_opengl
[LWJGL] 	Using SharedLibraryLoader...
[LWJGL] 	Found at: C:\Users\Mathias\AppData\Local\Temp\lwjglMathias\3.1.6-build-14\lwjgl_opengl.dll
[LWJGL] 	Loaded from org.lwjgl.librarypath: C:\Users\Mathias\AppData\Local\Temp\lwjglMathias\3.1.6-build-14\lwjgl_opengl.dll
[LWJGL] Loading library: opengl32
[LWJGL] 	opengl32.dll not found in org.lwjgl.librarypath=C:\Users\Mathias\AppData\Local\Temp\lwjglMathias\3.1.6-build-14
[LWJGL] 	Loaded from system paths
[LWJGL] [GL] GL_ARB_shader_subroutine was reported as available but an entry point is missing.
[LWJGL] [GL] Using KHR_debug for error logging.
Exception in thread "main" java.lang.NoSuchMethodError: org.lwjgl.opengl.GL43.glGetInteger(I)I
	at org.lwjglx.debug.opengl.GLUtil.setupDebugMessageCallback(GLUtil.java:134)
	at org.lwjglx.debug.opengl.GLUtil.setupDebugMessageCallback(GLUtil.java:41)
	at org.lwjglx.debug.opengl.GL.createCapabilities(GL.java:36)
	at org.lwjglx.debug.$Proxy$13.createCapabilities7(Unknown Source)
	at display.Display.create(Display.java:61)
	at main.Main.main(Main.java:27)
[LWJGL] 16 bytes leaked, thread 1 (main), address: 0x2350000
	at org.lwjgl.system.Callback.create(Callback.java:133)
	at org.lwjgl.system.Callback.<init>(Callback.java:83)
	at org.lwjgl.glfw.GLFWErrorCallback.<init>(GLFWErrorCallback.java:60)
	at org.lwjgl.glfw.GLFWErrorCallback$1.<init>(GLFWErrorCallback.java:97)
	at org.lwjgl.glfw.GLFWErrorCallback.createPrint(GLFWErrorCallback.java:97)
	at org.lwjglx.debug.$Proxy$13.createPrint2(Unknown Source)
	at display.Display.create(Display.java:38)
	at main.Main.main(Main.java:27)
[LWJGL] 16 bytes leaked, thread 1 (main), address: 0x1C780000
	at org.lwjgl.system.Callback.create(Callback.java:133)
	at org.lwjgl.system.CallbackI.address(CallbackI.java:34)
	at org.lwjgl.opengl.GLDebugMessageCallback.create(GLDebugMessageCallback.java:54)
	at org.lwjglx.debug.opengl.GLUtil.setupDebugMessageCallback(GLUtil.java:109)
	at org.lwjglx.debug.opengl.GLUtil.setupDebugMessageCallback(GLUtil.java:41)
	at org.lwjglx.debug.opengl.GL.createCapabilities(GL.java:36)
	at org.lwjglx.debug.$Proxy$13.createCapabilities7(Unknown Source)
	at display.Display.create(Display.java:61)
	at main.Main.main(Main.java:27)
[LWJGL] 16 bytes leaked, thread 1 (main), address: 0x24B0000
	at org.lwjgl.system.Callback.create(Callback.java:133)
	at org.lwjgl.system.Callback.<init>(Callback.java:83)
	at org.lwjgl.glfw.GLFWErrorCallback.<init>(GLFWErrorCallback.java:60)
	at org.lwjglx.debug.glfw.GLFW$1.<init>(GLFW.java:50)
	at org.lwjglx.debug.glfw.GLFW.glfwInit(GLFW.java:50)
	at org.lwjglx.debug.$Proxy$13.glfwInit4(Unknown Source)
	at display.Display.create(Display.java:41)
	at main.Main.main(Main.java:27)

I can’t see what the problem is…

Oh yeah, please do use the latest version of LWJGL, 3.2.0.

EDIT: Now it suffices if you just re-download: LWJGLX/debug

I found the problem!
Thank you for your help! :smiley:

Although I found the problem and how to solve it, I don’t really understand it… Maybe you could explain it to me?

The problem was in my uniform projection matrix.
I have the code:

	public void resize(Matrix4f m)
	{
		FloatBuffer buf = BufferUtils.createFloatBuffer(16);
		m.get(buf);
		buf.flip();		
		
		glUseProgram(shaderProgramID);
		glUniformMatrix4fv(projMatrixID, false, buf);
	}

And for some reason the m.get(buf) does not move the position of the buffer, so when I flip the buffer it sets the limit to 0.
Is this a bug in Matrix4f.get? I’m using JOML 1.9.10 btw.

If I remove the buf.flip() then the rendering works! :slight_smile:
But I thought you had to flip a buffer before reading from it…

JOML does not follow Java NIO’s behaviour with its relative Buffer.get() methods.
This irritates most people that are used to the behaviour of NIO’s relative Buffer methods.

However:

  1. Matrix4f.get(Buffer) is not a NIO buffer method (it just takes a Buffer as parameter). It might be irritating that the method is called “get” and that it therefore reminds people of a relative NIO Buffer method with the same name
  2. having get() modify the buffer position and requiring the user to call flip() is just cumbersome. It’s better to leave the buffer position as is
  3. when you call OpenGL methods that read from or write to a NIO buffer, such as glTexSubImage2D, glBufferData or glReadPixels, you would also not rewind/flip the buffer afterwards, would you, because you assume that those methods do not modify the buffer position and they don’t

LWJGLX/debug would also throw an exception if you tried to flip a buffer with position 0:


Exception in thread "main" java.lang.IllegalStateException: calling flip() on a buffer with position = 0. Check if you called a method that actually modifies the buffer position.
	at your.package.and.TheClass.withTheMethod(TheClass.java:123)
	...

For more common pitfalls like this, see: https://github.com/JOML-CI/JOML/wiki/Common-Pitfalls
Your particular problem is described here: https://github.com/JOML-CI/JOML/wiki/Common-Pitfalls#methods-taking-a-nio-buffer-do-not-modify-the-buffer-position

The behaviour of Matrix4f.get(FloatBuffer) is also described in its JavaDoc, which you can see here

[quote]This method will not increment the position of the given FloatBuffer.
[/quote]
I recommend you attach the sources jar to your project to have JavaDoc ready in your IDE for all JOML classes/methods.

I also recommend you use LWJGL’s MemoryStack class to more efficiently allocate the very-short-lived Buffer, like so:


  public void resize(Matrix4f m) {
    glUseProgram(shaderProgramID);
    try (MemoryStack frame = MemoryStack.stackPush()) {
      glUniformMatrix4fv(projMatrixID, false, m.get(frame.mallocFloat(16)));
    }
  }

For more information on efficient memory-management in LWJGL3, read: https://blog.lwjgl.org/memory-management-in-lwjgl-3/