|
| 1 | +package engine.processing; |
| 2 | + |
| 3 | +import engine.render.Material; |
| 4 | +import engine.resources.FilterMode; |
| 5 | +import engine.resources.Texture; |
| 6 | +import engine.vbo.VBO; |
| 7 | +import math.Vector2f; |
| 8 | +import math.Vector3f; |
| 9 | +import mesh.Face3D; |
| 10 | +import mesh.Mesh3D; |
| 11 | +import processing.core.PApplet; |
| 12 | +import processing.core.PGraphics; |
| 13 | +import processing.core.PShape; |
| 14 | + |
| 15 | +public class VBOProcessing implements VBO { |
| 16 | + private PShape shape; |
| 17 | + private PGraphics graphics; |
| 18 | + private int faceCount; |
| 19 | + private int vertexCount; |
| 20 | + |
| 21 | + public VBOProcessing(PGraphics graphics) { |
| 22 | + this.graphics = graphics; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public void create(Mesh3D mesh, Material material) { // TODO Auto-generated method stub |
| 27 | + faceCount = mesh.getFaceCount(); |
| 28 | + vertexCount = mesh.getVertexCount(); |
| 29 | + |
| 30 | + shape = graphics.createShape(); |
| 31 | + |
| 32 | + shape.beginShape(); |
| 33 | + |
| 34 | + shape.noStroke(); |
| 35 | + |
| 36 | + for (Face3D f : mesh.getFaces()) { |
| 37 | + if (f.indices.length == 3) { |
| 38 | + shape.beginShape(PApplet.TRIANGLES); |
| 39 | + } else if (f.indices.length == 4) { |
| 40 | + shape.beginShape(PApplet.QUADS); |
| 41 | + } else { |
| 42 | + shape.beginShape(PApplet.POLYGON); |
| 43 | + } |
| 44 | + |
| 45 | + // applyTexture(); |
| 46 | + |
| 47 | + int[] indices = f.indices; |
| 48 | + for (int i = 0; i < indices.length; i++) { |
| 49 | + Vector3f v = mesh.vertices.get(f.indices[i]); |
| 50 | + int uvIndex = f.getUvIndexAt(i); |
| 51 | + if (uvIndex != -1) { |
| 52 | + Vector2f uv = mesh.getUvAt(uvIndex); |
| 53 | + shape.vertex(v.getX(), v.getY(), v.getZ(), uv.getX(), 1 - uv.getY()); |
| 54 | + } else { |
| 55 | + shape.vertex(v.getX(), v.getY(), v.getZ()); |
| 56 | + } |
| 57 | + } |
| 58 | + // shape.endShape(); |
| 59 | + } |
| 60 | + |
| 61 | + shape.endShape(); |
| 62 | + |
| 63 | + Texture texture = material.getDiffuseTexture(); |
| 64 | + if (texture != null) { |
| 65 | + ProcessingTexture processingTexture = (ProcessingTexture) texture.getBackendTexture(); |
| 66 | + shape.setTextureMode(PApplet.NORMAL); |
| 67 | + shape.setTexture(processingTexture.getImage()); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private int getSamplingMode(FilterMode filterMode) { |
| 72 | + switch (filterMode) { |
| 73 | + case POINT: |
| 74 | + return 2; |
| 75 | + case LINEAR: |
| 76 | + return 3; |
| 77 | + case BILINEAR: |
| 78 | + return 4; |
| 79 | + case TRILINEAR: |
| 80 | + return 5; |
| 81 | + default: |
| 82 | + System.err.println("Warning: Unexpected filter mode value: " + filterMode); |
| 83 | + return 4; // Default to BILINEAR |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void create(float[] vertices, int[] indices) { |
| 89 | + // Create a PShape object for the mesh |
| 90 | + // shape = new PShape(PShape.POINTS, vertices.length / 3); |
| 91 | + shape = new PShape(); |
| 92 | + |
| 93 | + // Load vertices into the PShape |
| 94 | + shape.beginShape(); |
| 95 | + for (int i = 0; i < vertices.length; i += 3) { |
| 96 | + shape.vertex(vertices[i], vertices[i + 1], vertices[i + 2]); |
| 97 | + } |
| 98 | + shape.endShape(); |
| 99 | + |
| 100 | + // Optional: store indices if using indexed rendering |
| 101 | + vertexCount = vertices.length / 3; // Assuming vertices are in 3D (x, y, z) |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void bind() { |
| 106 | + // Binding is not necessary for PShape in Processing |
| 107 | + // Processing automatically handles the shape for rendering |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void unbind() { |
| 112 | + // Unbinding is not needed in Processing as it handles rendering context automatically |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void updateData(float[] newData) { |
| 117 | + // Update the vertices in the PShape |
| 118 | + shape.beginShape(); |
| 119 | + for (int i = 0; i < newData.length; i += 3) { |
| 120 | + shape.vertex(newData[i], newData[i + 1], newData[i + 2]); |
| 121 | + } |
| 122 | + shape.endShape(); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void delete() { |
| 127 | + // In Processing, there's no explicit delete method for PShape |
| 128 | + shape = null; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public int getVertexCount() { |
| 133 | + return vertexCount; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public int getFaceCount() { // TODO Auto-generated method stub |
| 138 | + return faceCount; |
| 139 | + } |
| 140 | + |
| 141 | + public void draw(PGraphics graphics) { |
| 142 | + // Use Processing's drawing methods to render the shape |
| 143 | + graphics.shape(shape, 0, 0); |
| 144 | + } |
| 145 | +} |
0 commit comments