Example usage for org.lwjgl.opengl GL11 glBindTexture

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

Introduction

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

Prototype

public static void glBindTexture(@NativeType("GLenum") int target, @NativeType("GLuint") int texture) 

Source Link

Document

Binds the a texture to a texture target.

Usage

From source file:com.dinasgames.engine.graphics.GL.java

public static void bindTexture(int textureId) {
    if (version >= 11) {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId);
    }
}

From source file:com.dinasgames.engine.graphics.Texture.java

public boolean create(int width, int height) {

    if (width == 0 && height == 0) {
        return false;
    }/*from w ww  .j  a  v a2  s  .com*/

    Vector2i actualSize = new Vector2i(getValidSize(width), getValidSize(height));

    int maxSize = getMaximumSize();
    if ((actualSize.x > maxSize) || (actualSize.y > maxSize)) {
        System.out.println("Failed to create texture, its internal size is too high (" + actualSize.x + ", "
                + actualSize.y + ") maximum is (" + maxSize + ", " + maxSize + ")");
        return false;
    }

    mWidth = width;
    mHeight = height;
    mActualWidth = actualSize.x;
    mActualHeight = actualSize.y;
    mPixelsFlipped = false;

    if (mTexture <= 0) {
        mTexture = GL11.glGenTextures();
    }

    int textureEdgeClamp = GL.clampToEdge();

    GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);
    GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, mActualWidth, mActualHeight, 0, GL11.GL_RGBA,
            GL11.GL_UNSIGNED_BYTE, 0);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S,
            (mRepeated ? GL11.GL_REPEAT : textureEdgeClamp));
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T,
            (mRepeated ? GL11.GL_REPEAT : textureEdgeClamp));
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
            (mSmooth ? GL11.GL_LINEAR : GL11.GL_NEAREST));
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
            (mSmooth ? GL11.GL_LINEAR : GL11.GL_NEAREST));
    mCacheId = getUniqueId();

    return true;

}

From source file:com.dinasgames.engine.graphics.Texture.java

public boolean loadFromImage(Image image, BoundingBox area) {

    int width = image.getWidth();
    int height = image.getHeight();

    if (area.width == 0 || area.height == 0
            || (area.x <= 0 && area.y <= 0 && area.width >= width && area.height >= height)) {

        // Load entire image
        if (create(width, height)) {

            update(image);//from www  . j  a  va  2s  .  co m
            GL11.glFlush();

            return true;
        }

        return false;

    } else {

        // Load part of the image
        BoundingBox r = new BoundingBox(area);
        if (r.x < 0) {
            r.x = 0;
        }
        if (r.y < 0) {
            r.y = 0;
        }
        if (r.x + r.width > width) {
            r.width = width - r.x;
        }
        if (r.y + r.height > height) {
            r.height = height - r.y;
        }

        if (create((int) r.width, (int) r.height)) {

            ByteBuffer pixels = image.toByteBuffer();
            int pos = 4 * ((int) r.x + (width * (int) r.y));

            GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);

            for (int i = 0; i < r.height; i++) {
                GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, 0, i, (int) r.width, 1, GL11.GL_RGBA,
                        GL11.GL_UNSIGNED_BYTE, pixels);
                pos += 4 * width;
            }

            GL11.glFlush();

            return true;
        }

        return false;

    }

}

From source file:com.dinasgames.engine.graphics.Texture.java

public Image copyToImage() {

    if (mTexture <= 0) {
        return null;
    }/*from ww  w. ja  v a2s .  c o  m*/

    ByteBuffer pixels = BufferUtils.createByteBuffer(mWidth * mHeight * 4);

    if ((mWidth == mActualWidth && mHeight == mActualHeight) && !mPixelsFlipped) {
        // Direct copy
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);
        GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels);
    } else {

        ByteBuffer allPixels = BufferUtils.createByteBuffer(mActualWidth * mActualHeight * 4);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);
        GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, allPixels);

        byte[] src = allPixels.array();
        byte[] dst = pixels.array();
        int srcPitch = mActualWidth * 4;
        int dstPitch = mActualHeight * 4;
        int srcI = 0;
        int dstI = 0;

        if (mPixelsFlipped) {
            srcI += srcPitch * (mHeight - 1);
            srcPitch = -srcPitch;
        }

        int k = 0;
        for (int i = 0; i < mHeight; i++) {
            for (int j = 0; j < dstPitch; j++) {
                dst[k++] = src[i + j];
            }
            srcI += srcPitch;
            dstI += dstPitch;
        }

    }

    // Create the image
    Image image = new Image();
    image.create(mWidth, mHeight, pixels.array());

    return image;

}

From source file:com.dinasgames.engine.graphics.Texture.java

protected void update(ByteBuffer pixels, int width, int height, int x, int y) {

    assert (x + width <= mWidth);
    assert (y + height <= mHeight);

    if (pixels != null && mTexture > 0) {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);
        GL11.glTexSubImage2D(GL11.GL_TEXTURE_2D, 0, x, y, width, height, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE,
                pixels);//from w  w w .j  a v  a2s .co  m
        mPixelsFlipped = false;
        mCacheId = getUniqueId();
    }

}

From source file:com.dinasgames.engine.graphics.Texture.java

protected void update(Window window, int x, int y) {

    assert (x + window.getWidth() <= mWidth);
    assert (y + window.getHeight() <= mHeight);

    if (mTexture > 0 && window.setActive(true)) {
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);
        GL11.glCopyTexSubImage2D(GL11.GL_TEXTURE_2D, 0, x, y, 0, 0, window.getWidth(), window.getHeight());
        mPixelsFlipped = true;//  w w  w . j  a  v a  2  s  . co m
        mCacheId = getUniqueId();
    }

}

