Skip to content

Instantly share code, notes, and snippets.

@arnemileswinter
Last active August 8, 2019 10:34
Show Gist options
  • Save arnemileswinter/42d93289c2d3c3c77c2a92151a4a81a5 to your computer and use it in GitHub Desktop.
Save arnemileswinter/42d93289c2d3c3c77c2a92151a4a81a5 to your computer and use it in GitHub Desktop.
LibGdx ModelBuilder box with faces seperated
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
/**
* Used to render boxes with their faces seperated.
*/
public class PlaneModelFactory {
/**
* Create a box with faces seperated.
*
* @return The model to create instances from.
*/
public Model create(){
ModelBuilder modelBuilder = new ModelBuilder();
return box(modelBuilder, 1, 0.25f, 1);
}
/**
* creates the box's faces. modelBuilder.begin() must be called first. The result is then retreived by
* modelBuilder.end().
*
* @param modelBuilder The modelBuilder to queue faces into.
* @param w The width of the box.
* @param h The height of the box.
* @param v The depth of the box.
*/
private Model box(final ModelBuilder modelBuilder, final float w, final float h, final float v){
final long attr = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal;
// used because entering a plain 0 makes intellij render help labels in a parameter list
final float o = 0;
// @formatter:off
// _____________
// 0,h,d ->/| f /|<- w,h,d
// /_|_____ ____/ |
// 0,h,0 ->| | c | |<- w,h,0
// | b| d |e|
// 0,0,d ->| |__________|_|<- w,0,d
// | / a | /
// 0,0,0 ->|/____________|/<- w,0,0
Model a = modelBuilder.createRect(
o, o, o,
w, o, o,
w, o, v,
o, o, v,
0, -1, 0,
new Material(ColorAttribute.createDiffuse(Color.MAGENTA)),
attr);
Model b = modelBuilder.createRect(
o, o, o,
o, o, v,
o, h, v,
o, h, o,
-1, 0, 0,
new Material(ColorAttribute.createDiffuse(Color.MAGENTA)),
attr);
Model c = modelBuilder.createRect(
o, o, v,
w, o, v,
w, h, v,
o, h, v,
0, 0, -1,
new Material(ColorAttribute.createDiffuse(Color.MAGENTA)),
attr);
Model d = modelBuilder.createRect(
o, o, o,
o, h, o,
w, h, o,
w, o, o,
0, 0, 1,
new Material(ColorAttribute.createDiffuse(Color.MAGENTA)),
attr);
Model e = modelBuilder.createRect(
w, o, o,
w, h, o,
w, h, v,
w, o, v,
1, 0, 0,
new Material(ColorAttribute.createDiffuse(Color.MAGENTA)),
attr);
Model f = modelBuilder.createRect(
o, h, o,
o, h, v,
w, h, v,
w, h, o,
0, 1, 0,
new Material(ColorAttribute.createDiffuse(Color.MAGENTA)),
attr);
modelBuilder.begin();
modelBuilder.node("a", a);
modelBuilder.node("b", b);
modelBuilder.node("c", c);
modelBuilder.node("d", d);
modelBuilder.node("e", e);
modelBuilder.node("f", f);
return modelBuilder.end();
// @formatter:off
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment