Skip to content

Commit

Permalink
Apply an index to a mesh in a separate step
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Aug 4, 2024
1 parent 07e0e66 commit 5b0ed87
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 49 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 @@ -29,9 +29,14 @@ import groovy.transform.TupleConstructor
enum Attribute {

// @formatter:off
POSITION ('position', 0, Vector2f.FLOATS),
COLOUR ('colour', 1, Colour.FLOATS),
TEXTURE_UVS ('textureUVs', 2, Vector2f.FLOATS)
POSITION ('position', 0, Vector2f.FLOATS),
COLOUR ('colour', 1, Colour.FLOATS),

TEXTURE_UVS ('textureUVs', 2, Vector2f.FLOATS),
FRAME_STEP_X ('frameStepX', 3, 1),
FRAME_STEP_Y ('frameStepY', 4, 1),
FRAMES_HORIZONTAL ('framesHorizontal', 5, 1),
FRAMES_VERTICAL ('framesVertical', 6, 1)
// @formatter:on

final String name
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,14 +34,24 @@ abstract class Mesh implements GraphicsResource {
final Vector2f[] vertices
final Colour colour
final Vector2f[] textureUVs
final int[] indices
final boolean dynamic

/**
* Add an index component to this mesh. This will then be used for rendering
* the mesh.
*/
abstract Mesh addIndex(int[] index)

/**
* Use this mesh in upcoming render operations.
*/
abstract void bind()

/**
* The index associated with this mesh, if any.
*/
abstract int[] getIndex()

/**
* Update the vertices part of the mesh data.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ class OpenGLMesh extends Mesh {

final int vertexArrayId
final int vertexBufferId
final int elementBufferId
int elementBufferId

private Vector2f[] lastVertices
private int[] index

/**
* 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) {

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

vertexArrayId = glGenVertexArrays()
glBindVertexArray(vertexArrayId)
Expand Down Expand Up @@ -80,24 +80,21 @@ class OpenGLMesh extends Mesh {
}
}

// Buffer for all the index data, if applicable
if (indices) {
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)
}
}
else {
elementBufferId = 0
}

lastVertices = vertices
}

@Override
OpenGLMesh addIndex(int[] index) {

elementBufferId = glGenBuffers()
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBufferId)
stackPush().withCloseable { stack ->
glBufferData(GL_ELEMENT_ARRAY_BUFFER, stack.mallocInt(index.size()).put(index).flip(), GL_STATIC_DRAW)
}
this.index = index
return this
}

/**
* A mesh is truthy/ready if its array data is valid.
*/
Expand All @@ -122,6 +119,12 @@ class OpenGLMesh extends Mesh {
glDeleteVertexArrays(vertexArrayId)
}

@Override
int[] getIndex() {

return index
}

@Override
void updateVertices(Vector2f[] vertices) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,13 @@ 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)
if (index) {
mesh.addIndex(index)
}
trigger(new MeshCreatedEvent(mesh))
return mesh
}
Expand Down Expand Up @@ -240,8 +243,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 +328,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 @@ -38,15 +38,15 @@ class SpriteShader extends ShaderConfig {
shader.setUniformTexture('mainTexture', 0, material.texture)
},
{ Shader shader, SpriteMaterial material, window ->
if (material.spriteMetadataBuffer) {
material.spriteMetadataBuffer.bind()
}
else {
shader.setUniform('framesHorizontal', material.framesHorizontal)
shader.setUniform('framesVertical', material.framesVertical)
shader.setUniform('frameStepX', material.frameStepX)
shader.setUniform('frameStepY', material.frameStepY)
}
// if (material.spriteMetadataBuffer) {
// material.spriteMetadataBuffer.bind()
// }
// else {
// shader.setUniform('framesHorizontal', material.framesHorizontal)
// shader.setUniform('framesVertical', material.framesVertical)
// shader.setUniform('frameStepX', material.frameStepX)
// shader.setUniform('frameStepY', material.frameStepY)
// }
shader.setUniform('frame', material.frame)
}
]
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 5b0ed87

Please sign in to comment.