com.jogamp.opengl.GLContext Java Examples

The following examples show how to use com.jogamp.opengl.GLContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Update the glyph textures on the shared GL context. The update will occur
 * via the GlyphManagerOpenGLController object, which checks its
 * GlyphManager to see if anything has changed, and if so, copies the
 * appropriate data to the open GL textures.
 *
 * @param glCurrent The GL context to switch back to after updating the
 * shared context.
 */
public static void updateGlyphTextureController(final GL3 glCurrent) {
    if (Utilities.isMac()) {
        glyphTextureController.update(glCurrent);
    } else {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }
            glyphTextureController.update(gl);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }
}
 
Example #2
Source File: GlDisplayList.java    From jtk with Apache License 2.0 6 votes vote down vote up
/**
 * Disposes this display list. When practical, this method should be called
 * explicitly. Otherwise, it will be called when this object is finalized
 * during garbage collection.
 */
public synchronized void dispose() {
  if (_context!=null) {
    GLContext current = GLContext.getCurrent();
    if (_context==current ||
        _context.makeCurrent()==GLContext.CONTEXT_CURRENT) {
      try {
        glDeleteLists(_list,_range);
      } finally {
        if (_context!=current) {
          _context.release();
          current.makeCurrent();
        }
      }
    }
    _context = null;
    _list = 0;
    _range = 0;
  }
}
 
Example #3
Source File: GlTextureName.java    From jtk with Apache License 2.0 6 votes vote down vote up
/**
 * Disposes this texture name. When practical, this method should be called
 * explicitly. Otherwise, it will be called when this object is finalized
 * during garbage collection.
 */
public synchronized void dispose() {
  if (_context!=null) {
    GLContext current = GLContext.getCurrent();
    if (_context==current ||
        _context.makeCurrent()==GLContext.CONTEXT_CURRENT) {
      try {
        //System.out.println("dispose: deleting name="+_name);
        int[] names = {_name};
        glDeleteTextures(1,names,0);
      } finally {
        if (_context!=current) {
          _context.release();
          current.makeCurrent();
        }
      }
    }
    _context = null;
    _name = 0;
  }
}
 
Example #4
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * The blaze shader draws visual attachments to the nodes.
 *
 * @param glCurrent the current OpenGL context.
 * @param colorTarget
 * @param colorShaderName the name of the color buffer in the shader source.
 * @param blazeInfoTarget
 * @param blazeInfoShaderName the name of the int buffer in the shader
 * source.
 * @return the name of the shader.
 * @throws IOException if an error occurs while reader the shader source.
 */
public static int getBlazeShader(final GL3 glCurrent, final int colorTarget, final String colorShaderName, final int blazeInfoTarget, final String blazeInfoShaderName) throws IOException {
    if (blazeShader == 0) {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }

            final String vp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Blaze.vs");
            final String gp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Blaze.gs");
            final String fp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Blaze.fs");
            blazeShader = GLTools.loadShaderSourceWithAttributes(gl, "Blaze", vp, gp, fp,
                    colorTarget, colorShaderName,
                    blazeInfoTarget, blazeInfoShaderName,
                    ShaderManager.FRAG_BASE, FRAG_COLOR);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }

    return blazeShader;
}
 
Example #5
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Each character is drawn individually.
 *
 * @param glCurrent the current OpenGL context.
 * @param labelFloatsTarget The ID of the float buffer in the label batch
 * @param labelFloatsShaderName the name of the float buffer in the shader
 * source.
 * @param labelIntsTarget The ID of the int buffer in the label batch
 * @param labelIntsShaderName the name of the int buffer in the shader
 * source.
 * @return the name of the shader.
 * @throws IOException if an error occurs while reader the shader source.
 */
public static int getConnectionLabelShader(final GL3 glCurrent, final int labelFloatsTarget, final String labelFloatsShaderName, final int labelIntsTarget, final String labelIntsShaderName) throws IOException {
    if (connectionLabelShader == 0) {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }

            final String vp = GLTools.loadFile(GLVisualProcessor.class, "shaders/ConnectionLabel.vs");
            final String gp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Label.gs");
            final String fp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Label.fs");
            connectionLabelShader = GLTools.loadShaderSourceWithAttributes(gl, "Label", vp, gp, fp,
                    labelFloatsTarget, labelFloatsShaderName,
                    labelIntsTarget, labelIntsShaderName,
                    ShaderManager.FRAG_BASE, FRAG_COLOR);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }

    return connectionLabelShader;
}
 
Example #6
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Each character is drawn individually.
 *
 * @param glCurrent the current OpenGL context.
 * @param labelFloatsTarget The ID of the float buffer in the label batch
 * @param labelFloatsShaderName the name of the float buffer in the shader.
 * @param labelIntsTarget The ID of the int buffer in the label batch
 * @param labelIntsShaderName the name of the int buffer in the shader.
 * @return the id of the shader.
 * @throws IOException if an error occurs while reading the shader source.
 */
public static int getNodeLabelShader(final GL3 glCurrent, final int labelFloatsTarget, final String labelFloatsShaderName, final int labelIntsTarget, final String labelIntsShaderName) throws IOException {
    if (nodeLabelShader == 0) {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }

            final String vp = GLTools.loadFile(GLVisualProcessor.class, "shaders/NodeLabel.vs");
            final String gp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Label.gs");
            final String fp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Label.fs");
            nodeLabelShader = GLTools.loadShaderSourceWithAttributes(gl, "Label", vp, gp, fp,
                    labelFloatsTarget, labelFloatsShaderName,
                    labelIntsTarget, labelIntsShaderName,
                    ShaderManager.FRAG_BASE, FRAG_COLOR);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }

    return nodeLabelShader;
}
 
Example #7
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Lines further away don't look good as triangles (too many artifacts), so
 * distant lines are drawn as lines (which is more efficient anyway).
 *
 * @param glCurrent the current OpenGL context.
 * @param colotTarget
 * @param colorShaderName
 * @param connectionInfoTarget
 * @param connectionInfoShaderName
 * @return the id of the line shader.
 * @throws IOException if an error occurs while reading the shader source.
 */
public static int getLineLineShader(final GL3 glCurrent, final int colotTarget, final String colorShaderName, final int connectionInfoTarget, final String connectionInfoShaderName) throws IOException {
    if (lineLineShader == 0) {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }

            final String vp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Line.vs");
            final String gp = GLTools.loadFile(GLVisualProcessor.class, "shaders/LineLine.gs");
            final String fp = GLTools.loadFile(GLVisualProcessor.class, "shaders/LineLine.fs");
            lineLineShader = GLTools.loadShaderSourceWithAttributes(gl, "LineLine", vp, gp, fp,
                    colotTarget, colorShaderName,
                    connectionInfoTarget, connectionInfoShaderName,
                    ShaderManager.FRAG_BASE, FRAG_COLOR);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }

    return lineLineShader;
}
 
Example #8
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Lines close to the camera are drawn as triangles to provide perspective.
 *
 * @param glCurrent the current OpenGL context.
 * @param colotTarget
 * @param colorShaderName
 * @param connectionInfoTarget
 * @param connectionInfoShaderName
 * @return the id of the line shader.
 * @throws IOException if an error occurs while reading the shader source.
 */
public static int getLineShader(final GL3 glCurrent, final int colotTarget, final String colorShaderName, final int connectionInfoTarget, final String connectionInfoShaderName) throws IOException {
    if (lineShader == 0) {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }

            final String vp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Line.vs");
            final String gp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Line.gs");
            final String fp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Line.fs");
            lineShader = GLTools.loadShaderSourceWithAttributes(gl, "Line", vp, gp, fp,
                    colotTarget, colorShaderName,
                    connectionInfoTarget, connectionInfoShaderName,
                    ShaderManager.FRAG_BASE, FRAG_COLOR);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }

    return lineShader;
}
 
Example #9
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * The vertex icon shader draws icons for vertices on the graph.
 *
 * @param glCurrent the current OpenGL context.
 * @param colorTarget
 * @param colorShaderName
 * @param iconShaderName
 * @param iconTarget
 * @return the id of the icon shader.
 * @throws IOException if an error occurs while reading the shader source.
 */
public static int getVertexIconShader(final GL3 glCurrent, final int colorTarget, final String colorShaderName, final int iconTarget, final String iconShaderName) throws IOException {
    if (vertexIconShader == 0) {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }

            final String vp = GLTools.loadFile(GLVisualProcessor.class, "shaders/VertexIcon.vs");
            final String gp = GLTools.loadFile(GLVisualProcessor.class, "shaders/VertexIcon.gs");
            final String fp = GLTools.loadFile(GLVisualProcessor.class, "shaders/VertexIcon.fs");
            vertexIconShader = GLTools.loadShaderSourceWithAttributes(gl, "VertexIcon", vp, gp, fp,
                    colorTarget, colorShaderName,
                    iconTarget, iconShaderName,
                    ShaderManager.FRAG_BASE, FRAG_COLOR);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }

    return vertexIconShader;
}
 
Example #10
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * Lines close to the camera are drawn as triangles to provide perspective.
 *
 * @param glCurrent the current OpenGL context.
 * @param colorTarget
 * @param colorShaderName
 * @param loopInfoTarget
 * @param loopInfoShaderName
 * @return the id of the loop shader.
 * @throws IOException if an error occurs while reading the shader source.
 */
public static int getLoopShader(final GL3 glCurrent, final int colorTarget, final String colorShaderName, final int loopInfoTarget, final String loopInfoShaderName) throws IOException {
    if (loopShader == 0) {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }

            final String vp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Loop.vs");
            final String gp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Loop.gs");
            final String fp = GLTools.loadFile(GLVisualProcessor.class, "shaders/Loop.fs");
            loopShader = GLTools.loadShaderSourceWithAttributes(gl, "Loop", vp, gp, fp,
                    colorTarget, colorShaderName,
                    loopInfoTarget, loopInfoShaderName,
                    ShaderManager.FRAG_BASE, FRAG_COLOR);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }

    return loopShader;
}
 
Example #11
Source File: SharedDrawable.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * The simple icon shader draws non-interactive icons.
 *
 * @param glCurrent the current OpenGL context.
 * @param colorTarget
 * @param colorShaderName
 * @param iconShaderName
 * @param iconTarget
 * @return the id of the icon shader.
 * @throws IOException if an error occurs while reading the shader source.
 */
