|
| 1 | +package gg.generations.rarecandy.tools.gui; |
| 2 | + |
| 3 | +import com.spinyowl.legui.system.context.CallbackKeeper; |
| 4 | +import com.spinyowl.legui.system.context.DefaultCallbackKeeper; |
| 5 | +import gg.generations.rarecandy.pokeutils.MaterialReference; |
| 6 | +import gg.generations.rarecandy.pokeutils.PixelAsset; |
| 7 | +import gg.generations.rarecandy.renderer.loading.ModelLoader; |
| 8 | +import gg.generations.rarecandy.renderer.rendering.FrameBuffer; |
| 9 | +import org.liquidengine.cbchain.IChainCharCallback; |
| 10 | +import org.lwjgl.glfw.GLFWKeyCallbackI; |
| 11 | +import org.lwjgl.glfw.GLFWWindowCloseCallbackI; |
| 12 | +import org.lwjgl.nanovg.*; |
| 13 | +import org.lwjgl.opengl.GL11; |
| 14 | +import org.lwjgl.opengl.GL30; |
| 15 | +import org.lwjgl.util.nfd.NativeFileDialog; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +import static gg.generations.rarecandy.tools.gui.MinimalQuad.mapDuplicatesWithStreams; |
| 23 | +import static org.lwjgl.glfw.GLFW.GLFW_KEY_ESCAPE; |
| 24 | +import static org.lwjgl.glfw.GLFW.GLFW_RELEASE; |
| 25 | +import static org.lwjgl.nanovg.NanoVG.*; |
| 26 | +import static org.lwjgl.nanovg.NanoVGGL2.*; |
| 27 | +import static org.lwjgl.nanovg.NanoVGGL3.*; |
| 28 | +import static org.lwjgl.nanovg.NanoVGGL3.NVG_ANTIALIAS; |
| 29 | +import static org.lwjgl.nanovg.NanoVGGL3.NVG_STENCIL_STROKES; |
| 30 | +import static org.lwjgl.opengl.GL11.*; |
| 31 | +import static org.lwjgl.opengl.GL30.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * Example usage of FrameBuffer with GLFWCanvas and NanoVG. |
| 35 | + */ |
| 36 | +public class FBOCanvasUsingFrameBuffer extends GLFWCanvas { |
| 37 | + |
| 38 | + private FrameBuffer fbo; |
| 39 | + private final int fboWidth = 200; |
| 40 | + private final int fboHeight = 200; |
| 41 | + |
| 42 | + private long nvgContext; |
| 43 | + private int fboImageID; |
| 44 | + |
| 45 | + private List<String> materialNames = new ArrayList<>(); |
| 46 | + |
| 47 | + |
| 48 | + // Track rotation |
| 49 | + private long lastTime = System.currentTimeMillis(); |
| 50 | + |
| 51 | + public FBOCanvasUsingFrameBuffer(int width, int height, String title) { |
| 52 | + super(width, height, title); |
| 53 | + initCallbacks(); |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) { |
| 57 | + |
| 58 | + |
| 59 | + FBOCanvasUsingFrameBuffer canvas = new FBOCanvasUsingFrameBuffer(1024, 1024, "Using FrameBuffer"); |
| 60 | + canvas.run(); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void initGL() { |
| 65 | + // Determine NanoVG version |
| 66 | + |
| 67 | + int flags = NVG_STENCIL_STROKES | NVG_ANTIALIAS; |
| 68 | + nvgContext = NanoVGGL3.nvgCreate(flags); |
| 69 | + if (nvgContext == 0) { |
| 70 | + throw new RuntimeException("Failed to create NanoVG context"); |
| 71 | + } |
| 72 | + |
| 73 | + // Create our off-screen FrameBuffer |
| 74 | + fbo = new FrameBuffer(fboWidth, fboHeight); |
| 75 | + |
| 76 | + // Tell NanoVG about the FrameBuffer’s texture so we can draw it on screen later |
| 77 | + fboImageID = NanoVGGL3.nnvglCreateImageFromHandle(nvgContext, fbo.getTextureId(), |
| 78 | + fboWidth, fboHeight, 0); |
| 79 | + |
| 80 | + glEnable(GL_BLEND); |
| 81 | + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void paintGL() { |
| 86 | + // Compute rotation angle |
| 87 | + long thisTime = System.currentTimeMillis(); |
| 88 | + float angle = (lastTime - thisTime)/10000000f; |
| 89 | + lastTime = thisTime; |
| 90 | + |
| 91 | + System.out.println(angle); |
| 92 | + |
| 93 | + // 1) Render the rotating rectangle onto our FrameBuffer |
| 94 | + renderToFrameBuffer(angle); |
| 95 | + |
| 96 | + // Clear main window |
| 97 | + glClearColor(0.3f, 0.3f, 0.5f, 1.0f); |
| 98 | + glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 99 | + |
| 100 | + renderBuffer(); |
| 101 | + |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + private void renderBuffer() { |
| 106 | + // Paint the FBO image as a fullscreen quad using NanoVG |
| 107 | + nvgBeginFrame(nvgContext, width, height, 1f); |
| 108 | + try (NVGPaint paint = NVGPaint.calloc()) { |
| 109 | + nvgImagePattern(nvgContext, width/4, height/4, width/2, height/4, 0f, fboImageID, 1f, paint); |
| 110 | + |
| 111 | + nvgBeginPath(nvgContext); |
| 112 | + nvgRect(nvgContext, width/4, height/4, width/2, height/4); |
| 113 | + nvgFillPaint(nvgContext, paint); |
| 114 | + nvgFill(nvgContext); |
| 115 | + } |
| 116 | + nvgEndFrame(nvgContext); |
| 117 | + } |
| 118 | + |
| 119 | + private void renderToFrameBuffer(float angle) { |
| 120 | + // Bind off-screen FBO for drawing |
| 121 | + fbo.bindFramebuffer(); |
| 122 | + glViewport(0, 0, fboWidth, fboHeight); |
| 123 | + |
| 124 | + // Clear off-screen area |
| 125 | + glClearColor(0.3f, 0.5f, 0.7f, 1f); |
| 126 | + glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 127 | + |
| 128 | + // Use NanoVG to draw rotating rectangle |
| 129 | + nvgBeginFrame(nvgContext, fboWidth, fboHeight, 1f); |
| 130 | + try (NVGColor green = NVGColor.calloc(); NVGColor black = NVGColor.calloc()) { |
| 131 | + green.r(0f).g(1f).b(0f).a(1f); |
| 132 | + black.r(0f).g(0f).b(0f).a(1f); |
| 133 | + |
| 134 | + nvgTranslate(nvgContext, fboWidth / 2f, fboHeight / 2f); |
| 135 | + nvgRotate(nvgContext, angle); |
| 136 | + |
| 137 | + nvgBeginPath(nvgContext); |
| 138 | + nvgRect(nvgContext, -fboWidth / 4f, -fboHeight / 4f, fboWidth / 2f, fboHeight / 2f); |
| 139 | + nvgStrokeColor(nvgContext, black); |
| 140 | + nvgStroke(nvgContext); |
| 141 | + |
| 142 | + nvgBeginPath(nvgContext); |
| 143 | + nvgRect(nvgContext, -fboWidth / 4f, -fboHeight / 4f, fboWidth / 2f, fboHeight / 2f); |
| 144 | + nvgFillColor(nvgContext, green); |
| 145 | + nvgFill(nvgContext); |
| 146 | + } |
| 147 | + nvgEndFrame(nvgContext); |
| 148 | + |
| 149 | + // Unbind FBO to return to default |
| 150 | + fbo.unbindFramebuffer(); |
| 151 | + |
| 152 | + glViewport(0, 0, width, height); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + protected void cleanup() { |
| 157 | + super.cleanup(); // destroys window and terminates GLFW |
| 158 | + if (nvgContext != 0) { |
| 159 | + boolean isVersionNew = (glGetInteger(GL30.GL_MAJOR_VERSION) > 3) |
| 160 | + || (glGetInteger(GL30.GL_MAJOR_VERSION) == 3 && glGetInteger(GL30.GL_MINOR_VERSION) >= 2); |
| 161 | + if (isVersionNew) { |
| 162 | + NanoVGGL3.nnvgDelete(nvgContext); |
| 163 | + } else { |
| 164 | + NanoVGGL2.nnvgDelete(nvgContext); |
| 165 | + } |
| 166 | + } |
| 167 | + if (fbo != null) { |
| 168 | + fbo.close(); // clean up GPU resources |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + protected void initCallbacks() { |
| 173 | + var keeper = new DefaultCallbackKeeper(); |
| 174 | + CallbackKeeper.registerCallbacks(window, keeper); |
| 175 | + |
| 176 | + GLFWKeyCallbackI glfwKeyCallbackI = (w, key, scancode, action, mods) -> { |
| 177 | + if (key == GLFW_KEY_ESCAPE && action != GLFW_RELEASE) { |
| 178 | + running = false; |
| 179 | + } |
| 180 | + }; |
| 181 | + |
| 182 | + keeper.getChainKeyCallback().add(glfwKeyCallbackI); |
| 183 | + |
| 184 | + GLFWWindowCloseCallbackI glfwWindowCloseCallbackI = w -> running = false; |
| 185 | + keeper.getChainWindowCloseCallback().add(glfwWindowCloseCallbackI); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + protected CallbackKeeper getCallBackKeeper() { |
| 190 | + return null; |
| 191 | + } |
| 192 | +} |
0 commit comments