Fix nullpointer in ModelRenderer

- Somehow managed to only do a check in a subclass
 - Add #empty() helper method to IModel
This commit is contained in:
Jozufozu 2021-08-04 14:34:13 -07:00
parent e3b1172925
commit 60c85ec4f6
3 changed files with 25 additions and 22 deletions

View file

@ -16,7 +16,7 @@ public class ArrayModelRenderer extends ModelRenderer {
@Override
public void draw() {
if (!isInitialized()) init();
if (!initialized) init();
if (!isValid()) return;
vao.bind();
@ -26,16 +26,12 @@ public class ArrayModelRenderer extends ModelRenderer {
vao.unbind();
}
private boolean isValid() {
return model != null && model.valid();
}
@Override
protected void init() {
initialized = true;
IModel model = modelSupplier.get();
if (model.vertexCount() <= 0) return;
if (model.empty()) return;
this.model = new IndexedModel(model);

View file

@ -19,30 +19,29 @@ public class ModelRenderer {
* Renders this model, checking first if there is anything to render.
*/
public void draw() {
if (!isInitialized()) init();
if (!model.valid()) return;
if (!initialized) init();
if (!isValid()) return;
model.setupState();
model.drawCall();
model.clearState();
}
protected void init() {
initialized = true;
IModel model = modelSupplier.get();
if (model.vertexCount() <= 0) return;
this.model = new IndexedModel(model);
}
public boolean isInitialized() {
return initialized;
}
public void delete() {
if (model != null)
model.delete();
}
protected void init() {
initialized = true;
IModel model = modelSupplier.get();
if (model.empty()) return;
this.model = new IndexedModel(model);
}
protected boolean isValid() {
return model != null && model.valid();
}
}

View file

@ -63,4 +63,12 @@ public interface IModel {
default int size() {
return vertexCount() * format().getStride();
}
/**
* Is there nothing to render?
* @return true if there are no vertices.
*/
default boolean empty() {
return vertexCount() == 0;
}
}