LukacikPavel

ugr10

Nov 26th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.06 KB | None | 0 0
  1.  
  2. import org.lwjgl.glfw.*;
  3. import org.lwjgl.opengl.GL;
  4. import org.lwjgl.opengl.GL12;
  5. import org.lwjgl.system.MemoryStack;
  6. import static org.lwjgl.stb.STBImage.*;
  7. import static org.lwjgl.stb.STBImageResize.*;
  8.  
  9. import static org.lwjgl.glfw.GLFW.*;
  10. import static org.lwjgl.opengl.GL11.*;
  11. import static org.lwjgl.system.MemoryUtil.*;
  12. import static org.lwjgl.system.MemoryStack.*;
  13. import static org.lwjgl.BufferUtils.*;
  14. import static org.lwjgl.opengl.ARBMultitexture.*;
  15.  
  16. import java.io.InputStream;
  17. import java.nio.*;
  18. import java.nio.channels.*;
  19. import java.nio.file.*;
  20.  
  21. import java.io.*;
  22.  
  23. public class cv091 {
  24.  
  25.     private int pfupjs;
  26.     private int stena;
  27.     private int podstava;
  28.     private int voda;
  29.     private int back, bottom, top, left, right, front;
  30.     private double posX = 0;
  31.     private double posZ = 0;
  32.     private double uhol = 0;
  33.     private double x = 0;
  34.     private double z = 0.5;
  35.     private float fogColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
  36.     float[] farba_ambient = {1.0f, 1.0f, 1.0f, 1.0f};
  37.     float[] farba_diffuse = {1.0f, 1.0f, 1.0f, 1.0f};
  38.     float[] pozicia = {0, 0, 0, 1};
  39.     float[] farba_red = {1.0f, 0f, 0f, 1.0f};
  40.     float[] farba_green = {0f, 1.0f, 0f, 1.0f};
  41.     float[] farba_blue = {0f, 0f, 1.0f, 1.0f};
  42.  
  43.     private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) {
  44.         ByteBuffer newBuffer = org.lwjgl.BufferUtils.createByteBuffer(newCapacity);
  45.         buffer.flip();
  46.         newBuffer.put(buffer);
  47.         return newBuffer;
  48.     }
  49.  
  50.     private ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
  51.         ByteBuffer buffer;
  52.  
  53.         Path path = Paths.get(resource);
  54.         if (Files.isReadable(path)) {
  55.             try (SeekableByteChannel fc = Files.newByteChannel(path)) {
  56.                 buffer = org.lwjgl.BufferUtils.createByteBuffer((int) fc.size() + 1);
  57.                 while (fc.read(buffer) != -1) {
  58.                     ;
  59.                 }
  60.             }
  61.         } else {
  62.             try (InputStream source = this.getClass().getClassLoader().getResourceAsStream(resource);
  63.                     ReadableByteChannel rbc = Channels.newChannel(source)) {
  64.                 buffer = createByteBuffer(bufferSize);
  65.  
  66.                 while (true) {
  67.                     int bytes = rbc.read(buffer);
  68.                     if (bytes == -1) {
  69.                         break;
  70.                     }
  71.                     if (buffer.remaining() == 0) {
  72.                         buffer = resizeBuffer(buffer, buffer.capacity() * 3 / 2); // 50%
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.  
  78.         buffer.flip();
  79.         return buffer.slice();
  80.     }
  81.  
  82.     private int createTexture(String Path) {
  83.         int texID = glGenTextures();
  84.         ByteBuffer imageBuffer;
  85.         try {
  86.             imageBuffer = ioResourceToByteBuffer(Path, 8 * 1024);
  87.         } catch (Exception e) {
  88.             throw new RuntimeException(e);
  89.         }
  90.         int w, h, comp;
  91.         ByteBuffer image;
  92.         try (MemoryStack stack = stackPush()) {
  93.             IntBuffer wB = stack.mallocInt(1);
  94.             IntBuffer hB = stack.mallocInt(1);
  95.             IntBuffer compB = stack.mallocInt(1);
  96.  
  97.             // Use info to read image metadata without decoding the entire image.
  98.             // We don't need this for this demo, just testing the API.
  99.             if (!stbi_info_from_memory
  100.  
  101.             (imageBuffer, wB, hB, compB)) {
  102.                 throw new RuntimeException("Failed to read image information: " + stbi_failure_reason());
  103.             }
  104.  
  105.             /*
  106.              * System.out.println("Image width: " + wB.get(0));
  107.              * System.out.println("Image height: " + hB.get(0));
  108.              * System.out.println("Image components: " + compB.get(0));
  109.              * System.out.println("Image HDR: " + stbi_is_hdr_from_memory(imageBuffer));
  110.              */
  111.  
  112.             // Decode the image
  113.             image = stbi_load_from_memory(imageBuffer, wB, hB, compB, 0);
  114.             if (image == null) {
  115.                 throw new RuntimeException("Failed to load image: " + stbi_failure_reason());
  116.             }
  117.  
  118.             w = wB.get(0);
  119.             h = hB.get(0);
  120.             comp = compB.get(0);
  121.         }
  122.  
  123.         glBindTexture(GL_TEXTURE_2D, texID);
  124.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  125.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  126. //          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  127. //          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  128.  
  129.         int format;
  130.  
  131.         if (comp == 3) {
  132.             if ((w & 3) != 0) {
  133.                 glPixelStorei(GL_UNPACK_ALIGNMENT, 2 - (w & 1));
  134.             }
  135.             format = GL_RGB;
  136.         } else {
  137. //              premultiplyAlpha();
  138. //              glEnable(GL_BLEND);
  139. //              glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  140.  
  141.             format = GL_RGBA;
  142.         }
  143.  
  144.         glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, image);
  145.  
  146.         ByteBuffer input_pixels = image;
  147.         int input_w = w;
  148.         int input_h = h;
  149.         int mipmapLevel = 0;
  150.         while (1 < input_w || 1 < input_h) {
  151.             int output_w = Math.max(1, input_w >> 1);
  152.             int output_h = Math.max(1, input_h >> 1);
  153.  
  154.             ByteBuffer output_pixels = memAlloc(output_w * output_h * comp);
  155.             stbir_resize_uint8_generic(input_pixels, input_w, input_h, input_w * comp, output_pixels, output_w,
  156.                     output_h, output_w * comp, comp, comp == 4 ? 3 : STBIR_ALPHA_CHANNEL_NONE,
  157.                     STBIR_FLAG_ALPHA_PREMULTIPLIED, STBIR_EDGE_CLAMP, STBIR_FILTER_MITCHELL, STBIR_COLORSPACE_SRGB);
  158.  
  159.             if (mipmapLevel == 0) {
  160.                 stbi_image_free(image);
  161.             } else {
  162.                 memFree(input_pixels);
  163.             }
  164.  
  165.             glTexImage2D(GL_TEXTURE_2D, ++mipmapLevel, format, output_w, output_h, 0, format, GL_UNSIGNED_BYTE,
  166.                     output_pixels);
  167.  
  168.             input_pixels = output_pixels;
  169.             input_w = output_w;
  170.             input_h = output_h;
  171.         }
  172.         if (mipmapLevel == 0) {
  173.             stbi_image_free(image);
  174.         } else {
  175.             memFree(input_pixels);
  176.         }
  177.  
  178.         return texID;
  179.     }
  180.  
  181.     private int sirka = 800;
  182.     private int vyska = 600;
  183.  
  184.     Vector3f[] vrcholy = { new Vector3f(-1, 0, +1), new Vector3f(+1, 0, +1), new Vector3f(+1, 0, -1),
  185.             new Vector3f(-1, 0, -1), new Vector3f(0, 2, 0), };
  186.  
  187.     Vector3f[] farba = { new Vector3f(1, 0, 0), new Vector3f(0, 1, 0), new Vector3f(0, 0, 1), new Vector3f(1, 0, 1),
  188.             new Vector3f(1, 1, 0), new Vector3f(0, 1, 1) };
  189.  
  190.     int[][] steny = { { 0, 1, 4 }, { 1, 2, 4 }, { 2, 3, 4 }, { 3, 0, 4 }, { 0, 3, 2, 1 } };
  191.  
  192.     double t = 0;
  193.  
  194.     void vykresliGL() {
  195.         // glLoadIdentity();
  196.         // glRotated(-0.03, 1, 1, 1);
  197.  
  198.         // kockaPF();
  199.         // pyramida();
  200.  
  201. //      t += 0.0001;
  202. //      glTranslated(-1, 0, 0);
  203. //      trojuholnik(voda, t);
  204. //      glTranslated(2, 0, 0);
  205. //      trojuholnik(stena, 0);
  206. //      glTranslated(-1, -2, 0);
  207. //      trojuholnik(stena, voda, t);
  208. //      glTranslated(0, 2, 0);
  209.  
  210.         kocka();
  211.  
  212.         for (int ax = -50; ax <= 50; ax += 2) {
  213.             for (int az = -50; az <= 50; az += 2) {
  214.                 if (Math.abs(ax + az) % 7 == Math.abs(ax - az) % 5) {
  215.                     ihlan(ax, az);
  216.                 } else {
  217.                     podlaha(ax, az);
  218.                 }
  219.             }
  220.         }
  221.        
  222.         glPointSize(100);
  223.         glBegin(GL_POINTS);
  224.         glVertex3d(pozicia[0], 0, pozicia[3]);
  225.         glEnd();
  226.     }
  227.  
  228.     private void podlaha(int ax, int az) {
  229.         glBindTexture(GL_TEXTURE_2D, podstava);
  230.         glBegin(GL_QUADS);
  231.         glTexCoord2d(0, 0);
  232.         glVertex3f(ax - 1f, 0f, az + 1f);
  233.         glTexCoord2d(1, 0);
  234.         glVertex3f(ax + 1f, 0, az + 1f);
  235.         glTexCoord2d(1, 1);
  236.         glVertex3f(ax + 1f, 0, az - 1f);
  237.         glTexCoord2d(0, 1);
  238.         glVertex3f(ax - 1f, 0, az - 1f);
  239.         glEnd();
  240.     }
  241.  
  242.     private void ihlan(int ax, int az) {
  243.         glBindTexture(GL_TEXTURE_2D, stena);
  244.         glBegin(GL_TRIANGLES);
  245.         glTexCoord2d(0, 1);
  246.         glNormal3f(ax - 1.0f, 0.0f, az + 1.0f);
  247.         glVertex3f(ax - 1.0f, 0.0f, az + 1.0f);
  248.         glTexCoord2d(1, 1);
  249.         glVertex3f(ax + 1.0f, 0.0f, az + 1.0f);
  250.         glTexCoord2d(0.5, 0);
  251.         glVertex3f(ax + 0.0f, 2.0f, az + 0.0f);
  252.  
  253.         glTexCoord2d(0, 1);
  254.         glNormal3f(ax + 1.0f, 0.0f, az + 1.0f);
  255.         glVertex3f(ax + 1.0f, 0.0f, az + 1.0f);
  256.         glTexCoord2d(1, 1);
  257.         glVertex3f(ax + 1.0f, 0.0f, az - 1.0f);
  258.         glTexCoord2d(0.5, 0);
  259.         glVertex3f(ax + 0.0f, 2.0f, az + 0.0f);
  260.  
  261.         glTexCoord2d(0, 1);
  262.         glNormal3f(ax + 1.0f, 0.0f, az - 1.0f);
  263.         glVertex3f(ax + 1.0f, 0.0f, az - 1.0f);
  264.         glTexCoord2d(1, 1);
  265.         glVertex3f(ax - 1.0f, 0.0f, az - 1.0f);
  266.         glTexCoord2d(0.5, 0);
  267.         glVertex3f(ax + 0.0f, 2.0f, az + 0.0f);
  268.  
  269.         glTexCoord2d(0, 1);
  270.         glNormal3f(ax - 1.0f, 0.0f, az - 1.0f);
  271.         glVertex3f(ax - 1.0f, 0.0f, az - 1.0f);
  272.         glTexCoord2d(1, 1);
  273.         glVertex3f(ax - 1.0f, 0.0f, az + 1.0f);
  274.         glTexCoord2d(0.5, 0);
  275.         glVertex3f(ax + 0.0f, 2.0f, az + 0.0f);
  276.         glEnd();
  277.  
  278.         glBindTexture(GL_TEXTURE_2D, podstava);
  279.         glBegin(GL_QUADS);
  280.         glTexCoord2d(0, 0);
  281.         glVertex3f(ax - 1.0f, 0.0f, az - 1.0f);
  282.         glTexCoord2d(1, 0);
  283.         glVertex3f(ax + 1.0f, 0.0f, az - 1.0f);
  284.         glTexCoord2d(1, 1);
  285.         glVertex3f(ax + 1.0f, 0.0f, az + 1.0f);
  286.         glTexCoord2d(0, 1);
  287.         glVertex3f(ax - 1.0f, 0.0f, az + 1.0f);
  288.         glEnd();
  289.  
  290.     }
  291.  
  292.     void trojuholnik(int textura, double t) {
  293.         glActiveTextureARB(GL_TEXTURE0_ARB);
  294.         glBindTexture(GL_TEXTURE_2D, textura);
  295.         glEnable(GL_TEXTURE_2D);
  296.  
  297.         glBegin(GL_TRIANGLES);
  298.         glTexCoord2d(0 + t, 1 + t);
  299.         glVertex2f(-1.0f, 0.0f);
  300.         glTexCoord2d(1 + t, 1 + t);
  301.         glVertex2f(1.0f, 0.0f);
  302.         glTexCoord2d(0.5 + t, 0 + t);
  303.         glVertex2f(0.0f, 2.0f);
  304.         glEnd();
  305.  
  306.         glActiveTextureARB(GL_TEXTURE0_ARB);
  307.         glDisable(GL_TEXTURE_2D);
  308.     }
  309.  
  310.     void trojuholnik(int textura1, int textura2, double t) {
  311.         glActiveTextureARB(GL_TEXTURE1_ARB);
  312.         glBindTexture(GL_TEXTURE_2D, textura1);
  313.         glEnable(GL_TEXTURE_2D);
  314.  
  315.         glActiveTextureARB(GL_TEXTURE2_ARB);
  316.         glBindTexture(GL_TEXTURE_2D, textura2);
  317.         glEnable(GL_TEXTURE_2D);
  318.  
  319.         glBegin(GL_TRIANGLES);
  320.         glMultiTexCoord2dARB(GL_TEXTURE1_ARB, 0, 1);
  321.         glMultiTexCoord2dARB(GL_TEXTURE2_ARB, 0 + t, 1 + t);
  322.         glVertex2f(-1.0f, 0.0f);
  323.         glMultiTexCoord2dARB(GL_TEXTURE1_ARB, 1, 1);
  324.         glMultiTexCoord2dARB(GL_TEXTURE2_ARB, 1 + t, 1 + t);
  325.         glVertex2f(1.0f, 0.0f);
  326.         glMultiTexCoord2dARB(GL_TEXTURE1_ARB, 0.5, 0);
  327.         glMultiTexCoord2dARB(GL_TEXTURE2_ARB, 0.5 + t, 0 + t);
  328.         glVertex2f(0.0f, 2.0f);
  329.         glEnd();
  330.  
  331.         glActiveTextureARB(GL_TEXTURE1_ARB);
  332.         glDisable(GL_TEXTURE_2D);
  333.  
  334.         glActiveTextureARB(GL_TEXTURE2_ARB);
  335.         glDisable(GL_TEXTURE_2D);
  336.     }
  337.  
  338.     void kockaPF() {
  339.         glBindTexture(GL_TEXTURE_2D, pfupjs);
  340.         glBegin(GL_QUADS);
  341.         glTexCoord2d(0, 1);
  342.         glVertex3d(-1, -1, 0);
  343.         glTexCoord2d(1, 1);
  344.         glVertex3d(+1, -1, 0);
  345.         glTexCoord2d(1, 0);
  346.         glVertex3d(+1, +1, 0);
  347.         glTexCoord2d(0, 0);
  348.         glVertex3d(-1, +1, 0);
  349.  
  350.         glTexCoord2d(0, 2);
  351.         glVertex3d(-1, -1, -2);
  352.         glTexCoord2d(2, 2);
  353.         glVertex3d(-1, -1, 0);
  354.         glTexCoord2d(2, 0);
  355.         glVertex3d(-1, 1, 0);
  356.         glTexCoord2d(0, 0);
  357.         glVertex3d(-1, 1, -2);
  358.  
  359.         glTexCoord2d(0, 0);
  360.         glVertex3d(-1, -1, -2);
  361.         glTexCoord2d(0, 3);
  362.         glVertex3d(-1, 1, -2);
  363.         glTexCoord2d(3, 3);
  364.         glVertex3d(1, 1, -2);
  365.         glTexCoord2d(3, 0);
  366.         glVertex3d(1, -1, -2);
  367.  
  368.         glTexCoord2d(0, 4);
  369.         glVertex3d(1, -1, 0);
  370.         glTexCoord2d(4, 4);
  371.         glVertex3d(1, -1, -2);
  372.         glTexCoord2d(4, 0);
  373.         glVertex3d(1, 1, -2);
  374.         glTexCoord2d(0, 0);
  375.         glVertex3d(1, 1, 0);
  376.  
  377.         glTexCoord2d(0, 5);
  378.         glVertex3d(-1, 1, 0);
  379.         glTexCoord2d(5, 5);
  380.         glVertex3d(1, 1, 0);
  381.         glTexCoord2d(5, 0);
  382.         glVertex3d(1, 1, -2);
  383.         glTexCoord2d(0, 0);
  384.         glVertex3d(-1, 1, -2);
  385.  
  386.         glTexCoord2d(0, 6);
  387.         glVertex3d(-1, -1, -2);
  388.         glTexCoord2d(6, 6);
  389.         glVertex3d(1, -1, -2);
  390.         glTexCoord2d(6, 0);
  391.         glVertex3d(1, -1, 0);
  392.         glTexCoord2d(0, 0);
  393.         glVertex3d(-1, -1, 0);
  394.  
  395.         glEnd();
  396.     }
  397.  
  398.     void kocka() {
  399.         // back
  400.         glBindTexture(GL_TEXTURE_2D, front);
  401.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  402.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  403.         glBegin(GL_QUADS);
  404.         glTexCoord2d(0, 1);
  405.         glVertex3d(50, -50, 50);
  406.         glTexCoord2d(1, 1);
  407.         glVertex3d(-50, -50, 50);
  408.         glTexCoord2d(1, 0);
  409.         glVertex3d(-50, 50, 50);
  410.         glTexCoord2d(0, 0);
  411.         glVertex3d(50, 50, 50);
  412.         glEnd();
  413.  
  414.         // left
  415.         glBindTexture(GL_TEXTURE_2D, left);
  416.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  417.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  418.         glBegin(GL_QUADS);
  419.         glTexCoord2d(0, 1);
  420.         glVertex3d(-50, -50, 50);
  421.         glTexCoord2d(1, 1);
  422.         glVertex3d(-50, -50, -50);
  423.         glTexCoord2d(1, 0);
  424.         glVertex3d(-50, 50, -50);
  425.         glTexCoord2d(0, 0);
  426.         glVertex3d(-50, 50, 50);
  427.         glEnd();
  428.  
  429.         // front
  430.         glBindTexture(GL_TEXTURE_2D, back);
  431.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  432.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  433.         glBegin(GL_QUADS);
  434.         glTexCoord2d(0, 1);
  435.         glVertex3d(-50, -50, -50);
  436.         glTexCoord2d(1, 1);
  437.         glVertex3d(50, -50, -50);
  438.         glTexCoord2d(1, 0);
  439.         glVertex3d(50, 50, -50);
  440.         glTexCoord2d(0, 0);
  441.         glVertex3d(-50, 50, -50);
  442.         glEnd();
  443.  
  444.         // right
  445.         glBindTexture(GL_TEXTURE_2D, right);
  446.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  447.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  448.         glBegin(GL_QUADS);
  449.         glTexCoord2d(0, 1);
  450.         glVertex3d(50, -50, -50);
  451.         glTexCoord2d(1, 1);
  452.         glVertex3d(50, -50, 50);
  453.         glTexCoord2d(1, 0);
  454.         glVertex3d(50, 50, 50);
  455.         glTexCoord2d(0, 0);
  456.         glVertex3d(50, 50, -50);
  457.         glEnd();
  458.  
  459.         // top
  460.         glBindTexture(GL_TEXTURE_2D, top);
  461.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  462.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  463.         glBegin(GL_QUADS);
  464.         glTexCoord2d(0, 1);
  465.         glVertex3d(-50, 50, -50);
  466.         glTexCoord2d(1, 1);
  467.         glVertex3d(50, 50, -50);
  468.         glTexCoord2d(1, 0);
  469.         glVertex3d(50, 50, 50);
  470.         glTexCoord2d(0, 0);
  471.         glVertex3d(-50, 50, 50);
  472.         glEnd();
  473.  
  474.         // bottom
  475.         glBindTexture(GL_TEXTURE_2D, bottom);
  476.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
  477.         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
  478.         glBegin(GL_QUADS);
  479.         glTexCoord2d(0, 1);
  480.         glVertex3d(-50, -50, 50);
  481.         glTexCoord2d(1, 1);
  482.         glVertex3d(50, -50, 50);
  483.         glTexCoord2d(1, 0);
  484.         glVertex3d(50, -50, -50);
  485.         glTexCoord2d(0, 0);
  486.         glVertex3d(-50, -50, -50);
  487.         glEnd();
  488.     }
  489.  
  490.     void pyramidaVoda() {
  491.         glBindTexture(GL_TEXTURE_2D, voda);
  492.         glBegin(GL_TRIANGLES);
  493.         glTexCoord2d(0, 1);
  494.         glVertex3f(-1.0f, 0.0f, 1.0f);
  495.         glTexCoord2d(1, 1);
  496.         glVertex3f(1.0f, 0.0f, 1.0f);
  497.         glTexCoord2d(0.5, 0);
  498.         glVertex3f(0.0f, 2.0f, 0.0f);
  499.  
  500.         glTexCoord2d(0, 1);
  501.         glVertex3f(1.0f, 0.0f, 1.0f);
  502.         glTexCoord2d(1, 1);
  503.         glVertex3f(1.0f, 0.0f, -1.0f);
  504.         glTexCoord2d(0.5, 0);
  505.         glVertex3f(0.0f, 2.0f, 0.0f);
  506.  
  507.         glTexCoord2d(0, 1);
  508.         glVertex3f(1.0f, 0.0f, -1.0f);
  509.         glTexCoord2d(1, 1);
  510.         glVertex3f(-1.0f, 0.0f, -1.0f);
  511.         glTexCoord2d(0.5, 0);
  512.         glVertex3f(0.0f, 2.0f, 0.0f);
  513.  
  514.         glTexCoord2d(0, 1);
  515.         glVertex3f(-1.0f, 0.0f, -1.0f);
  516.         glTexCoord2d(1, 1);
  517.         glVertex3f(-1.0f, 0.0f, 1.0f);
  518.         glTexCoord2d(0.5, 0);
  519.         glVertex3f(0.0f, 2.0f, 0.0f);
  520.         glEnd();
  521.  
  522.         glBegin(GL_QUADS);
  523.         glTexCoord2d(0, 0);
  524.         glVertex3f(-1.0f, 0.0f, -1.0f);
  525.         glTexCoord2d(1, 0);
  526.         glVertex3f(1.0f, 0.0f, -1.0f);
  527.         glTexCoord2d(1, 1);
  528.         glVertex3f(1.0f, 0.0f, 1.0f);
  529.         glTexCoord2d(0, 1);
  530.         glVertex3f(-1.0f, 0.0f, 1.0f);
  531.         glEnd();
  532.     }
  533.  
  534.     void pyramida() {
  535.         glBindTexture(GL_TEXTURE_2D, stena);
  536.         glBegin(GL_TRIANGLES);
  537.         glTexCoord2d(0, 1);
  538.         glVertex3f(-1.0f, 0.0f, 1.0f);
  539.         glTexCoord2d(1, 1);
  540.         glVertex3f(1.0f, 0.0f, 1.0f);
  541.         glTexCoord2d(0.5, 0);
  542.         glVertex3f(0.0f, 2.0f, 0.0f);
  543.  
  544.         glTexCoord2d(0, 1);
  545.         glVertex3f(1.0f, 0.0f, 1.0f);
  546.         glTexCoord2d(1, 1);
  547.         glVertex3f(1.0f, 0.0f, -1.0f);
  548.         glTexCoord2d(0.5, 0);
  549.         glVertex3f(0.0f, 2.0f, 0.0f);
  550.  
  551.         glTexCoord2d(0, 1);
  552.         glVertex3f(1.0f, 0.0f, -1.0f);
  553.         glTexCoord2d(1, 1);
  554.         glVertex3f(-1.0f, 0.0f, -1.0f);
  555.         glTexCoord2d(0.5, 0);
  556.         glVertex3f(0.0f, 2.0f, 0.0f);
  557.  
  558.         glTexCoord2d(0, 1);
  559.         glVertex3f(-1.0f, 0.0f, -1.0f);
  560.         glTexCoord2d(1, 1);
  561.         glVertex3f(-1.0f, 0.0f, 1.0f);
  562.         glTexCoord2d(0.5, 0);
  563.         glVertex3f(0.0f, 2.0f, 0.0f);
  564.         glEnd();
  565.  
  566.         glBindTexture(GL_TEXTURE_2D, podstava);
  567.         glBegin(GL_QUADS);
  568.         glTexCoord2d(0, 0);
  569.         glVertex3f(-1.0f, 0.0f, -1.0f);
  570.         glTexCoord2d(1, 0);
  571.         glVertex3f(1.0f, 0.0f, -1.0f);
  572.         glTexCoord2d(1, 1);
  573.         glVertex3f(1.0f, 0.0f, 1.0f);
  574.         glTexCoord2d(0, 1);
  575.         glVertex3f(-1.0f, 0.0f, 1.0f);
  576.         glEnd();
  577.     }
  578.  
  579.     long window;
  580.     GLFWErrorCallback errorCallback;
  581.     GLFWKeyCallback keyCallback;
  582.  
  583.     void spusti() {
  584.         try {
  585.             init();
  586.             loop();
  587.  
  588.             glfwDestroyWindow(window);
  589.             keyCallback.free();
  590.         } finally {
  591.             glfwTerminate();
  592.             errorCallback.free();
  593.         }
  594.     }
  595.  
  596.     void init() {
  597.         glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
  598.         if (!glfwInit())
  599.             throw new IllegalStateException("Chyba pri inicializacii GLFW!!!");
  600.  
  601.         window = glfwCreateWindow(sirka, vyska, "UGR1", NULL, NULL);
  602.         if (window == NULL)
  603.             throw new RuntimeException("Chyba pri vytvoreni GLFW okna!!!");
  604.  
  605.         glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
  606.             @Override
  607.             public void invoke(long window, int key, int scancode, int action, int mods) {
  608.                 if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
  609.                     glfwSetWindowShouldClose(window, true);
  610.                 if (key == GLFW_KEY_W) {
  611.                     glTranslated(x, 0, z);
  612.                     posX += x;
  613.                     posZ += z;
  614.  
  615.                     System.out.println("posX = " + posX + " posZ = " + posZ);
  616.                 }
  617.                 if (key == GLFW_KEY_S) {
  618.                     glTranslated(-x, 0, -z);
  619.                     posX -= x;
  620.                     posZ -= z;
  621.                     System.out.println("posX = " + posX + " posZ = " + posZ);
  622.                 }
  623.                 if (key == GLFW_KEY_A) {
  624.                     glTranslated(-posX, 0, -posZ);
  625.                     glRotated(-1, 0, 1, 0);
  626.                     glTranslated(posX, 0, posZ);
  627.                     if (uhol == -180) {
  628.                         uhol += 360;
  629.                     }
  630.                     uhol -= 1;
  631.                     x = -0.5 * Math.sin(Math.toRadians(uhol));
  632.                     z = 0.5 * Math.cos(Math.toRadians(uhol));
  633.                     System.out.println(uhol);
  634.                 }
  635.                 if (key == GLFW_KEY_D) {
  636.                     glTranslated(-posX, 0, -posZ);
  637.                     glRotated(1, 0, 1, 0);
  638.                     glTranslated(posX, 0, posZ);
  639.                     if (uhol == 180) {
  640.                         uhol -= 360;
  641.                     }
  642.                     uhol += 1;
  643.                     x = -0.5 * Math.sin(Math.toRadians(uhol));
  644.                     z = 0.5 * Math.cos(Math.toRadians(uhol));
  645.                     System.out.println(uhol);
  646.                 }
  647.                 if (key == GLFW_KEY_Z) {
  648.                     glTranslated(0, -1, 0);
  649.                 }
  650.                 if (key == GLFW_KEY_X) {
  651.                     glTranslated(0, 1, 0);
  652.                 }
  653.                 if (key == GLFW_KEY_C) {
  654.                     glRotated(1, -1, 0, 0);
  655.                 }
  656.                 if (key == GLFW_KEY_V) {
  657.                     glRotated(1, 1, 0, 0);
  658.                 }
  659.             }
  660.         });
  661.  
  662.         GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  663.         glfwSetWindowPos(window, (vidmode.width() - sirka) / 2, (vidmode.height() - vyska) / 2);
  664.  
  665.         glfwMakeContextCurrent(window);
  666.         glfwSwapInterval(0);
  667.         glfwShowWindow(window);
  668.  
  669.         GL.createCapabilities();
  670.  
  671.         glEnable(GL_TEXTURE_2D);
  672.         // glBindTexture(GL_TEXTURE_2D, createTexture("D:\\pfupjs.png"));
  673.         pfupjs = createTexture("D:\\pfupjs.png");
  674.         stena = createTexture("D:\\stena.jpg");
  675.         podstava = createTexture("D:\\podstava.jpg");
  676.         voda = createTexture("D:\\voda.jpg");
  677.         back = createTexture("D:\\back.jpg");
  678.         bottom = createTexture("D:\\bottom.jpg");
  679.         top = createTexture("D:\\top.jpg");
  680.         left = createTexture("D:\\left.jpg");
  681.         right = createTexture("D:\\right.jpg");
  682.         front = createTexture("D:\\front.jpg");
  683.        
  684.         glClearColor(fogColor[0], fogColor[1], fogColor[2], fogColor[3]);
  685.         glFogi(GL_FOG_MODE, GL_LINEAR); //GL_EXP, GL_EXP2, GL_LINEAR
  686.         glFogfv(GL_FOG_COLOR, fogColor);
  687.         glHint(GL_FOG_HINT, GL_DONT_CARE);
  688.  
  689.         //near/far (GL_LINEAR);
  690.         glFogf(GL_FOG_START, 10f);
  691.         glFogf(GL_FOG_END, 20f);
  692.         //density (GL_EXP/GL_EXP2);
  693.         glFogf(GL_FOG_DENSITY, 0.5f);
  694.  
  695.         //glEnable(GL_FOG);
  696.        
  697.         glLightfv(GL_LIGHT0, GL_AMBIENT, farba_ambient);
  698.         glLightfv(GL_LIGHT0, GL_DIFFUSE, farba_diffuse);
  699.         glLightfv(GL_LIGHT0, GL_POSITION, pozicia);
  700.         glEnable(GL_LIGHTING); //Zapnutie osvetlenia
  701.         glEnable(GL_LIGHT0); //Zapnutie prvého svetla
  702.         glEnable(GL_NORMALIZE);
  703.  
  704.     }
  705.  
  706.     private static void gluPerspective(double fov, double aspect, double zNear, double zFar) {
  707.         double fH = Math.tan(fov * Math.PI / 360.0) * zNear;
  708.         double fW = fH * aspect;
  709.         glFrustum(-fW, fW, -fH, fH, zNear, zFar);
  710.     }
  711.  
  712.     void loop() {
  713.         glViewport(0, 0, sirka, vyska);
  714.  
  715.         glMatrixMode(GL_PROJECTION);
  716.         glLoadIdentity();
  717.         gluPerspective(45.0f, (sirka / (1.0 * vyska)), 0.1f, 100.0f);
  718.  
  719.         glMatrixMode(GL_MODELVIEW);
  720.         glLoadIdentity();
  721.  
  722.         glClearColor(0.f, 0.f, 0.f, 1.f); // Initialize clear color
  723.         glTranslated(0, -1, 0);
  724.         glShadeModel(GL_SMOOTH);
  725.         glCullFace(GL_BACK);
  726.         glEnable(GL_CULL_FACE); // Zneviditeľnenie odvrátených strán
  727.         glDepthFunc(GL_LEQUAL);
  728.         glEnable(GL_DEPTH_TEST);
  729.        
  730.         while (!glfwWindowShouldClose(window)) {
  731.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  732.             vykresliGL();
  733.  
  734.             glfwSwapBuffers(window);
  735.  
  736.             glfwPollEvents();
  737.         }
  738.     }
  739.  
  740.     public static void main(String[] args) {
  741.         new cv091().spusti();
  742.     }
  743. }
Add Comment
Please, Sign In to add comment