Advertisement
Guest User

Untitled

a guest
Sep 20th, 2020
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.65 KB | None | 0 0
  1. package com.julian.ogl.main;
  2.  
  3. import static org.lwjgl.glfw.GLFW.*;
  4. import static org.lwjgl.opengl.GL11.*;
  5. import static org.lwjgl.opengl.GL20.*;
  6. import static org.lwjgl.opengl.GL30.*;
  7.  
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11.  
  12. import org.joml.Matrix4f;
  13. import org.joml.Vector3f;
  14.  
  15. import static org.lwjgl.opengl.GL.*;
  16.  
  17. import org.lwjgl.glfw.GLFWCursorPosCallback;
  18. import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
  19. import org.lwjgl.glfw.GLFWScrollCallback;
  20.  
  21. import de.javagl.obj.Obj;
  22. import de.javagl.obj.ObjData;
  23. import de.javagl.obj.ObjReader;
  24. import de.javagl.obj.ObjUtils;
  25.  
  26. public class Main {
  27.     public static int WIDTH = 800, HEIGHT = 600;
  28.     long window;
  29.    
  30.     static Camera camera = new Camera(new Vector3f(0.0f, -3.0f, 0.0f));
  31.     static float lastX = WIDTH / 2.0f;
  32.     static float lastY = HEIGHT / 2.0f;
  33.     static boolean firstMouse = true;
  34.    
  35.     //for framerate limit
  36.     float delta = 0.0f;
  37.     float lastFrame = 0.0f;
  38.    
  39.     public Main() throws FileNotFoundException, IOException {
  40.         //set window hints to make the window use opengl core profile and 4.0 opengl version
  41.         glfwInit();
  42.         glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  43.         glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  44.         glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  45.        
  46.         //create window and make context current also error check
  47.         window = glfwCreateWindow(WIDTH, HEIGHT, "titele", 0, 0);
  48.         if(window == 0) {
  49.             glfwTerminate();
  50.             throw new RuntimeException("Error window creation failed");
  51.         }
  52.         glfwMakeContextCurrent(window);
  53.        
  54.         createCapabilities();
  55.        
  56.         glViewport(0, 0, WIDTH, HEIGHT);
  57.        
  58.         glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
  59.         glfwSetScrollCallback(window, scroll_callback);
  60.         glfwSetCursorPosCallback(window, mouse_callback);
  61.        
  62.         mouseState(true);
  63.        
  64.         Texture tex = new Texture("res\\textures\\white.jpg");
  65.        
  66.         Shader shader = new Shader("res\\shaders\\basic.vert", "res\\shaders\\basic.frag");
  67.         Shader lightShader = new Shader("res\\shaders\\lighting.vert", "res\\shaders\\lighting.frag");
  68.        
  69.         Matrix4f model = new Matrix4f();
  70.        
  71.         Matrix4f projection = new Matrix4f();
  72.         projection = projection.perspective(45.0f, 800.0f / 600.0f, 0.1f, 100.0f);
  73.        
  74.         Obj object = ObjUtils.convertToRenderable(ObjReader.read(new FileInputStream("res\\models\\dragon.obj")));
  75.         Obj cube = ObjUtils.convertToRenderable(ObjReader.read(new FileInputStream("res\\models\\cube.obj")));
  76.        
  77.         Light light = new Light(cube, lightShader, new Vector3f(10.0f, 0.0f, 0.0f));
  78.        
  79.         int EBO;
  80.         EBO = glGenBuffers();
  81.        
  82.         int VAO;
  83.         VAO = glGenVertexArrays();
  84.         glBindVertexArray(VAO);
  85.        
  86.         int VBO = glGenBuffers();
  87.         glBindBuffer(GL_ARRAY_BUFFER, VBO);
  88.         glBufferData(GL_ARRAY_BUFFER, LoadFromObj.loadFromObj(object), GL_STATIC_DRAW);
  89.        
  90.         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
  91.         glBufferData(GL_ELEMENT_ARRAY_BUFFER, ObjData.getFaceVertexIndices(object), GL_STATIC_DRAW);
  92.        
  93.         //positions
  94.         glVertexAttribPointer(0, 3, GL_FLOAT, false, 32, 0);
  95.         glEnableVertexAttribArray(0);
  96.        
  97.         //texture coords
  98.         glVertexAttribPointer(1, 2, GL_FLOAT, false, 32, 12);
  99.         glEnableVertexAttribArray(1);
  100.        
  101.         //normals
  102.         glVertexAttribPointer(2, 3, GL_FLOAT, false, 32, 20);
  103.         glEnableVertexAttribArray(2);
  104.        
  105.         glEnable(GL_DEPTH_TEST);
  106.        
  107.         while(!glfwWindowShouldClose(window)) {
  108.             //frame logic
  109.             float currentFrame = (float) glfwGetTime();
  110.             delta = currentFrame - lastFrame;
  111.             lastFrame = currentFrame;
  112.            
  113.             //input
  114.             processInput(delta);
  115.            
  116.             //rendering
  117.             glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  118.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  119.            
  120.            
  121.            
  122.             shader.use();
  123.             shader.setInt("Texture", 0);
  124.            
  125.             projection = projection.perspective((float) camera.Zoom, WIDTH / HEIGHT, 0.1f, 100.0f);
  126.             shader.setMatrix4f("projection", projection);
  127.            
  128.             Matrix4f view = camera.getViewMatrix();
  129.             shader.setMatrix4f("view", view);
  130.             shader.setMatrix4f("model", model);
  131.             //camera.printVectorData();
  132.             //light.render(view, projection);
  133.             tex.use();
  134.             glBindVertexArray(VAO);
  135.             glDrawElements(GL_TRIANGLES, ObjData.getTotalNumFaceVertices(object), GL_UNSIGNED_INT, 0);
  136.             glBindVertexArray(0);
  137.            
  138.             //swap buffers and poll events
  139.             glfwSwapBuffers(window);
  140.             glfwPollEvents();
  141.         }
  142.        
  143.         glDeleteVertexArrays(VAO);
  144.         glDeleteBuffers(VBO);
  145.         glDeleteTextures(tex.getglTex());
  146.         tex.free();
  147.         glfwTerminate();
  148.     }
  149.    
  150.     private void processInput(float delta) {
  151.         //cehck for input
  152.         if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
  153.             glfwSetWindowShouldClose(window, true);
  154.         if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
  155.             glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  156.         else {
  157.             glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  158.         }
  159.        
  160.         if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  161.             camera.ProcessKeyboard(Camera.Camera_Movement.FORWARD, delta);
  162.         if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  163.             camera.ProcessKeyboard(Camera.Camera_Movement.BACKWARD, delta);
  164.         if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  165.             camera.ProcessKeyboard(Camera.Camera_Movement.LEFT, delta);
  166.         if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  167.             camera.ProcessKeyboard(Camera.Camera_Movement.RIGHT, delta);
  168.     }
  169.    
  170.     public void mouseState(boolean lock) {
  171.         glfwSetInputMode(window, GLFW_CURSOR, lock ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
  172.     }
  173.    
  174.     private static GLFWScrollCallback scroll_callback = new GLFWScrollCallback() {
  175.         @Override
  176.         public void invoke(long window, double xoffset, double yoffset) {
  177.             camera.ProcessMouseScroll((float) yoffset);
  178.         }
  179.     };
  180.    
  181.     private static GLFWCursorPosCallback mouse_callback = new GLFWCursorPosCallback() {
  182.         @Override
  183.         public void invoke(long window, double xpos, double ypos) {
  184.             if (firstMouse)
  185.             {
  186.                 lastX = (float) xpos;
  187.                 lastY = (float) ypos;
  188.                 firstMouse = false;
  189.             }
  190.  
  191.             float xoffset = (float) (xpos - lastX);
  192.             float yoffset = (float) (lastY - ypos); // reversed since y-coordinates go from bottom to top
  193.  
  194.             lastX = (float) xpos;
  195.             lastY = (float) ypos;
  196.  
  197.             camera.ProcessMouseMovement(xoffset, yoffset, true);
  198.         }
  199.     };
  200.    
  201.     //framebuffersize callback gets called when window resizes
  202.     private static GLFWFramebufferSizeCallback framebuffer_size_callback = new GLFWFramebufferSizeCallback(){
  203.         @Override
  204.         public void invoke(long window, int width, int height){
  205.             glViewport(0,0,width,height);
  206.         }
  207.     };
  208.    
  209.     public static void main(String[] args) {
  210.         try {
  211.             new Main();
  212.         } catch (FileNotFoundException e) {
  213.             e.printStackTrace();
  214.         } catch (IOException e) {
  215.             e.printStackTrace();
  216.         }
  217.     }
  218.    
  219.    
  220. }
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement