use of org.lwjgl.glfw.GLFWFramebufferSizeCallback in project lwjgl3-demos by LWJGL.
the class NoVerticesPolygonDemo method init.
void init() throws IOException {
glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);
@Override
public void invoke(int error, long description) {
if (error == GLFW_VERSION_UNAVAILABLE)
System.err.println("This demo requires OpenGL 3.0 or higher.");
delegate.invoke(error, description);
}
@Override
public void free() {
delegate.free();
}
});
if (!glfwInit())
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(width, height, "No vertices polygon shader demo", NULL, NULL);
if (window == NULL) {
throw new AssertionError("Failed to create the GLFW window");
}
glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
@Override
public void invoke(long window, int width, int height) {
if (width > 0 && height > 0 && (NoVerticesPolygonDemo.this.width != width || NoVerticesPolygonDemo.this.height != height)) {
NoVerticesPolygonDemo.this.width = width;
NoVerticesPolygonDemo.this.height = height;
}
}
});
System.out.println("Press 'arrow up' to increase the polygon vertex count");
System.out.println("Press 'arrow down' to decrease the polygon vertex count");
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
glfwSetWindowShouldClose(window, true);
} else if (key == GLFW_KEY_UP && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
count++;
} else if (key == GLFW_KEY_DOWN && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
count = Math.max(3, count - 1);
}
}
});
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwShowWindow(window);
caps = GL.createCapabilities();
debugProc = GLUtil.setupDebugMessageCallback();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// Create all needed GL resources
createProgram();
// and set some GL state
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
use of org.lwjgl.glfw.GLFWFramebufferSizeCallback in project lwjgl3-demos by LWJGL.
the class EdgeShaderDemo20 method init.
void init() throws IOException {
glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);
@Override
public void invoke(int error, long description) {
if (error == GLFW_VERSION_UNAVAILABLE)
System.err.println("This demo requires OpenGL 2.0 or higher.");
delegate.invoke(error, description);
}
@Override
public void free() {
delegate.free();
}
});
if (!glfwInit())
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(width, height, "Silhouette rendering with Sobel edge detection shader", NULL, NULL);
if (window == NULL) {
throw new AssertionError("Failed to create the GLFW window");
}
System.out.println("Press letter 'O' to toggle between outline/edges.");
System.out.println("Press spacebar to show/hide edges.");
glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
@Override
public void invoke(long window, int width, int height) {
if (width > 0 && height > 0 && (EdgeShaderDemo20.this.width != width || EdgeShaderDemo20.this.height != height)) {
EdgeShaderDemo20.this.width = width;
EdgeShaderDemo20.this.height = height;
EdgeShaderDemo20.this.resize = true;
}
}
});
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (action != GLFW_RELEASE)
return;
if (key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(window, true);
} else if (key == GLFW_KEY_O) {
outlineOnly = !outlineOnly;
} else if (key == GLFW_KEY_SPACE) {
showEdge = !showEdge;
}
}
});
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwMakeContextCurrent(window);
glfwSwapInterval(0);
glfwShowWindow(window);
caps = GL.createCapabilities();
if (!caps.GL_EXT_framebuffer_object) {
throw new AssertionError("This demo requires the EXT_framebuffer_object extension");
}
debugProc = GLUtil.setupDebugMessageCallback();
// using alpha = 0.0 is important here for the outline to work!
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
/* Create all needed GL resources */
createCube();
createQuad();
createNormalProgram();
createEdgeProgram();
createOutlineProgram();
createTex();
createFbo();
}
use of org.lwjgl.glfw.GLFWFramebufferSizeCallback in project lwjgl3-demos by LWJGL.
the class SilhouetteDemo method init.
void init() throws IOException {
glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);
@Override
public void invoke(int error, long description) {
if (error == GLFW_VERSION_UNAVAILABLE)
System.err.println("This demo requires OpenGL 2.0 or higher.");
delegate.invoke(error, description);
}
@Override
public void free() {
delegate.free();
}
});
if (!glfwInit())
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
window = glfwCreateWindow(width, height, "Silhouette rendering with geometry shader", NULL, NULL);
if (window == NULL) {
throw new AssertionError("Failed to create the GLFW window");
}
glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
@Override
public void invoke(long window, int width, int height) {
if (width > 0 && height > 0 && (SilhouetteDemo.this.width != width || SilhouetteDemo.this.height != height)) {
SilhouetteDemo.this.width = width;
SilhouetteDemo.this.height = height;
}
}
});
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
if (action != GLFW_RELEASE)
return;
if (key == GLFW_KEY_ESCAPE) {
glfwSetWindowShouldClose(window, true);
}
}
});
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
glfwMakeContextCurrent(window);
glfwSwapInterval(0);
glfwShowWindow(window);
caps = GL.createCapabilities();
if (!caps.GL_EXT_geometry_shader4) {
throw new AssertionError("This demo requires the EXT_geometry_shader4 extension");
}
debugProc = GLUtil.setupDebugMessageCallback();
glClearColor(0.55f, 0.75f, 0.95f, 1.0f);
glEnable(GL_DEPTH_TEST);
/* Create all needed GL resources */
createBoxVao();
createRasterProgram();
initProgram();
}
use of org.lwjgl.glfw.GLFWFramebufferSizeCallback in project chunkstories by Hugobros3.
the class GLFWGameWindow method run.
public void run() {
try {
// Client.onStart();
new IconLoader(this);
// Resize window logic
glfwSetFramebufferSizeCallback(glfwWindowHandle, (framebufferSizeCallback = new GLFWFramebufferSizeCallback() {
@Override
public void invoke(long window, int width, int height) {
windowWidth = width;
windowHeight = height;
glViewport(0, 0, width, height);
Layer layer = GLFWGameWindow.this.layer;
while (layer != null) {
layer.onResize(width, height);
layer = layer.getParentLayer();
}
}
}));
while (glfwWindowShouldClose(glfwWindowHandle) == false && !closeRequest) {
// Update pending actions
vramUsageVerticesObjects = VertexBufferGL.updateVerticesObjects();
Texture2DGL.updateTextureObjects();
FrameBufferObjectGL.updateFrameBufferObjects();
// Clear windows
renderingContext.getRenderTargetManager().clearBoundRenderTargetAll();
// Do scene changes etc
Iterator<SynchronousTask> is = mainThreadQueue.iterator();
while (is.hasNext()) {
SynchronousTask st = is.next();
st.run.run();
st.signal();
is.remove();
}
/*for (Runnable r : mainThreadQueue)
r.run();
mainThreadQueue.clear();*/
// Update audio
soundManager.update();
// update inputs first
client.getInputsManager().pollLWJGLInputs();
// Run scene content
if (layer != null) {
// then do the game logic
try {
layer.render(renderingContext);
}// Fucking tired of handling npes everywhere
catch (NullPointerException npe) {
npe.printStackTrace();
}
}
renderingContext.getGuiRenderer().drawBuffer();
tick();
// Clamp fps
int targetFPS = this.client.getConfiguration().getIntOption("client.video.framerateCap");
if (targetFPS != -1) {
// long time = System.currentTimeMillis();
sync(targetFPS);
// glFinish();
// long timeTook = System.currentTimeMillis() - time;
// timeTookLastTime = timeTook;
}
// Draw graph
if (client.getConfiguration().getBooleanOption("client.debug.frametimeGraph")) {
FrametimeRenderer.draw(renderingContext);
MemUsageRenderer.draw(renderingContext);
WorldLogicTimeRenderer.draw(renderingContext);
}
// Update the screen
// Display.update();
glfwSwapBuffers(glfwWindowHandle);
// Reset counters
GLCalls.nextFrame();
}
// Sane way of ending the game.
client.logger().info("Game exitting cleanly.");
soundManager.destroy();
client.onClose();
glfwDestroyWindow(glfwWindowHandle);
System.exit(0);
} catch (Throwable e) {
logger().error("A fatal error occured ! If you see the dev, show him this message !", e);
// e.printStackTrace();
// e.printStackTrace(logger().getPrintWriter());
}
}
use of org.lwjgl.glfw.GLFWFramebufferSizeCallback in project lwjgl3-demos by LWJGL.
the class TwoRotatingTrianglesDemo method main.
public static void main(String[] args) throws IOException {
if (!glfwInit()) {
throw new RuntimeException("Failed to initialize GLFW");
}
if (!glfwVulkanSupported()) {
throw new AssertionError("GLFW failed to find the Vulkan loader");
}
/* Look for instance extensions */
PointerBuffer requiredExtensions = glfwGetRequiredInstanceExtensions();
if (requiredExtensions == null) {
throw new AssertionError("Failed to find list of required Vulkan extensions");
}
// Create the Vulkan instance
final VkInstance instance = createInstance(requiredExtensions);
final VkDebugReportCallbackEXT debugCallback = new VkDebugReportCallbackEXT() {
public int invoke(int flags, int objectType, long object, long location, int messageCode, long pLayerPrefix, long pMessage, long pUserData) {
System.err.println("ERROR OCCURED: " + VkDebugReportCallbackEXT.getString(pMessage));
return 0;
}
};
final long debugCallbackHandle = setupDebugging(instance, VK_DEBUG_REPORT_ERROR_BIT_EXT | VK_DEBUG_REPORT_WARNING_BIT_EXT, debugCallback);
final VkPhysicalDevice physicalDevice = getFirstPhysicalDevice(instance);
final DeviceAndGraphicsQueueFamily deviceAndGraphicsQueueFamily = createDeviceAndGetGraphicsQueueFamily(physicalDevice);
final VkDevice device = deviceAndGraphicsQueueFamily.device;
int queueFamilyIndex = deviceAndGraphicsQueueFamily.queueFamilyIndex;
final VkPhysicalDeviceMemoryProperties memoryProperties = deviceAndGraphicsQueueFamily.memoryProperties;
// Create GLFW window
glfwDefaultWindowHints();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
long window = glfwCreateWindow(800, 600, "GLFW Vulkan Demo", NULL, NULL);
GLFWKeyCallback keyCallback;
glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
public void invoke(long window, int key, int scancode, int action, int mods) {
if (action != GLFW_RELEASE)
return;
if (key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, true);
}
});
LongBuffer pSurface = memAllocLong(1);
int err = glfwCreateWindowSurface(instance, window, null, pSurface);
final long surface = pSurface.get(0);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to create surface: " + translateVulkanResult(err));
}
// Create static Vulkan resources
final ColorAndDepthFormatAndSpace colorAndDepthFormatAndSpace = getColorFormatAndSpace(physicalDevice, surface);
final long commandPool = createCommandPool(device, queueFamilyIndex);
final VkCommandBuffer setupCommandBuffer = createCommandBuffer(device, commandPool);
final VkCommandBuffer postPresentCommandBuffer = createCommandBuffer(device, commandPool);
final VkQueue queue = createDeviceQueue(device, queueFamilyIndex);
final long renderPass = createRenderPass(device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat);
final long renderCommandPool = createCommandPool(device, queueFamilyIndex);
final Vertices vertices = createVertices(memoryProperties, device);
UboDescriptor uboDescriptor = createUniformBuffer(memoryProperties, device);
final long descriptorPool = createDescriptorPool(device);
final long descriptorSetLayout = createDescriptorSetLayout(device);
final long descriptorSet = createDescriptorSet(device, descriptorPool, descriptorSetLayout, uboDescriptor);
final Pipeline pipeline = createPipeline(device, renderPass, vertices.createInfo, descriptorSetLayout);
final class SwapchainRecreator {
boolean mustRecreate = true;
void recreate() {
// Begin the setup command buffer (the one we will use for swapchain/framebuffer creation)
VkCommandBufferBeginInfo cmdBufInfo = VkCommandBufferBeginInfo.calloc().sType(VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO).pNext(NULL);
int err = vkBeginCommandBuffer(setupCommandBuffer, cmdBufInfo);
cmdBufInfo.free();
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to begin setup command buffer: " + translateVulkanResult(err));
}
long oldChain = swapchain != null ? swapchain.swapchainHandle : VK_NULL_HANDLE;
// Create the swapchain (this will also add a memory barrier to initialize the framebuffer images)
swapchain = createSwapChain(device, physicalDevice, surface, oldChain, setupCommandBuffer, width, height, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.colorSpace);
// Create depth-stencil image
depthStencil = createDepthStencil(device, memoryProperties, colorAndDepthFormatAndSpace.depthFormat, setupCommandBuffer);
err = vkEndCommandBuffer(setupCommandBuffer);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to end setup command buffer: " + translateVulkanResult(err));
}
submitCommandBuffer(queue, setupCommandBuffer);
vkQueueWaitIdle(queue);
if (framebuffers != null) {
for (int i = 0; i < framebuffers.length; i++) vkDestroyFramebuffer(device, framebuffers[i], null);
}
framebuffers = createFramebuffers(device, swapchain, renderPass, width, height, depthStencil);
// Create render command buffers
if (renderCommandBuffers != null) {
vkResetCommandPool(device, renderCommandPool, VK_FLAGS_NONE);
}
renderCommandBuffers = createRenderCommandBuffers(device, renderCommandPool, framebuffers, renderPass, width, height, pipeline, descriptorSet, vertices.verticesBuf);
mustRecreate = false;
}
}
final SwapchainRecreator swapchainRecreator = new SwapchainRecreator();
// Handle canvas resize
GLFWFramebufferSizeCallback framebufferSizeCallback = new GLFWFramebufferSizeCallback() {
public void invoke(long window, int width, int height) {
if (width <= 0 || height <= 0)
return;
swapchainRecreator.mustRecreate = true;
TwoRotatingTrianglesDemo.width = width;
TwoRotatingTrianglesDemo.height = height;
}
};
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
glfwShowWindow(window);
// Pre-allocate everything needed in the render loop
IntBuffer pImageIndex = memAllocInt(1);
int currentBuffer = 0;
PointerBuffer pCommandBuffers = memAllocPointer(1);
LongBuffer pSwapchains = memAllocLong(1);
LongBuffer pImageAcquiredSemaphore = memAllocLong(1);
LongBuffer pRenderCompleteSemaphore = memAllocLong(1);
// Info struct to create a semaphore
VkSemaphoreCreateInfo semaphoreCreateInfo = VkSemaphoreCreateInfo.calloc().sType(VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO).pNext(NULL).flags(VK_FLAGS_NONE);
// Info struct to submit a command buffer which will wait on the semaphore
IntBuffer pWaitDstStageMask = memAllocInt(1);
pWaitDstStageMask.put(0, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT);
VkSubmitInfo submitInfo = VkSubmitInfo.calloc().sType(VK_STRUCTURE_TYPE_SUBMIT_INFO).pNext(NULL).waitSemaphoreCount(pImageAcquiredSemaphore.remaining()).pWaitSemaphores(pImageAcquiredSemaphore).pWaitDstStageMask(pWaitDstStageMask).pCommandBuffers(pCommandBuffers).pSignalSemaphores(pRenderCompleteSemaphore);
// Info struct to present the current swapchain image to the display
VkPresentInfoKHR presentInfo = VkPresentInfoKHR.calloc().sType(VK_STRUCTURE_TYPE_PRESENT_INFO_KHR).pNext(NULL).pWaitSemaphores(pRenderCompleteSemaphore).swapchainCount(pSwapchains.remaining()).pSwapchains(pSwapchains).pImageIndices(pImageIndex).pResults(null);
// The render loop
long lastTime = System.nanoTime();
float time = 0.0f;
while (!glfwWindowShouldClose(window)) {
// Handle window messages. Resize events happen exactly here.
// So it is safe to use the new swapchain images and framebuffers afterwards.
glfwPollEvents();
if (swapchainRecreator.mustRecreate)
swapchainRecreator.recreate();
// Create a semaphore to wait for the swapchain to acquire the next image
err = vkCreateSemaphore(device, semaphoreCreateInfo, null, pImageAcquiredSemaphore);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to create image acquired semaphore: " + translateVulkanResult(err));
}
// Create a semaphore to wait for the render to complete, before presenting
err = vkCreateSemaphore(device, semaphoreCreateInfo, null, pRenderCompleteSemaphore);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to create render complete semaphore: " + translateVulkanResult(err));
}
// Get next image from the swap chain (back/front buffer).
// This will setup the imageAquiredSemaphore to be signalled when the operation is complete
err = vkAcquireNextImageKHR(device, swapchain.swapchainHandle, UINT64_MAX, pImageAcquiredSemaphore.get(0), VK_NULL_HANDLE, pImageIndex);
currentBuffer = pImageIndex.get(0);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to acquire next swapchain image: " + translateVulkanResult(err));
}
// Select the command buffer for the current framebuffer image/attachment
pCommandBuffers.put(0, renderCommandBuffers[currentBuffer]);
// Update UBO
long thisTime = System.nanoTime();
time += (thisTime - lastTime) / 1E9f;
lastTime = thisTime;
updateUbo(device, uboDescriptor, time);
// Submit to the graphics queue
err = vkQueueSubmit(queue, submitInfo, VK_NULL_HANDLE);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to submit render queue: " + translateVulkanResult(err));
}
// Present the current buffer to the swap chain
// This will display the image
pSwapchains.put(0, swapchain.swapchainHandle);
err = vkQueuePresentKHR(queue, presentInfo);
if (err != VK_SUCCESS) {
throw new AssertionError("Failed to present the swapchain image: " + translateVulkanResult(err));
}
// Create and submit post present barrier
vkQueueWaitIdle(queue);
// Destroy this semaphore (we will create a new one in the next frame)
vkDestroySemaphore(device, pImageAcquiredSemaphore.get(0), null);
vkDestroySemaphore(device, pRenderCompleteSemaphore.get(0), null);
submitPostPresentBarrier(swapchain.images[currentBuffer], postPresentCommandBuffer, queue);
}
presentInfo.free();
memFree(pWaitDstStageMask);
submitInfo.free();
memFree(pImageAcquiredSemaphore);
memFree(pRenderCompleteSemaphore);
semaphoreCreateInfo.free();
memFree(pSwapchains);
memFree(pCommandBuffers);
vkDestroyDebugReportCallbackEXT(instance, debugCallbackHandle, null);
framebufferSizeCallback.free();
keyCallback.free();
glfwDestroyWindow(window);
glfwTerminate();
// We don't bother disposing of all Vulkan resources.
// Let the OS process manager take care of it.
}
Aggregations