From source file:com.dinasgames.engine.graphics.Texture.java

public Texture setSmooth(boolean smooth) {

    if (smooth != mSmooth) {

        mSmooth = smooth;//from  ww w.  j av a2s . c o m

        if (mTexture > 0) {
            GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
                    (mSmooth ? GL11.GL_LINEAR : GL11.GL_NEAREST));
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
                    (mSmooth ? GL11.GL_LINEAR : GL11.GL_NEAREST));
        }

    }

    return this;

}

From source file:com.dinasgames.engine.graphics.Texture.java

public Texture setRepeated(boolean repeated) {

    if (mRepeated != repeated) {

        mRepeated = repeated;//from ww w .jav a  2 s. co m

        if (mTexture > 0) {

            int textureEdgeClamp = GL.clampToEdge();

            GL11.glBindTexture(GL11.GL_TEXTURE_2D, mTexture);
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S,
                    (mRepeated ? GL11.GL_REPEAT : textureEdgeClamp));
            GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T,
                    (mRepeated ? GL11.GL_REPEAT : textureEdgeClamp));

        }

    }

    return this;

}

From source file:com.dinasgames.engine.graphics.Texture.java

public static void bind(Texture texture, CoordinateType type) {

    if (texture != null && texture.getTextureID() > 0) {

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());

        if ((type == CoordinateType.Pixels) || texture.getPixelsFlipped()) {

            float matrix[] = new float[] { 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f,
                    0.f, 1.f };//from  w  w  w  . j  av a 2 s . c o  m

            if (type == CoordinateType.Pixels) {
                matrix[0] = 1.f / texture.getActualWidth();
                matrix[5] = 1.f / texture.getActualHeight();
            }

            if (texture.getPixelsFlipped()) {
                matrix[5] = -matrix[5];
                matrix[13] = (float) (texture.getHeight() / texture.getActualHeight());
            }

            GL.setMatrixMode(GL.MatrixMode.Texture);
            GL.loadMatrix(matrix);

            GL.setMatrixMode(GL.MatrixMode.ModelView);

        }

    } else {

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

        GL.setMatrixMode(GL.MatrixMode.Texture);
        GL.loadIdentity();

        GL.setMatrixMode(GL.MatrixMode.ModelView);

    }

}

From source file:com.dyonovan.tcnodetracker.lib.truetyper.TrueTypeFont.java

License:Open Source License

public void drawString(float x, float y, String whatchars, int startIndex, int endIndex, float scaleX,
        float scaleY, int format, float... rgba) {
    if (rgba.length == 0)
        rgba = new float[] { 1f, 1f, 1f, 1f };
    GL11.glPushMatrix();//from ww  w . j av a  2 s. co  m
    GL11.glScalef(scaleX, scaleY, 1.0f);

    FloatObject floatObject = null;
    int charCurrent;

    float totalwidth = 0;
    int i = startIndex, d, c;
    float startY = 0;

    switch (format) {
    case ALIGN_RIGHT: {
        d = -1;
        c = correctR;

        while (i < endIndex) {
            if (whatchars.charAt(i) == '\n')
                startY -= fontHeight;
            i++;
        }
        break;
    }
    case ALIGN_CENTER: {
        for (int l = startIndex; l <= endIndex; l++) {
            charCurrent = whatchars.charAt(l);
            if (charCurrent == '\n')
                break;
            if (charCurrent < 256) {
                floatObject = charArray[charCurrent];
            } else {
                floatObject = (FloatObject) customChars.get(new Character((char) charCurrent));
            }
            totalwidth += floatObject.width - correctL;
        }
        totalwidth /= -2;
    }
    case ALIGN_LEFT:
    default: {
        d = 1;
        c = correctL;
        break;
    }

    }
    GL11.glBindTexture(GL11.GL_TEXTURE_2D, fontTextureID);
    Tessellator t = Tessellator.instance;
    t.startDrawingQuads();
    //   GL11.glBegin(GL11.GL_QUADS);
    if (rgba.length == 4)
        t.setColorRGBA_F(rgba[0], rgba[1], rgba[2], rgba[3]);
    while (i >= startIndex && i <= endIndex) {

        charCurrent = whatchars.charAt(i);
        if (charCurrent < 256) {
            floatObject = charArray[charCurrent];
        } else {
            floatObject = (FloatObject) customChars.get(new Character((char) charCurrent));
        }

        if (floatObject != null) {
            if (d < 0)
                totalwidth += (floatObject.width - c) * d;
            if (charCurrent == '\n') {
                startY -= fontHeight * d;
                totalwidth = 0;
                if (format == ALIGN_CENTER) {
                    for (int l = i + 1; l <= endIndex; l++) {
                        charCurrent = whatchars.charAt(l);
                        if (charCurrent == '\n')
                            break;
                        if (charCurrent < 256) {
                            floatObject = charArray[charCurrent];
                        } else {
                            floatObject = (FloatObject) customChars.get(new Character((char) charCurrent));
                        }
                        totalwidth += floatObject.width - correctL;
                    }
                    totalwidth /= -2;
                }
                //if center get next lines total width/2;
            } else {
                drawQuad((totalwidth + floatObject.width) + x / scaleX, startY + y / scaleY,
                        totalwidth + x / scaleX, (startY + floatObject.height) + y / scaleY,
                        floatObject.storedX + floatObject.width, floatObject.storedY + floatObject.height,
                        floatObject.storedX, floatObject.storedY);
                if (d > 0)
                    totalwidth += (floatObject.width - c) * d;
            }
            i += d;

        }
    }
    t.draw();
    //   GL11.glEnd();

    GL11.glPopMatrix();
}