Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.julian.ogl.main;
- import static org.lwjgl.glfw.GLFW.*;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL20.*;
- import static org.lwjgl.opengl.GL30.*;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import org.joml.Matrix4f;
- import org.joml.Vector3f;
- import static org.lwjgl.opengl.GL.*;
- import org.lwjgl.glfw.GLFWCursorPosCallback;
- import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
- import org.lwjgl.glfw.GLFWScrollCallback;
- import de.javagl.obj.Obj;
- import de.javagl.obj.ObjData;
- import de.javagl.obj.ObjReader;
- import de.javagl.obj.ObjUtils;
- public class Main {
- public static int WIDTH = 800, HEIGHT = 600;
- long window;
- static Camera camera = new Camera(new Vector3f(0.0f, -3.0f, 0.0f));
- static float lastX = WIDTH / 2.0f;
- static float lastY = HEIGHT / 2.0f;
- static boolean firstMouse = true;
- //for framerate limit
- float delta = 0.0f;
- float lastFrame = 0.0f;
- public Main() throws FileNotFoundException, IOException {
- //set window hints to make the window use opengl core profile and 4.0 opengl version
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- //create window and make context current also error check
- window = glfwCreateWindow(WIDTH, HEIGHT, "titele", 0, 0);
- if(window == 0) {
- glfwTerminate();
- throw new RuntimeException("Error window creation failed");
- }
- glfwMakeContextCurrent(window);
- createCapabilities();
- glViewport(0, 0, WIDTH, HEIGHT);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- glfwSetScrollCallback(window, scroll_callback);
- glfwSetCursorPosCallback(window, mouse_callback);
- mouseState(true);
- Texture tex = new Texture("res\\textures\\white.jpg");
- Shader shader = new Shader("res\\shaders\\basic.vert", "res\\shaders\\basic.frag");
- Shader lightShader = new Shader("res\\shaders\\lighting.vert", "res\\shaders\\lighting.frag");
- Matrix4f model = new Matrix4f();
- Matrix4f projection = new Matrix4f();
- projection = projection.perspective(45.0f, 800.0f / 600.0f, 0.1f, 100.0f);
- Obj object = ObjUtils.convertToRenderable(ObjReader.read(new FileInputStream("res\\models\\dragon.obj")));
- Obj cube = ObjUtils.convertToRenderable(ObjReader.read(new FileInputStream("res\\models\\cube.obj")));
- Light light = new Light(cube, lightShader, new Vector3f(10.0f, 0.0f, 0.0f));
- int EBO;
- EBO = glGenBuffers();
- int VAO;
- VAO = glGenVertexArrays();
- glBindVertexArray(VAO);
- int VBO = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- glBufferData(GL_ARRAY_BUFFER, LoadFromObj.loadFromObj(object), GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, ObjData.getFaceVertexIndices(object), GL_STATIC_DRAW);
- //positions
- glVertexAttribPointer(0, 3, GL_FLOAT, false, 32, 0);
- glEnableVertexAttribArray(0);
- //texture coords
- glVertexAttribPointer(1, 2, GL_FLOAT, false, 32, 12);
- glEnableVertexAttribArray(1);
- //normals
- glVertexAttribPointer(2, 3, GL_FLOAT, false, 32, 20);
- glEnableVertexAttribArray(2);
- glEnable(GL_DEPTH_TEST);
- while(!glfwWindowShouldClose(window)) {
- //frame logic
- float currentFrame = (float) glfwGetTime();
- delta = currentFrame - lastFrame;
- lastFrame = currentFrame;
- //input
- processInput(delta);
- //rendering
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- shader.use();
- shader.setInt("Texture", 0);
- projection = projection.perspective((float) camera.Zoom, WIDTH / HEIGHT, 0.1f, 100.0f);
- shader.setMatrix4f("projection", projection);
- Matrix4f view = camera.getViewMatrix();
- shader.setMatrix4f("view", view);
- shader.setMatrix4f("model", model);
- //camera.printVectorData();
- //light.render(view, projection);
- tex.use();
- glBindVertexArray(VAO);
- glDrawElements(GL_TRIANGLES, ObjData.getTotalNumFaceVertices(object), GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- //swap buffers and poll events
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glDeleteVertexArrays(VAO);
- glDeleteBuffers(VBO);
- glDeleteTextures(tex.getglTex());
- tex.free();
- glfwTerminate();
- }
- private void processInput(float delta) {
- //cehck for input
- if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, true);
- if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- }
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
- camera.ProcessKeyboard(Camera.Camera_Movement.FORWARD, delta);
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
- camera.ProcessKeyboard(Camera.Camera_Movement.BACKWARD, delta);
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
- camera.ProcessKeyboard(Camera.Camera_Movement.LEFT, delta);
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
- camera.ProcessKeyboard(Camera.Camera_Movement.RIGHT, delta);
- }
- public void mouseState(boolean lock) {
- glfwSetInputMode(window, GLFW_CURSOR, lock ? GLFW_CURSOR_DISABLED : GLFW_CURSOR_NORMAL);
- }
- private static GLFWScrollCallback scroll_callback = new GLFWScrollCallback() {
- @Override
- public void invoke(long window, double xoffset, double yoffset) {
- camera.ProcessMouseScroll((float) yoffset);
- }
- };
- private static GLFWCursorPosCallback mouse_callback = new GLFWCursorPosCallback() {
- @Override
- public void invoke(long window, double xpos, double ypos) {
- if (firstMouse)
- {
- lastX = (float) xpos;
- lastY = (float) ypos;
- firstMouse = false;
- }
- float xoffset = (float) (xpos - lastX);
- float yoffset = (float) (lastY - ypos); // reversed since y-coordinates go from bottom to top
- lastX = (float) xpos;
- lastY = (float) ypos;
- camera.ProcessMouseMovement(xoffset, yoffset, true);
- }
- };
- //framebuffersize callback gets called when window resizes
- private static GLFWFramebufferSizeCallback framebuffer_size_callback = new GLFWFramebufferSizeCallback(){
- @Override
- public void invoke(long window, int width, int height){
- glViewport(0,0,width,height);
- }
- };
- public static void main(String[] args) {
- try {
- new Main();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement