Advertisement
LukacikPavel

cv06 ugr-katastrofa

Oct 29th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.69 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. import java.io.File;
  9. import java.io.FileNotFoundException;
  10. import java.util.Locale;
  11. import java.util.Scanner;
  12.  
  13. public class cv04 {
  14.     private int sirka = 800;
  15.     private int vyska = 600;
  16.  
  17.     Vector3f[] vrcholy = { new Vector3f(-0.5f, -0.5f, 0.5f), new Vector3f(0.5f, -0.5f, 0.5f),
  18.             new Vector3f(0.5f, -0.5f, -0.5f), new Vector3f(-0.5f, -0.5f, -0.5f), new Vector3f(-0.5f, 0.5f, 0.5f),
  19.             new Vector3f(0.5f, 0.5f, 0.5f), new Vector3f(0.5f, 0.5f, -0.5f), new Vector3f(-0.5f, 0.5f, -0.5f) };
  20.  
  21.     int[][] hrany = { { 0, 1 }, { 1, 2 }, { 2, 3 }, { 3, 0 }, { 4, 5 }, { 5, 6 }, { 6, 7 }, { 7, 4 }, { 0, 4 },
  22.             { 1, 5 }, { 2, 6 }, { 3, 7 } };
  23.  
  24.     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 } };
  25.  
  26.     Matrix4f matica;
  27.  
  28.     Vector4f cart2hom(Vector3f v) {
  29.         return new Vector4f(v.x, v.y, v.z, 1.0f);
  30.     }
  31.  
  32.     Vector2f priemet(Vector4f v) {
  33.         // return new Vector2f(v.x + v.z * 0.5f, v.y + v.z * 0.5f);
  34.         return new Vector2f(v.x, v.y);
  35.     }
  36.  
  37.     Vector3f[] farby = { new Vector3f(0, 1, 1), new Vector3f(1, 0, 1), new Vector3f(1, 1, 0), new Vector3f(1, 1, 1),
  38.             new Vector3f(1, 0, 0), new Vector3f(0, 0, 1), new Vector3f(0, 1, 0), new Vector3f(0.5f, 1, 1),
  39.             new Vector3f(1, 0.5f, 1), new Vector3f(1, 1, 0.5f), new Vector3f(1, 0, 0.5f),
  40.             new Vector3f(0.5f, 0.5f, 0.5f) };
  41.  
  42.     Matrix4f maticaVelkosti(float tx, float ty, float tz) {
  43.         return new Matrix4f(new Vector4f(tx, 0, 0, 0), new Vector4f(0, ty, 0, 0), new Vector4f(0, 0, tz, 0),
  44.                 new Vector4f(0, 0, 0, 1));
  45.     }
  46.  
  47.     Matrix4f maticaPos(float tx, float ty, float tz) {
  48.         return new Matrix4f(new Vector4f(1, 0, 0, 0), new Vector4f(0, 1, 0, 0), new Vector4f(0, 0, 1, 0),
  49.                 new Vector4f(tx, ty, tz, 1));
  50.     }
  51.  
  52.     Matrix4f maticaOtocX(float uhol) {
  53.         float cos = (float) (Math.cos(uhol));
  54.         float sin = (float) (Math.sin(uhol));
  55.         return new Matrix4f(new Vector4f(1, 0, 0, 0), new Vector4f(0, cos, sin, 0), new Vector4f(0, -sin, cos, 0),
  56.                 new Vector4f(0, 0, 0, 1));
  57.     }
  58.  
  59.     Matrix4f maticaOtocY(float uhol) {
  60.         float cos = (float) (Math.cos(uhol));
  61.         float sin = (float) (Math.sin(uhol));
  62.         return new Matrix4f(new Vector4f(cos, 0, sin, 0), new Vector4f(0, 1, 0, 0), new Vector4f(-sin, 0, cos, 0),
  63.                 new Vector4f(0, 0, 0, 1));
  64.     }
  65.  
  66.     Matrix4f maticaOtocZ(float uhol) {
  67.         float cos = (float) (Math.cos(uhol));
  68.         float sin = (float) (Math.sin(uhol));
  69.         return new Matrix4f(new Vector4f(cos, sin, 0, 0), new Vector4f(-sin, cos, 0, 0), new Vector4f(0, 0, 1, 0),
  70.                 new Vector4f(0, 0, 0, 1));
  71.     }
  72.  
  73.     boolean privratena(int[] s) {
  74.         // Vector3f A = vrcholy[s[0]];
  75.         // Vector3f B = vrcholy[s[1]];
  76.         // Vector3f C = vrcholy[s[2]];
  77.         Vector4f A_hom = cart2hom(vrcholy[s[0]]);
  78.         Vector4f A_trans = matica.multiply(A_hom);
  79.  
  80.         Vector4f B_hom = cart2hom(vrcholy[s[1]]);
  81.         Vector4f B_trans = matica.multiply(B_hom);
  82.  
  83.         Vector4f C_hom = cart2hom(vrcholy[s[2]]);
  84.         Vector4f C_trans = matica.multiply(C_hom);
  85.  
  86.         Vector4f u = A_trans.subtract(B_trans);
  87.         Vector4f v = C_trans.subtract(B_trans);
  88.  
  89.         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);
  90.         Vector3f svetlo = new Vector3f(0, 1, 0);
  91.         double cosinus = (n.x * svetlo.x + n.y * svetlo.y + n.z * svetlo.z)
  92.                 / (Math.sqrt(n.x * n.x + n.y * n.y + n.z * n.z)
  93.                         * Math.sqrt(svetlo.x * svetlo.x + svetlo.y * svetlo.y + svetlo.z * svetlo.z));
  94.         if (n.z > 0) {
  95.             return true;
  96.         }
  97.         return false;
  98.     }
  99.  
  100.     float[][] ZBuffer = new float[sirka][vyska];
  101.  
  102.     void trojuholnik(Vector4f troj[]) {
  103.         // y1<=y2<=y3
  104.         Vector4f pom;
  105.         if (troj[1].y < troj[0].y) {
  106.             pom = troj[1];
  107.             troj[1] = troj[0];
  108.             troj[0] = pom;
  109.         }
  110.  
  111.         // horna cast
  112.         int y1 = Math.round(troj[0].y);
  113.         int y2 = Math.round(troj[1].y);
  114.         int y3 = Math.round(troj[2].y);
  115.         int x1 = Math.round(troj[0].x);
  116.         int x2 = Math.round(troj[1].x);
  117.         int x3 = Math.round(troj[2].x);
  118.         int z1 = Math.round(troj[0].z);
  119.         int z2 = Math.round(troj[1].z);
  120.         int z3 = Math.round(troj[2].z);
  121.         float xL = Math.round(troj[0].x), xR = Math.round(troj[0].x);
  122.         float z = troj[0].z;
  123.         float zL = z, zR = z;
  124.         for (int y = y1; y < y2; y++) {
  125.             z = zL;
  126.             for (int x = Math.round(xL); x <= Math.round(xR); x++) {
  127.                 if (ZBuffer[x][y] < z) {
  128.                     glVertex2f(x, y);
  129.                     ZBuffer[x][y] = z;
  130.                 }
  131.                 z += (zR-zL)/(1.0f*xR-xL);
  132.             }
  133.             xL += (x2-x1)*1.0f/(y1-y2); xR += (x3-x1)*1.0f/(y3-y1);
  134.             zL += (z2-z1)*1.0f/(y1-y2); zR += (z3-z1)*1.0f/(y3-y1);
  135.         }
  136.         // dolna cast
  137.         for (int y = y2; y < y3; y++) {
  138.            
  139.         }
  140.     }
  141.  
  142.     void vykresliGL() {
  143.         // Clear the screen and depth buffer
  144.         glLoadIdentity();
  145.  
  146.         for (int x = 0; x < sirka; x++) {
  147.             for (int y = 0; y < vyska; y++) {
  148.                 ZBuffer[x][y] = -Float.MAX_VALUE;
  149.             }
  150.  
  151.         }
  152.  
  153.         // glPointSize(3);
  154.         glBegin(GL_QUADS);
  155.         glColor3f(1, 1, 1);
  156.         int poc = 0;
  157.         for (int[] s : steny) {
  158.  
  159.             // glColor3f(farby[poc].x, farby[poc].y, farby[poc].z);
  160.             Vector4f[] troj = new Vector4f[3];
  161.             troj[0] = matica.multiply(cart2hom(vrcholy[s[0]]));
  162.             troj[1] = matica.multiply(cart2hom(vrcholy[s[1]]));
  163.             troj[2] = matica.multiply(cart2hom(vrcholy[s[2]]));
  164.  
  165.             poc++;
  166.         }
  167.         glEnd();
  168.     }
  169.  
  170.     long window;
  171.     GLFWErrorCallback errorCallback;
  172.     GLFWKeyCallback keyCallback;
  173.  
  174.     void spusti() {
  175.         try {
  176.             init();
  177.             loop();
  178.  
  179.             glfwDestroyWindow(window);
  180.             keyCallback.free();
  181.         } finally {
  182.             glfwTerminate();
  183.             errorCallback.free();
  184.         }
  185.     }
  186.  
  187.     void init() {
  188.         glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
  189.         if (!glfwInit())
  190.             throw new IllegalStateException("Chyba pri inicializacii GLFW!!!");
  191.  
  192.         window = glfwCreateWindow(sirka, vyska, "UGR1", NULL, NULL);
  193.         if (window == NULL)
  194.             throw new RuntimeException("Chyba pri vytvoreni GLFW okna!!!");
  195.  
  196.         glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
  197.             @Override
  198.             public void invoke(long window, int key, int scancode, int action, int mods) {
  199.                 if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
  200.                     glfwSetWindowShouldClose(window, true);
  201.                 if (key == GLFW_KEY_RIGHT)
  202.                     matica = matica.multiply(maticaPos(0.01f, 0, 0));
  203.                 if (key == GLFW_KEY_LEFT)
  204.                     matica = matica.multiply(maticaPos(-0.01f, 0, 0));
  205.                 if (key == GLFW_KEY_UP)
  206.                     matica = matica.multiply(maticaPos(0, 0.01f, 0));
  207.                 if (key == GLFW_KEY_DOWN)
  208.                     matica = matica.multiply(maticaPos(0, -0.01f, 0));
  209.                 if (key == GLFW_KEY_N)
  210.                     matica = matica.multiply(maticaPos(0, 0, 0.01f));
  211.                 if (key == GLFW_KEY_M)
  212.                     matica = matica.multiply(maticaPos(0, 0, -0.01f));
  213.                 if (key == GLFW_KEY_W)
  214.                     matica = matica.multiply(maticaOtocX(0.05f));
  215.                 if (key == GLFW_KEY_S)
  216.                     matica = matica.multiply(maticaOtocX(-0.05f));
  217.                 if (key == GLFW_KEY_A)
  218.                     matica = matica.multiply(maticaOtocY(-0.05f));
  219.                 if (key == GLFW_KEY_D)
  220.                     matica = matica.multiply(maticaOtocY(0.05f));
  221.                 if (key == GLFW_KEY_Q)
  222.                     matica = matica.multiply(maticaOtocZ(0.05f));
  223.                 if (key == GLFW_KEY_E)
  224.                     matica = matica.multiply(maticaOtocZ(-0.05f));
  225.                 if (key == GLFW_KEY_X)
  226.                     matica = matica.multiply(maticaVelkosti(1.1f, 1.1f, 1.1f));
  227.                 if (key == GLFW_KEY_Z)
  228.                     matica = matica.multiply(maticaVelkosti(1 / 1.1f, 1 / 1.1f, 1 / 1.1f));
  229.             }
  230.         });
  231.  
  232.         GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  233.         glfwSetWindowPos(window, (vidmode.width() - sirka) / 2, (vidmode.height() - vyska) / 2);
  234.  
  235.         glfwMakeContextCurrent(window);
  236.         glfwSwapInterval(0);
  237.         glfwShowWindow(window);
  238.  
  239.         GL.createCapabilities();
  240.  
  241.         matica = new Matrix4f();
  242.         matica.setIdentity();
  243.         try {
  244.             Scanner sc = new Scanner(new File("01.txt")).useLocale(Locale.ENGLISH);
  245.             int n = sc.nextInt();
  246.             vrcholy = new Vector3f[n];
  247.             for (int i = 0; i < vrcholy.length; i++) {
  248.                 vrcholy[i] = new Vector3f(sc.nextFloat(), sc.nextFloat(), sc.nextFloat());
  249.             }
  250.             int m = sc.nextInt();
  251.             steny = new int[m][3];
  252.             for (int i = 0; i < steny.length; i++) {
  253.                 steny[i] = new int[3];
  254.                 steny[i][0] = sc.nextInt() - 1;
  255.                 steny[i][1] = sc.nextInt() - 1;
  256.                 steny[i][2] = sc.nextInt() - 1;
  257.             }
  258.         } catch (Exception e) {
  259.             // TODO: handle exception
  260.         }
  261.  
  262.     }
  263.  
  264.     void loop() {
  265.         glViewport(0, 0, sirka, vyska);
  266.  
  267.         glMatrixMode(GL_PROJECTION);
  268.         glLoadIdentity();
  269.         glOrtho(-1, 1, -1, 1, 0, 1);
  270.  
  271.         glMatrixMode(GL_MODELVIEW);
  272.         glLoadIdentity();
  273.  
  274.         glClearColor(0.f, 0.f, 0.f, 1.f); // Initialize clear color
  275.  
  276.         while (!glfwWindowShouldClose(window)) {
  277.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  278.             vykresliGL();
  279.  
  280.             glfwSwapBuffers(window);
  281.  
  282.             glfwPollEvents();
  283.         }
  284.     }
  285.  
  286.     public static void main(String[] args) {
  287.         new cv04().spusti();
  288.     }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement