Search in sources :

Example 1 with GL

use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.

the class GLRenderer method updateTexImageData.

/**
     * Uploads the given image to the GL driver.
     *
     * @param img The image to upload
     * @param type How the data in the image argument should be interpreted.
     * @param unit The texture slot to be used to upload the image, not important
     * @param scaleToPot If true, the image will be scaled to power-of-2 dimensions
     * before being uploaded.
     */
public void updateTexImageData(Image img, Texture.Type type, int unit, boolean scaleToPot) {
    int texId = img.getId();
    if (texId == -1) {
        // create texture
        gl.glGenTextures(intBuf1);
        texId = intBuf1.get(0);
        img.setId(texId);
        objManager.registerObject(img);
        statistics.onNewTexture();
    }
    // bind texture
    int target = convertTextureType(type, img.getMultiSamples(), -1);
    bindTextureAndUnit(target, img, unit);
    if (!img.hasMipmaps() && img.isGeneratedMipmapsRequired()) {
        if (!caps.contains(Caps.FrameBuffer) && gl2 != null) {
            gl2.glTexParameteri(target, GL2.GL_GENERATE_MIPMAP, GL.GL_TRUE);
            img.setMipmapsGenerated(true);
        } else {
        // For OpenGL3 and up.
        // We'll generate mipmaps via glGenerateMipmapEXT (see below)
        }
    } else if (img.hasMipmaps()) {
        // Image already has mipmaps, set the max level based on the 
        // number of mipmaps we have.
        gl.glTexParameteri(target, GL.GL_TEXTURE_MAX_LEVEL, img.getMipMapSizes().length - 1);
    } else {
        // Image does not have mipmaps and they are not required.
        // Specify that that the texture has no mipmaps.
        gl.glTexParameteri(target, GL.GL_TEXTURE_MAX_LEVEL, 0);
    }
    int imageSamples = img.getMultiSamples();
    if (imageSamples > 1) {
        if (img.getFormat().isDepthFormat()) {
            img.setMultiSamples(Math.min(limits.get(Limits.DepthTextureSamples), imageSamples));
        } else {
            img.setMultiSamples(Math.min(limits.get(Limits.ColorTextureSamples), imageSamples));
        }
    }
    // Check if graphics card doesn't support multisample textures
    if (!caps.contains(Caps.TextureMultisample)) {
        if (img.getMultiSamples() > 1) {
            throw new RendererException("Multisample textures are not supported by the video hardware");
        }
    }
    // Check if graphics card doesn't support depth textures
    if (img.getFormat().isDepthFormat() && !caps.contains(Caps.DepthTexture)) {
        throw new RendererException("Depth textures are not supported by the video hardware");
    }
    if (target == GL.GL_TEXTURE_CUBE_MAP) {
        // Check max texture size before upload
        int cubeSize = limits.get(Limits.CubemapSize);
        if (img.getWidth() > cubeSize || img.getHeight() > cubeSize) {
            throw new RendererException("Cannot upload cubemap " + img + ". The maximum supported cubemap resolution is " + cubeSize);
        }
        if (img.getWidth() != img.getHeight()) {
            throw new RendererException("Cubemaps must have square dimensions");
        }
    } else {
        int texSize = limits.get(Limits.TextureSize);
        if (img.getWidth() > texSize || img.getHeight() > texSize) {
            throw new RendererException("Cannot upload texture " + img + ". The maximum supported texture resolution is " + texSize);
        }
    }
    Image imageForUpload;
    if (scaleToPot) {
        imageForUpload = MipMapGenerator.resizeToPowerOf2(img);
    } else {
        imageForUpload = img;
    }
    if (target == GL.GL_TEXTURE_CUBE_MAP) {
        List<ByteBuffer> data = imageForUpload.getData();
        if (data.size() != 6) {
            logger.log(Level.WARNING, "Invalid texture: {0}\n" + "Cubemap textures must contain 6 data units.", img);
            return;
        }
        for (int i = 0; i < 6; i++) {
            texUtil.uploadTexture(imageForUpload, GL.GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, i, linearizeSrgbImages);
        }
    } else if (target == GLExt.GL_TEXTURE_2D_ARRAY_EXT) {
        if (!caps.contains(Caps.TextureArray)) {
            throw new RendererException("Texture arrays not supported by graphics hardware");
        }
        List<ByteBuffer> data = imageForUpload.getData();
        // -1 index specifies prepare data for 2D Array
        texUtil.uploadTexture(imageForUpload, target, -1, linearizeSrgbImages);
        for (int i = 0; i < data.size(); i++) {
            // upload each slice of 2D array in turn
            // this time with the appropriate index
            texUtil.uploadTexture(imageForUpload, target, i, linearizeSrgbImages);
        }
    } else {
        texUtil.uploadTexture(imageForUpload, target, 0, linearizeSrgbImages);
    }
    if (img.getMultiSamples() != imageSamples) {
        img.setMultiSamples(imageSamples);
    }
    if (caps.contains(Caps.FrameBuffer) || gl2 == null) {
        if (!img.hasMipmaps() && img.isGeneratedMipmapsRequired() && img.getData(0) != null) {
            glfbo.glGenerateMipmapEXT(target);
            img.setMipmapsGenerated(true);
        }
    }
    img.clearUpdateNeeded();
}
Also used : List(java.util.List) Image(com.jme3.texture.Image)