public static int getSimpleIconShader(final GL3 glCurrent, final int colorTarget, final String colorShaderName, final int iconTarget, final String iconShaderName) throws IOException {
    if (simpleIconShader == 0) {
        glCurrent.getContext().release();
        try {
            final int result = gl.getContext().makeCurrent();
            if (result == GLContext.CONTEXT_NOT_CURRENT) {
                glCurrent.getContext().makeCurrent();
                throw new RenderException(COULD_NOT_CONTEXT_CURRENT);
            }

            final String vp = GLTools.loadFile(GLVisualProcessor.class, "shaders/SimpleIcon.vs");
            final String gp = GLTools.loadFile(GLVisualProcessor.class, "shaders/SimpleIcon.gs");
            final String fp = GLTools.loadFile(GLVisualProcessor.class, "shaders/SimpleIcon.fs");
            simpleIconShader = GLTools.loadShaderSourceWithAttributes(gl, "SimpleIcon", vp, gp, fp,
                    colorTarget, colorShaderName,
                    iconTarget, iconShaderName,
                    ShaderManager.FRAG_BASE, FRAG_COLOR);
        } finally {
            gl.getContext().release();
            glCurrent.getContext().makeCurrent();
        }
    }

    return simpleIconShader;
}
 
Example #12
Source File: GlTextureName.java    From jtk with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a texture name in the current OpenGL context.
 * Calls glGenTextures to create one texture object.
 * @exception IllegalStateException if the current OpenGL context is null.
 */
public GlTextureName() {
  _context = GLContext.getCurrent();
  Check.state(_context!=null,"OpenGL context is not null");
  int[] names = new int[1];
  glGenTextures(1,names,0);
  _name = names[0];
  //System.out.println("GlTextureName: generated name="+_name);
}
 
Example #13
Source File: GlDisplayList.java    From jtk with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs display lists in the current OpenGL context.
 * Calls glGenLists to create the specified number of display list objects.
 * @param range the number of display lists.
 * @exception IllegalStateException if the current OpenGL context is null.
 */
public GlDisplayList(int range) {
  _context = GLContext.getCurrent();
  Check.state(_context!=null,"OpenGL context is not null");
  _list = glGenLists(range);
  _range = range;
}
 
Example #14
Source File: DwPixelFlow.java    From PixelFlow with MIT License 5 votes vote down vote up
public void printGL(){
    GLContext glcontext = gl.getContext();
    
//    String opengl_version    = gl.glGetString(GL2ES2.GL_VERSION).trim();
//    String opengl_vendor     = gl.glGetString(GL2ES2.GL_VENDOR).trim();
    String opengl_renderer   = gl.glGetString(GL2ES2.GL_RENDERER).trim();
//    String opengl_extensions = gl.glGetString(GL2ES2.GL_EXTENSIONS).trim();
//    String glsl_version      = gl.glGetString(GL2ES2.GL_SHADING_LANGUAGE_VERSION).trim();
    
    String GLSLVersionString = glcontext.getGLSLVersionString().trim();
    String GLSLVersionNumber = glcontext.getGLSLVersionNumber()+"";
    String GLVersion         = glcontext.getGLVersion().trim();
//    System.out.println("    OPENGL_VENDOR:         "+opengl_vendor);
//    System.out.println("    OPENGL_RENDERER:       "+opengl_renderer);
//    System.out.println("    OPENGL_VERSION:        "+opengl_version);
//    System.out.println("    OPENGL_EXTENSIONS:     "+opengl_extensions);
//    System.out.println("    GLSL_VERSION:          "+glsl_version);
//    System.out.println("    GLSLVersionString:     "+ GLSLVersionString);
//    System.out.println("    GLSLVersionNumber:     "+ GLSLVersionNumber);
//    System.out.println("    GLVersion:             "+ glcontext.getGLVersion().trim());
//    System.out.println("    GLVendorVersionNumber: "+ glcontext.getGLVendorVersionNumber());
//    System.out.println("    GLVersionNumber:       "+ glcontext.getGLVersionNumber());
    
    System.out.println();
    System.out.println("[-] DEVICE ... " + opengl_renderer);
    System.out.println("[-] GLSL ..... " + GLSLVersionString + " / "+ GLSLVersionNumber);
    System.out.println("[-] GL ....... " + GLVersion);
    System.out.println();
  }
 
Example #15
Source File: Renderer.java    From jaamsim with Apache License 2.0 5 votes vote down vote up
private void initSharedContext() {
	assert (Thread.currentThread() == renderThread);
	assert (drawContext == null);

	int res = sharedContext.makeCurrent();
	assert (res == GLContext.CONTEXT_CURRENT);

	if (USE_DEBUG_GL) {
		sharedContext.setGL(new DebugGL4bc((GL4bc)sharedContext.getGL().getGL2GL3()));
	}

	LogBox.formatRenderLog("Found OpenGL version: %s", sharedContext.getGLVersion());
	LogBox.formatRenderLog("Found GLSL: %s", sharedContext.getGLSLVersionString());
	VersionNumber vn = sharedContext.getGLVersionNumber();
	boolean isCore = sharedContext.isGLCoreProfile();
	LogBox.formatRenderLog("OpenGL Major: %d Minor: %d IsCore:%s", vn.getMajor(), vn.getMinor(), isCore);
	if (vn.getMajor() < 2) {
		throw new RenderException("OpenGL version is too low. OpenGL >= 2.1 is required.");
	}
	GL2GL3 gl = sharedContext.getGL().getGL2GL3();
	if (!isCore && (!gl3Supported || safeGraphics))
		initShaders(gl);
	else
		initCoreShaders(gl, sharedContext.getGLSLVersionString());

	// Sub system specific initializations
	DebugUtils.init(this, gl);
	Polygon.init(this, gl);
	MeshProto.init(this, gl);
	texCache.init(gl);

	// Load the bad mesh proto
	badData = MeshDataCache.getBadMesh();
	badProto = new MeshProto(badData, safeGraphics, false);
	badProto.loadGPUAssets(gl, this);

	skybox = new Skybox();

	sharedContext.release();
}
 
Example #16
Source File: Screenshots.java    From depan with Apache License 2.0 5 votes vote down vote up
/**
 * Stolen from com.jogamp.opengl.util.awt.Screenshot.readToBufferedImage()
 * 
 * JOGL 2.1.2
 */
private static BufferedImage readToBufferedImage(
    int x,int y, int width, int height, boolean alpha) throws GLException {

  int bufImgType = (alpha ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR);
  int readbackType = (alpha ? GL2.GL_ABGR_EXT : GL2ES3.GL_BGR);

  // Allocate necessary storage
  BufferedImage image = new BufferedImage(width, height, bufImgType);

  GLContext glc = GLContext.getCurrent();
  GL gl = glc.getGL();

  // Set up pixel storage modes
  GLPixelStorageModes psm = new GLPixelStorageModes();
  psm.setPackAlignment(gl, 1);

  // read the BGR values into the image
  gl.glReadPixels(x, y, width, height, readbackType,
      GL.GL_UNSIGNED_BYTE,
      ByteBuffer.wrap(((DataBufferByte) image.getRaster().getDataBuffer()).getData()));

  // Restore pixel storage modes
  psm.restore(gl);

  if( glc.getGLDrawable().isGLOriented() ) {
    // Must flip BufferedImage vertically for correct results
    ImageUtil.flipImageVertically(image);
  }
  return image;
}
 
Example #17
Source File: OpenGLGFXCMD.java    From sagetv with Apache License 2.0 5 votes vote down vote up
private void unloadImage(GL2 gl, int handle)
{
  int texturet[] = (int []) imageMap.get(new Integer(handle));
  // This can happen when we unload an image and then notify the server about it being unloaded...this results in a callback made to
  // us to tell us to unload the image because of how that code path works on the server.
  if (texturet != null)
  {
    if(inframe==false)
    {
      if(pbuffer.getContext().makeCurrent()==GLContext.CONTEXT_NOT_CURRENT)
      {
        System.out.println("Couldn't make pbuffer current?");
        return;
      }
      gl = pbuffer.getGL().getGL2();
    }
    if(texturet.length==4) // fb object
    {
      gl.glDeleteFramebuffers(1, texturet, 0);
      gl.glDeleteTextures(1, texturet, 1);
    }
    else
    {
      imageCacheSize -= ((Integer) imageMapSizes.get(new Integer(handle))).intValue();
      gl.glDeleteTextures(1, texturet, 0);
    }
    if(inframe==false)
    {
      pbuffer.getContext().release();
    }
  }
  imageMap.remove(new Integer(handle));
  imageMapSizes.remove(new Integer(handle));
  myConn.clearImageAccess(handle);
}
 
Example #18
Source File: Test.java    From jogl-samples with MIT License 5 votes vote down vote up
private boolean checkGLVersion() {

        GLProfile glp = GLProfile.getMaxProgrammableCore(true);
//        int majorVersionContext = GLContext.getMaxMajor(GLContext.CONTEXT_CURRENT);
//        int minorVersionContext = GLContext.getMaxMinor(GLContext.CONTEXT_CURRENT, majorVersionContext);
//        System.out.println("OpenGL Version Needed " + major + "." + minor
//                + " ( " + majorVersionContext + "," + minorVersionContext + " found)");
        System.out.println("OpenGL Version Needed " + major + "." + minor + " ( " + glp.getName() + " found)");
//        return version(majorVersionContext[0], minorVersionContext[0])
//                >= version(major, minor);
        return GLContext.isValidGLVersion(GLContext.CONTEXT_CURRENT, major, minor);
    }
 
Example #19
Source File: GLScene.java    From depan with Apache License 2.0 5 votes vote down vote up
private GLContext createGLContext() {

    try {
      GLLogger.LOG.info("Create context...");
      GLDrawableFactory drawableFactory = GLDrawableFactory.getFactory(DEFAULT_PROFILE);
      GLContext result = drawableFactory.createExternalGLContext();
      GLLogger.LOG.info("    Done.");

      return result;
    } catch (Throwable errGl) {
      GLLogger.LOG.error("Unable to create GL Context", errGl);
      errGl.printStackTrace();
      throw new RuntimeException(errGl);
    }
  }
 
Example #20
Source File: VertexBufferObject.java    From Robot-Overlord-App with GNU General Public License v2.0 4 votes vote down vote up
public void destroy() {
	GL2 gl2 = GLContext.getCurrent().getGL().getGL2();

	gl2.glDeleteBuffers(NUM_BUFFERS, VBO,0);
}
 
Example #21
Source File: VertexBufferObject.java    From Robot-Overlord-App with GNU General Public License v2.0 4 votes vote down vote up
public VertexBufferObject() {
	GL2 gl2 = GLContext.getCurrent().getGL().getGL2();

	int [] VBO = new int[NUM_BUFFERS];
	gl2.glGenBuffers(NUM_BUFFERS, VBO, 0);  // 2 = one for vertexes, one for normals
}
 
Example #22
Source File: Renderer.java    From jaamsim with Apache License 2.0 4 votes vote down vote up
private void loadMeshProtoImp(final MeshProtoKey key) {

		//long startNanos = System.nanoTime();

		assert(drawContext == null || !drawContext.isCurrent());

		if (protoCache.get(key) != null) {
			return; // This mesh has already been loaded
		}

		int res = sharedContext.makeCurrent();
		assert (res == GLContext.CONTEXT_CURRENT);

		GL2GL3 gl = sharedContext.getGL().getGL2GL3();

		MeshProto proto;
		boolean canBatch = indirectSupported;

		MeshData data = MeshDataCache.getMeshData(key);
		if (data == badData) {
			proto = badProto;
		} else {
			proto = new MeshProto(data, safeGraphics, canBatch);

			assert (proto != null);
			proto.loadGPUAssets(gl, this);

			if (!proto.isLoadedGPU()) {
				// This did not load cleanly, clear it out and use the default bad mesh asset
				proto.freeResources(gl);

				LogBox.formatRenderLog("Could not load GPU assset: %s\n", key.getURI().toString());

				proto = badProto;
			}
		}
		protoCache.put(key, proto);

		sharedContext.release();

//		long endNanos = System.nanoTime();
//		long ms = (endNanos - startNanos) /1000000L;
//		LogBox.formatRenderLog("LoadMeshProtoImp time:" + ms + "ms");

	}
 
Example #23
Source File: GLEventListenerButton.java    From jogl-samples with MIT License 4 votes vote down vote up
@Override
public void drawShape(final GL2ES2 gl, final RegionRenderer renderer, final int[] sampleCount) {
    if( null == fboGLAD ) {
        final ImageSequence imgSeq = (ImageSequence)texSeq;

        final GLContext ctx = gl.getContext();
        final GLDrawable drawable = ctx.getGLDrawable();
        final GLCapabilitiesImmutable reqCaps = drawable.getRequestedGLCapabilities();
        final GLCapabilities caps = (GLCapabilities) reqCaps.cloneMutable();
        caps.setFBO(true);
        caps.setDoubleBuffered(false);
        if( !useAlpha ) {
            caps.setAlphaBits(0);
        }
        final GLDrawableFactory factory = GLDrawableFactory.getFactory(caps.getGLProfile());

        fboGLAD = (GLOffscreenAutoDrawable.FBO) factory.createOffscreenAutoDrawable(
                        drawable.getNativeSurface().getGraphicsConfiguration().getScreen().getDevice(),
                        caps, null, fboWidth, fboHeight);
        fboWidth = 0;
        fboHeight = 0;
        fboGLAD.setSharedContext(ctx);
        fboGLAD.setTextureUnit(imgSeq.getTextureUnit());
        fboGLAD.addGLEventListener(glel);
        fboGLAD.display(); // 1st init!

        final FBObject.TextureAttachment texA01 = fboGLAD.getColorbuffer(GL.GL_FRONT).getTextureAttachment();
        final Texture tex = new Texture(texA01.getName(), imgSeq.getTextureTarget(),
                                fboGLAD.getSurfaceWidth(), fboGLAD.getSurfaceHeight(), fboGLAD.getSurfaceWidth(), fboGLAD.getSurfaceHeight(),
                                false /* mustFlipVertically */);
        imgSeq.addFrame(gl, tex);
        markStateDirty();
    } else if( 0 != fboWidth*fboHeight ) {
        fboGLAD.setSurfaceSize(fboWidth, fboHeight);
        fboWidth = 0;
        fboHeight = 0;
        markStateDirty();
    } else if( animateGLEL ) {
        fboGLAD.display();
    }

    super.drawShape(gl, renderer, sampleCount);

    if( animateGLEL ) {
        markStateDirty(); // keep on going
    }
}
 
