Search in sources :

Example 1 with ContentFormat

use of maspack.render.TextureContent.ContentFormat in project artisynth_core by artisynth.

the class GLTextureLoader method createTexture.

public GLTexture createTexture(GL gl, int target, TextureContent content) {
    // create the texture ID for this texture
    int textureID = createTextureId(gl);
    GLTexture texture = new GLTexture(target, textureID);
    int width = content.getWidth();
    int height = content.getHeight();
    int pixelSize = content.getPixelSize();
    texture.setWidth(width);
    texture.setHeight(height);
    int glFormat = 0;
    int glType = 0;
    int[] swizzle = null;
    ContentFormat format = content.getFormat();
    switch(format) {
        case GRAYSCALE_ALPHA_BYTE_2:
            if (gl.isGL3()) {
                glFormat = GL3.GL_RG;
                swizzle = SWIZZLE_GRAY_ALPHA;
            } else if (gl.isGL2()) {
                glFormat = GL2.GL_LUMINANCE_ALPHA;
            }
            glType = GL.GL_UNSIGNED_BYTE;
            break;
        case GRAYSCALE_ALPHA_SHORT_2:
            if (gl.isGL3()) {
                glFormat = GL3.GL_RG;
                swizzle = SWIZZLE_GRAY_ALPHA;
            } else if (gl.isGL2()) {
                glFormat = GL2.GL_LUMINANCE_ALPHA;
            }
            glType = GL.GL_UNSIGNED_SHORT;
            break;
        case GRAYSCALE_BYTE:
            if (gl.isGL3()) {
                glFormat = GL3.GL_RED;
                swizzle = SWIZZLE_GRAY;
            } else if (gl.isGL2()) {
                glFormat = GL2.GL_LUMINANCE;
            }
            glType = GL.GL_BYTE;
            break;
        case GRAYSCALE_UBYTE:
            if (gl.isGL3()) {
                glFormat = GL3.GL_RED;
                swizzle = SWIZZLE_GRAY;
            } else if (gl.isGL2()) {
                glFormat = GL2.GL_LUMINANCE;
            }
            glType = GL.GL_UNSIGNED_BYTE;
            break;
        case GRAYSCALE_SHORT:
            if (gl.isGL3()) {
                glFormat = GL3.GL_RED;
                swizzle = SWIZZLE_GRAY;
            } else if (gl.isGL2()) {
                glFormat = GL2.GL_LUMINANCE;
            }
            glType = GL.GL_SHORT;
            break;
        case GRAYSCALE_USHORT:
            if (gl.isGL3()) {
                glFormat = GL3.GL_RED;
                swizzle = SWIZZLE_GRAY;
            } else if (gl.isGL2()) {
                glFormat = GL2.GL_LUMINANCE;
            }
            glType = GL.GL_UNSIGNED_SHORT;
            break;
        case RGBA_BYTE_4:
            glFormat = GL.GL_RGBA;
            glType = GL.GL_UNSIGNED_BYTE;
            break;
        case RGBA_INTEGER:
            glFormat = GL2GL3.GL_RGBA_INTEGER;
            glType = GL2GL3.GL_UNSIGNED_INT_8_8_8_8;
            break;
        case RGB_BYTE_3:
            glFormat = GL.GL_RGB;
            glType = GL.GL_UNSIGNED_BYTE;
            break;
        default:
            break;
    }
    ByteBuffer buff = BufferUtilities.newNativeByteBuffer(width * height * pixelSize);
    content.getData(buff);
    buff.flip();
    texture.fill(gl, width, height, pixelSize, glFormat, glType, swizzle, buff);
    buff = BufferUtilities.freeDirectBuffer(buff);
    content.markClean();
    return texture;
}
Also used : ContentFormat(maspack.render.TextureContent.ContentFormat) ByteBuffer(java.nio.ByteBuffer)

Example 2 with ContentFormat

use of maspack.render.TextureContent.ContentFormat in project artisynth_core by artisynth.

the class GLSharedResources method maybeUpdateTexture.

private boolean maybeUpdateTexture(GL gl, GLTexture texture, TextureContent content) {
    boolean update = false;
    Rectangle dirty = content.getDirty();
    if (dirty != null) {
        ContentFormat format = content.getFormat();
        int glFormat = 0;
        int glType = 0;
        int[] swizzle = null;
        switch(format) {
            case GRAYSCALE_ALPHA_BYTE_2:
                if (gl.isGL3()) {
                    glFormat = GL3.GL_RG;
                    swizzle = GLTextureLoader.SWIZZLE_GRAY_ALPHA;
                } else if (gl.isGL2()) {
                    glFormat = GL2.GL_LUMINANCE_ALPHA;
                }
                glType = GL.GL_UNSIGNED_BYTE;
                break;
            case GRAYSCALE_ALPHA_SHORT_2:
                if (gl.isGL3()) {
                    glFormat = GL3.GL_RG;
                    swizzle = GLTextureLoader.SWIZZLE_GRAY_ALPHA;
                } else if (gl.isGL2()) {
                    glFormat = GL2.GL_LUMINANCE_ALPHA;
                }
                glType = GL.GL_UNSIGNED_SHORT;
                break;
            case GRAYSCALE_BYTE:
                if (gl.isGL3()) {
                    glFormat = GL3.GL_RED;
                    swizzle = GLTextureLoader.SWIZZLE_GRAY;
                } else if (gl.isGL2()) {
                    glFormat = GL2.GL_LUMINANCE;
                }
                glType = GL.GL_BYTE;
                break;
            case GRAYSCALE_UBYTE:
                if (gl.isGL3()) {
                    glFormat = GL3.GL_RED;
                    swizzle = GLTextureLoader.SWIZZLE_GRAY;
                } else if (gl.isGL2()) {
                    glFormat = GL2.GL_LUMINANCE;
                }
                glType = GL.GL_UNSIGNED_BYTE;
                break;
            case GRAYSCALE_SHORT:
                if (gl.isGL3()) {
                    glFormat = GL3.GL_RED;
                    swizzle = GLTextureLoader.SWIZZLE_GRAY;
                } else if (gl.isGL2()) {
                    glFormat = GL2.GL_LUMINANCE;
                }
                glType = GL.GL_SHORT;
                break;
            case GRAYSCALE_USHORT:
                if (gl.isGL3()) {
                    glFormat = GL3.GL_RED;
                    swizzle = GLTextureLoader.SWIZZLE_GRAY;
                } else if (gl.isGL2()) {
                    glFormat = GL2.GL_LUMINANCE;
                }
                glType = GL.GL_UNSIGNED_SHORT;
                break;
            case RGBA_BYTE_4:
                glFormat = GL.GL_RGBA;
                glType = GL.GL_UNSIGNED_BYTE;
                if (gl.isGL3()) {
                    swizzle = GLTextureLoader.SWIZZLE_RGBA;
                }
                break;
            case RGBA_INTEGER:
                glFormat = GL2GL3.GL_RGBA_INTEGER;
                glType = GL2GL3.GL_UNSIGNED_INT_8_8_8_8;
                if (gl.isGL3()) {
                    swizzle = GLTextureLoader.SWIZZLE_RGBA;
                }
                break;
            case RGB_BYTE_3:
                glFormat = GL.GL_RGB;
                glType = GL.GL_UNSIGNED_BYTE;
                if (gl.isGL3()) {
                    swizzle = GLTextureLoader.SWIZZLE_RGBA;
                }
                break;
            default:
                break;
        }
        update = true;
        int psize = content.getPixelSize();
        int width = dirty.width();
        int height = dirty.height();
        if (texture.getFormat() != glFormat || texture.getType() != glType) {
            // re-create entire texture
            width = content.getWidth();
            height = content.getHeight();
            ByteBuffer buff = BufferUtilities.newNativeByteBuffer(width * height * psize);
            content.getData(buff);
            buff.flip();
            texture.fill(gl, width, height, psize, glFormat, glType, swizzle, buff);
            buff = BufferUtilities.freeDirectBuffer(buff);
        } else {
            ByteBuffer buff = BufferUtilities.newNativeByteBuffer(width * height * psize);
            content.getData(dirty, buff);
            buff.flip();
            texture.fill(gl, dirty.x(), dirty.y(), width, height, psize, glFormat, glType, buff);
            buff = BufferUtilities.freeDirectBuffer(buff);
        }
        content.markClean();
    }
    return update;
}
Also used : ContentFormat(maspack.render.TextureContent.ContentFormat) Rectangle(maspack.util.Rectangle) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 ContentFormat (maspack.render.TextureContent.ContentFormat)2 Rectangle (maspack.util.Rectangle)1