Example 2 with GL

use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.

the class KTXLoader method getImageFormat.

/**
     * returns the JME image format from gl formats and types.
     * @param glFormat
     * @param glInternalFormat
     * @param glType
     * @return 
     */
private Image.Format getImageFormat(int glFormat, int glInternalFormat, int glType) {
    EnumSet<Caps> caps = EnumSet.allOf(Caps.class);
    GLImageFormat[][] formats = GLImageFormats.getFormatsForCaps(caps);
    for (GLImageFormat[] format : formats) {
        for (int j = 0; j < format.length; j++) {
            GLImageFormat glImgFormat = format[j];
            if (glImgFormat != null) {
                if (glImgFormat.format == glFormat && glImgFormat.dataType == glType) {
                    if (glFormat == glInternalFormat || glImgFormat.internalFormat == glInternalFormat) {
                        return Image.Format.values()[j];
                    }
                }
            }
        }
    }
    return null;
}
Also used : GLImageFormat(com.jme3.renderer.opengl.GLImageFormat) Caps(com.jme3.renderer.Caps)

Example 3 with GL

use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.

the class LwjglContext method initContextFirstTime.

protected void initContextFirstTime() {
    final GLCapabilities capabilities = createCapabilities(settings.getRenderer().equals(AppSettings.LWJGL_OPENGL3));
    if (!capabilities.OpenGL20) {
        throw new RendererException("OpenGL 2.0 or higher is required for jMonkeyEngine");
    }
    if (settings.getRenderer().equals(AppSettings.LWJGL_OPENGL2) || settings.getRenderer().equals(AppSettings.LWJGL_OPENGL3)) {
        GL gl = new LwjglGL();
        GLExt glext = new LwjglGLExt();
        GLFbo glfbo;
        if (capabilities.OpenGL30) {
            glfbo = new LwjglGLFboGL3();
        } else {
            glfbo = new LwjglGLFboEXT();
        }
        if (settings.getBoolean("GraphicsDebug")) {
            gl = new GLDebugDesktop(gl, glext, glfbo);
            glext = (GLExt) gl;
            glfbo = (GLFbo) gl;
        }
        if (settings.getBoolean("GraphicsTiming")) {
            GLTimingState timingState = new GLTimingState();
            gl = (GL) GLTiming.createGLTiming(gl, timingState, GL.class, GL2.class, GL3.class, GL4.class);
            glext = (GLExt) GLTiming.createGLTiming(glext, timingState, GLExt.class);
            glfbo = (GLFbo) GLTiming.createGLTiming(glfbo, timingState, GLFbo.class);
        }
        if (settings.getBoolean("GraphicsTrace")) {
            gl = (GL) GLTracer.createDesktopGlTracer(gl, GL.class, GL2.class, GL3.class, GL4.class);
            glext = (GLExt) GLTracer.createDesktopGlTracer(glext, GLExt.class);
            glfbo = (GLFbo) GLTracer.createDesktopGlTracer(glfbo, GLFbo.class);
        }
        renderer = new GLRenderer(gl, glext, glfbo);
        renderer.initialize();
    } else {
        throw new UnsupportedOperationException("Unsupported renderer: " + settings.getRenderer());
    }
    if (capabilities.GL_ARB_debug_output && settings.getBoolean("GraphicsDebug")) {
        ARBDebugOutput.glDebugMessageCallbackARB(new LwjglGLDebugOutputHandler(), 0);
    }
    renderer.setMainFrameBufferSrgb(settings.isGammaCorrection());
    renderer.setLinearizeSrgbImages(settings.isGammaCorrection());
    // Init input
    if (keyInput != null) {
        keyInput.initialize();
    }
    if (mouseInput != null) {
        mouseInput.initialize();
    }
    if (joyInput != null) {
        joyInput.initialize();
    }
    renderable.set(true);
}
Also used : GLFbo(com.jme3.renderer.opengl.GLFbo) GLRenderer(com.jme3.renderer.opengl.GLRenderer) GL(com.jme3.renderer.opengl.GL) LwjglGL(com.jme3.renderer.lwjgl.LwjglGL) LwjglGLExt(com.jme3.renderer.lwjgl.LwjglGLExt) LwjglGLFboGL3(com.jme3.renderer.lwjgl.LwjglGLFboGL3) LwjglGLExt(com.jme3.renderer.lwjgl.LwjglGLExt) GLExt(com.jme3.renderer.opengl.GLExt) GLCapabilities(org.lwjgl.opengl.GLCapabilities) RendererException(com.jme3.renderer.RendererException) GLDebugDesktop(com.jme3.renderer.opengl.GLDebugDesktop) GLTimingState(com.jme3.renderer.opengl.GLTimingState) LwjglGLFboEXT(com.jme3.renderer.lwjgl.LwjglGLFboEXT) LwjglGL(com.jme3.renderer.lwjgl.LwjglGL)

