/**
* Copyright 2014 Jason Sorensen (sorensenj@smert.net)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package net.smert.frameworkgl.examples.gl1dynamicvertexbufferobject;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
import net.smert.frameworkgl.Fw;
import net.smert.frameworkgl.Screen;
import net.smert.frameworkgl.examples.common.DynamicMeshWorld;
import net.smert.frameworkgl.examples.common.DynamicSphereGameObject;
import net.smert.frameworkgl.gameobjects.AABBGameObject;
import net.smert.frameworkgl.gameobjects.GameObject;
import net.smert.frameworkgl.gameobjects.SimpleOrientationAxisGameObject;
import net.smert.frameworkgl.gameobjects.ViewFrustumGameObject;
import net.smert.frameworkgl.helpers.Keyboard;
import net.smert.frameworkgl.math.AABB;
import net.smert.frameworkgl.math.Vector3f;
import net.smert.frameworkgl.math.Vector4f;
import net.smert.frameworkgl.opengl.AmbientLight;
import net.smert.frameworkgl.opengl.GL;
import net.smert.frameworkgl.opengl.GLLight;
import net.smert.frameworkgl.opengl.MaterialLight;
import net.smert.frameworkgl.opengl.camera.Camera;
import net.smert.frameworkgl.opengl.camera.CameraController;
import net.smert.frameworkgl.opengl.camera.FrustumCullingClipSpaceSymmetrical;
import net.smert.frameworkgl.opengl.constants.GetString;
import net.smert.frameworkgl.utils.FpsTimer;
import net.smert.frameworkgl.utils.MemoryUsage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Jason Sorensen <sorensenj@smert.net>
*/
public class DynamicVertexBufferObject extends Screen {
private final static Logger log = LoggerFactory.getLogger(DynamicVertexBufferObject.class);
private boolean renderAabbs;
private boolean renderSimpleOrientationAxis;
private boolean wireframe;
private AABBGameObject aabbGameObject;
private AmbientLight ambientLight;
private Camera camera;
private CameraController cameraController;
private DynamicMeshWorld dynamicMeshesWorld;
private DynamicSphereGameObject dynamicSphereGameObject;
private FloatBuffer lightFloatBuffer;
private FpsTimer fpsTimer;
private GLLight glLight;
private final List<GameObject> gameObjectsToRender;
private MaterialLight materialLight;
private MemoryUsage memoryUsage;
private SimpleOrientationAxisGameObject simpleOrientationAxisGameObject;
private ViewFrustumGameObject viewFrustumGameObject;
public DynamicVertexBufferObject(String[] args) {
renderAabbs = false;
renderSimpleOrientationAxis = false;
wireframe = false;
gameObjectsToRender = new ArrayList<>();
}
private void handleInput() {
if (Fw.input.isKeyDown(Keyboard.ESCAPE)) {
Fw.app.stopRunning();
}
if (Fw.input.isKeyDown(Keyboard.F1) && !Fw.input.wasKeyDown(Keyboard.F1)) {
wireframe = !wireframe;
if (wireframe) {
GL.o1.setPolygonModeFrontLine();
} else {
GL.o1.setPolygonModeFrontFill();
}
}
if (Fw.input.isKeyDown(Keyboard.B) && !Fw.input.wasKeyDown(Keyboard.B)) {
renderAabbs = !renderAabbs;
}
if (Fw.input.isKeyDown(Keyboard.O) && !Fw.input.wasKeyDown(Keyboard.O)) {
renderSimpleOrientationAxis = !renderSimpleOrientationAxis;
}
if (Fw.input.isKeyDown(Keyboard.F)) {
camera.updatePlanes();
viewFrustumGameObject.getRenderableState().setInFrustum(true);
viewFrustumGameObject.getWorldTransform().getRotation().set(camera.getRotationMatrix());
viewFrustumGameObject.setWorldPosition(camera.getPosition());
viewFrustumGameObject.update(camera.getAspectRatio(), camera.getFieldOfView(), camera.getZNear(),
camera.getZFar());
Fw.graphics.updateAabb(viewFrustumGameObject);
Fw.graphics.performCulling(camera, dynamicMeshesWorld.getGameObjects());
Fw.graphics.performCulling(camera, dynamicSphereGameObject);
updateGameObjectsToRender();
}
cameraController.update();
}
private void updateGameObjectsToRender() {
gameObjectsToRender.clear();
for (GameObject gameObject : dynamicMeshesWorld.getGameObjects()) {
if (gameObject.getRenderableState().isInFrustum()) {
gameObjectsToRender.add(gameObject);
}
}
if (dynamicSphereGameObject.getRenderableState().isInFrustum()) {
gameObjectsToRender.add(dynamicSphereGameObject);
}
}
@Override
public void destroy() {
for (GameObject gameObject : dynamicMeshesWorld.getGameObjects()) {
gameObject.destroy();
}
dynamicSphereGameObject.destroy();
viewFrustumGameObject.destroy();
Fw.input.removeInputProcessor(cameraController);
Fw.input.releaseMouseCursor();
}
@Override
public void init() {
// Create timer
fpsTimer = new FpsTimer();
// Setup camera and controller
camera = GL.cameraFactory.createCamera();
camera.lookAt(new Vector3f(0f, 2f, 5f), new Vector3f(0f, 2f, -1f), Vector3f.WORLD_Y_AXIS);
camera.setPerspectiveProjection(
70f,
(float) Fw.config.getCurrentWidth() / (float) Fw.config.getCurrentHeight(),
.05f, 128f);
cameraController = GL.cameraFactory.createCameraController();
cameraController.setCamera(camera);
// Memory usage
memoryUsage = new MemoryUsage();
// Create ambient light, glLight and material light
ambientLight = GL.glFactory.createAmbientLight();
glLight = GL.glFactory.createGLLight();
glLight.setPosition(new Vector4f(0f, 15f, 10f, 1f));
materialLight = GL.glFactory.createMaterialLight();
// Float buffer for light
lightFloatBuffer = GL.bufferHelper.createFloatBuffer(4);
// AABB game object
aabbGameObject = new AABBGameObject();
aabbGameObject.getColor0().set("yellow");
aabbGameObject.init(new AABB()); // Empty AABB just so the vertex data is right
// Create dynamic mesh world
dynamicMeshesWorld = new DynamicMeshWorld();
dynamicMeshesWorld.init();
// Dynamic sphere
dynamicSphereGameObject = new DynamicSphereGameObject();
dynamicSphereGameObject.init();
// Simple axis game object
simpleOrientationAxisGameObject = new SimpleOrientationAxisGameObject();
simpleOrientationAxisGameObject.getColor0().set("red");
simpleOrientationAxisGameObject.getColor1().set("green");
simpleOrientationAxisGameObject.getColor2().set("blue");
simpleOrientationAxisGameObject.init();
// View frustum game object
viewFrustumGameObject = new ViewFrustumGameObject();
viewFrustumGameObject.getColor0().set("black");
viewFrustumGameObject.getColor1().set("yellow");
viewFrustumGameObject.getColor2().set("yellow");
viewFrustumGameObject.getColor3().set("white");
viewFrustumGameObject.getColor3().setA(.4f);
viewFrustumGameObject.getRenderableState().setInFrustum(false);
viewFrustumGameObject.init(camera.getAspectRatio(), camera.getFieldOfView(), camera.getZNear(),
camera.getZFar());
// Frustum culling
FrustumCullingClipSpaceSymmetrical frustumCulling = GL.cameraFactory.createFrustumCullingClipSpaceSymmetrical();
camera.setFrustumCulling(frustumCulling);
updateGameObjectsToRender();
// Update AABBs
Fw.graphics.updateAabb(dynamicMeshesWorld.getGameObjects());
// OpenGL settings
GL.o1.setBlendingFunctionSrcAlphaAndOneMinusSrcAlpha();
GL.o1.enableCulling();
GL.o1.cullBackFaces();
GL.o1.enableDepthTest();
GL.o1.setDepthFuncLess();
GL.o1.enableDepthMask();
GL.o1.setClearDepth(1f);
GL.o1.enableColorMaterial();
GL.o1.enableLight0();
GL.o1.enableLighting();
GL.o1.setSmoothLighting(true);
GL.o1.clear();
log.info("OpenGL version: " + GL.o1.getString(GetString.VERSION));
// Add camera controller to input
Fw.input.addInputProcessor(cameraController);
Fw.input.grabMouseCursor();
}
@Override
public void pause() {
}
@Override
public void render() {
fpsTimer.update();
memoryUsage.update();
if (Fw.timer.isGameTick()) {
// Do nothing
}
if (Fw.timer.isRenderTick()) {
handleInput();
// Clear screen
GL.o1.clear();
// Update dynamic mesh
dynamicSphereGameObject.update();
// Update AABB
Fw.graphics.updateAabb(dynamicSphereGameObject);
// Update camera
Fw.graphics.setCamera(camera);
// Update ambient light, light and material light
ambientLight.updateOpenGL(lightFloatBuffer);
glLight.updateOpenGL(lightFloatBuffer);
materialLight.updateOpenGL(lightFloatBuffer);
// Render directly
Fw.graphics.render(gameObjectsToRender);
// Render debug
GL.o1.disableLighting();
// View frustum
if (viewFrustumGameObject.getRenderableState().isInFrustum()) {
Fw.graphics.renderBlend(viewFrustumGameObject);
}
// AABBs
if (renderAabbs) {
for (GameObject gameObject : gameObjectsToRender) {
AABB worldAabb = gameObject.getWorldAabb();
// Updating AABBs this way is costly
aabbGameObject.update(worldAabb);
// AABB is already in world coordinates so we don't translate
Fw.graphics.render(aabbGameObject.getRenderable(), 0f, 0f, 0f);
}
}
// Orientation axis
if (renderSimpleOrientationAxis) {
GL.o1.disableDepthTest();
for (GameObject gameObject : gameObjectsToRender) {
simpleOrientationAxisGameObject.setWorldTransform(gameObject.getWorldTransform());
Fw.graphics.render(simpleOrientationAxisGameObject);
}
GL.o1.enableDepthTest();
}
// Restore lighting
GL.o1.enableLighting();
}
}
@Override
public void resize(int width, int height) {
GL.o1.setViewport(0, 0, width, height);
}
@Override
public void resume() {
}
}