Example usage for org.lwjgl.opengl GL11 glTexImage2D

List of usage examples for org.lwjgl.opengl GL11 glTexImage2D

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL11 glTexImage2D.

Prototype

public static void glTexImage2D(@NativeType("GLenum") int target, @NativeType("GLint") int level,
        @NativeType("GLint") int internalformat, @NativeType("GLsizei") int width,
        @NativeType("GLsizei") int height, @NativeType("GLint") int border, @NativeType("GLenum") int format,
        @NativeType("GLenum") int type, @Nullable @NativeType("void const *") double[] pixels) 

Source Link

Document

Array version of: #glTexImage2D TexImage2D

Usage

From source file:eu.over9000.veya.gui.TrueTypeFont.java

License:Open Source License

private void buildTexture(final BufferedImage imgTemp) throws IOException {
    textureId = GL11.glGenTextures();//from w w  w.ja va 2 s  .  c  o  m

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);

    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    ImageIO.write(imgTemp, "png", byteArrayOutputStream);
    final PNGDecoder decoder = new PNGDecoder(new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
    final ByteBuffer buffer = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight());
    decoder.decode(buffer, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
    buffer.flip();

    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, decoder.getWidth(), decoder.getHeight(), 0,
            GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
}

From source file:eu.over9000.veya.rendering.Shadow.java

License:Open Source License

public void init() {
    depthMap = GL11.glGenTextures();/*from   ww w  .j a v a2 s .  c  o  m*/
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, depthMap);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_DEPTH_COMPONENT, SHADOW_WIDTH, SHADOW_HEIGHT, 0,
            GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (ByteBuffer) null);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL13.GL_CLAMP_TO_BORDER);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL13.GL_CLAMP_TO_BORDER);

    shadowFBO = GL30.glGenFramebuffers();
    GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, shadowFBO);
    GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, depthMap, 0);
    GL11.glDrawBuffer(GL11.GL_NONE);
    GL11.glReadBuffer(GL11.GL_NONE);
    GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
}

From source file:eu.over9000.veya.util.TextureLoader.java

License:Open Source License

public static int loadFontTexture(final int textureUnit) {

    final int texId = GL11.glGenTextures();

    try {//from   w ww. j  a  v a2 s  . c  o  m
        final InputStream in = TextureLoader.class.getResourceAsStream("/textures/font.png");
        final PNGDecoder decoder = new PNGDecoder(in);

        final int sourceTexWidth = decoder.getWidth();
        final int sourceTexHeight = decoder.getHeight();

        final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(byteBuffer, decoder.getWidth() * 4, Format.RGBA);
        byteBuffer.flip();

        GL13.glActiveTexture(textureUnit);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, sourceTexWidth, sourceTexHeight, 0, GL11.GL_RGBA,
                GL11.GL_UNSIGNED_BYTE, byteBuffer);

        // Setup the ST coordinate system
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

        // Setup what to do when the texture has to be scaled
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);

        org.lwjgl.opengl.Util.checkGLError();

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

    } catch (final IOException e) {
        e.printStackTrace();
        System.exit(-1);
    }

    return texId;
}

From source file:fi.conf.ae.gl.texture.GLTextureRoutines.java

License:LGPL

public static void initializeTexture(int textureID, byte[] imageData, int width, int height) {

    ByteBuffer byteBuffer = BufferUtils.createByteBuffer(width * height * 4);

    byteBuffer.put(imageData);//from  w  w w.  jav  a  2s . c  o m
    byteBuffer.flip(); // rewind buffer, set limit, remove mark

    int prevTextureID = bindTexture(textureID);

    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, 4, width, height, 0, GL12.GL_BGRA, GL11.GL_UNSIGNED_BYTE,
            byteBuffer);

    bindTexture(prevTextureID);
}

From source file:fr.guillaume.prive.viper.core.model.texture.PNGTextureLoader.java

