# Java/LWJGL 3 Optimization and Render error

(I asked the same question on StackOverflow but didn't got an answer. It's the first time I post here so idk if this is not allowed or anything, so if it is please tell me)

I was following ThinMatrix's tutorial on OPENGL/LWJGL 3 3D Game Tutorial series (here), even though my code was wrote a little different from his, it was going well.

I could render an object like the image below using basic LWJGL 3 methods for rendering/shading. Basically, using this method in the Render class:

 //Old method

glBindVertexArray(mesh.getVaoId());
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);

Matrix4f transformationMatrix = Maths.createTransfMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(),
entity.getRotZ(), entity.getScale());

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model.getTexture());

glDrawElements(GL_TRIANGLES, mesh.getVertexCount(), GL_UNSIGNED_INT, 0);

mesh.cleanUp();
}


And calling it out in the main class like this:

public static void main(String[] args){

Render render = new Render();
window = render.init();
glfwMakeContextCurrent(window);
glfwSwapInterval(1);

glfwShowWindow(window);

createCapabilities();
Light light = new Light(new Vector3f(0,0,20), new Vector3f(1,1,1));

squareModel.setShineDamper(10);
squareModel.setReflectivity(1);

Camera camera = new Camera();

while(!glfwWindowShouldClose(window))
{
render.prepare();
processInput(window);

glfwSetFramebufferSizeCallback(window, resizeWindow);
GLFW.glfwSetScrollCallback(window, new GLFWScrollCallback() {
@Override public void invoke (long win, double dx, double dy) {
XScroll = (float) dx;
YScroll = (float) dy;
}
});

camera.move(window, YScroll);
YScroll = 0;

Matrix4f viewMatrix = Maths.createViewMatrix(camera);

glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();

}


Until the 13 video, Optimizing, where he teached how to do the same procedure in a more optimized way, using HashMaps and ArrayLists to store the entities and calling the VAO elements only once per list of entities. So, I made this MasterRender class:

public class Master {

private Render renderer = new Render(shader);

public void render(Light sun, Camera camera){
renderer.prepare();

Matrix4f viewMatrix = Maths.createViewMatrix(camera);

}

if(batch!=null) {
}else {
entities.put(entityModel, newBatch);
}
}

public void renderEntity() {

renderer.render(entities);

entities.clear();
}

return renderer.init();
}

public void cleanup() {
}

}


And used new methods in the Render class:

//Optimized methods

prepareModel(model);
prepareEntity(entity);
glDrawElements(GL_TRIANGLES, model.createModel().getVertexCount(), GL_UNSIGNED_INT, 0);
}
unbindModel();
}

}

glBindVertexArray(mesh.getVaoId());
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, model.getTexture());

}

private void unbindModel() {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
glBindVertexArray(0);

glBindTexture(GL_TEXTURE_2D, 0);

}

Matrix4f transformationMatrix = Maths.createTransfMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(),
entity.getRotZ(), entity.getScale());

}


Finally, I adjusted the main class, much like he did in the video

public static void main(String[] args){

Master render = new Master();
...

while(!glfwWindowShouldClose(window))
{
processInput(window);

glfwSetFramebufferSizeCallback(window, resizeWindow);
GLFW.glfwSetScrollCallback(window, new GLFWScrollCallback() {
@Override public void invoke (long win, double dx, double dy) {
XScroll = (float) dx;
YScroll = (float) dy;
}
});

camera.move(window, YScroll);
YScroll = 0;

render.render(light, camera);
render.processEntity(Ico);
Ico.changeRot(0, 3, 0);
}
render.renderEntity();

glfwSwapBuffers(window);
glfwPollEvents();
}


Not only it didn't work, it bought up a log error:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ff8b8cd07b0, pid=11800, tid=7260
#
# JRE version: OpenJDK Runtime Environment (16.0.2+7) (build 16.0.2+7-67)
# Java VM: OpenJDK 64-Bit Server VM (16.0.2+7-67, mixed mode, tiered, compressed oops, compressed class ptrs, g1 gc, windows-amd64)
# Problematic frame:
# C  [nvoglv64.dll+0x8507b0]
#
# No core dump will be written. Minidumps are not enabled by default on client versions of Windows
#
# If you would like to submit a bug report, please visit:
#   https://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#

---------------  S U M M A R Y ------------

Command Line: -Dfile.encoding=Cp1252 -XX:+ShowCodeDetailsInExceptionMessages br.com.renanlima.git.main

---------------  T H R E A D  ---------------

Stack: [0x0000007d29000000,0x0000007d29100000],  sp=0x0000007d290ff350,  free space=1020k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [nvoglv64.dll+0x8507b0]

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  org.lwjgl.opengl.GL11C.nglDrawElements(IIIJ)V+0
j  org.lwjgl.opengl.GL11C.glDrawElements(IIIJ)V+4
j  org.lwjgl.opengl.GL11.glDrawElements(IIIJ)V+4
j  br.com.renanlima.render.Render.render(Ljava/util/Map;)V+84
j  br.com.renanlima.render.Master.renderEntity()V+8
j  br.com.renanlima.git.main.main([Ljava/lang/String;)V+254
v  ~StubRoutines::call_stub

...


My question is: What did I do wrong in this new method of rendering? I looked up other tutorials besides ThinMatrix's one and they seen to all do it in a similar way.

EDIT 1: So, using Eclipse's debugger I found out 2 things: 1 - For some reason, the "transformationMatrix" is loading different, even though the two methods are the same and the parameters they are getting are the same.

This are the matrix variables using the "createModel()" method, the one that works

And this are the matrix variables using the optimized method, the one that doesn't work

This seems to be the only matrix/vertex in which this happens, but I could be overlooking something

2 - I think I've pinpointed the specific part where the crash happens. When running the "glDrawElements" method.

More especificaly, on this part of the method:

But none of the variables seem to be wrong before that method is called. Again, I could be overlooking something, but there are a sh*tton of variables at the "findNative()" part, so I will look better into it.

EDIT 1.2: I solved the transformationMatrix error. It was because of the "changeRot(0, 3, 0);" being called early. But I still can't find out why the crash at "glDrawElements" happens. Can someone please help me with this? If the question is confusing, please tell me so I can try to explain it better.

• Surely you've already checked your favorite debugger? What does it say?
– user35344
Oct 27 '21 at 12:04
• I put an edit with the debugging process, using Eclipse's debugger. Oct 28 '21 at 1:13