Example #24
Source File: OpenGLGFXCMD.java    From sagetv with Apache License 2.0 4 votes vote down vote up
private void initpbuffer()
{
  GL2 gl;
  System.out.println("initpbuffer");
  osdwidth=newosdwidth;
  osdheight=newosdheight;
  GLCapabilities caps = new GLCapabilities(null);
  caps.setHardwareAccelerated(true);
  caps.setDoubleBuffered(false);
  caps.setAlphaBits(8);
  caps.setRedBits(8);
  caps.setGreenBits(8);
  caps.setBlueBits(8);
  caps.setDepthBits(0);
  caps.setFBO(false);
  System.out.println("initpbuffer2");
  if (!GLDrawableFactory.getFactory(caps.getGLProfile()).canCreateGLPbuffer(null, caps.getGLProfile()))
  {
    throw new GLException("pbuffers unsupported");
  }
  if(pbuffer!=null) pbuffer.destroy();
  System.out.println("initpbuffer3");
  pbuffer = GLDrawableFactory.getFactory(caps.getGLProfile()).createOffscreenAutoDrawable(null,
      caps,
      null,
      osdwidth,
      osdheight
      );
  pbuffer.setContext(pbuffer.createContext(c.getContext()), true);
  //pbuffer.setContext(c.getContext(), false);
  System.out.println("initpbuffer4: pbuffers is null? " + (pbuffer==null));
  if(pbuffer.getContext().makeCurrent()==GLContext.CONTEXT_NOT_CURRENT)
  {
    System.out.println("Couldn't make pbuffer current?");
    return;
  }
  System.out.println("initpbuffer5");
  gl = pbuffer.getGL().getGL2();

  gl.glClearColor( 0.0f, 0.0f, 0.0f, 0.0f);

  gl.glClear( gl.GL_COLOR_BUFFER_BIT);

  gl.glViewport(0, 0, osdwidth, osdheight);
  gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
  gl.glLoadIdentity();
  gl.glOrtho(0,osdwidth,0,osdheight,-1.0,1.0);
  gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
  gl.glLoadIdentity();

  // TODO: look into reusing same texture like OSX version...
  if(osdt!=null) gl.glDeleteTextures(1, osdt, 0);
  osdt = new int[1];
  byte img[] = new byte[osdwidth*osdheight*4];
  gl.glGenTextures(1, osdt, 0);
  gl.glEnable(gl.GL_TEXTURE_RECTANGLE);
  gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE,osdt[0]);
  gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
  gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
  gl.glTexImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 4, osdwidth, osdheight, 0,
      gl.GL_BGRA, bigendian ? gl.GL_UNSIGNED_INT_8_8_8_8_REV : gl.GL_UNSIGNED_BYTE, java.nio.ByteBuffer.wrap(img));

  gl.glEnable(gl.GL_TEXTURE_RECTANGLE);
  gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, osdt[0]);
  gl.glCopyTexSubImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 0, 0, 0, 0, osdwidth, osdheight);
  gl.glDisable(gl.GL_TEXTURE_RECTANGLE);
  System.out.println("initpbuffer6");
  pbuffer.getContext().release();
  System.out.println("initpbuffer7");
}
 
Example #25
Source File: OpenGLVideoRenderer.java    From sagetv with Apache License 2.0 4 votes vote down vote up
private void updateVideo(int frametype)
{
  if(owner.isInFrame() || (owner.getPbuffer().getContext().makeCurrent()==GLContext.CONTEXT_NOT_CURRENT))
  {
    if(!owner.isInFrame())
    {
      System.out.println("Couldn't make pbuffer current?");
      return;
    }
    else
    {
      System.out.println("update video while already in frame");
    }
  }
  GL2 gl = owner.getPbuffer().getGL().getGL2();

  if(pureNativeMode) {
    try {
      updateVideo0();
    } catch(Throwable t) {}
  } else {
    //System.out.println("20,20 pixel: "+videobuffer.get(20*720+20));
    //System.out.println(" width: "+videowidth+" height: "+videoheight);
    gl.glEnable(gl.GL_TEXTURE_RECTANGLE);
    gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, videot[0]);
    gl.glPixelStorei(gl.GL_UNPACK_SKIP_PIXELS, videoy);
    gl.glTexSubImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 0, 0, videowidth, videoheight,
        gl.GL_LUMINANCE, gl.GL_UNSIGNED_BYTE, videobuffer);

    gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, videot[1]);
    gl.glPixelStorei(gl.GL_UNPACK_SKIP_PIXELS, videou);
    gl.glTexSubImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 0, 0, videowidth/2, videoheight/2,
        gl.GL_LUMINANCE, gl.GL_UNSIGNED_BYTE, videobuffer);

    gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, videot[2]);
    gl.glPixelStorei(gl.GL_UNPACK_SKIP_PIXELS, videov);
    gl.glTexSubImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 0, 0, videowidth/2, videoheight/2,
        gl.GL_LUMINANCE, gl.GL_UNSIGNED_BYTE, videobuffer);
    gl.glPixelStorei(gl.GL_UNPACK_SKIP_PIXELS, 0);
    gl.glDisable(gl.GL_TEXTURE_RECTANGLE);
    gl.glFlush();
  }
  if(!owner.isInFrame()) owner.getPbuffer().getContext().release();
}
 
