Search in sources :

Example 46 with Vector4f

use of org.joml.Vector4f in project chunkstories by Hugobros3.

the class GuiRendererImplementation method drawCorneredBoxTiled.

@Override
public void drawCorneredBoxTiled(float posx, float posy, float width, float height, int cornerSize, Texture2D texture, int textureSize, int scale) {
    GuiRenderer guiRenderer = this;
    // - width / 2;
    float topLeftCornerX = posx;
    // - height / 2;
    float topLeftCornerY = posy;
    // / 2;
    float botRightCornerX = posx + width;
    // / 2;
    float botRightCornerY = posy + height;
    // Debug helper
    // guiRenderer.drawBoxWindowsSpace(topLeftCornerX, topLeftCornerY, botRightCornerX, botRightCornerY, 0, 0, 0, 0, null, true, false, new Vector4f(1.0, 1.0, 0.0, 1.0));
    int cornerSizeScaled = scale * cornerSize;
    float textureSizeInternal = textureSize - cornerSize * 2;
    float insideWidth = width - cornerSizeScaled * 2;
    float insideHeight = height - cornerSizeScaled * 2;
    float texCoordInsideTopLeft = ((float) cornerSize) / textureSize;
    float texCoordInsideBottomRight = ((float) (textureSize - cornerSize)) / textureSize;
    // Fill the inside of the box
    for (int fillerX = 0; fillerX < insideWidth; fillerX += textureSizeInternal * scale) {
        for (int fillerY = 0; fillerY < insideHeight; fillerY += textureSizeInternal * scale) {
            float toFillX = Math.min(textureSizeInternal * scale, insideWidth - fillerX);
            float toFillY = Math.min(textureSizeInternal * scale, insideHeight - fillerY);
            float startX = topLeftCornerX + cornerSizeScaled + fillerX;
            float startY = topLeftCornerY + cornerSizeScaled + fillerY;
            guiRenderer.drawBoxWindowsSpace(startX, startY, startX + toFillX, startY + toFillY, texCoordInsideTopLeft, texCoordInsideTopLeft + toFillY / textureSize / scale, texCoordInsideTopLeft + toFillX / textureSize / scale, texCoordInsideTopLeft, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
        }
    }
    // Fill the horizontal sides
    for (int fillerX = 0; fillerX < insideWidth; fillerX += textureSizeInternal * scale) {
        float toFillX = Math.min(textureSizeInternal * scale, insideWidth - fillerX);
        float startX = topLeftCornerX + cornerSizeScaled + fillerX;
        float startY = topLeftCornerY;
        guiRenderer.drawBoxWindowsSpace(startX, startY + height - cornerSizeScaled, startX + toFillX, startY + height, texCoordInsideTopLeft, texCoordInsideTopLeft, texCoordInsideTopLeft + toFillX / textureSize / scale, 0, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
        guiRenderer.drawBoxWindowsSpace(startX, startY, startX + toFillX, startY + cornerSizeScaled, texCoordInsideTopLeft, 1.0f, texCoordInsideTopLeft + toFillX / textureSize / scale, texCoordInsideBottomRight, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
    }
    // Fill the vertical sides
    for (int fillerY = 0; fillerY < insideHeight; fillerY += textureSizeInternal * scale) {
        float toFillY = Math.min(textureSizeInternal * scale, insideHeight - fillerY);
        float startY = topLeftCornerY + cornerSizeScaled + fillerY;
        float startX = topLeftCornerX;
        guiRenderer.drawBoxWindowsSpace(startX, startY, startX + cornerSizeScaled, startY + toFillY, 0, texCoordInsideBottomRight - (textureSizeInternal * scale - toFillY) / textureSize / scale, texCoordInsideTopLeft, texCoordInsideTopLeft, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
        guiRenderer.drawBoxWindowsSpace(startX + width - cornerSizeScaled, startY, startX + width, startY + toFillY, texCoordInsideBottomRight, texCoordInsideBottomRight - (textureSizeInternal * scale - toFillY) / textureSize / scale, 1.0f, texCoordInsideTopLeft, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
    }
    // Fill the 4 corners
    guiRenderer.drawBoxWindowsSpace(topLeftCornerX, botRightCornerY - cornerSizeScaled, topLeftCornerX + cornerSizeScaled, botRightCornerY, 0, texCoordInsideTopLeft, texCoordInsideTopLeft, 0, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
    guiRenderer.drawBoxWindowsSpace(topLeftCornerX, topLeftCornerY, topLeftCornerX + cornerSizeScaled, topLeftCornerY + cornerSizeScaled, 0, 1.0f, texCoordInsideTopLeft, texCoordInsideBottomRight, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
    guiRenderer.drawBoxWindowsSpace(botRightCornerX - cornerSizeScaled, botRightCornerY - cornerSizeScaled, botRightCornerX, botRightCornerY, texCoordInsideBottomRight, texCoordInsideTopLeft, 1.0f, 0, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
    guiRenderer.drawBoxWindowsSpace(botRightCornerX - cornerSizeScaled, topLeftCornerY, botRightCornerX, topLeftCornerY + cornerSizeScaled, texCoordInsideBottomRight, 1.0f, 1.0f, texCoordInsideBottomRight, texture, true, false, new Vector4f(1.0f, 1.0f, 1.0f, 1.0f));
}
Also used : Vector4f(org.joml.Vector4f) GuiRenderer(io.xol.chunkstories.api.gui.GuiRenderer)

Example 47 with Vector4f

use of org.joml.Vector4f in project chunkstories by Hugobros3.

the class ObjectRenderer method renderTexturedRotatedRectRVBA.

public static void renderTexturedRotatedRectRVBA(float xpos, float ypos, float w, float h, float rot, float tcsx, float tcsy, float tcex, float tcey, String textureName, float r, float v, float b, float a) {
    /*if (textureName.startsWith("internal://"))
			textureName = textureName.substring("internal://".length());
		else if (textureName.startsWith("gameDir://"))
			textureName = textureName.substring("gameDir://".length());//GameDirectory.getGameFolderPath() + "/" + tex.substring("gameDir://".length());
		else if (textureName.contains("../"))
			textureName = ("./" + textureName.replace("../", "") + ".png");
		else
			textureName = ("./textures/" + textureName + ".png");
		 */
    Texture2DGL texture = TexturesHandler.getTexture(textureName);
    texture.setLinearFiltering(false);
    // TexturesHandler.mipmapLevel(texture, -1);
    Client.getInstance().getGameWindow().getRenderingInterface().getGuiRenderer().drawBoxWindowsSpace(xpos - w / 2, ypos + h / 2, xpos + w / 2, ypos - h / 2, tcsx, tcsy, tcex, tcey, texture, false, true, new Vector4f(r, v, b, a));
}
Also used : Vector4f(org.joml.Vector4f) Texture2DGL(io.xol.chunkstories.renderer.opengl.texture.Texture2DGL)

Example 48 with Vector4f

use of org.joml.Vector4f in project chunkstories by Hugobros3.

the class OpenGLRenderTargetManager method clearBoundRenderTargetAll.

@Override
public void clearBoundRenderTargetAll() {
    // Resets those to default values
    setClearDepth(1);
    setClearColor(new Vector4f(0));
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
Also used : Vector4f(org.joml.Vector4f)

Example 49 with Vector4f

use of org.joml.Vector4f in project chunkstories by Hugobros3.

the class VoxelTexturesStoreAndAtlaser method buildTextureAtlas.

public void buildTextureAtlas() {
    try {
        // Clear previous values
        texMap.clear();
        // colors.clear();
        // Compute all sizes first.
        int totalSurfacedNeeded = 0;
        // File folder = new File("./res/voxels/textures/");
        // Get all sizes :
        List<VoxelTextureAtlased> voxelTexturesSortedBySize = new ArrayList<VoxelTextureAtlased>();
        // First we want to iterate over every file to get an idea of how many textures (and of how many sizes) we are dealing
        Iterator<AssetHierarchy> allFiles = content.modsManager().getAllUniqueEntries();
        AssetHierarchy entry;
        Asset f;
        while (allFiles.hasNext()) {
            entry = allFiles.next();
            if (entry.getName().startsWith("./voxels/textures/")) {
                String name = entry.getName().replace("./voxels/textures/", "");
                if (name.contains("/"))
                    continue;
                f = entry.topInstance();
                if (f.getName().endsWith(".png")) {
                    String textureName = name.replace(".png", "");
                    // System.out.println("texName:"+textureName+" "+entry.getKey());
                    if (!texMap.containsKey(textureName)) {
                        VoxelTextureAtlased voxelTexture = new VoxelTextureAtlased(textureName, uniquesIds);
                        uniquesIds++;
                        voxelTexture.imageFileDimensions = getImageSize(f);
                        voxelTexturesSortedBySize.add(voxelTexture);
                        totalSurfacedNeeded += voxelTexture.imageFileDimensions * voxelTexture.imageFileDimensions;
                    }
                }
            }
        }
        // Sort them by size
        Collections.sort(voxelTexturesSortedBySize, new Comparator<VoxelTextureAtlased>() {

            @Override
            public int compare(VoxelTextureAtlased a, VoxelTextureAtlased b) {
                return Integer.compare(b.imageFileDimensions, a.imageFileDimensions);
            }
        });
        for (VoxelTextureAtlased voxelTexture : voxelTexturesSortedBySize) {
            // System.out.println(vt.imageFileDimensions);
            texMap.put(voxelTexture.getName(), voxelTexture);
        }
        // Estimates the required texture atlas size by surface
        int sizeRequired = 16;
        for (int i = 4; i < 14; i++) {
            int iSize = (int) Math.pow(2, i);
            if (iSize * iSize >= totalSurfacedNeeded) {
                sizeRequired = iSize;
                break;
            }
        }
        // ChunkStoriesLogger.getInstance().info("At least " + sizeRequired + " by " + sizeRequired + " for TextureAtlas (surfacedNeeded : " + totalSurfacedNeeded + ")");
        // Delete previous atlases
        File diffuseTextureFile = new File(GameDirectory.getGameFolderPath() + "/cache/tiles_merged_albedo.png");
        if (diffuseTextureFile.exists())
            diffuseTextureFile.delete();
        File normalTextureFile = new File(GameDirectory.getGameFolderPath() + "/cache/tiles_merged_normal.png");
        if (normalTextureFile.exists())
            normalTextureFile.delete();
        File materialTextureFile = new File(GameDirectory.getGameFolderPath() + "/cache/tiles_merged_material.png");
        if (materialTextureFile.exists())
            materialTextureFile.delete();
        // Build the new one
        boolean loadedOK = false;
        while (// Security to prevend
        !loadedOK && sizeRequired <= 8192) // HUGE-ASS textures
        {
            // We need this
            BLOCK_ATLAS_SIZE = sizeRequired;
            BLOCK_ATLAS_FACTOR = 32768 / BLOCK_ATLAS_SIZE;
            loadedOK = true;
            // Create boolean bitfield
            boolean[][] used = new boolean[sizeRequired / 16][sizeRequired / 16];
            diffuseTextureImage = null;
            normalTextureImage = null;
            materialTextureImage = null;
            if (content.getContext() instanceof ClientInterface) {
                diffuseTextureImage = new BufferedImage(sizeRequired, sizeRequired, Transparency.TRANSLUCENT);
                normalTextureImage = new BufferedImage(sizeRequired, sizeRequired, Transparency.TRANSLUCENT);
                materialTextureImage = new BufferedImage(sizeRequired, sizeRequired, Transparency.TRANSLUCENT);
                logger.debug("This is a client so we'll make the texture atlas");
            }
            BufferedImage imageBuffer;
            for (VoxelTextureAtlased vt : voxelTexturesSortedBySize) {
                // Find a free spot on the atlas
                boolean foundSpot = false;
                int spotX = 0, spotY = 0;
                for (int a = 0; (a < sizeRequired / 16 && !foundSpot); a++) for (int b = 0; (b < sizeRequired / 16 && !foundSpot); b++) {
                    if (// Unused
                    used[a][b] == false && a + vt.imageFileDimensions / 16 <= sizeRequired / 16 && b + vt.imageFileDimensions / 16 <= sizeRequired / 16) {
                        boolean usedAlready = false;
                        // Not pretty loops that do clamped space checks
                        for (int i = 0; (i < vt.imageFileDimensions / 16 && a + i < sizeRequired / 16); i++) for (int j = 0; (j < vt.imageFileDimensions / 16 && b + j < sizeRequired / 16); j++) if (// Well
                        used[a + i][b + j] == true)
                            // fuck
                            // it
                            usedAlready = true;
                        if (!usedAlready) {
                            spotX = a * 16;
                            spotY = b * 16;
                            vt.setAtlasS(spotX * BLOCK_ATLAS_FACTOR);
                            vt.setAtlasT(spotY * BLOCK_ATLAS_FACTOR);
                            vt.setAtlasOffset(vt.imageFileDimensions * BLOCK_ATLAS_FACTOR);
                            foundSpot = true;
                            for (int i = 0; (i < vt.imageFileDimensions / 16 && a + i < sizeRequired / 16); i++) for (int j = 0; (j < vt.imageFileDimensions / 16 && b + j < sizeRequired / 16); j++) used[a + i][b + j] = true;
                        }
                    }
                }
                if (!foundSpot) {
                    System.out.println("Failed to find a space to place the texture in. Retrying with a larger atlas.");
                    loadedOK = false;
                    break;
                }
                imageBuffer = ImageIO.read(content.modsManager().getAsset("./voxels/textures/" + vt.getName() + ".png").read());
                // imageBuffer = ImageIO.read(GameContent.getTextureFileLocation());
                float alphaTotal = 0;
                int nonNullPixels = 0;
                Vector3f color = new Vector3f();
                for (int x = 0; x < vt.imageFileDimensions; x++) {
                    for (int y = 0; y < vt.imageFileDimensions; y++) {
                        int rgb = imageBuffer.getRGB(x, y);
                        if (diffuseTextureImage != null)
                            diffuseTextureImage.setRGB(spotX + x, spotY + y, rgb);
                        float alpha = ((rgb & 0xFF000000) >>> 24) / 255f;
                        // System.out.println("a:"+alpha);
                        alphaTotal += alpha;
                        if (alpha > 0)
                            nonNullPixels++;
                        float red = ((rgb & 0xFF0000) >> 16) / 255f * alpha;
                        float green = ((rgb & 0x00FF00) >> 8) / 255f * alpha;
                        float blue = (rgb & 0x0000FF) / 255f * alpha;
                        color.add(new Vector3f(red, green, blue));
                    // Vector3f.add(color, new Vector3f(red, green, blue), color);
                    }
                }
                color.mul(1f / alphaTotal);
                if (nonNullPixels > 0)
                    alphaTotal /= nonNullPixels;
                vt.setColor(new Vector4f(color.x(), color.y(), color.z(), alphaTotal));
                // Don't bother if it's not a Client context
                if (diffuseTextureImage == null)
                    continue;
                // Do also the normal maps !
                Asset normalMap = content.modsManager().getAsset("./voxels/textures/normal/" + vt.getName() + ".png");
                if (normalMap == null)
                    normalMap = content.modsManager().getAsset("./voxels/textures/normal/notex.png");
                imageBuffer = ImageIO.read(normalMap.read());
                for (int x = 0; x < vt.imageFileDimensions; x++) {
                    for (int y = 0; y < vt.imageFileDimensions; y++) {
                        int rgb = imageBuffer.getRGB(x % imageBuffer.getWidth(), y % imageBuffer.getHeight());
                        normalTextureImage.setRGB(spotX + x, spotY + y, rgb);
                    }
                }
                // And the materials !
                Asset materialMap = content.modsManager().getAsset("./voxels/textures/material/" + vt.getName() + ".png");
                if (materialMap == null)
                    materialMap = content.modsManager().getAsset("./voxels/textures/material/notex.png");
                imageBuffer = ImageIO.read(materialMap.read());
                for (int x = 0; x < vt.imageFileDimensions; x++) {
                    for (int y = 0; y < vt.imageFileDimensions; y++) {
                        int rgb = imageBuffer.getRGB(x % imageBuffer.getWidth(), y % imageBuffer.getHeight());
                        materialTextureImage.setRGB(spotX + x, spotY + y, rgb);
                    }
                }
            }
            if (loadedOK && diffuseTextureImage != null) {
                // save it son
                ImageIO.write(diffuseTextureImage, "PNG", diffuseTextureFile);
                ImageIO.write(normalTextureImage, "PNG", normalTextureFile);
                ImageIO.write(materialTextureImage, "PNG", materialTextureFile);
                diffuseTexture = null;
                normalTexture = null;
                materialTexture = null;
            } else
                // It's too small, initial estimation was wrong !
                sizeRequired *= 2;
        }
        // Read textures metadata
        // TODO read all overrides in priority
        readTexturesMeta(content.modsManager().getAsset("./voxels/textures/meta.txt"));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ArrayList(java.util.ArrayList) ClientInterface(io.xol.chunkstories.api.client.ClientInterface) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) Vector4f(org.joml.Vector4f) Vector3f(org.joml.Vector3f) Asset(io.xol.chunkstories.api.content.Asset) AssetHierarchy(io.xol.chunkstories.api.content.mods.AssetHierarchy) File(java.io.File)

Example 50 with Vector4f

use of org.joml.Vector4f in project IDEProgram by Itay2805.

the class GLContext method drawPoint.

private static void drawPoint(Vertex p, Vector4f[] color, float[] depth, GLContext gl) {
    int o = (int) (Math.floor(p.coord.x) + Math.floor(p.coord.y) * gl.w);
    if (gl.depthEnabled) {
        float d = p.coord.z;
        if (d > depth[o])
            return;
        else
            depth[o] = d;
    }
    // Vertex color
    Vector4f fragColor = new Vector4f();
    fragColor.x = p.color.x;
    fragColor.y = p.color.y;
    fragColor.z = p.color.z;
    fragColor.w = p.color.w;
    // Texture sample
    Texture tex = gl.textures.get(gl.curTexture);
    if (gl.textureEnabled && tex != null) {
        float u = p.texCoord.x;
        float v = p.texCoord.y;
        u = (float) (Math.floor(u * tex.w) % tex.w);
        v = (float) (Math.floor(v * tex.h) % tex.h);
        int to = (int) (u + v * tex.w);
        fragColor.x *= tex.pixels[to].x;
        fragColor.y *= tex.pixels[to].y;
        fragColor.z *= tex.pixels[to].z;
        fragColor.w *= tex.pixels[to].w;
    }
    color[o].x = fragColor.x;
    color[o].y = fragColor.y;
    color[o].z = fragColor.z;
    color[o].w = fragColor.w;
}
Also used : Vector4f(org.joml.Vector4f)

Aggregations

Vector4f (org.joml.Vector4f)55 Font (io.xol.chunkstories.api.rendering.text.FontRenderer.Font)12 Vector3f (org.joml.Vector3f)7 Texture2D (io.xol.chunkstories.api.rendering.textures.Texture2D)6 Vector3d (org.joml.Vector3d)6 Shader (io.xol.chunkstories.api.rendering.shader.Shader)5 CellData (io.xol.chunkstories.api.world.cell.CellData)5 Mouse (io.xol.chunkstories.api.input.Mouse)4 Texture2DGL (io.xol.chunkstories.renderer.opengl.texture.Texture2DGL)4 ItemPile (io.xol.chunkstories.api.item.inventory.ItemPile)3 STBEasyFont.stb_easy_font_print (org.lwjgl.stb.STBEasyFont.stb_easy_font_print)3 Location (io.xol.chunkstories.api.Location)2 Entity (io.xol.chunkstories.api.entity.Entity)2 EntityLiving (io.xol.chunkstories.api.entity.EntityLiving)2 GuiRenderer (io.xol.chunkstories.api.gui.GuiRenderer)2 CollisionBox (io.xol.chunkstories.api.physics.CollisionBox)2 Voxel (io.xol.chunkstories.api.voxel.Voxel)2 Chunk (io.xol.chunkstories.api.world.chunk.Chunk)2 ByteBuffer (java.nio.ByteBuffer)2 Matrix4f (org.joml.Matrix4f)2