Java Code Examples for org.lwjgl.system.MemoryStack#stackPush()

The following examples show how to use org.lwjgl.system.MemoryStack#stackPush() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example 2
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example 3
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(String fileName) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(fileName, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + fileName  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example 4
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example 5
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example 6
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example 7
Source File: SoundBuffer.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private ShortBuffer readVorbis(String resource, int bufferSize, STBVorbisInfo info) throws Exception {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        vorbis = Utils.ioResourceToByteBuffer(resource, bufferSize);
        IntBuffer error = stack.mallocInt(1);
        long decoder = stb_vorbis_open_memory(vorbis, error, null);
        if (decoder == NULL) {
            throw new RuntimeException("Failed to open Ogg Vorbis file. Error: " + error.get(0));
        }

        stb_vorbis_get_info(decoder, info);

        int channels = info.channels();

        int lengthSamples = stb_vorbis_stream_length_in_samples(decoder);

        pcm = MemoryUtil.memAllocShort(lengthSamples);

        pcm.limit(stb_vorbis_get_samples_short_interleaved(decoder, channels, pcm) * channels);
        stb_vorbis_close(decoder);

        return pcm;
    }
}
 
Example 8
Source File: Texture.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
public Texture(ByteBuffer imageBuffer) throws Exception {
    ByteBuffer buf;
    // Load Texture file
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load_from_memory(imageBuffer, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    this.id = createTexture(buf);

    stbi_image_free(buf);
}
 
Example 9
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f[] matrices) {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        int length = matrices != null ? matrices.length : 0;
        FloatBuffer fb = stack.mallocFloat(16 * length);
        for (int i = 0; i < length; i++) {
            matrices[i].get(16 * i, fb);
        }
        glUniformMatrix4fv(uniforms.get(uniformName), false, fb);
    }
}
 
Example 10
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f[] matrices) {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        int length = matrices != null ? matrices.length : 0;
        FloatBuffer fb = stack.mallocFloat(16 * length);
        for (int i = 0; i < length; i++) {
            matrices[i].get(16 * i, fb);
        }
        glUniformMatrix4fv(uniforms.get(uniformName), false, fb);
    }
}
 
Example 11
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f value) {
    // Dump the matrix into a float buffer
    try (MemoryStack stack = MemoryStack.stackPush()) {
        glUniformMatrix4fv(uniforms.get(uniformName), false,
                           value.get(stack.mallocFloat(16)));
    }
}
 
Example 12
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f value) {
    // Dump the matrix into a float buffer
    try (MemoryStack stack = MemoryStack.stackPush()) {
        glUniformMatrix4fv(uniforms.get(uniformName), false,
                           value.get(stack.mallocFloat(16)));
    }
}
 
Example 13
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f value) {
    // Dump the matrix into a float buffer
    try (MemoryStack stack = MemoryStack.stackPush()) {
        glUniformMatrix4fv(uniforms.get(uniformName), false,
                           value.get(stack.mallocFloat(16)));
    }
}
 
Example 14
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f[] matrices) {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        int length = matrices != null ? matrices.length : 0;
        FloatBuffer fb = stack.mallocFloat(16 * length);
        for (int i = 0; i < length; i++) {
            matrices[i].get(16 * i, fb);
        }
        glUniformMatrix4fv(uniforms.get(uniformName), false, fb);
    }
}
 
Example 15
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f value) {
    // Dump the matrix into a float buffer
    try (MemoryStack stack = MemoryStack.stackPush()) {
        glUniformMatrix4fv(uniforms.get(uniformName), false,
                           value.get(stack.mallocFloat(16)));
    }
}
 
Example 16
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f value) {
    // Dump the matrix into a float buffer
    try (MemoryStack stack = MemoryStack.stackPush()) {
        glUniformMatrix4fv(uniforms.get(uniformName), false,
                           value.get(stack.mallocFloat(16)));
    }
}
 
Example 17
Source File: ShaderProgram.java    From lwjglbook with Apache License 2.0 5 votes vote down vote up
public void setUniform(String uniformName, Matrix4f[] matrices) {
    try (MemoryStack stack = MemoryStack.stackPush()) {
        int length = matrices != null ? matrices.length : 0;
        FloatBuffer fb = stack.mallocFloat(16 * length);
        for (int i = 0; i < length; i++) {
            matrices[i].get(16 * i, fb);
        }
        glUniformMatrix4fv(uniforms.get(uniformName), false, fb);
    }
}
 
Example 18
Source File: Terrain.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
/**
 * A Terrain is composed by blocks, each block is a GameItem constructed
 * from a HeightMap.
 *
 * @param terrainSize The number of blocks will be terrainSize * terrainSize
 * @param scale The scale to be applied to each terrain block
 * @param minY The minimum y value, before scaling, of each terrain block
 * @param maxY The maximum y value, before scaling, of each terrain block
 * @param heightMapFile
 * @param textureFile
 * @param textInc
 * @throws Exception
 */