Example #26
Source File: OpenGLVideoRenderer.java    From sagetv with Apache License 2.0 4 votes vote down vote up
private void createVideo()
{
  //System.out.println("createvideo");
  if(owner.isInFrame() || (owner.getPbuffer().getContext().makeCurrent()==GLContext.CONTEXT_NOT_CURRENT))
  {
    if(!owner.isInFrame())
    {
      System.out.println("Couldn't make pbuffer current?");
      return;
    }
    else
    {
      System.out.println("Create video while already in frame");
    }
  }
  GL2 gl = owner.getPbuffer().getGL().getGL2();
  System.out.println("createvideo width: "+videowidth+" height: "+videoheight);
  if(videot!=null) gl.glDeleteTextures(3, videot, 0);
  //		try {
  //			closeVideo0();
  //		} catch(Throwable t) {}

  if(pureNativeMode) {
    videot = null;
    videoMode = 0; // release resources and force reallocation when we leave native mode
    try {
      createVideo0(videowidth, videoheight);
    } catch(Throwable t) {}
  } else {
    byte img[] = new byte[videowidth*videoheight*2];
    videot = new int[3];
    gl.glGenTextures(3, videot, 0);
    gl.glEnable(gl.GL_TEXTURE_RECTANGLE);
    gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, videot[0]);
    if(MiniClient.MAC_OS_X) {
      gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, 0x85BC/*GL_TEXTURE_STORAGE_HINT_APPLE*/, 0x85Be/*GL_STORAGE_CACHED_APPLE*/);
      gl.glPixelStorei(0x85B2/*GL_UNPACK_CLIENT_STORAGE_APPLE*/, gl.GL_TRUE);
    }
    gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
    gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
    gl.glTexImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 1, videowidth, videoheight, 0,
        gl.GL_LUMINANCE, gl.GL_UNSIGNED_BYTE, java.nio.ByteBuffer.wrap(img));
    gl.glDisable(gl.GL_TEXTURE_RECTANGLE);
    gl.glEnable(gl.GL_TEXTURE_RECTANGLE);
    gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, videot[1]);
    if(MiniClient.MAC_OS_X) {
      gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, 0x85BC/*GL_TEXTURE_STORAGE_HINT_APPLE*/, 0x85Be/*GL_STORAGE_CACHED_APPLE*/);
      gl.glPixelStorei(0x85B2/*GL_UNPACK_CLIENT_STORAGE_APPLE*/, gl.GL_TRUE);
    }
    gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
    gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
    gl.glTexImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 1, videowidth/2, videoheight/2, 0,
        gl.GL_LUMINANCE, gl.GL_UNSIGNED_BYTE, java.nio.ByteBuffer.wrap(img));
    gl.glDisable(gl.GL_TEXTURE_RECTANGLE);
    gl.glEnable(gl.GL_TEXTURE_RECTANGLE);
    gl.glBindTexture(gl.GL_TEXTURE_RECTANGLE, videot[2]);
    if(MiniClient.MAC_OS_X) {
      gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, 0x85BC/*GL_TEXTURE_STORAGE_HINT_APPLE*/, 0x85Be/*GL_STORAGE_CACHED_APPLE*/);
      gl.glPixelStorei(0x85B2/*GL_UNPACK_CLIENT_STORAGE_APPLE*/, gl.GL_TRUE);
    }
    gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR);
    gl.glTexParameteri(gl.GL_TEXTURE_RECTANGLE, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR);
    gl.glTexImage2D(gl.GL_TEXTURE_RECTANGLE, 0, 1, videowidth/2, videoheight/2, 0,
        gl.GL_LUMINANCE, gl.GL_UNSIGNED_BYTE, java.nio.ByteBuffer.wrap(img));
    gl.glDisable(gl.GL_TEXTURE_RECTANGLE);
  }
  if(!owner.isInFrame()) owner.getPbuffer().getContext().release();
}
 
Example #27
Source File: GraphRenderable.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
 * Make our GL context current. It may not be as the glyph rendering may
 * have switched it. The JOGL way to do this is to switch contexts multiple
 * times in the one frame. Chapter 2 of 'Pro Java 6 3D Game Development' has
 * a section that explains:
 *
 * "
 * This coding approach means that the context is current for the entire
 * duration of the thread's execution. This causes no problems on most
 * platforms (e.g. it's fine on Windows), but unfortunately there's an issue
 * when using X11. On X11 platforms, a AWT lock is created between the
 * GLContext.makeCurrent() and GLContext.release() calls, stopping mouse and
 * keyboard input from being processed. "
 *
 * The JOGL model of the event listener's display being called in response
 * to input events, as opposed to a continuous render loop, means that this
 * lock may be responsible for the glyph context being current when the
 * other batchers are making their draw calls.
 *
 * This 'fix' was tested on El Capitan 10.11 and could be breaking on other
 * versions. Testing required.
 *
 * @param gl
 */
private void makeContentCurrent(GL3 gl) {
    GLContext context = gl.getContext();
    try {
        while (context.makeCurrent() == GLContext.CONTEXT_NOT_CURRENT) {
            Thread.sleep(100);
        }
    } catch (InterruptedException ex) {
        final String msg
                = "Unable to switch GL context.  This code should only be run "
                + "on OSX and may need to be restricted to specific versions "
                + "where label rendering is broken.  "
                + "Please inform CONSTELLATION support, including the text of this message.\n\n"
                + ex.getMessage();
        Logger.getLogger(GraphRenderable.class
                .getName()).log(Level.SEVERE, msg, ex);
        final InfoTextPanel itp = new InfoTextPanel(msg);
        final NotifyDescriptor.Message nd = new NotifyDescriptor.Message(itp, NotifyDescriptor.ERROR_MESSAGE);
        nd.setTitle("Graph Render Error");
        DialogDisplayer.getDefault().notify(nd);
    }
}
 
Example #28
Source File: GLTools.java    From constellation with Apache License 2.0 4 votes vote down vote up
/**
 * Load an array of icon textures.
 * <p>
 * We assume that the textures being loaded are icons, and therefore are
 * roughly the same size (with a maximum of (width,height).
 * <p>
 * The array is limited to GL_MAX_ARRAY_TEXTURE_LAYERS layers. This can be
 * fairly low (512 on low-end systems), so icons are loaded into an 8x8 icon
 * matrix in each layer, thus giving a maximum of 512x8x8=32768 icons. (This
 * assumes that GL_MAX_3D_TEXTURE_SIZE is big enough to take that many
 * pixels. With the current icon size of 256x256, then
 * GL_MAX_3D_TEXTURE_SIZE must be at least 2048.)
 * <p>
 * Icons that are smaller than (width,height) are offset so they are
 * centred, so the shader can just draw the icons without worrying about
 * where in the texture they are.
 * <p>
 * It appears that the images must have a row length that is a multiple of
 * four. This is probably due to the particular format we're using, and
 * could probably be worked around, but the simple fix is to check your row
 * length.
 *
 * @param glCurrent the current OpenGL context.
 * @param icons a list of icons that need to added to the buffer.
 * @param width the width of each icon.
 * @param height the height of each icon.
 *
 * @return the id of the texture buffer.
 */
public static int loadSharedIconTextures(final GL3 glCurrent, final List<ConstellationIcon> icons, final int width, final int height) {
    final int[] v = new int[1];
    glCurrent.glGetIntegerv(GL3.GL_MAX_ARRAY_TEXTURE_LAYERS, v, 0);
    final int maxIcons = v[0] * 64;
    if (icons.size() > maxIcons) {
        System.out.printf("****\n**** Warning: nIcons %d > GL_MAX_ARRAY_TEXTURE_LAYERS %d\n****\n", icons.size(), maxIcons);
    }

    final int nIcons = Math.min(icons.size(), maxIcons);

    glCurrent.getContext().release();
    final GL3 gl = (GL3) SharedDrawable.getSharedAutoDrawable().getGL();
    final int result = gl.getContext().makeCurrent();
    if (result == GLContext.CONTEXT_NOT_CURRENT) {
        glCurrent.getContext().makeCurrent();
        throw new RenderException("Could not make texture context current.");
    }

    final int[] textureName = new int[1];
    try {
        textureName[0] = SharedDrawable.getIconTextureName();
        gl.glBindTexture(GL3.GL_TEXTURE_2D_ARRAY, textureName[0]);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_WRAP_S, GL3.GL_CLAMP_TO_EDGE);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_WRAP_T, GL3.GL_CLAMP_TO_EDGE);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_LINEAR);
        gl.glTexParameteri(GL3.GL_TEXTURE_2D_ARRAY, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_LINEAR);
        gl.glTexImage3D(GL3.GL_TEXTURE_2D_ARRAY, 0, GL.GL_RGBA, width * 8, height * 8, (nIcons + 63) / 64, 0, GL.GL_RGBA, GL3.GL_UNSIGNED_BYTE, null);

        final Iterator<ConstellationIcon> iconIterator = icons.iterator();
        for (int i = 0; i < nIcons; i++) {
            final ConstellationIcon icon = iconIterator.next();
            try {
                BufferedImage iconImage = icon.buildBufferedImage();

                if (iconImage != null) {
                    // Appears to be a bug in JOGL where texture provider for PNG files does not flip the texture.
                    final TextureData data = AWTTextureIO.newTextureData(gl.getGLProfile(), iconImage, false);

                    if (data.getWidth() > width || data.getHeight() > height) {
                        throw new RenderException(String.format("Image %d is too large (width %d>%d, height %d>%d)", i, data.getWidth(), width, data.getHeight(), height));
                    }

                    // Offset each icon into an 8x8 matrix.
                    // There are multiple icons in each
                    // Allow for icons that are smaller than width,height.
                    final int xoffset = (width - data.getWidth()) / 2 + (width * (i & 7));
                    final int yoffset = (height - data.getHeight()) / 2 + (height * ((i >>> 3) & 7));
                    final int zoffset = i >>> 6;
                    gl.glTexSubImage3D(GL3.GL_TEXTURE_2D_ARRAY, 0, xoffset, yoffset, zoffset, data.getWidth(), data.getHeight(), 1, data.getPixelFormat(), GL3.GL_UNSIGNED_BYTE, data.getBuffer());
                    data.destroy();
                }
            } catch (final RuntimeException ex) {
                System.out.printf("##%n## GLTools.loadTextures() icon %d throwable: %s%n##%n", i, ex);
                LOGGER.log(Level.SEVERE, null, ex);
            }
        }
    } finally {
        gl.getContext().release();
        glCurrent.getContext().makeCurrent();
    }

    return textureName[0];
}