public static int loadTexture(String filename, int textureUnit) {
    ByteBuffer buf = null;//from w  ww  .  j av a 2  s.c  om
    int tWidth = 0;
    int tHeight = 0;

    try (InputStream in = new FileInputStream(filename)) {
        PNGDecoder decoder = new PNGDecoder(in);

        tWidth = decoder.getWidth();
        tHeight = decoder.getHeight();

        buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
        decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
        buf.flip();
        in.close();
    } catch (IOException e) {
        throw new IllegalStateException("PNG file i/o error", e);
    }

    // Create a new texture object in memory and bind it
    int texId = GL11.glGenTextures();
    GL13.glActiveTexture(textureUnit);
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);

    // All RGB bytes are aligned to each other and each component is 1 byte
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    // Upload the texture data and generate mip maps (for scaling)
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA16, tWidth, tHeight, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, buf);
    GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);

    // Setup the ST coordinate system
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);

    // Setup what to do when the texture has to be scaled
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR);

    GraphicMotor.exitOnGLError("loadPNGTexture");

    return texId;
}

From source file:fr.ign.cogit.geoxygene.appli.render.DisplayableTextRenderer.java

License:Open Source License

private void drawText() throws GLException {
    if (this.program == null || this.textImage == null) {
        Logger.getRootLogger().debug("The GeoxGLTextRenderer " + this.hashCode() + "is not ready yet");
        return;//from  www .j  av  a2 s  .  com
    }
    this.textImage.getRGB(0, 0, width, height, this.pixels, 0, width);
    this.buffer.rewind();
    for (int y = height - 1; y >= 0; y--) {
        for (int x = 0; x < width; x++) {
            int pixel = this.pixels[y * width + x];
            this.buffer.put((byte) (pixel >> 16 & 0xFF)); // Red component
            this.buffer.put((byte) (pixel >> 8 & 0xFF)); // Green component
            this.buffer.put((byte) (pixel >> 0 & 0xFF)); // Blue component
            this.buffer.put((byte) (pixel >> 24 & 0xFF)); // Alpha component
        }
    }
    this.buffer.rewind();
    glEnable(GL_TEXTURE_2D);
    GL13.glActiveTexture(GL13.GL_TEXTURE0 + 2);
    glBindTexture(GL_TEXTURE_2D, this.getTextTextureId());

    // Setup texture scaling filtering
    GL11.glTexParameteri(GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexImage2D(GL_TEXTURE_2D, 0, GL11.GL_RGBA8, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE,
            this.buffer);

    Integer fbow = (Integer) GLContext.getActiveGlContext()
            .getSharedUniform(GeoxygeneConstants.GL_VarName_FboWidth);
    Integer fboh = (Integer) GLContext.getActiveGlContext()
            .getSharedUniform(GeoxygeneConstants.GL_VarName_FboHeight);
    GL11.glViewport(0, 0, fbow, fboh);
    glDisable(GL11.GL_POLYGON_SMOOTH);
    GLContext.getActiveGlContext().setCurrentProgram(program);
    program.setUniform1i("colorTexture2", 2);
    GLTools.glCheckError("texture binding");

    GL11.glDepthMask(false);
    glDisable(GL11.GL_DEPTH_TEST);

    GL30.glBindVertexArray(LayerViewGLPanel.getScreenQuad().getVaoId());
    GLTools.glCheckError("before drawing textured quad VAO binding");

    program.setUniform(GeoxygeneConstants.GL_VarName_ObjectOpacityVarName, 1f);
    program.setUniform(GeoxygeneConstants.GL_VarName_GlobalOpacityVarName, 1f);
    glEnable(GL_BLEND);
    GL11.glBlendFunc(GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL);
    GLTools.glCheckError("blending set for textured quad");

    LwjglLayerRenderer.drawComplex(LayerViewGLPanel.getScreenQuad());
    GLTools.glCheckError("Drawing textured quad");

    GL30.glBindVertexArray(0); // unbind VAO
    GLTools.glCheckError("exiting Text rendering");
    glBindTexture(GL_TEXTURE_2D, 0); // unbind texture

}

From source file:go.graphics.swing.opengl.LWJGLDrawContext.java

License:Open Source License

@Override
public TextureHandle generateTexture(int width, int height, ShortBuffer data) {
    // 1 byte aligned.
    GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);

    int texture = GL11.glGenTextures();
    if (texture == 0) {
        return null;
    }//  ww  w .  j  a v a 2s.c om

    //fix strange alpha test problem (minimap and landscape are unaffected)
    ShortBuffer bfr = BufferUtils.createShortBuffer(data.capacity());
    int cap = data.capacity();
    for (int i = 0; i != cap; i++)
        bfr.put(i, data.get(i));

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA,
            GL12.GL_UNSIGNED_SHORT_4_4_4_4, bfr);
    setTextureParameters();

    return new LWJGLTextureHandle(this, texture);
}

