Skip to content

Commit

Permalink
apps: add AssimpTest
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Aug 10, 2023
1 parent b0ca2c2 commit 79af16c
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 0 deletions.
3 changes: 3 additions & 0 deletions apps/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ plugins {

// tasks to run specific apps:

tasks.register('AssimpTest', JavaExec) {
mainClass = 'com.github.stephengold.vsport.test.AssimpTest'
}
tasks.register('HelloVSport', JavaExec) {
mainClass = 'com.github.stephengold.vsport.test.HelloVSport'
}
Expand Down
126 changes: 126 additions & 0 deletions apps/src/main/java/com/github/stephengold/vsport/test/AssimpTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
Copyright (c) 2023, Stephen Gold
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.github.stephengold.vsport.test;

import com.github.stephengold.vsport.AssimpUtils;
import com.github.stephengold.vsport.BaseApplication;
import com.github.stephengold.vsport.Camera;
import com.github.stephengold.vsport.Geometry;
import com.github.stephengold.vsport.Mesh;
import com.github.stephengold.vsport.TextureKey;
import com.github.stephengold.vsport.Vertex;
import com.github.stephengold.vsport.input.CameraInputProcessor;
import com.github.stephengold.vsport.input.RotateMode;
import java.util.ArrayList;
import java.util.List;
import org.joml.Matrix3f;
import org.joml.Matrix3fc;
import org.joml.Vector3f;
import org.joml.Vector3fc;

/**
* Test loading a 3-D mesh using Assimp.
*
* @author Stephen Gold [email protected]
*/
public class AssimpTest extends BaseApplication {
// *************************************************************************
// constants

/**
* rotation matrix to transform Z-up coordinates to Y-up coordinates
*/
final private static Matrix3fc zupToYup = new Matrix3f(
0f, 1f, 0f,
0f, 0f, 1f,
1f, 0f, 0f);
// *************************************************************************
// new methods exposed

/**
* Main entry point for the AssimpTest application.
*
* @param arguments the array of command-line arguments (not null)
*/
public static void main(String[] arguments) {
AssimpTest application = new AssimpTest();
application.start();
}
// *************************************************************************
// BaseApplication methods

/**
* Callback invoked by V-Sport after the main update loop terminates.
*/
@Override
public void cleanUp() {
// do nothing
}

/**
* Initialize the application.
*/
@Override
public void initialize() {
// Load the viking_room model using Assimp:
String modelName = "/Models/viking_room/viking_room.obj";
int postFlags = 0x0;
List<Integer> indices = null;
List<Vertex> vertices = new ArrayList<>();
AssimpUtils.extractTriangles(modelName, postFlags, indices, vertices);

// The 3-D model is Z-up, but the camera is Y-up, so rotate each vertex:
for (Vertex v : vertices) {
v.rotate(zupToYup);
}

// De-duplicate the list of vertices as we create the mesh:
Mesh roomMesh = Mesh.newInstance(vertices);

TextureKey roomKey = new TextureKey(
"classpath:/Models/viking_room/viking_room.png");

Geometry room = new Geometry(roomMesh);
room.setProgram("Unshaded/Texture");
room.setTexture(roomKey);

// Configure the camera:
Camera camera = getCamera();
camera.setZClip(0.1f, 10f);

Vector3fc eye = new Vector3f(2f, 2f, 2f);
Vector3fc target = new Vector3f(0f, 0f, 0f);
camera.reposition(eye, target);

CameraInputProcessor cip = getCameraInputProcessor();
cip.setMoveSpeed(2f);
cip.setRotationMode(RotateMode.DragLMB);
}
}
4 changes: 4 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ configurations.all {
resolutionStrategy.cacheChangingModulesFor 0, 'seconds' // to disable caching of SNAPSHOTs
}

tasks.register('AssimpTest') {
dependsOn ':apps:AssimpTest'
description 'Runs the Assimp test app.'
}
tasks.register('HelloVSport') {
dependsOn ':apps:HelloVSport'
description 'Runs the HelloVSport tutorial app.'
Expand Down

0 comments on commit 79af16c

Please sign in to comment.