Login  Register

Could someone help me port these two files from FOBS to JOGL2?

Posted by ramonjsantiago on Mar 23, 2012; 4:28am
URL: https://forum.jogamp.org/Could-someone-help-me-port-these-two-files-from-FOBS-to-JOGL2-tp3850707.html

OpenGLRenderer.java
FlatRenderer.java

File #1

/******************************************************************************
* FOBS Java CrossPlatform JMF PlugIn
* Copyright (c) 2004 Omnividea Multimedia S.L
*
*    This file is part of FOBS.
*
*    FOBS is free software; you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as
*    published by the Free Software Foundation; either version 2.1
*    of the License, or (at your option) any later version.
*
*    FOBS is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public
*    License along with FOBS; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
******************************************************************************/

/*
 * Created on 06.02.2005 by Robert Binna
 * ChangeLog:
 *  Jose San Pedro Wandelmer 2005/02/21 - Major changes to code structure
 *      - Package changed to fit Fobs package naming
 *      - Automatic Generated comments removed
 *      - Header included with license information
 *      - Jogl animator use have been removed. Explicit calls to display() seems to be more efficient.
 *      - The Renderer is generated by the class (FlatRenderer only at the moment)
 *      - Thanks to Robert Binna for his contribution to the project!!
 *  Robert Hastings 2007/01/04
 *      - Native Library Location routines
 *      - Improvements to frame buffer management
 */

package com.omnividea.media.renderer.video;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import javax.media.Buffer;
import javax.media.Format;
import javax.media.ResourceUnavailableException;
import javax.media.format.RGBFormat;
import javax.media.renderer.VideoRenderer;
import javax.media.control.FrameGrabbingControl;
import javax.media.util.ImageToBuffer;

import net.java.games.jogl.Animator;
import net.java.games.jogl.GLCanvas;
import net.java.games.jogl.GLCapabilities;
import net.java.games.jogl.GLDrawableFactory;
import javax.swing.JPanel;

import com.omnividea.media.renderer.video.opengl.RenderEventListener;
import com.omnividea.media.renderer.video.opengl.FlatRenderer;
import com.omnividea.FobsConfiguration;

public class OpenGLRenderer implements VideoRenderer, FrameGrabbingControl {

    private BufferStrategy strategy;
    private GLCanvas canvas;
    private Animator animator = null;
    private RenderEventListener listener;
    private FlatRenderer flatRenderer = null;
    private JPanel panel = null;
    private Image lastImage;

    public OpenGLRenderer() {
        GLCapabilities capbilities = new GLCapabilities();
        capbilities.setDoubleBuffered(true);
        capbilities.setAlphaBits(8);
        capbilities.setRedBits(8);
        capbilities.setGreenBits(8);
        capbilities.setBlueBits(8);        
        System.out.println("OpenGL Renderer: cons");
        canvas = GLDrawableFactory.getFactory().createGLCanvas(capbilities);
        flatRenderer = new FlatRenderer();
        //flatRenderer.init(canvas);
        addRenderEventListener(flatRenderer);
        panel = new JPanel();
        panel.setIgnoreRepaint(true);
        panel.add(canvas);
    }
   
    public void addRenderEventListener(RenderEventListener listener) {
        System.out.println("OpenGL Renderer: add Render Event");
        this.listener = listener;
        canvas.addGLEventListener(listener);
        canvas.addMouseMotionListener(listener);
    }
   
        private RGBFormat vf;
    private BufferedImage bufferedImage;
    private int []pixels;
    //private int[][] pixels = new int[2][];
   
    private int formatWidth;
    private int formatHeight;
    private int currentIndex = 0;
   
    void setValue(Object aValue,boolean isSelected) {
        System.out.println(aValue.getClass().getName());
    }

    public Format[] getSupportedInputFormats() {
        RGBFormat format = new RGBFormat();
        return new Format[] {  format };
    }

