Advertisement
LukacikPavel

cv08 urg

Nov 12th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.98 KB | None | 0 0
  1.  
  2. import org.lwjgl.glfw.*;
  3. import org.lwjgl.opengl.GL;
  4. import org.lwjgl.system.MemoryStack;
  5. import static org.lwjgl.stb.STBImage.*;
  6. import static org.lwjgl.stb.STBImageResize.*;
  7.  
  8. import static org.lwjgl.glfw.GLFW.*;
  9. import static org.lwjgl.opengl.GL11.*;
  10. import static org.lwjgl.system.MemoryUtil.*;
  11. import static org.lwjgl.system.MemoryStack.*;
  12. import static org.lwjgl.BufferUtils.*;
  13.  
  14. import java.io.InputStream;
  15. import java.nio.*;
  16. import java.nio.channels.*;
  17. import java.nio.file.*;
  18.  
  19. import java.io.*;
  20.  
  21. public class cv081 {
  22.    
  23.     private int upjs;
  24.     private int stena;
  25.  
  26.     private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) {
  27.         ByteBuffer newBuffer = org.lwjgl.BufferUtils.createByteBuffer(newCapacity);
  28.         buffer.flip();
  29.         newBuffer.put(buffer);
  30.         return newBuffer;
  31. }  
  32.     private ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException {
  33.         ByteBuffer buffer;
  34.  
  35.         Path path = Paths.get(resource);
  36.         if (Files.isReadable(path)) {
  37.             try (SeekableByteChannel fc = Files.newByteChannel(path)) {
  38.                 buffer = org.lwjgl.BufferUtils.createByteBuffer((int)fc.size() + 1);
  39.                 while (fc.read(buffer) != -1) {
  40.                     ;
  41.                 }
  42.             }
  43.         } else {
  44.             try (
  45.                 InputStream source = this.getClass().getClassLoader().getResourceAsStream(resource);
  46.                 ReadableByteChannel rbc = Channels.newChannel(source)
  47.             ) {
  48.                 buffer = createByteBuffer(bufferSize);
  49.  
  50.                 while (true) {
  51.                     int bytes = rbc.read(buffer);
  52.                     if (bytes == -1) {
  53.                         break;
  54.                     }
  55.                     if (buffer.remaining() == 0) {
  56.                         buffer = resizeBuffer(buffer, buffer.capacity() * 3 / 2); // 50%
  57.                     }
  58.                 }
  59.             }
  60.         }
  61.  
  62.         buffer.flip();
  63.         return buffer.slice();
  64.     }
  65.    
  66.    
  67.      private int createTexture(String Path) {
  68.             int texID = glGenTextures();
  69.             ByteBuffer imageBuffer;
  70.             try {
  71.                 imageBuffer = ioResourceToByteBuffer(Path, 8 * 1024);
  72.             } catch (Exception e) {
  73.                 throw new RuntimeException(e);
  74.             }
  75.             int w,h,comp;
  76.             ByteBuffer image;
  77.             try (MemoryStack stack = stackPush()) {
  78.                 IntBuffer wB    = stack.mallocInt(1);
  79.                 IntBuffer hB    = stack.mallocInt(1);
  80.                 IntBuffer compB = stack.mallocInt(1);
  81.  
  82.                 // Use info to read image metadata without decoding the entire image.
  83.                 // We don't need this for this demo, just testing the API.
  84.                 if (!stbi_info_from_memory                 
  85.                        
  86.                         (imageBuffer, wB, hB, compB)) {
  87.                     throw new RuntimeException("Failed to read image information: " + stbi_failure_reason());
  88.                 }
  89.  
  90. /*              System.out.println("Image width: " + wB.get(0));
  91.                 System.out.println("Image height: " + hB.get(0));
  92.                 System.out.println("Image components: " + compB.get(0));
  93.                 System.out.println("Image HDR: " + stbi_is_hdr_from_memory(imageBuffer));*/
  94.  
  95.                 // Decode the image
  96.                 image = stbi_load_from_memory(imageBuffer, wB, hB, compB, 0);
  97.                 if (image == null) {
  98.                     throw new RuntimeException("Failed to load image: " + stbi_failure_reason());
  99.                 }
  100.  
  101.                 w = wB.get(0);
  102.                 h = hB.get(0);
  103.                 comp = compB.get(0);
  104.     }          
  105.  
  106.             glBindTexture(GL_TEXTURE_2D, texID);
  107.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  108.             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  109. //          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  110. //          glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  111.  
  112.             int format;
  113.  
  114.             if (comp == 3) {
  115.                 if ((w & 3) != 0) {
  116.                     glPixelStorei(GL_UNPACK_ALIGNMENT, 2 - (w & 1));
  117.                 }
  118.                 format = GL_RGB;
  119.             } else {
  120. //              premultiplyAlpha();
  121. //              glEnable(GL_BLEND);
  122. //              glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  123.  
  124.                 format = GL_RGBA;
  125.     }
  126.  
  127.             glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, format, GL_UNSIGNED_BYTE, image);
  128.  
  129.             ByteBuffer input_pixels = image;
  130.             int        input_w      = w;
  131.             int        input_h      = h;
  132.             int        mipmapLevel  = 0;
  133.             while (1 < input_w || 1 < input_h) {
  134.                 int output_w = Math.max(1, input_w >> 1);
  135.                 int output_h = Math.max(1, input_h >> 1);
  136.  
  137.                 ByteBuffer output_pixels = memAlloc(output_w * output_h * comp);
  138.                 stbir_resize_uint8_generic(
  139.                     input_pixels, input_w, input_h, input_w * comp,
  140.                     output_pixels, output_w, output_h, output_w * comp,
  141.                     comp, comp == 4 ? 3 : STBIR_ALPHA_CHANNEL_NONE, STBIR_FLAG_ALPHA_PREMULTIPLIED,
  142.                     STBIR_EDGE_CLAMP,
  143.                     STBIR_FILTER_MITCHELL,
  144.                     STBIR_COLORSPACE_SRGB
  145.                 );
  146.  
  147.                 if (mipmapLevel == 0) {
  148.                     stbi_image_free(image);
  149.                 } else {
  150.                     memFree(input_pixels);
  151.                 }
  152.  
  153.                 glTexImage2D(GL_TEXTURE_2D, ++mipmapLevel, format, output_w, output_h, 0, format, GL_UNSIGNED_BYTE, output_pixels);
  154.  
  155.                 input_pixels = output_pixels;
  156.                 input_w = output_w;
  157.                 input_h = output_h;
  158.             }
  159.             if (mipmapLevel == 0) {
  160.                 stbi_image_free(image);
  161.             } else {
  162.                 memFree(input_pixels);
  163.             }
  164.  
  165.             return texID;
  166.     }
  167.    
  168.     private int sirka = 800;
  169.     private int vyska = 600;
  170.  
  171.     Vector3f[] vrcholy = {
  172.             new Vector3f(-1,0,+1),
  173.             new Vector3f(+1,0,+1),
  174.             new Vector3f(+1,0,-1),
  175.             new Vector3f(-1,0,-1),
  176.             new Vector3f(0,2,0),
  177.             };
  178.  
  179.     Vector3f[] farba = {
  180.             new Vector3f(1,0,0),
  181.             new Vector3f(0,1,0),
  182.             new Vector3f(0,0,1),
  183.             new Vector3f(1,0,1),
  184.             new Vector3f(1,1,0),
  185.             new Vector3f(0,1,1)
  186. };
  187.  
  188.     int[][] steny = {
  189.             {0,1,4},
  190.             {1,2,4},
  191.             {2,3,4},
  192.             {3,0,4},
  193.             {0,3,2,1}
  194.     };
  195.    
  196.     double uhol=0;
  197.    
  198.     void vykresliGL() {
  199.         //glLoadIdentity();
  200.         glRotated(0.03, 1, 1, 1);
  201.        
  202.         glBegin(GL_QUADS);
  203.         glTexCoord2d(0, 1);
  204.         glVertex3d(-1, -1, 0);
  205.         glTexCoord2d(1, 1);
  206.         glVertex3d(+1, -1, 0);
  207.         glTexCoord2d(1, 0);
  208.         glVertex3d(+1, +1, 0);
  209.         glTexCoord2d(0, 0);
  210.         glVertex3d(-1, +1, 0);
  211.        
  212.         glTexCoord2d(0, 2);
  213.         glVertex3d(-1, -1, -2);
  214.         glTexCoord2d(2, 2);
  215.         glVertex3d(-1, -1, 0);
  216.         glTexCoord2d(2, 0);
  217.         glVertex3d(-1, 1, 0);
  218.         glTexCoord2d(0, 0);
  219.         glVertex3d(-1, 1, -2);
  220.        
  221.         glTexCoord2d(0, 0);
  222.         glVertex3d(-1, -1, -2);
  223.         glTexCoord2d(0, 3);
  224.         glVertex3d(-1, 1, -2);
  225.         glTexCoord2d(3, 3);
  226.         glVertex3d(1, 1, -2);
  227.         glTexCoord2d(3, 0);
  228.         glVertex3d(1, -1, -2);
  229.        
  230.         glTexCoord2d(0, 4);
  231.         glVertex3d(1, -1, 0);
  232.         glTexCoord2d(4, 4);
  233.         glVertex3d(1, -1, -2);
  234.         glTexCoord2d(4, 0);
  235.         glVertex3d(1, 1, -2);
  236.         glTexCoord2d(0, 0);
  237.         glVertex3d(1, 1, 0);
  238.        
  239.         glTexCoord2d(0, 5);
  240.         glVertex3d(-1, 1, 0);
  241.         glTexCoord2d(5, 5);
  242.         glVertex3d(1, 1, 0);
  243.         glTexCoord2d(5, 0);
  244.         glVertex3d(1, 1, -2);
  245.         glTexCoord2d(0, 0);
  246.         glVertex3d(-1, 1, -2);
  247.        
  248.         glTexCoord2d(0, 6);
  249.         glVertex3d(-1, -1, -2);
  250.         glTexCoord2d(6, 6);
  251.         glVertex3d(1, -1, -2);
  252.         glTexCoord2d(6, 0);
  253.         glVertex3d(1, -1, 0);
  254.         glTexCoord2d(0, 0);
  255.         glVertex3d(-1, -1, 0);
  256.        
  257.         glEnd();
  258.  
  259.     }
  260.  
  261.     long window;
  262.     GLFWErrorCallback errorCallback;
  263.     GLFWKeyCallback   keyCallback;
  264.  
  265.     void spusti() {
  266.         try {
  267.             init();
  268.             loop();
  269.  
  270.             glfwDestroyWindow(window);
  271.             keyCallback.free();
  272.         } finally {
  273.             glfwTerminate();
  274.             errorCallback.free();
  275.         }
  276.     }
  277.  
  278.     void init() {
  279.         glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
  280.         if (!glfwInit())
  281.             throw new IllegalStateException("Chyba pri inicializacii GLFW!!!");
  282.  
  283.         window = glfwCreateWindow(sirka, vyska, "UGR1", NULL, NULL);
  284.         if ( window == NULL )
  285.             throw new RuntimeException("Chyba pri vytvoreni GLFW okna!!!");
  286.  
  287.         glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
  288.             @Override
  289.             public void invoke(long window, int key,
  290.                     int scancode, int action, int mods) {
  291.                 if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
  292.                     glfwSetWindowShouldClose(window, true);
  293.            
  294.             }});
  295.  
  296.         GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  297.         glfwSetWindowPos(window, (vidmode.width() - sirka) / 2, (vidmode.height() - vyska) / 2);
  298.  
  299.         glfwMakeContextCurrent(window);
  300.         glfwSwapInterval(0);
  301.         glfwShowWindow(window);        
  302.  
  303.         GL.createCapabilities();
  304.    
  305.         glEnable(GL_TEXTURE_2D);
  306.         glBindTexture(GL_TEXTURE_2D, createTexture("D:\\pfupjs.png"));
  307.         upjs = createTexture("D:\\pfupjs.png");
  308.         stena = createTexture("D:\\stena.png");
  309.        
  310.     }
  311.  
  312.     private static void gluPerspective(double fov, double aspect, double zNear, double zFar) {
  313.         double fH = Math.tan(fov*Math.PI/360.0) * zNear;
  314.         double fW = fH * aspect;
  315.         glFrustum( -fW, fW, -fH, fH, zNear, zFar);     
  316.     }
  317.  
  318.    
  319.     void loop() {
  320.         glViewport(0,0,sirka,vyska);        
  321.  
  322.         glMatrixMode(GL_PROJECTION);
  323.         glLoadIdentity();
  324.         gluPerspective(45.0f, (sirka/(1.0*vyska)), 0.1f, 100.0f);      
  325.  
  326.         glMatrixMode( GL_MODELVIEW );
  327.         glLoadIdentity();
  328.  
  329.         glClearColor( 0.f, 0.f, 0.f, 1.f ); //Initialize clear color
  330.         glTranslated(0, 0, -5);
  331.         glShadeModel(GL_SMOOTH);
  332.         glCullFace(GL_BACK); glEnable(GL_CULL_FACE); //Zneviditeľnenie odvrátených strán
  333.        
  334.         while ( !glfwWindowShouldClose(window) ) {
  335.             glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  336.             vykresliGL();
  337.  
  338.             glfwSwapBuffers(window);
  339.  
  340.             glfwPollEvents();
  341.         }
  342.     }
  343.  
  344.     public static void main(String[] args) {
  345.         new cv081().spusti();
  346.     }
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement