/** * Load an image from the given path. * * @param path Image path. * * @return Image. */ public static Image loadFromPath(String path) { // Create a byte buffer for the image to load ByteBuffer imageBuffer; // Load the image file into the image buffer try { imageBuffer = BufferUtil.fileToByteBuffer(path); } catch(IOException e) { // TODO: Use a default image! System.out.println("Could not find the image at the path '" + path + "'"); throw new RuntimeException(e); } // Create some integer buffers for STB interaction IntBuffer width = BufferUtils.createIntBuffer(1); IntBuffer height = BufferUtils.createIntBuffer(1); IntBuffer components = BufferUtils.createIntBuffer(1); // Load the image into memory using STB ByteBuffer image = STBImage.stbi_load_from_memory(imageBuffer, width, height, components, 0); // Create the image instance and return it return new Image(image, width.get(0), height.get(0), components.get(0)); }
public void setIcon(String icon16Path, String icon32Path){ //setup buffers to work with stb IntBuffer w = BufferUtils.createIntBuffer(1); IntBuffer h = BufferUtils.createIntBuffer(1); IntBuffer comp = BufferUtils.createIntBuffer(1); //these will be the data buffers for the textures ByteBuffer icon16,icon32; try{ //populate the buffers with the raw image data icon16 = ResourceLoader.getBytes(icon16Path); icon32 = ResourceLoader.getBytes(icon32Path); //setup image buffers for the images to be processed try(GLFWImage.Buffer icons = GLFWImage.malloc(2)){ //process both images with stb //16x16 icon ByteBuffer p16 = STBImage.stbi_load_from_memory(icon16, w, h, comp, 4); icons.position(0).width(w.get(0)).height(h.get(0)).pixels(p16); //32x32 icon ByteBuffer p32 = STBImage.stbi_load_from_memory(icon32, w, h, comp, 4); icons.position(1).width(w.get(0)).height(h.get(0)).pixels(p32); //reset the icons buffer position icons.position(0); GLFW.glfwSetWindowIcon(handle, icons); //free the stb resources STBImage.stbi_image_free(p16); STBImage.stbi_image_free(p32); } }catch(Exception e){ e.printStackTrace(); } }
@Override public void close() throws Exception { if (this.loadFromSTB) { STBImage.stbi_image_free(this.pixels); } BITMAPS.remove(this); }
/** * Load an image from the given byte buffer. * * @param imageBuffer Byte buffer containing the image. * * @return Image. */ public static Image loadFromByteBuffer(ByteBuffer imageBuffer) { // Create some integer buffers for STB interaction IntBuffer width = BufferUtils.createIntBuffer(1); IntBuffer height = BufferUtils.createIntBuffer(1); IntBuffer components = BufferUtils.createIntBuffer(1); // Load the image into memory using STB ByteBuffer image = STBImage.stbi_load_from_memory(imageBuffer, width, height, components, 0); // Create the image instance and return it return new Image(image, width.get(0), height.get(0), components.get(0)); }
/** * Dispose the image, and free the memory. */ public void dispose() { // Free the image memory through STB STBImage.stbi_image_free(image); // Remove the image from the image manager ImageTracker.untrackImage(this); }
@Override public Texture createFromRaw(Raw raw) { ByteBuffer buffer = raw.asByteBuffer(); IntBuffer w = BufferUtils.createIntBuffer(1); IntBuffer h = BufferUtils.createIntBuffer(1); IntBuffer c = BufferUtils.createIntBuffer(1); STBImage.stbi_set_flip_vertically_on_load(true); ByteBuffer image = STBImage.stbi_load_from_memory(buffer, w, h, c, 4); return new Texture(image, w.get(), h.get()); }
public Cubemap(String[] images) { id = glGenTextures(); glBindTexture(GL_TEXTURE_CUBE_MAP, this.id); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 10); STBImage.stbi_set_flip_vertically_on_load(false); //iterate over all 6 textures and send their raw data to the cubemap for (int i = 0; i < 6; i++) { IntBuffer w = BufferUtils.createIntBuffer(1); IntBuffer h = BufferUtils.createIntBuffer(1); IntBuffer comp = BufferUtils.createIntBuffer(1); FloatBuffer buffer = STBImage.stbi_loadf(Settings.RESOURCE_PATH + "textures/" + images[i], w, h, comp, 3); int width = w.get(0); int height = w.get(0); glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, buffer); } //interpolation settings and texture wrapping for the texture glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glGenerateMipmap(GL_TEXTURE_CUBE_MAP); }
/** * Creates an openGL texture from the image specified by <code>fileName</code>. * * @param fileName * File name relative to the game engines texture resource folder. * @return Returns the texture id for the newly created texture. * @throws IOException * If loading the texture fails. * * @see <a href="http://stackoverflow.com/a/33858800">stackoverflow.com/33858800</a> */ public static int loadTexture(final String fileName) throws IOException { ByteBuffer imageRaw = loadImageFile("./res/textures/" + fileName); IntBuffer imageWidthBuffer = BufferUtils.createIntBuffer(1); IntBuffer imageHeightBuffer = BufferUtils.createIntBuffer(1); IntBuffer numComponentsBuffer = BufferUtils.createIntBuffer(1); ByteBuffer image = STBImage.stbi_load_from_memory(imageRaw, imageWidthBuffer, imageHeightBuffer, numComponentsBuffer, BYTE_PER_PIXEL); if (image == null) throw new RuntimeException("Failed to load texture: " + STBImage.stbi_failure_reason()); int imageWidth = imageWidthBuffer.get(0); int imageHeight = imageHeightBuffer.get(0); // int numComponents = numComponentsBuffer.get(0); int textureID = glGenTextures(); glBindTexture(GL_TEXTURE_2D, textureID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); return textureID; }