List of usage examples for org.lwjgl.opengl GL30 glGenerateMipmap
public static void glGenerateMipmap(@NativeType("GLenum") int target)
From source file:de.ikosa.mars.viewer.glviewer.engine.GLTextureBuilder.java
License:Open Source License
public static GLTexture createEmptyTexture(String name, int width, int height, byte r, byte g, byte b, byte a) { try {//from ww w .j a v a 2 s .co m int textureId = GL11.glGenTextures(); ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4); for (int i = 0; i < width * height; i++) { buffer.put(r); buffer.put(g); buffer.put(b); buffer.put(a); } buffer.flip(); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); return new GLTexture(name, textureId); } catch (Exception e) { ML.f(e); } return null; }
From source file:de.ikosa.mars.viewer.glviewer.engine.GLTextureBuilder.java
License:Open Source License
public static GLTexture createEmptyTexture(String name, int width, int height, float initial) { try {//w ww . ja va 2 s .c om int textureId = GL11.glGenTextures(); ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4); for (int i = 0; i < width * height * 4; i++) { buffer.put((byte) 0); } buffer.flip(); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_DEPTH_COMPONENT, width, height, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, buffer); GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); return new GLTexture(name, textureId); } catch (Exception e) { ML.f(e); } return null; }
From source file:de.ikosa.mars.viewer.glviewer.engine.GLTextureBuilder.java
License:Open Source License
private int createRGBTexture(BufferedImage image, byte[] x) { ByteBuffer buffer;/* www .j a va 2 s.c om*/ buffer = ByteBuffer.allocateDirect(x.length); for (int i = 0; i < x.length; i++) { buffer.put(x[i]); } buffer.flip(); int textureId = GL11.glGenTextures(); GLRenderer2Stage.errorCheck("generating texture id"); GL13.glActiveTexture(GL13.GL_TEXTURE0); GLRenderer2Stage.errorCheck("activating texture image unit"); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); GLRenderer2Stage.errorCheck("binding 2d texture"); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1); GLRenderer2Stage.errorCheck("setting unpack aligment"); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, image.getWidth(), image.getHeight(), 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buffer); GLRenderer2Stage.errorCheck("storing 2d texture data"); GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); GLRenderer2Stage.errorCheck("generating 2d texture array mipmaps"); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); GLRenderer2Stage.errorCheck("unbinding 2d texture array"); return textureId; }
From source file:de.ikosa.mars.viewer.glviewer.engine.GLTextureBuilder.java
License:Open Source License
private int createGrayscaleTexture(BufferedImage image, byte[] x) { ShortBuffer buffer;/* w w w . j ava2 s .c o m*/ buffer = BufferUtils.createShortBuffer(image.getHeight() * image.getWidth()); for (int i = 0; i < x.length; i += 2) { short val = (short) (fromByte(x[i]) + (fromByte(x[i + 1]) << 8)); buffer.put(val); } buffer.flip(); int textureId = GL11.glGenTextures(); GLRenderer2Stage.errorCheck("generating texture id"); GL13.glActiveTexture(GL13.GL_TEXTURE0); GLRenderer2Stage.errorCheck("activating texture image unit"); GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureId); GLRenderer2Stage.errorCheck("binding 2d texture"); GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 2); GLRenderer2Stage.errorCheck("setting unpack aligment"); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL30.GL_R16, image.getWidth(), image.getHeight(), 0, GL11.GL_RED, GL11.GL_UNSIGNED_SHORT, buffer); GLRenderer2Stage.errorCheck("storing 2d texture data"); GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D); GLRenderer2Stage.errorCheck("generating 2d texture array mipmaps"); GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0); GLRenderer2Stage.errorCheck("unbinding 2d texture array"); return textureId; }
From source file:eu.over9000.veya.util.TextureLoader.java
License:Open Source License
public static int loadBlockTexture(final int textureUnit) { final InputStream in = TextureLoader.class.getResourceAsStream("/textures/blocks.png"); ByteBuffer convertedBuffer = null; int sourceTexWidth = 0; int sourceTexHeight = 0; int texCount = 0; try {//from w ww . j av a2s . c o m // Link the PNG decoder to this stream final PNGDecoder decoder = new PNGDecoder(in); // Get the width and height of the texture sourceTexWidth = decoder.getWidth(); sourceTexHeight = decoder.getHeight(); texCount = sourceTexWidth / TextureLoader.TEXTURE_WIDTH * (sourceTexHeight / TextureLoader.TEXTURE_WIDTH); // Decode the PNG file in a ByteBuffer final ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight()); decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA); buf.flip(); convertedBuffer = TextureLoader.convertBuffer(buf, sourceTexWidth, sourceTexHeight); in.close(); } catch (final IOException e) { e.printStackTrace(); System.exit(-1); } // Create a new texture object in memory and bind it final int texId = GL11.glGenTextures(); GL13.glActiveTexture(textureUnit); GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 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 model and generate mip maps (for scaling) // GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf); GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, TextureLoader.TEXTURE_WIDTH, TextureLoader.TEXTURE_HEIGHT, texCount, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, convertedBuffer); GL30.glGenerateMipmap(GL30.GL_TEXTURE_2D_ARRAY); // Setup the ST coordinate system GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); // Setup what to do when the texture has to be scaled GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST); GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST_MIPMAP_LINEAR); org.lwjgl.opengl.Util.checkGLError(); System.out .println("loading block texture, id=" + texId + ", w=" + sourceTexWidth + ", h=" + sourceTexHeight); GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0); return texId; }
From source file:fr.guillaume.prive.viper.core.model.texture.PNGTextureLoader.java
public static int loadTexture(String filename, int textureUnit) { ByteBuffer buf = null;/* w w w .j a va2 s.c o m*/ 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.util.gl.GLTools.java
License:Open Source License
/** * @param image/*w w w . ja v a2 s .c o m*/ * @param mipmap * @param buffer * @return */ private static int setGlTexture(final int width, int height, boolean mipmap, ByteBuffer buffer) { // You now have a ByteBuffer filled with the color data of each pixel. // Now just create a texture ID and bind it. Then you can load it using // whatever OpenGL method you want, for example: int textureID = glGenTextures(); // Generate texture ID glBindTexture(GL_TEXTURE_2D, textureID); // Bind texture ID // Setup wrap mode // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Setup texture scaling filtering glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Send texel data to OpenGL glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); if (mipmap) { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR); GL30.glGenerateMipmap(GL_TEXTURE_2D); } return textureID; }
From source file:ivengine.Util.java
License:Creative Commons License
/** * Stores a variable amount of textures defined by URLS <filenames> into a texture array. Uses a magfilter <magfilter>, a minfilter <minfilter>. * If <mipmap> is true, mipmaps will be activated. * If <anisotropic> is true, anisotropic filtering will be activated, if supported. *///from ww w . jav a2 s . c om public static int loadTextureAtlasIntoTextureArray(URL[] filenames, int magfilter, int minfilter, boolean mipmap, boolean anisotropic) { int tex = GL11.glGenTextures(); GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, tex); GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MAG_FILTER, magfilter); GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_MIN_FILTER, minfilter); GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT); GL11.glTexParameteri(GL30.GL_TEXTURE_2D_ARRAY, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT); ByteBuffer buf = null; PNGDecoder decoder = null; try { InputStream in = filenames[0].openStream(); decoder = new PNGDecoder(in); buf = BufferUtils.createByteBuffer(4 * decoder.getWidth() * decoder.getHeight()); decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA); buf.flip(); in.close(); } catch (IOException e) { e.printStackTrace(); System.exit(-1); } int tileWidth = decoder.getWidth(); System.out.println(tileWidth); int tileHeight = decoder.getHeight(); System.out.println(tileHeight); GL12.glTexImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, GL11.GL_RGBA, tileWidth, tileHeight, filenames.length, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer) null); for (int i = 0; i < filenames.length; i++) { GL12.glTexSubImage3D(GL30.GL_TEXTURE_2D_ARRAY, 0, /*tileWidth*x*/0, /*tileHeight*y*/0, i, tileWidth, tileHeight, 1, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf); buf.rewind(); if (i < filenames.length - 1) loadTexture(filenames[i + 1], buf); } if (mipmap) GL30.glGenerateMipmap(GL30.GL_TEXTURE_2D_ARRAY); if (anisotropic) { if (GLContext.getCapabilities().GL_EXT_texture_filter_anisotropic) { float maxanis = GL11.glGetFloat(EXTTextureFilterAnisotropic.GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT); System.out.println("Anisotropic filtering activated with a resolution of " + maxanis); System.out.println(GL11.glGetError()); GL11.glTexParameterf(GL30.GL_TEXTURE_2D_ARRAY, EXTTextureFilterAnisotropic.GL_TEXTURE_MAX_ANISOTROPY_EXT, maxanis); System.out.println(GL11.glGetError()); } else { System.err.println( "WARNING - Anisotropic filtering not supported by this graphics card. Setting it as disabled"); } } GL11.glBindTexture(GL30.GL_TEXTURE_2D_ARRAY, 0); return tex; }
From source file:main.java.com.YeAJG.game.io.FileIOHandler.java
License:Open Source License
public static int loadPNGTexture(String filename, int textureUnit) throws IOException { ByteBuffer buf = null;/*from ww w. j a va2s.com*/ int tWidth = 0; int tHeight = 0; try { // Open the PNG file as an InputStream InputStream in = new FileInputStream(filename); // Link the PNG decoder to this stream PNGDecoder decoder = new PNGDecoder(in); // Get the width and height of the texture tWidth = decoder.getWidth(); tHeight = decoder.getHeight(); // Decode the PNG file in a ByteBuffer buf = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight()); decoder.decode(buf, decoder.getWidth() * 4, PNGDecoder.RGBA); buf.flip(); in.close(); } catch (IOException e) { throw new IOException(e.getMessage()); } // 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_RGBA, 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_LINEAR); GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR_MIPMAP_LINEAR); Game.exitOnGLError("loadPNGTexture"); return texId; }
From source file:me.thehutch.fusion.engine.render.opengl.gl30.OpenGL30Texture.java
License:Open Source License
@Override public void setPixelData(ByteBuffer pixels, InternalFormat format, int width, int height, boolean generateMipmaps) { ensureCreated("Texture must be created to set pixel data."); // Upload the pixel data to the GPU GL11.glTexImage2D(GL_TEXTURE_2D, 0, format.getGLConstant(), width, height, 0, format.getFormat().getGLConstant(), format.getDataType().getGLConstant(), pixels); // Check if mipmaps should be generated for this texture if (generateMipmaps) { GL30.glGenerateMipmap(GL_TEXTURE_2D); }/*from w w w . j a va 2 s . co m*/ }