Example 4 with GL

use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.

the class OpaqueComparatorTest method testSort.

/**
     * Given a correctly sorted list of materials, check if the 
     * opaque comparator can sort a reversed list of them.
     * 
     * Each material will be cloned so that none of them will be equal to each other
     * in reference, forcing the comparator to compare the material sort ID.
     * 
     * E.g. for a list of materials A, B, C, the following list will be generated:
     * <pre>C, B, A, C, B, A, C, B, A</pre>, it should result in
     * <pre>A, A, A, B, B, B, C, C, C</pre>.
     * 
     * @param materials The pre-sorted list of materials to check sorting for.
     */
private void testSort(Material... materials) {
    GeometryList gl = new GeometryList(comparator);
    for (int g = 0; g < 5; g++) {
        for (int i = materials.length - 1; i >= 0; i--) {
            Geometry geom = new Geometry("geom", mesh);
            Material clonedMaterial = materials[i].clone();
            if (materials[i].getActiveTechnique() != null) {
                String techniqueName = materials[i].getActiveTechnique().getDef().getName();
                clonedMaterial.selectTechnique(techniqueName, renderManager);
            } else {
                clonedMaterial.selectTechnique(TechniqueDef.DEFAULT_TECHNIQUE_NAME, renderManager);
            }
            geom.setMaterial(clonedMaterial);
            gl.add(geom);
        }
    }
    gl.sort();
    for (int i = 0; i < gl.size(); i++) {
        Material mat = gl.get(i).getMaterial();
        String sortId = Integer.toHexString(mat.getSortId()).toUpperCase();
        System.out.print(sortId + "\t");
        System.out.println(mat);
    }
    Set<String> alreadySeen = new HashSet<String>();
    Material current = null;
    for (int i = 0; i < gl.size(); i++) {
        Material mat = gl.get(i).getMaterial();
        if (current == null) {
            current = mat;
        } else if (!current.getName().equals(mat.getName())) {
            assert !alreadySeen.contains(mat.getName());
            alreadySeen.add(current.getName());
            current = mat;
        }
    }
    for (int i = 0; i < materials.length; i++) {
        for (int g = 0; g < 5; g++) {
            int index = i * 5 + g;
            Material mat1 = gl.get(index).getMaterial();
            Material mat2 = materials[i];
            assert mat1.getName().equals(mat2.getName()) : mat1.getName() + " != " + mat2.getName() + " for index " + index;
        }
    }
}
Also used : Geometry(com.jme3.scene.Geometry) GeometryList(com.jme3.renderer.queue.GeometryList) Material(com.jme3.material.Material) HashSet(java.util.HashSet)

Example 5 with GL

use of com.jme3.renderer.opengl.GL in project jmonkeyengine by jMonkeyEngine.

the class JoglContext method determineMaxSamples.

protected int determineMaxSamples(int requestedSamples) {
    GL gl = GLContext.getCurrentGL();
    if (gl.hasFullFBOSupport()) {
        return gl.getMaxRenderbufferSamples();
    } else {
        if (gl.isExtensionAvailable("GL_ARB_framebuffer_object") || gl.isExtensionAvailable("GL_EXT_framebuffer_multisample")) {
            IntBuffer intBuf1 = IntBuffer.allocate(1);
            gl.glGetIntegerv(GL2GL3.GL_MAX_SAMPLES, intBuf1);
            return intBuf1.get(0);
        } else {
            return Integer.MAX_VALUE;
        }
    }
}
Also used : GL(com.jogamp.opengl.GL) JoglGL(com.jme3.renderer.jogl.JoglGL) IntBuffer(java.nio.IntBuffer)

Aggregations

GLExt (com.jme3.renderer.opengl.GLExt)4 GLFbo (com.jme3.renderer.opengl.GLFbo)4 GLRenderer (com.jme3.renderer.opengl.GLRenderer)4 RendererException (com.jme3.renderer.RendererException)3 GL (com.jme3.renderer.opengl.GL)3 Image (com.jme3.texture.Image)3 GL (com.jogamp.opengl.GL)3 JoglGL (com.jme3.renderer.jogl.JoglGL)2 LwjglGL (com.jme3.renderer.lwjgl.LwjglGL)2 LwjglGLExt (com.jme3.renderer.lwjgl.LwjglGLExt)2 LwjglGLFboEXT (com.jme3.renderer.lwjgl.LwjglGLFboEXT)2 LwjglGLFboGL3 (com.jme3.renderer.lwjgl.LwjglGLFboGL3)2 GLDebugDesktop (com.jme3.renderer.opengl.GLDebugDesktop)2 GLTimingState (com.jme3.renderer.opengl.GLTimingState)2 Format (com.jme3.texture.Image.Format)2 ByteBuffer (java.nio.ByteBuffer)2 GLCapabilities (org.lwjgl.opengl.GLCapabilities)2 IosInputHandler (com.jme3.input.ios.IosInputHandler)1 Material (com.jme3.material.Material)1 Caps (com.jme3.renderer.Caps)1