From source file:illarion.graphics.lwjgl.TextureAtlasLWJGL.java

License:Open Source License

/**
 * Activate the texture and prepare it for usage by OpenGL.
 * //from  ww  w. j  av a  2  s.  co m
 * @param resizeable true in case the texture shall be loaded with advanced
 *            rescaling methods, that are more calculation intensive but
 *            look better then the normal ones
 * @param allowCompression true if the texture is compressed at default
 *            settings, false if not. Best disallow compression for static
 *            images such as tiles, since the effects of the compression
 *            will be quite visible there
 */
@Override
@SuppressWarnings("nls")
public void activateTexture(final boolean resizeable, final boolean allowCompression) {

    if (!hasTextureData()) {
        throw new IllegalStateException("No texturedata loaded");
    }

    final int quality = Graphics.getInstance().getQuality();

    if (getTextureID() != 0) {
        removeTexture();
    }

    // generate new texture ID
    final int texID = getNewTextureID();
    setTextureID(texID);

    // bind texture ID
    DriverSettingsLWJGL.getInstance().enableTexture(texID);

    // prepare texture data
    if (resizeable) { // Textures will be resized -> smoothing would be good
        if (quality <= Graphics.QUALITY_LOW) {
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
        } else if ((quality <= Graphics.QUALITY_NORMAL) || isNoMipMaps()) {
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        } else {
            if (GLContext.getCapabilities().OpenGL14) {
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_GENERATE_MIPMAP, GL11.GL_TRUE);
            } else {
                setNoMipMaps(true);
            }
            if (!isNoMipMaps()) {
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
                        GL11.GL_LINEAR_MIPMAP_LINEAR);
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
            } else {
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
                GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
            }
        }
    } else {
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
    }
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP);

    if (GLContext.getCapabilities().OpenGL13) {
        GL11.glHint(GL13.GL_TEXTURE_COMPRESSION_HINT, GL11.GL_NICEST);
    }

    // setup texture compression
    final boolean activateCompression = GLContext.getCapabilities().OpenGL13
            && ((allowCompression && (quality < Graphics.QUALITY_MAX)) || (quality <= Graphics.QUALITY_LOW));
    if (isTextureRGBA()) {
        internalFormat = GL11.GL_RGBA;
        sourceFormat = GL11.GL_RGBA;
        if (activateCompression) {
            internalFormat = GL13.GL_COMPRESSED_RGBA;
        }
    } else if (isTextureRGB()) {
        internalFormat = GL11.GL_RGB;
        sourceFormat = GL11.GL_RGB;
        if (activateCompression) {
            internalFormat = GL13.GL_COMPRESSED_RGB;
        }
    } else if (isTextureGrey()) {
        internalFormat = GL11.GL_LUMINANCE;
        sourceFormat = GL11.GL_LUMINANCE;
        if (activateCompression) {
            internalFormat = GL13.GL_COMPRESSED_LUMINANCE;
        }
    } else if (isTextureGreyAlpha()) {
        internalFormat = GL11.GL_LUMINANCE_ALPHA;
        sourceFormat = GL11.GL_LUMINANCE_ALPHA;
        if (activateCompression) {
            internalFormat = GL13.GL_COMPRESSED_LUMINANCE_ALPHA;
        }
    }

    final ByteBuffer texData = getTextureData();

    final int texWidth = getTextureWidth();
    final int texHeight = getTextureHeight();

    // produce a texture from the byte buffer
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, internalFormat, texWidth, texHeight, 0, sourceFormat,
            GL11.GL_UNSIGNED_BYTE, texData);

    if (quality < Graphics.QUALITY_MAX) {
        texIDBuffer.rewind();
        GL11.glGetTexLevelParameter(GL11.GL_TEXTURE_2D, 0, GL13.GL_TEXTURE_COMPRESSED, texIDBuffer);

        texData.rewind();
        if (texIDBuffer.get(0) == GL11.GL_FALSE) {
            int newInternalFormat = internalFormat;
            if (internalFormat == GL13.GL_COMPRESSED_LUMINANCE_ALPHA) {
                newInternalFormat = GL13.GL_COMPRESSED_RGBA;
            } else if (internalFormat == GL13.GL_COMPRESSED_LUMINANCE) {
                newInternalFormat = GL13.GL_COMPRESSED_RGB;
            }
            final int orgSize = texData.remaining();
            if (newInternalFormat != internalFormat) {
                GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, newInternalFormat, texWidth, texHeight, 0,
                        sourceFormat, GL11.GL_UNSIGNED_BYTE, texData);
                GL11.glGetTexLevelParameter(GL11.GL_TEXTURE_2D, 0, GL13.GL_TEXTURE_COMPRESSED_IMAGE_SIZE,
                        texIDBuffer);
                final int newSize = texIDBuffer.get(0);
                if (newSize > orgSize) {
                    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, internalFormat, texWidth, texHeight, 0,
                            sourceFormat, GL11.GL_UNSIGNED_BYTE, texData);
                }
            }
        }
    }

    texIDBuffer.rewind();
    GL11.glGetTexLevelParameter(GL11.GL_TEXTURE_2D, 0, GL11.GL_TEXTURE_INTERNAL_FORMAT, texIDBuffer);
    internalFormat = texIDBuffer.get();

    discardImageData();
}

From source file:im.bci.jnuit.lwjgl.assets.AssetsLoader.java

License:Open Source License

private Texture loadPngTexture(String name) throws FileNotFoundException, IOException {
    InputStream is = vfs.open(name);
    try {//from  ww  w. ja  v a 2  s .  c  om
        PNGDecoder decoder = new PNGDecoder(is);
        int bpp;
        PNGDecoder.Format format;
        int pixelFormat;
        int texWidth = decoder.getWidth();
        int texHeight = decoder.getHeight();
        boolean hasAlpha = decoder.hasAlpha();
        if (hasAlpha) {
            bpp = 4;
            format = PNGDecoder.Format.RGBA;
            pixelFormat = GL11.GL_RGBA;
        } else {
            bpp = 3;
            format = PNGDecoder.Format.RGB;
            pixelFormat = GL11.GL_RGB;
        }

        int stride = bpp * texWidth;
        ByteBuffer buffer = ByteBuffer.allocateDirect(stride * texHeight);
        decoder.decode(buffer, stride, format);
        buffer.flip();
        Texture texture = new Texture(texWidth, texHeight, hasAlpha);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId());
        GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
        LwjglHelper.setupGLTextureParams();
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, pixelFormat, texWidth, texHeight, 0, pixelFormat,
                GL11.GL_UNSIGNED_BYTE, buffer);
        return texture;
    } finally {
        is.close();
    }
}

From source file:im.bci.jnuit.lwjgl.assets.AssetsLoader.java

License:Open Source License

private Texture loadJpegTexture(String name) throws IOException {
    InputStream is = vfs.open(name);
    try {//  www. j  a  v a  2 s. c om
        JPEGDecoder decoder = new JPEGDecoder(is);
        decoder.startDecode();
        int texWidth = decoder.getImageWidth();
        int texHeight = decoder.getImageHeight();
        int stride = 3 * texWidth;
        ByteBuffer buffer = ByteBuffer.allocateDirect(stride * texHeight);
        decoder.decode(buffer, stride, decoder.getNumMCURows(), YUVtoRGB.instance);
        buffer.flip();
        Texture texture = new Texture(texWidth, texHeight, false);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getId());
        GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
        LwjglHelper.setupGLTextureParams();
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, texWidth, texHeight, 0, GL11.GL_RGB,
                GL11.GL_UNSIGNED_BYTE, buffer);
        return texture;
    } finally {
        is.close();
    }
}