Advertisement
LukacikPavel

cv05 ugr

Oct 22nd, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.79 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.     int[][] steny = { { 0, 1, 5, 4 }, { 1, 2, 6, 5 }, { 2, 3, 7, 6 }, { 3, 0, 4, 7 }, { 4, 5, 6, 7 }, { 3, 2, 1, 0 } };
  20.  
  21.     Matrix4f matica;
  22.  
  23.     Vector4f cart2hom(Vector3f v) {
  24.         return new Vector4f(v.x, v.y, v.z, 1.0f);
  25.     }
  26.  
  27.     Vector2f priemet(Vector4f v) {
  28.         // return new Vector2f(v.x + v.z * 0.5f, v.y + v.z * 0.5f);
  29.         return new Vector2f(v.x, v.y);
  30.     }
  31.  
  32.     Vector3f[] farby = { new Vector3f(0, 1, 1), new Vector3f(1, 0, 1), new Vector3f(1, 1, 0), new Vector3f(1, 1, 1),
  33.             new Vector3f(1, 0, 0), new Vector3f(0, 0, 1), new Vector3f(0, 1, 0), new Vector3f(0.5f, 1, 1),
  34.             new Vector3f(1, 0.5f, 1), new Vector3f(1, 1, 0.5f), new Vector3f(1, 0, 0.5f),
  35.             new Vector3f(0.5f, 0.5f, 0.5f) };
  36.  
  37.     Matrix4f maticaVelkosti(float tx, float ty, float tz) {
  38.         return new Matrix4f(new Vector4f(tx, 0, 0, 0), new Vector4f(0, ty, 0, 0), new Vector4f(0, 0, tz, 0),
  39.                 new Vector4f(0, 0, 0, 1));
  40.     }
  41.  
  42.     Matrix4f maticaPos(float tx, float ty, float tz) {
  43.         return new Matrix4f(new Vector4f(1, 0, 0, 0), new Vector4f(0, 1, 0, 0), new Vector4f(0, 0, 1, 0),
  44.                 new Vector4f(tx, ty, tz, 1));
  45.     }
  46.  
  47.     Matrix4f maticaOtocX(float uhol) {
  48.         float cos = (float) (Math.cos(uhol));
  49.         float sin = (float) (Math.sin(uhol));
  50.         return new Matrix4f(new Vector4f(1, 0, 0, 0), new Vector4f(0, cos, sin, 0), new Vector4f(0, -sin, cos, 0),
  51.                 new Vector4f(0, 0, 0, 1));
  52.     }
  53.  
  54.     Matrix4f maticaOtocY(float uhol) {
  55.         float cos = (float) (Math.cos(uhol));
  56.         float sin = (float) (Math.sin(uhol));
  57.         return new Matrix4f(new Vector4f(cos, 0, sin, 0), new Vector4f(0, 1, 0, 0), new Vector4f(-sin, 0, cos, 0),
  58.                 new Vector4f(0, 0, 0, 1));
  59.     }
  60.  
  61.     Matrix4f maticaOtocZ(float uhol) {
  62.         float cos = (float) (Math.cos(uhol));
  63.         float sin = (float) (Math.sin(uhol));
  64.         return new Matrix4f(new Vector4f(cos, sin, 0, 0), new Vector4f(-sin, cos, 0, 0), new Vector4f(0, 0, 1, 0),
  65.                 new Vector4f(0, 0, 0, 1));
  66.     }
  67.  
  68.     boolean privratena(int[] s) {
  69.         // Vector3f A = vrcholy[s[0]];
  70.         // Vector3f B = vrcholy[s[1]];
  71.         // Vector3f C = vrcholy[s[2]];
  72.         Vector4f A_hom = cart2hom(vrcholy[s[0]]);
  73.         Vector4f A_trans = matica.multiply(A_hom);
  74.  
  75.         Vector4f B_hom = cart2hom(vrcholy[s[1]]);
  76.         Vector4f B_trans = matica.multiply(B_hom);
  77.  
  78.         Vector4f C_hom = cart2hom(vrcholy[s[2]]);
  79.         Vector4f C_trans = matica.multiply(C_hom);
  80.  
  81.         Vector4f u = A_trans.subtract(B_trans);
  82.         Vector4f v = C_trans.subtract(B_trans);
  83.        
  84.         Vector3f n = new Vector3f(u.y * v.z - u.z * v.y, u.z * v.x - u.x * v.z ,u.x * v.y - u.y * v.x);
  85.         Vector3f svetlo = new Vector3f(0, 1, 0);
  86.         double cosinus = (n.x*svetlo.x + n.y*svetlo.y + n.z*svetlo.z)/(Math.sqrt(n.x*n.x + n.y*n.y + n.z*n.z) * Math.sqrt(svetlo.x*svetlo.x + svetlo.y*svetlo.y + svetlo.z*svetlo.z));
  87.         if (n.z > 0) {
  88.             return true;
  89.         }
  90.         return false;
  91.     }
  92.  
  93.     void vykresliGL() {
  94.         // Clear the screen and depth buffer
  95.         glLoadIdentity();
  96.  
  97.         glPointSize(3);
  98.         glBegin(GL_QUADS);
  99.         glColor3f(1, 1, 1);
  100.         int poc = 0;
  101.         for (int[] s : steny) {
  102.             if (privratena(s)) {
  103.                 glColor3f(farby[poc].x, farby[poc].y, farby[poc].z);
  104.                 for (int v : s) {
  105.                     Vector4f v_hom = cart2hom(vrcholy[v]);
  106.                     Vector4f v_trans = matica.multiply(v_hom);
  107.                     Vector2f p = priemet(v_trans);
  108.                     glVertex2f(p.x, p.y);
  109.                 }
  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