public Terrain(int terrainSize, float scale, float minY, float maxY, String heightMapFile, String textureFile, int textInc) throws Exception {
    this.terrainSize = terrainSize;
    gameItems = new GameItem[terrainSize * terrainSize];

    ByteBuffer buf = null;
    int width;
    int height;
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(heightMapFile, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + heightMapFile  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    // The number of vertices per column and row
    verticesPerCol = width - 1;
    verticesPerRow = height - 1;

    heightMapMesh = new HeightMapMesh(minY, maxY, buf, width, height, textureFile, textInc);
    boundingBoxes = new Box2D[terrainSize][terrainSize];
    for (int row = 0; row < terrainSize; row++) {
        for (int col = 0; col < terrainSize; col++) {
            float xDisplacement = (col - ((float) terrainSize - 1) / (float) 2) * scale * HeightMapMesh.getXLength();
            float zDisplacement = (row - ((float) terrainSize - 1) / (float) 2) * scale * HeightMapMesh.getZLength();

            GameItem terrainBlock = new GameItem(heightMapMesh.getMesh());
            terrainBlock.setScale(scale);
            terrainBlock.setPosition(xDisplacement, 0, zDisplacement);
            gameItems[row * terrainSize + col] = terrainBlock;

            boundingBoxes[row][col] = getBoundingBox(terrainBlock);
        }
    }

    stbi_image_free(buf);
}
 
Example 19
Source File: Terrain.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
/**
 * A Terrain is composed by blocks, each block is a GameItem constructed
 * from a HeightMap.
 *
 * @param terrainSize The number of blocks will be terrainSize * terrainSize
 * @param scale The scale to be applied to each terrain block
 * @param minY The minimum y value, before scaling, of each terrain block
 * @param maxY The maximum y value, before scaling, of each terrain block
 * @param heightMapFile
 * @param textureFile
 * @param textInc
 * @throws Exception
 */
public Terrain(int terrainSize, float scale, float minY, float maxY, String heightMapFile, String textureFile, int textInc) throws Exception {
    this.terrainSize = terrainSize;
    gameItems = new GameItem[terrainSize * terrainSize];

    ByteBuffer buf = null;
    int width;
    int height;
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(heightMapFile, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + heightMapFile  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    // The number of vertices per column and row
    verticesPerCol = width - 1;
    verticesPerRow = height - 1;

    heightMapMesh = new HeightMapMesh(minY, maxY, buf, width, height, textureFile, textInc);
    boundingBoxes = new Box2D[terrainSize][terrainSize];
    for (int row = 0; row < terrainSize; row++) {
        for (int col = 0; col < terrainSize; col++) {
            float xDisplacement = (col - ((float) terrainSize - 1) / (float) 2) * scale * HeightMapMesh.getXLength();
            float zDisplacement = (row - ((float) terrainSize - 1) / (float) 2) * scale * HeightMapMesh.getZLength();

            GameItem terrainBlock = new GameItem(heightMapMesh.getMesh());
            terrainBlock.setScale(scale);
            terrainBlock.setPosition(xDisplacement, 0, zDisplacement);
            gameItems[row * terrainSize + col] = terrainBlock;

            boundingBoxes[row][col] = getBoundingBox(terrainBlock);
        }
    }

    stbi_image_free(buf);
}
 
Example 20
Source File: Terrain.java    From lwjglbook with Apache License 2.0 4 votes vote down vote up
/**
 * A Terrain is composed by blocks, each block is a GameItem constructed
 * from a HeightMap.
 *
 * @param terrainSize The number of blocks will be terrainSize * terrainSize
 * @param scale The scale to be applied to each terrain block
 * @param minY The minimum y value, before scaling, of each terrain block
 * @param maxY The maximum y value, before scaling, of each terrain block
 * @param heightMapFile
 * @param textureFile
 * @param textInc
 * @throws Exception
 */
public Terrain(int terrainSize, float scale, float minY, float maxY, String heightMapFile, String textureFile, int textInc) throws Exception {
    this.terrainSize = terrainSize;
    gameItems = new GameItem[terrainSize * terrainSize];

    ByteBuffer buf = null;
    int width;
    int height;
    try (MemoryStack stack = MemoryStack.stackPush()) {
        IntBuffer w = stack.mallocInt(1);
        IntBuffer h = stack.mallocInt(1);
        IntBuffer channels = stack.mallocInt(1);

        buf = stbi_load(heightMapFile, w, h, channels, 4);
        if (buf == null) {
            throw new Exception("Image file [" + heightMapFile  + "] not loaded: " + stbi_failure_reason());
        }

        width = w.get();
        height = h.get();
    }

    // The number of vertices per column and row
    verticesPerCol = width - 1;
    verticesPerRow = height - 1;

    heightMapMesh = new HeightMapMesh(minY, maxY, buf, width, height, textureFile, textInc);
    boundingBoxes = new Box2D[terrainSize][terrainSize];
    for (int row = 0; row < terrainSize; row++) {
        for (int col = 0; col < terrainSize; col++) {
            float xDisplacement = (col - ((float) terrainSize - 1) / (float) 2) * scale * HeightMapMesh.getXLength();
            float zDisplacement = (row - ((float) terrainSize - 1) / (float) 2) * scale * HeightMapMesh.getZLength();

            GameItem terrainBlock = new GameItem(heightMapMesh.getMesh());
            terrainBlock.setScale(scale);
            terrainBlock.setPosition(xDisplacement, 0, zDisplacement);
            gameItems[row * terrainSize + col] = terrainBlock;

            boundingBoxes[row][col] = getBoundingBox(terrainBlock);
        }
    }

    stbi_image_free(buf);
}