Для программы софтварного рендера необходимо сделать модуль Сцены. Сейчас в программе только одна модель. (Класс модели выглядит следующим образом: public class Model {
code
public ArrayList<Vector3f> vertices = new ArrayList<Vector3f>();
public ArrayList<Vector2f> textureVertices = new ArrayList<Vector2f>();
public ArrayList<Vector3f> normals = new ArrayList<Vector3f>();
public ArrayList<Polygon> polygons = new ArrayList<Polygon>();). Нужно добавить работу с несколькими. При этом должна остаться возможность перемещать, вращать и как-то изменять (см. работу другого студента) только одну из них. Про-думайте, как можно пользователю выбирать, какая/какие (множественный вариант сложнее в реализации, но интереснее) из моделей сейчас активные, т.е выбраны для применения разных трансформаций, сохранения и т.д. В этом пункте очень важен выбор грамотной архитектуры внутри приложения. Код нужен на Java
Assistant
java
import java.util.*;
import org.joml.Vector2f;
import org.joml.Vector3f;
// Model class representing a 3D model
public class Model {
private static int nextId = 0;
private int id;
public ArrayList<Vector3f> vertices = new ArrayList<>();
public ArrayList<Vector2f> textureVertices = new ArrayList<>();
public ArrayList<Vector3f> normals = new ArrayList<>();
public ArrayList<Polygon> polygons = new ArrayList<>();
public Model() {
this.id = nextId++;
}
public int getId() {
return id;
}
// Apply a transformation to this model
public void applyTransformation(Transformation t) {
// Transform vertices
for (int i = 0; i < vertices.size(); i++) {
vertices.set(i, t.transform(vertices.get(i)));
}
// Transform normals if necessary
for (int i = 0; i < normals.size(); i++) {
normals.set(i, t.transform(normals.get(i)).normalize());
}
}
// Additional methods for saving, loading, etc., can be added here
}
// Interface representing a transformation
public interface Transformation {
Vector3f transform(Vector3f v);
}
// Example Transformation implementation: Translation
public class Translation implements Transformation {
private Vector3f offset;
public Translation(Vector3f offset) {
this.offset = offset;
}
@Override
public Vector3f transform(Vector3f v) {
return new Vector3f(v).add(offset);
}
}
// Example Transformation implementation: Scaling
public class Scaling implements Transformation {
private float scaleFactor;
public Scaling(float scaleFactor) {
this.scaleFactor = scaleFactor;
}
@Override
public Vector3f transform(Vector3f v) {
return new Vector3f(v).mul(scaleFactor);
}
}
// Example Transformation implementation: Rotation (around Y axis)
public class Rotation implements Transformation {
private float angle; // in radians
public Rotation(float angle) {
this.angle = angle;
}
@Override
public Vector3f transform(Vector3f v) {
float x = (float) (v.x * Math.cos(angle) - v.z * Math.sin(angle));
float z = (float) (v.x * Math.sin(angle) + v.z * Math.cos(angle));
return new Vector3f(x, v.y, z);
}
}
// Scene class managing multiple models and their selection
public class Scene {
private List<Model> models = new ArrayList<>();
private Set<Model> selectedModels = new HashSet<>();
// Add a model to the scene
public void addModel(Model model) {
models.add(model);
}
// Remove a model from the scene
public void removeModel(Model model) {
models.remove(model);
selectedModels.remove(model);
}
// Select a model
public void selectModel(Model model) {
if (models.contains(model)) {
selectedModels.add(model);
}
}
// Deselect a model
public void deselectModel(Model model) {
selectedModels.remove(model);
}
// Clear all selections
public void clearSelection() {
selectedModels.clear();
}
// Apply a transformation to all selected models
public void applyTransformationToSelected(Transformation t) {
for (Model model : selectedModels) {
model.applyTransformation(t);
}
}
// Apply a transformation to a specific model
public void applyTransformation(Model model, Transformation t) {
if (models.contains(model)) {
model.applyTransformation(t);
}
}
// Get the list of all models
public List<Model> getModels() {
return models;
}
// Get the set of selected models
public Set<Model> getSelectedModels() {
return selectedModels;
}
// Select a model by its ID
public void selectModelById(int id) {
for (Model model : models) {
if (model.getId() == id) {
selectedModels.add(model);
break;
}
}
}
// Deselect a model by its ID
public void deselectModelById(int id) {
for (Model model : models) {
if (model.getId() == id) {
selectedModels.remove(model);
break;
}
}
}
}
// Example usage
public class Main {
public static void main(String[] args) {
Scene scene = new Scene();
// Create some models
Model model1 = new Model();
Model model2 = new Model();
// Add models to the scene
scene.addModel(model1);
scene.addModel(model2);
// Select models
scene.selectModel(model1); // Select model1
scene.selectModel(model2); // Select model2
// Create a transformation (e.g., translate by (1, 0, 0))
Transformation translation = new Translation(new Vector3f(1, 0, 0));
// Apply transformation to selected models
scene.applyTransformationToSelected(translation);
// Deselect model2
scene.deselectModel(model2);
// Apply a different transformation to the remaining selected model (model1)
Transformation scaling = new Scaling(2.0f);
scene.applyTransformationToSelected(scaling);
// Alternatively, apply a transformation to a specific model
Transformation rotation = new Rotation((float) Math.toRadians(45));
scene.applyTransformation(model2, rotation);
}
}
In this code:
The Model class represents individual 3D models and includes methods to apply transformations.
The Scene class manages multiple Model instances, keeps track of selected models, and applies transformations to them.
The Transformation interface allows different types of transformations (translation, scaling, rotation) to be applied to models.
Users can select one or multiple models, and apply transformations specifically to selected models or to individual models.
Unique IDs are assigned to each Model instance for easy identification and selection.
The example usage demonstrates how to create models, add them to the scene, select them, and apply transformations.
This architecture ensures that:
Multiple models can be managed within a scene.
Users have the flexibility to select which models are active.
Transformations can be applied to either the selected models or individually specified models.
The design promotes extensibility by allowing new types of transformations to be added easily.