Skip to content

Commit

Permalink
Reorder mesh index arg so that other method signatures line up nicer
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Aug 5, 2024
1 parent 07e0e66 commit b58da2f
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ class Map extends Node<Map> {
return scene
.requestCreateOrGet(new MeshRequest(MeshType.TRIANGLES,
new VertexBufferLayout(Attribute.POSITION, Attribute.COLOUR, Attribute.TEXTURE_UVS),
allVertices, Colour.WHITE, allTextureUVs, allIndices))
allVertices, Colour.WHITE, allTextureUVs, false, allIndices))
.thenAcceptAsync { newMesh ->
fullMesh = newMesh
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ interface GraphicsRenderer extends AutoCloseable, EventTarget {
*/
default Mesh createMesh(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour) {

return createMesh(type, layout, vertices, colour, null, null, false)
return createMesh(type, layout, vertices, colour, null, false, null)
}

/**
* Create a mesh with all of the mesh parts.
*/
Mesh createMesh(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, Vector2f[] textureUVs,
int[] indices, boolean dynamic)
boolean dynamic, int[] index)

/**
* Create a new shader program from the given configuration, or return the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ interface GraphicsRequests {

@ImmutableOptions(knownImmutables = ['layout', 'colour'])
static record MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour,
Vector2f[] textureUVs, int[] indices, boolean dynamic = false) implements Request<Mesh> {
Vector2f[] textureUVs, boolean dynamic, int[] index) implements Request<Mesh> {

MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, int[] indices, boolean dynamic = false) {
this(type, layout, vertices, colour, null, indices, dynamic)
MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, boolean dynamic, int[] index) {
this(type, layout, vertices, colour, null, dynamic, index)
}

MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, boolean dynamic = false) {
this(type, layout, vertices, colour, null, null, dynamic)
MeshRequest(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, boolean dynamic) {
this(type, layout, vertices, colour, null, dynamic, null)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class GraphicsSystem extends EngineSystem implements GraphicsRequests {
var resource = switch (request) {
case ShaderRequest -> renderer.createShader(request.shaderConfig())
case MeshRequest -> renderer.createMesh(request.type(), request.layout(), request.vertices(), request.colour(),
request.textureUVs(), request.indices(), request.dynamic())
request.textureUVs(), request.dynamic(), request.index())
case SpriteMeshRequest -> renderer.createSpriteMesh(request.surface(), request.textureUVs())
case TextureRequest -> renderer.createTexture(request.width(), request.height(), request.format(), request.data())
case SpriteSheetRequest -> renderer.createSpriteSheet(request.width(), request.height(), request.format(), request.data())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ abstract class Mesh implements GraphicsResource {
final Vector2f[] vertices
final Colour colour
final Vector2f[] textureUVs
final int[] indices
final boolean dynamic
final int[] index

/**
* Use this mesh in upcoming render operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class OpenGLMesh extends Mesh {
/**
* Constructor, creates a new OpenGL mesh.
*/
OpenGLMesh(int type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, Vector2f[] textureUVs, int[] indices,
boolean dynamic) {
OpenGLMesh(int type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, Vector2f[] textureUVs,
boolean dynamic, int[] index) {

super(type, layout, vertices, colour, textureUVs, indices, dynamic)
super(type, layout, vertices, colour, textureUVs, dynamic, index)

vertexArrayId = glGenVertexArrays()
glBindVertexArray(vertexArrayId)
Expand All @@ -58,15 +58,12 @@ class OpenGLMesh extends Mesh {
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferId)
stackPush().withCloseable { stack ->
var vertexBuffer = stack.mallocFloat(layout.size() * vertices.size())
vertices.eachWithIndex { vertex, index ->
vertices.eachWithIndex { vertex, i ->
layout.attributes.each { attribute ->
switch (attribute) {
case Attribute.POSITION -> vertexBuffer.put(vertex.x, vertex.y)
case Attribute.COLOUR -> vertexBuffer.put(colour.r, colour.g, colour.b, colour.a)
case Attribute.TEXTURE_UVS -> {
var textureUV = textureUVs[index]
vertexBuffer.put(textureUV.x, textureUV.y)
}
case Attribute.TEXTURE_UVS -> vertexBuffer.put(textureUVs[i].x, textureUVs[i].y)
default -> throw new UnsupportedOperationException("Unhandled vertex layout part ${attribute.name()}")
}
}
Expand All @@ -80,15 +77,11 @@ class OpenGLMesh extends Mesh {
}
}

// Buffer for all the index data, if applicable
if (indices) {
if (index) {
elementBufferId = glGenBuffers()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferId)
stackPush().withCloseable { stack ->
var indexBuffer = stack.mallocInt(indices.size())
.put(indices)
.flip()
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexBuffer, GL_STATIC_DRAW)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, stack.mallocInt(index.size()).put(index).flip(), GL_STATIC_DRAW)
}
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ class OpenGLRenderer implements GraphicsRenderer {

@Override
Mesh createMesh(MeshType type, VertexBufferLayout layout, Vector2f[] vertices, Colour colour, Vector2f[] textureUVs,
int[] indices, boolean dynamic) {
boolean dynamic, int[] index) {

var mesh = new OpenGLMesh(type == MeshType.LINES ? GL_LINES : type == MeshType.LINE_LOOP ? GL_LINE_LOOP : GL_TRIANGLES,
layout, vertices, colour, textureUVs, indices, dynamic)
layout, vertices, colour, textureUVs, dynamic, index)
trigger(new MeshCreatedEvent(mesh))
return mesh
}
Expand Down Expand Up @@ -240,8 +240,8 @@ class OpenGLRenderer implements GraphicsRenderer {
surface as Vector2f[],
Colour.WHITE,
textureUVs as Vector2f[],
[0, 1, 2, 0, 2, 3] as int[],
false
false,
[0, 1, 2, 0, 2, 3] as int[]
)
}

Expand Down Expand Up @@ -325,8 +325,8 @@ class OpenGLRenderer implements GraphicsRenderer {
shader.use()
shader.applyUniforms(transform, material, window)
mesh.bind()
if (mesh.indices) {
glDrawElements(mesh.vertexType, mesh.indices.size(), GL_UNSIGNED_INT, 0)
if (mesh.index) {
glDrawElements(mesh.vertexType, mesh.index.size(), GL_UNSIGNED_INT, 0)
}
else {
glDrawArrays(mesh.vertexType, 0, mesh.vertices.size())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class Primitive extends Node<Primitive> implements GraphicsElement {
return CompletableFuture.allOf(
scene
.requestCreateOrGet(type == MeshType.TRIANGLES ?
new MeshRequest(type, new VertexBufferLayout(Attribute.POSITION, Attribute.COLOUR), this.points, colour,
[0, 1, 3, 1, 2, 3] as int[], dynamic) :
new MeshRequest(type, new VertexBufferLayout(Attribute.POSITION, Attribute.COLOUR), this.points, colour, dynamic,
[0, 1, 3, 1, 2, 3] as int[]) :
new MeshRequest(type, new VertexBufferLayout(Attribute.POSITION, Attribute.COLOUR), this.points, colour, dynamic))
.thenAcceptAsync { newMesh ->
mesh = newMesh
Expand Down

0 comments on commit b58da2f

Please sign in to comment.