    public Format setInputFormat(Format format) {
        FobsConfiguration.reset();
        FobsConfiguration.videoFrameFormat=FobsConfiguration.YUV420;

/*        vf = (RGBFormat) format;
                this.formatWidth = (int) vf.getSize().getWidth();
            this.formatHeight = (int) vf.getSize().getHeight();
            listener.setFormat(formatWidth,formatHeight);
            pixels[0] = new int[formatWidth * formatHeight];
            pixels[1] = new int[formatWidth * formatHeight];

        FobsConfiguration.videoFrameFormat=FobsConfiguration.RGBA;*/

        vf = (RGBFormat) format;
                int formatWidth = (int) vf.getSize().getWidth();
            int formatHeight = (int) vf.getSize().getHeight();
            listener.setFormat(formatWidth,formatHeight);
       
        /*
        bufferedImage = new BufferedImage(formatWidth,formatHeight, BufferedImage.TYPE_INT_RGB);
        pixels = ( (DataBufferInt) bufferedImage.getRaster().getDataBuffer()).getData();
       
        FobsConfiguration.properties.put("BufferedImage", bufferedImage);
        FobsConfiguration.properties.put("BufferedImageIntBuffer", pixels);
            */
       
            canvas.setSize(new Dimension(formatWidth,formatHeight));
            panel.setPreferredSize(new Dimension(formatWidth,formatHeight));
        return format;
    }

    public void start() {
        /*
        if(animator == null) {
            animator = new Animator(canvas);
            animator.start();
            System.out.println("animator started");
        }*/
       
    }

    public void stop() {
        /*
        if(animator != null) {
            animator.stop();
            animator = null;
        }
        */
    }

    public int process(Buffer buffer) {
       
        /*int[] bufferdata = (int[]) buffer.getData();
        int nextIndex = (currentIndex +1)%2;
        currentIndex = nextIndex;
        */
        /*
        int[] destination = pixels[nextIndex];
        int size = destination.length;
        int offsetSrc = 0;
        int offsetDest = destination.length - formatWidth;
                for(int i=0; i < formatHeight; ++i) {
                    for(int j=formatWidth - 1; j >= 0; --j) {
                        destination[offsetDest] = (bufferdata[offsetSrc] << 8);
                        ++offsetDest;
                        ++offsetSrc;
                    }
                   
                    offsetDest -= formatWidth * 2;
                }
               
                if(listener != null) {
                    listener.setRenderingData(pixels[currentIndex]);
            canvas.display();
                }
        */
                if(listener != null) {
                    listener.setRenderingData(pixels/*bufferdata*/);
            canvas.display();
                }

               
               
                return BUFFER_PROCESSED_OK;
    }

    public String getName() {
        return "Fobs GL Renderer";
    }

    public void open() throws ResourceUnavailableException {
    }

    public void close() {
       
       
    }

    public void reset() {
    }


    public Component getComponent() {
        //return canvas;
        return panel;
    }

    public boolean setComponent(Component arg0) {
        return false;
    }

    public void setBounds(Rectangle arg0) {
        System.out.println("set bounds :: " + arg0);
       
    }

    public Rectangle getBounds() {
        return null;
    }
   
   
   
  // support for FrameGrabbingControl
  public Buffer grabFrame() {
    //BufferedImage bufimg = (BufferedImage)FobsConfiguration.properties.get("BufferedImage");
    Buffer buf = ImageToBuffer.createBuffer(lastImage,(float)0);
    return buf;
  }
   
  // No awt component is needed for FrameGrabbingControl
  public Component getControlComponent() {
    return null;
  }
   
  public Object[] getControls() {
    Object[] obj = { this };
    return obj;
  }
   
  public Object getControl(String arg) {
    Object rc = null;
    if(arg.equals("javax.media.control.FrameGrabbingControl")) rc = this;
    return rc;
  }
  // new code-end
}

---
File #2


/******************************************************************************
* FOBS Java CrossPlatform JMF PlugIn
* Copyright (c) 2004 Omnividea Multimedia S.L
*
*    This file is part of FOBS.
*
*    FOBS is free software; you can redistribute it and/or modify
*    it under the terms of the GNU Lesser General Public License as
*    published by the Free Software Foundation; either version 2.1
*    of the License, or (at your option) any later version.
*
*    FOBS is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU Lesser General Public License for more details.
*
*    You should have received a copy of the GNU Lesser General Public
*    License along with FOBS; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
******************************************************************************/

/*
 * Created on 06.02.2005 by Robert Binna
 * ChangeLog:
 *  Jose San Pedro Wandelmer 2005/02/21 - Major changes to code structure
 *      - Package changed to fit Fobs package naming
 *      - Automatic Generated comments removed
 *      - Header included with license information
 *      - Included code to do CSC in a Pixel Shader
 *      - Thanks to Robert Binna for his contribution to the project!!
 */

package com.omnividea.media.renderer.video.opengl;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;

import net.java.games.jogl.GL;
import net.java.games.jogl.GLDrawable;
import net.java.games.jogl.GLU;
import net.java.games.cg.CgGL;
import net.java.games.cg.CGcontext;
import net.java.games.cg.CGprogram;
import net.java.games.cg.CGparameter;


import com.omnividea.FobsConfiguration;

public class FlatRenderer implements RenderEventListener {
   
    int width = 1024;
    int height = 1024;
    int[] textures = new int[1];
    private int[] firstpixels;
    private int formatWidth;
    private int formatHeight;
    private int[] data;
    private float extendX;
    private float extendY;
    private int inputColorOrder;
    private int inputFormat;
    private CGcontext cgContext = null;
    private CGprogram cgProgram = null;
    private CGparameter cgParameter = null;
    private String program =
       
        "float4 main(half2 coords : TEX0,                                   \n"+
        "            uniform sampler2D Y) : COLOR                          \n"+
        "{                                                                  \n"+
        "    float4 c  = tex2D(Y, coords);                             \n"+
        "    float3 res1 = (c.rrr-(half3(0.062745,0.062745,0.062745)))*1.164;\n"+
        "    res1 += (c.bbb-half3(0.5,0.5,0.5))*half3(1.596,-0.813,0);\n"+
        "    res1 += (c.ggg-half3(0.5,0.5,0.5))*half3(0,-0.391,2.018);\n"+
        "    return clamp(float4(res1,1.0), 0.062745, 0.92); \n"+
        "}                                                                  \n";
 

    public FlatRenderer() {
        inputColorOrder = com.omnividea.media.parser.video.Parser.isBigEndian()?GL.GL_RGBA:GL.GL_BGRA;
        inputFormat = com.omnividea.media.parser.video.Parser.isBigEndian()?GL.GL_UNSIGNED_INT_8_8_8_8:GL.GL_UNSIGNED_INT_8_8_8_8_REV;
    }
   
    public boolean hasFragmentShaderSupport()
    {
        if(CgGL.cgGLIsProfileSupported(CgGL.CG_PROFILE_FP20)||CgGL.cgGLIsProfileSupported(CgGL.CG_PROFILE_ARBFP1))
        {
            return true;
        }
        else return false;
    }
   
    public int getBestProfile()
    {
        if(CgGL.cgGLIsProfileSupported(CgGL.CG_PROFILE_ARBFP1))
        {
            return CgGL.CG_PROFILE_ARBFP1;
        }    
        else
        {
            return CgGL.CG_PROFILE_FP20;
        }
    }
   
    public void init(GLDrawable drawable) {
        System.out.println("init 3d");
       
       
       
       
        GL gl = drawable.getGL();
        GLU glu = drawable.getGLU();
        gl.glGenTextures(1, textures);
        gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
        gl.glDisable(GL.GL_DEPTH_TEST);
        gl.glDisable(GL.GL_LIGHTING);
        gl.glEnable(GL.GL_TEXTURE_2D);
        //gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);

        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
        gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);

        gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, width, height, 0,  GL.GL_RGBA, GL.GL_UNSIGNED_INT_8_8_8_8, (char[])null);

        //System.out.println("time :: " + (System.currentTimeMillis() - time));  
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);
        gl.glMatrixMode(GL.GL_PROJECTION);    
        gl.glLoadIdentity();              
        glu.gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
       
        cgContext = CgGL.cgCreateContext();
        int errorCg = CgGL.cgGetError();
        System.out.println("cgCreateContext: "+CgGL.cgGetErrorString(errorCg));

        if(hasFragmentShaderSupport())
        {
           
            cgProgram = CgGL.cgCreateProgram(cgContext,  CgGL.CG_SOURCE, program,  getBestProfile(),  "main",  null);
            //errorCg = CgGL.cgGetError();
            //System.out.println("cgCreateProgram: "+CgGL.cgGetErrorString(errorCg));
           
            CgGL.cgGLLoadProgram(cgProgram);
            //errorCg = CgGL.cgGetError();
            //System.out.println("cgLoadProgram: "+CgGL.cgGetErrorString(errorCg));
            CgGL.cgGLBindProgram(cgProgram);
            //errorCg = CgGL.cgGetError();
            //System.out.println("cgBindPRogram: "+CgGL.cgGetErrorString(errorCg));
           
            cgParameter = CgGL.cgGetNamedParameter(cgProgram,  "Y");
            CgGL.cgGLSetTextureParameter(cgParameter,  textures[0]);
            //errorCg = CgGL.cgGetError();
            //System.out.println("cgSetParam: "+CgGL.cgGetErrorString(errorCg));
           
            CgGL.cgGLEnableTextureParameter(cgParameter);

            gl.glBindTexture(GL.GL_TEXTURE_2D, textures[0]);
            CgGL.cgGLEnableProfile(getBestProfile());
            //errorCg = CgGL.cgGetError();
            //System.out.println("cgEnableProfile: "+CgGL.cgGetErrorString(errorCg));
        }
        else
        {
            FobsConfiguration.videoFrameFormat=FobsConfiguration.RGBA;
            inputColorOrder = GL.GL_BGRA;
            inputFormat = GL.GL_UNSIGNED_INT_8_8_8_8_REV;
            System.out.println("Your graphics hardware is not capable of doing Color Space Conversion\n");
        }
    }

    public void display(GLDrawable drawable) {
        GL gl = drawable.getGL();
        GLU glu = drawable.getGLU();
       
        if(data != null) {
            gl.glTexSubImage2D(GL.GL_TEXTURE_2D, 0, 0, 0, formatWidth, formatHeight,  inputColorOrder, inputFormat, data);
        }
               
        gl.glBegin(GL.GL_QUADS);
                gl.glTexCoord2f(0,0);
                gl.glVertex3d(-1, 1,0);
                gl.glTexCoord2f(extendX,0);
                gl.glVertex3d(1, 1,0);
                gl.glTexCoord2f(extendX,extendY);
                gl.glVertex3d(1, -1,0);
                gl.glTexCoord2f(0,extendY);
                gl.glVertex3d(-1, -1,0);
        gl.glEnd();
       
        drawable.swapBuffers();
         
       
 
       
    }

    public void reshape(GLDrawable drawable, int x, int y, int width, int height) {
    }

    public void displayChanged(GLDrawable arg0, boolean arg1, boolean arg2) {
    }
   
    public void mouseClicked(MouseEvent e) {}
   
    public void mouseDragged(MouseEvent e) {

    }
   
    public void mouseMoved(MouseEvent e) {}

    public void setRenderingData(int[] data) {
        this.data = data;
       
    }



    public void setFormat(int videoWidth, int videoHeight) {
        formatWidth = videoWidth;
        formatHeight = videoHeight;      
        extendX = ((float) formatWidth)/width;
        extendY = ((float) formatHeight)/height;

    }
 
   
}

---

I tried but couldn't locate the original writer.

Help is very much appreciated.

Thank you,
rjs