Advertisement
LukacikPavel

cv04 ugr

Oct 15th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. import org.lwjgl.glfw.*;
  2. import org.lwjgl.opengl.GL;
  3.  
  4. import static org.lwjgl.glfw.GLFW.*;
  5. import static org.lwjgl.opengl.GL11.*;
  6. import static org.lwjgl.system.MemoryUtil.*;
  7.  
  8. public class cv04 {
  9.     private int sirka = 800;
  10.     private int vyska = 600;
  11.  
  12.     Vector3f[] vrcholy = { new Vector3f(-0.5f, -0.5f, 0.5f), new Vector3f(0.5f, -0.5f, 0.5f),
  13.             new Vector3f(0.5f, -0.5f, -0.5f), new Vector3f(-0.5f, -0.5f, -0.5f), new Vector3f(-0.5f, 0.5f, 0.5f),
  14.             new Vector3f(0.5f, 0.5f, 0.5f), new Vector3f(0.5f, 0.5f, -0.5f), new Vector3f(-0.5f, 0.5f, -0.5f) };
  15.  
  16.     int[][] hrany = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 }, { 0, 4 },
  17.             { 1, 5 }, { 2, 6 }, { 3, 7 } };
  18.  
  19.     Matrix4f matica;
  20.  
  21.     Vector4f cart2hom(Vector3f v) {
  22.         return new Vector4f(v.x, v.y, v.z, 1.0f);
  23.     }
  24.  
  25.     Vector2f priemet(Vector4f v) {
  26.         return new Vector2f(v.x + v.z * 0.5f, v.y + v.z * 0.5f);
  27.     }
  28.    
  29.     Vector3f[] farby = {
  30.             new Vector3f(0, 1, 1),
  31.             new Vector3f(1, 0, 1),
  32.             new Vector3f(1, 1, 0),
  33.             new Vector3f(1, 1, 1),
  34.             new Vector3f(1, 0, 0),
  35.             new Vector3f(0, 0, 1),
  36.             new Vector3f(0, 1, 0),
  37.             new Vector3f(0.5f, 1, 1),
  38.             new Vector3f(1, 0.5f, 1),
  39.             new Vector3f(1, 1, 0.5f),
  40.             new Vector3f(1, 0, 0.5f),
  41.             new Vector3f(0.5f, 0.5f, 0.5f)
  42.     };
  43.    
  44.     Matrix4f maticaVelkosti(float tx, float ty, float tz) {
  45.         return new Matrix4f(
  46.                 new Vector4f(tx, 0, 0, 0),
  47.                 new Vector4f(0, ty, 0, 0),
  48.                 new Vector4f(0, 0, tz, 0),
  49.                 new Vector4f(0, 0, 0, 1)
  50.                 );
  51.     }
  52.  
  53.     Matrix4f maticaPos(float tx, float ty, float tz) {
  54.         return new Matrix4f(
  55.                 new Vector4f(1, 0, 0, 0),
  56.                 new Vector4f(0, 1, 0, 0),
  57.                 new Vector4f(0, 0, 1, 0),
  58.                 new Vector4f(tx, ty, tz, 1)
  59.                 );
  60.     }
  61.    
  62.     Matrix4f maticaOtocX(float uhol) {
  63.         float cos = (float)(Math.cos(uhol));
  64.         float sin = (float)(Math.sin(uhol));
  65.         return new Matrix4f(
  66.                 new Vector4f(1, 0, 0, 0),
  67.                 new Vector4f(0, cos, sin, 0),
  68.                 new Vector4f(0, -sin, cos, 0),
  69.                 new Vector4f(0, 0, 0, 1)
  70.                 );
  71.     }
  72.    
  73.     Matrix4f maticaOtocY(float uhol) {
  74.         float cos = (float)(Math.cos(uhol));
  75.         float sin = (float)(Math.sin(uhol));
  76.         return new Matrix4f(
  77.                 new Vector4f(cos, 0, sin, 0),
  78.                 new Vector4f(0, 1, 0, 0),
  79.                 new Vector4f(-sin, 0, cos, 0),
  80.                 new Vector4f(0, 0, 0, 1)
  81.                 );
  82.     }
  83.    
  84.     Matrix4f maticaOtocZ(float uhol) {
  85.         float cos = (float)(Math.cos(uhol));
  86.         float sin = (float)(Math.sin(uhol));
  87.         return new Matrix4f(
  88.                 new Vector4f(cos, sin, 0, 0),
  89.                 new Vector4f(-sin, cos, 0, 0),
  90.                 new Vector4f(0, 0, 1, 0),
  91.                 new Vector4f(0, 0, 0, 1)
  92.                 );
  93.     }
  94.  
  95.     void vykresliGL() {
  96.         // Clear the screen and depth buffer
  97.         glLoadIdentity();
  98.  
  99.         glPointSize(3);
  100.         glBegin(GL_LINES);
  101.         glColor3f(1, 1, 1);
  102.         int poc = 0;
  103.         for (int[] h : hrany) {
  104.             glColor3f(farby[poc].x, farby[poc].y, farby[poc].z);
  105.             for (int v : h) {
  106.                 Vector4f v_hom = cart2hom(vrcholy[v]);
  107.                 Vector4f v_trans = matica.multiply(v_hom);
  108.                 Vector2f p = priemet(v_trans);
  109.                 glVertex2f(p.x, p.y);
  110.             }
  111.             poc++;
  112.         }
  113.         glEnd();
  114.     }
  115.  
  116.     long window;
  117.     GLFWErrorCallback errorCallback;
  118.     GLFWKeyCallback keyCallback;
  119.  
  120.     void spusti() {
  121.         try {
  122.             init();
  123.             loop();
  124.  
  125.             glfwDestroyWindow(window);
  126.             keyCallback.free();
  127.         } finally {
  128.             glfwTerminate();
  129.             errorCallback.free();
  130.         }
  131.     }
  132.  
  133.     void init() {
  134.         glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
  135.         if (!glfwInit())
  136.             throw new IllegalStateException("Chyba pri inicializacii GLFW!!!");
  137.  
  138.         window = glfwCreateWindow(sirka, vyska, "UGR1", NULL, NULL);
  139.         if (window == NULL)
  140.             throw new RuntimeException("Chyba pri vytvoreni GLFW okna!!!");
  141.  
  142.         glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
  143.             @Override
  144.             public void invoke(long window, int key, int scancode, int action, int mods) {
  145.                 if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
  146.                     glfwSetWindowShouldClose(window, true);
  147.                 if (key == GLFW_KEY_RIGHT)
  148.                     matica = matica.multiply(maticaPos(0.01f, 0, 0));
  149.                 if (key == GLFW_KEY_LEFT)
  150.                     matica = matica.multiply(maticaPos(-0.01f, 0, 0));
  151.                 if (key == GLFW_KEY_UP)
  152.                     matica = matica.multiply(maticaPos(0, 0.01f, 0));
  153.                 if (key == GLFW_KEY_DOWN)
  154.                     matica = matica.multiply(maticaPos(0, -0.01f, 0));
  155.                 if (key == GLFW_KEY_N)
  156.                     matica = matica.multiply(maticaPos(0, 0, 0.01f));
  157.                 if (key == GLFW_KEY_M)
  158.                     matica = matica.multiply(maticaPos(0, 0, -0.01f));
  159.                 if (key == GLFW_KEY_W)
  160.                     matica = matica.multiply(maticaOtocX(0.05f));
  161.                 if (key == GLFW_KEY_S)
  162.                     matica = matica.multiply(maticaOtocX(-0.05f));
  163.                 if (key == GLFW_KEY_A)
  164.                     matica = matica.multiply(maticaOtocY(-0.05f));
  165.                 if (key == GLFW_KEY_D)
  166.                     matica = matica.multiply(maticaOtocY(0.05f));
  167.                 if (key == GLFW_KEY_Q)
  168.                     matica = matica.multiply(maticaOtocZ(0.05f));
  169.                 if (key == GLFW_KEY_E)
  170.                     matica = matica.multiply(maticaOtocZ(-0.05f));
  171.                 if (key == GLFW_KEY_X)
  172.                     matica = matica.multiply(maticaVelkosti(1.1f, 1.1f, 1.1f));
  173.                 if (key == GLFW_KEY_Z)
  174.                     matica = matica.multiply(maticaVelkosti(1/1.1f, 1/1.1f, 1/1.1f));
  175.             }
  176.         });
  177.  
  178.         GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  179.         glfwSetWindowPos(window, (vidmode.width() - sirka) / 2, (vidmode.height() - vyska) / 2);
  180.  
  181.         glfwMakeContextCurrent(window);
  182.         glfwSwapInterval(0);
  183.         glfwShowWindow(window);
  184.  
  185.         GL.createCapabilities();
  186.        
  187.         matica = new Matrix4f();
  188.         matica.setIdentity();
  189.  
  190.     }
  191.  
  192.     void loop() {
  193.         glViewport(0, 0, sirka, vyska);
  194.  
  195.         glMatrixMode(GL_PROJECTION);
  196.         glLoadIdentity();
  197.         glOrtho(-1, 1, -1, 1, 0, 1);
  198.  
  199.         glMatrixMode(GL_MODELVIEW);
  200.         glLoadIdentity();
  201.  
  202.         glClearColor(0.f, 0.f, 0.f, 1.f); // Initialize clear color
  203.  
  204.         while (!glfwWindowShouldClose(window)) {
  205.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  206.             vykresliGL();
  207.  
  208.             glfwSwapBuffers(window);
  209.  
  210.             glfwPollEvents();
  211.         }
  212.     }
  213.  
  214.     public static void main(String[] args) {
  215.         new cv04().spusti();
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement