openglengine.OpenGLengine.java Source code

Java tutorial

Introduction

Here is the source code for openglengine.OpenGLengine.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
*
*https://github.com/OskarVeerhoek/YouTube-tutorials/blob/master/src/utility/VersionTest.java
*
 */
package openglengine;

import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.stage.Stage;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.util.vector.Vector3f;

/**
 *
 * @author rpetit
 */
public class OpenGLengine extends Application {

    public static final int DISPLAY_HEIGHT = 768;
    public static final int DISPLAY_WIDTH = 1024;

    private static CameraGL camera;
    private static int bunnyDisplayList;

    private static final String MODEL_LOCATION = "BALPLATGC.STL";

    @Override
    public void start(Stage stage) throws Exception {
        //Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        //Scene scene = new Scene(root);
        //stage.setScene(scene);
        //stage.show();
        //versionGL();
        startGL();
    }

    public static void versionGL() {
        try {
            Display.create();
            System.out.println("Your OpenGL version is " + GL11.glGetString(GL11.GL_VERSION));
            Display.destroy();
        } catch (LWJGLException ex) {
            Logger.getLogger(OpenGLengine.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void startGL() {
        setUpDisplay();
        setUpDisplayLists();
        setUpCamera();
        while (!Display.isCloseRequested()) {
            render();
            checkInput();
            Display.update();
            Display.sync(60);
        }
        cleanUp();
        System.exit(0);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    private static void setUpDisplayLists() {
        bunnyDisplayList = glGenLists(1);
        glNewList(bunnyDisplayList, GL_COMPILE);
        {
            ModelGL m = new ModelGL().loadSTLfromFile(MODEL_LOCATION);
            glBegin(GL_TRIANGLES);
            for (FaceGL face : m.getFaces()) {
                //Vector3f n1 = m.getNormals().get(face.getNormalIndices()[0] - 1);
                //glNormal3f(n1.x, n1.y, n1.z);
                Vector3f v1 = m.getVertices().get(face.getVertex(0));
                glVertex3f(v1.x, v1.y, v1.z);
                //Vector3f n2 = m.getNormals().get(face.getNormalIndices()[1] - 1);
                //glNormal3f(n2.x, n2.y, n2.z);
                Vector3f v2 = m.getVertices().get(face.getVertex(1));
                glVertex3f(v2.x, v2.y, v2.z);
                //Vector3f n3 = m.getNormals().get(face.getNormalIndices()[2] - 1);
                //glNormal3f(n3.x, n3.y, n3.z);
                Vector3f v3 = m.getVertices().get(face.getVertex(2));
                glVertex3f(v3.x, v3.y, v3.z);
            }
            glEnd();
        }
        glEndList();
    }

    private static void checkInput() {
        //camera.processMouse(1, 120, -120);
        camera.processKeyboard(16, 100, 100, 100);
        /*
        if (Mouse.isButtonDown(0)) {
        Mouse.setGrabbed(true);
        } else if (Mouse.isButtonDown(1)) {
        Mouse.setGrabbed(false);
        }
          */
    }

    private static void cleanUp() {
        glDeleteLists(bunnyDisplayList, 1);
        Display.destroy();
    }

    private static void render() {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        camera.applyTranslations();
        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
        glCallList(bunnyDisplayList);
    }

    private static void setUpCamera() {
        camera = new EulerCameraGL((float) Display.getWidth() / Display.getHeight());
        //camera.setRotation(-1.12f, 0.16f, 0f);
        camera.setRotation(0f, 0f, 0f);
        //camera.setPosition(-1.38f, 1.36f, 7.95f);
        camera.setPosition(0f, 0f, 500f);
        camera.setFieldOfView(45);
        camera.applyOptimalStates();
        camera.applyPerspectiveMatrix();
    }

    private static void setUpDisplay() {
        try {
            Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH, DISPLAY_HEIGHT));
            Display.setVSyncEnabled(true);
            Display.setTitle("STL");
            Display.create();
        } catch (LWJGLException e) {
            System.err.println("The display wasn't initialized correctly. :(");
            Display.destroy();
            System.exit(1);
        }
    }

}