/*
 * www.javagl.de - JglTF
 *
 * Copyright 2015-2016 Marco Hutter - http://www.javagl.de
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package de.javagl.jgltf.viewer.lwjgl;

import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glColorMask;
import static org.lwjgl.opengl.GL11.glDepthMask;

import java.awt.Canvas;
import java.awt.Component;
import java.awt.Dimension;
import java.util.logging.Logger;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;

import de.javagl.jgltf.viewer.AbstractGltfViewer;
import de.javagl.jgltf.viewer.GlContext;

/**
 * Implementation of a glTF viewer based on LWJGL
 */
public class GltfViewerLwjgl extends AbstractGltfViewer
{
    /**
     * The logger used in this class
     */
    private static final Logger logger =
        Logger.getLogger(GltfViewerLwjgl.class.getName());
    
    /**
     * The AWTGLCanvas, i.e. the rendering component of this renderer
     */
    private Component glComponent;
    
    /**
     * The {@link GlContext}
     */
    private final GlContextLwjgl glContext;
    
    /**
     * Creates a new GltfViewerJogl
     */
    public GltfViewerLwjgl()
    {
        try
        {
            this.glComponent = new AWTGLCanvas()
            {
                /**
                 * Serial UID
                 */
                private static final long serialVersionUID = 1L;

                @Override
                public void paintGL()
                {
                    doRender();
                    try
                    {
                        swapBuffers();
                    }
                    catch (LWJGLException e)
                    {
                        logger.severe("Could not swap buffers");
                    }
                }
                
            };
        }
        catch (LWJGLException e)
        {
            logger.severe("Could not create AWTGLCanvas");
            this.glComponent = new Canvas();
        }
        
        // Without setting the minimum size, the canvas cannot 
        // be resized when it is embedded in a JSplitPane
        this.glComponent.setMinimumSize(new Dimension(10, 10));
        
        this.glContext = new GlContextLwjgl();
    }
    
    @Override
    public GlContext getGlContext()
    {
        return glContext;
    }
    
    @Override
    public Component getRenderComponent()
    {
        return glComponent;
    }
    
    @Override
    protected void prepareRender()
    {
        // Nothing to do here
    }
    
    @Override
    protected void render()
    {
        // Enable the color and depth mask explicitly before calling glClear.
        // When they are not enabled, they will not be cleared!
        glColorMask(true, true, true, true);
        glDepthMask(true); 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        renderGltfs();
    }

    

}

@@ -1499,16 +1499,65 @@
 thMask;%0D
+%0Aimport static org.lwjgl.opengl.GL11.glViewport;%0D
 %0A%0D%0Aimpor
@@ -1628,24 +1628,104 @@
 Dimension;%0D%0A
+import java.awt.event.ComponentAdapter;%0D%0Aimport java.awt.event.ComponentEvent;%0D%0A
 import java.
@@ -2421,24 +2421,170 @@
  glContext;%0D
+%0A%0D%0A    /**%0D%0A     * Whether the component was resized, and glViewport has to be called%0D%0A     */%0D%0A    private boolean viewportNeedsUpdate = true;  %0D
 %0A    %0D%0A    /
@@ -2898,18 +2898,34 @@
  = 1L;%0D%0A
+                
 %0D%0A
-
         
@@ -2993,32 +2993,246 @@
              %7B%0D%0A
+                    if (viewportNeedsUpdate)%0D%0A                    %7B%0D%0A                        glViewport(0, 0, getWidth(), getHeight());%0D%0A                        viewportNeedsUpdate = false;%0D%0A                    %7D%0D%0A
                 
@@ -3546,32 +3546,32 @@
               %0D%0A
-
             %7D;%0D%0A
@@ -3570,16 +3570,314 @@
     %7D;%0D%0A
+            %0D%0A            this.glComponent.addComponentListener(new ComponentAdapter()%0D%0A            %7B%0D%0A                @Override%0D%0A                public void componentResized(ComponentEvent e)%0D%0A                %7B%0D%0A                    viewportNeedsUpdate = true;%0D%0A                %7D%0D%0A            %7D);%0D%0A
         

Update GL viewport when LWJGL canvas was resized