lwjglgamedev / lwjglbook-leg

Source code of the chapters of the book 3D Game Development with LWJGL 3
https://ahbejarano.gitbook.io/lwjglgamedev/
Apache License 2.0
560 stars 202 forks source link

Possible bug in c21 (Instanced Rendering) #109

Closed TheStackTraceWhisperer closed 2 years ago

TheStackTraceWhisperer commented 2 years ago

In chapter 21 Instanced Rendering, the InstancedMesh class leverages an OpenGL API that seems to only be available in OpenGL 3.3+ despite creating an OpenGL 3.2 context defined in the Window class. On my machine this triggers fatal error

FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
    at org.lwjgl.opengl.GL33C.glVertexAttribDivisor(Native Method)
    at org.lwjgl.opengl.GL33.glVertexAttribDivisor(GL33.java:614)
    at org.lwjglb.engine.graph.InstancedMesh.<init>(InstancedMesh.java:60)
    at org.lwjglb.engine.loaders.obj.OBJLoader.reorderLists(OBJLoader.java:89)
    at org.lwjglb.engine.loaders.obj.OBJLoader.loadMesh(OBJLoader.java:60)
    at org.lwjglb.game.DummyGame.init(DummyGame.java:103)
    at org.lwjglb.engine.GameEngine.init(GameEngine.java:51)
    at org.lwjglb.engine.GameEngine.run(GameEngine.java:38)
    at org.lwjglb.game.Main.main(Main.java:17)

OpenGL context creation: https://github.com/lwjglgamedev/lwjglbook/blob/master/chapter21/c21-p3/src/main/java/org/lwjglb/engine/Window.java#L51

InstancedMesh import from GL33: https://github.com/lwjglgamedev/lwjglbook/blob/master/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java#L22

InstanceMesh usage of GL33: https://github.com/lwjglgamedev/lwjglbook/blob/master/chapter21/c21-p3/src/main/java/org/lwjglb/engine/graph/InstancedMesh.java#L60

Unfortunately, I'm still working through validating the fixes on my machine.

https://github.com/lwjglgamedev/lwjglbook/blob/master/chapter21/c21-p3/src/main/java/org/lwjglb/engine/Window.java#L51

Changing the above line to glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); (from 2) seems to alleviate the problem.

Is this a suitable solution that I should PR into the chapter 21 supplemental codebase? If not feel free to guide me towards the desired fix and I can attempt to provide a PR

lwjglgamedev commented 2 years ago

glVertexAttribDivisor is only available since version 3.3 so the fix you propose is ok. I can provide a fix ofr that or you can send a PR (in this case, if you can, please update later chapters).

Thanks !

lwjglgamedev commented 2 years ago

I've just published new version of the book using old URLs (https://github.com/lwjglgamedev/lwjglbook and https://github.com/lwjglgamedev/lwjglbook-bookcontents).

Closing all the pending issues