Example usage for org.lwjgl.opengl KHRDebug glDebugMessageControl

List of usage examples for org.lwjgl.opengl KHRDebug glDebugMessageControl

Introduction

In this page you can find the example usage for org.lwjgl.opengl KHRDebug glDebugMessageControl.

Prototype

public static void glDebugMessageControl(@NativeType("GLenum") int source, @NativeType("GLenum") int type,
        @NativeType("GLenum") int severity, @Nullable @NativeType("GLuint const *") int[] ids,
        @NativeType("GLboolean") boolean enabled) 

Source Link

Document

Array version of: #glDebugMessageControl DebugMessageControl

Usage

From source file:org.lwjgl.demo.nuklear.GLFWDemo.java

License:Open Source License

private void run() {
    GLFWErrorCallback.createPrint().set();
    if (!glfwInit()) {
        throw new IllegalStateException("Unable to initialize glfw");
    }/*from w ww .  ja  va2s. co  m*/

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    if (Platform.get() == Platform.MACOSX) {
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
    }
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);

    int WINDOW_WIDTH = 640;
    int WINDOW_HEIGHT = 640;

    win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "GLFW Nuklear Demo", NULL, NULL);
    if (win == NULL) {
        throw new RuntimeException("Failed to create the GLFW window");
    }

    glfwMakeContextCurrent(win);
    GLCapabilities caps = GL.createCapabilities();
    Callback debugProc = GLUtil.setupDebugMessageCallback();

    if (caps.OpenGL43) {
        glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_OTHER, GL_DEBUG_SEVERITY_NOTIFICATION,
                (IntBuffer) null, false);
    } else if (caps.GL_KHR_debug) {
        KHRDebug.glDebugMessageControl(KHRDebug.GL_DEBUG_SOURCE_API, KHRDebug.GL_DEBUG_TYPE_OTHER,
                KHRDebug.GL_DEBUG_SEVERITY_NOTIFICATION, (IntBuffer) null, false);
    } else if (caps.GL_ARB_debug_output) {
        glDebugMessageControlARB(GL_DEBUG_SOURCE_API_ARB, GL_DEBUG_TYPE_OTHER_ARB, GL_DEBUG_SEVERITY_LOW_ARB,
                (IntBuffer) null, false);
    }

    NkContext ctx = setupWindow(win);

    int BITMAP_W = 1024;
    int BITMAP_H = 1024;

    int FONT_HEIGHT = 18;
    int fontTexID = glGenTextures();

    STBTTFontinfo fontInfo = STBTTFontinfo.create();
    STBTTPackedchar.Buffer cdata = STBTTPackedchar.create(95);

    float scale;
    float descent;

    try (MemoryStack stack = stackPush()) {
        stbtt_InitFont(fontInfo, ttf);
        scale = stbtt_ScaleForPixelHeight(fontInfo, FONT_HEIGHT);

        IntBuffer d = stack.mallocInt(1);
        stbtt_GetFontVMetrics(fontInfo, null, d, null);
        descent = d.get(0) * scale;

        ByteBuffer bitmap = memAlloc(BITMAP_W * BITMAP_H);

        STBTTPackContext pc = STBTTPackContext.mallocStack(stack);
        stbtt_PackBegin(pc, bitmap, BITMAP_W, BITMAP_H, 0, 1, NULL);
        stbtt_PackSetOversampling(pc, 4, 4);
        stbtt_PackFontRange(pc, ttf, 0, FONT_HEIGHT, 32, cdata);
        stbtt_PackEnd(pc);

        // Convert R8 to RGBA8
        ByteBuffer texture = memAlloc(BITMAP_W * BITMAP_H * 4);
        for (int i = 0; i < bitmap.capacity(); i++) {
            texture.putInt((bitmap.get(i) << 24) | 0x00FFFFFF);
        }
        texture.flip();

        glBindTexture(GL_TEXTURE_2D, fontTexID);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, BITMAP_W, BITMAP_H, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV,
                texture);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

        memFree(texture);
        memFree(bitmap);
    }

    default_font.width((handle, h, text, len) -> {
        float text_width = 0;
        try (MemoryStack stack = stackPush()) {
            IntBuffer unicode = stack.mallocInt(1);

            int glyph_len = nnk_utf_decode(text, memAddress(unicode), len);
            int text_len = glyph_len;

            if (glyph_len == 0) {
                return 0;
            }

            IntBuffer advance = stack.mallocInt(1);
            while (text_len <= len && glyph_len != 0) {
                if (unicode.get(0) == NK_UTF_INVALID) {
                    break;
                }

                /* query currently drawn glyph information */
                stbtt_GetCodepointHMetrics(fontInfo, unicode.get(0), advance, null);
                text_width += advance.get(0) * scale;

                /* offset next glyph */
                glyph_len = nnk_utf_decode(text + text_len, memAddress(unicode), len - text_len);
                text_len += glyph_len;
            }
        }
        return text_width;
    }).height(FONT_HEIGHT).query((handle, font_height, glyph, codepoint, next_codepoint) -> {
        try (MemoryStack stack = stackPush()) {
            FloatBuffer x = stack.floats(0.0f);
            FloatBuffer y = stack.floats(0.0f);

            STBTTAlignedQuad q = STBTTAlignedQuad.mallocStack(stack);
            IntBuffer advance = stack.mallocInt(1);

            stbtt_GetPackedQuad(cdata, BITMAP_W, BITMAP_H, codepoint - 32, x, y, q, false);
            stbtt_GetCodepointHMetrics(fontInfo, codepoint, advance, null);

            NkUserFontGlyph ufg = NkUserFontGlyph.create(glyph);

            ufg.width(q.x1() - q.x0());
            ufg.height(q.y1() - q.y0());
            ufg.offset().set(q.x0(), q.y0() + (FONT_HEIGHT + descent));
            ufg.xadvance(advance.get(0) * scale);
            ufg.uv(0).set(q.s0(), q.t0());
            ufg.uv(1).set(q.s1(), q.t1());
        }
    }).texture().id(fontTexID);

    nk_style_set_font(ctx, default_font);

    glfwShowWindow(win);
    while (!glfwWindowShouldClose(win)) {
        /* Input */
        newFrame();

        demo.layout(ctx, 50, 50);
        calc.layout(ctx, 300, 50);

        try (MemoryStack stack = stackPush()) {
            IntBuffer width = stack.mallocInt(1);
            IntBuffer height = stack.mallocInt(1);

            glfwGetWindowSize(win, width, height);
            glViewport(0, 0, width.get(0), height.get(0));

            NkColorf bg = demo.background;
            glClearColor(bg.r(), bg.g(), bg.b(), bg.a());
        }
        glClear(GL_COLOR_BUFFER_BIT);
        /*
         * IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
         * with blending, scissor, face culling, depth test and viewport and
         * defaults everything back into a default state.
         * Make sure to either a.) save and restore or b.) reset your own state after
         * rendering the UI.
         */
        render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
        glfwSwapBuffers(win);
    }

    shutdown();

    glfwFreeCallbacks(win);
    if (debugProc != null) {
        debugProc.free();
    }
    glfwTerminate();
    Objects.requireNonNull(glfwSetErrorCallback(null)).free();
}

From source file:org.lwjgl.demo.util.tootle.HelloTootle.java

License:Open Source License

private HelloTootle() {
    GLFWErrorCallback.createPrint().set();
    if (!glfwInit()) {
        throw new IllegalStateException("Unable to initialize GLFW");
    }//  w w  w  .  ja  va2s.  com

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
    if (Platform.get() == Platform.MACOSX) {
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
        glfwWindowHint(GLFW_COCOA_RETINA_FRAMEBUFFER, GLFW_FALSE);
    }
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);

    window = glfwCreateWindow(width, height, "AMD Tootle demo", NULL, NULL);
    if (window == NULL) {
        throw new RuntimeException("Failed to create the GLFW window");
    }

    glfwMakeContextCurrent(window);
    GLCapabilities caps = GL.createCapabilities();
    if (!caps.OpenGL33) {
        throw new IllegalStateException("OpenGL 3.3 is required to run this demo.");
    }
    debugCB = GLUtil.setupDebugMessageCallback();
    if (debugCB != null) {
        if (caps.OpenGL43) {
            glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_OTHER, GL_DEBUG_SEVERITY_NOTIFICATION,
                    (IntBuffer) null, false);
        } else if (caps.GL_KHR_debug) {
            KHRDebug.glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_OTHER,
                    GL_DEBUG_SEVERITY_NOTIFICATION, (IntBuffer) null, false);
        }
    }

    glfwSwapInterval(0);

    glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
        if (action != GLFW_RELEASE) {
            return;
        }

        int scale;
        if ((mods & GLFW_MOD_CONTROL) != 0) {
            scale = 100;
        } else if ((mods & GLFW_MOD_SHIFT) != 0) {
            scale = 10;
        } else {
            scale = 1;
        }
        switch (key) {
        case GLFW_KEY_DOWN:
            if (slices > 3) {
                slices -= scale;
                if (slices < 3) {
                    slices = 3;
                }
                updateMesh();
            }
            break;
        case GLFW_KEY_UP:
            slices += scale;
            updateMesh();
            break;
        case GLFW_KEY_LEFT:
            if (stacks > 1) {
                stacks -= scale;
                if (stacks < 1) {
                    stacks = 1;
                }
                updateMesh();
            }
            break;
        case GLFW_KEY_RIGHT:
            stacks += scale;
            updateMesh();
            break;
        case GLFW_KEY_PAGE_DOWN:
            if (meshKey != GLFW_KEY_8) {
                seed--;
                updateMesh();
            } else {
                if (subdivisions > 1) {
                    subdivisions--;
                    updateMesh();
                }
            }
            break;
        case GLFW_KEY_PAGE_UP:
            if (meshKey != GLFW_KEY_8) {
                seed++;
                updateMesh();
            } else {
                subdivisions++;
                updateMesh();
            }
            break;
        case GLFW_KEY_KP_SUBTRACT:
            if ((mods & GLFW_MOD_CONTROL) != 0) {
                if (1 < cubeSize) {
                    setCubeSize(cubeSize - 1);
                }
            } else if (16 < vcacheSize) {
                vcacheSize -= 8;
                setMesh(mesh);
            }
            break;
        case GLFW_KEY_KP_ADD:
            if ((mods & GLFW_MOD_CONTROL) != 0) {
                setCubeSize(cubeSize + 1);
            } else if (vcacheSize < 32) {
                vcacheSize += 8;
                setMesh(mesh);
            }
            break;
        case GLFW_KEY_1:
        case GLFW_KEY_2:
        case GLFW_KEY_3:
        case GLFW_KEY_4:
        case GLFW_KEY_5:
        case GLFW_KEY_6:
        case GLFW_KEY_7:
        case GLFW_KEY_8:
            updateMesh(key);
            break;
        case GLFW_KEY_F1:
        case GLFW_KEY_F2:
        case GLFW_KEY_F3:
            vcacheMethod = key - GLFW_KEY_F1;
            setMesh(mesh);
            break;
        case GLFW_KEY_F4:
            optimizeVertexMemory = !optimizeVertexMemory;
            setMesh(mesh);
            break;
        case GLFW_KEY_F:
            fragmentShader = !fragmentShader;
            updateHUD();
            break;
        case GLFW_KEY_O:
            if ((mods & GLFW_MOD_CONTROL) != 0) {
                meshKey = -1;
                importMesh();
            } else {
                optimized = !optimized;
                if (mesh != null) {
                    updateMeshGPU();
                }
                updateHUD();
            }
            break;
        case GLFW_KEY_W:
            wireframe = !wireframe;
            updateHUD();
            break;
        case GLFW_KEY_ESCAPE:
            glfwSetWindowShouldClose(window, true);
            break;
        case GLFW_KEY_SPACE:
            if (mesh != null) {
                shuffleMesh();
            }
            break;
        }
    });

    glfwSetFramebufferSizeCallback(window, (window, width, height) -> {
        this.width = width;
        this.height = height;
        updateViewport(width, height);
    });

    // center window
    GLFWVidMode vidmode = Objects.requireNonNull(glfwGetVideoMode(glfwGetPrimaryMonitor()));
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);

    glfwShowWindow(window);

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glEnable(GL_CULL_FACE);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    updateViewport(width, height);

    vao = glGenVertexArrays();
    glBindVertexArray(vao); // bind and ignore

    vboMesh = glGenBuffers();
    iboMesh = glGenBuffers();
    vboHUD = glGenBuffers();
    vboPerf = glGenBuffers();

    glBindBuffer(GL_ARRAY_BUFFER, vboPerf);
    glBufferData(GL_ARRAY_BUFFER, 4 * 1024, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    updateMesh(GLFW_KEY_1);
    setCubeSize(8);

    programMesh = buildShaderProgram("#version 330\n" + "\n" + "uniform mat4 mMVP;\n"
            + "uniform mat3 mNORMAL;\n" + "uniform int cubeSize;\n" + "\n"
            + "layout(location = 0) in vec3 iPosition;\n" + "layout(location = 1) in vec3 iNormal;\n" + "\n"
            + "out vec3 vNormal;\n" + "\n" + "void main(void) {\n"
            + "  int x = gl_InstanceID / (cubeSize * cubeSize);\n"
            + "  int y = (gl_InstanceID / cubeSize) % cubeSize;\n" + "  int z = gl_InstanceID % cubeSize;\n"
            + "  float offset = -(cubeSize / 2);\n" + "  if (cubeSize + offset * 2 == 0) {\n"
            + "    offset += 0.5;" + "  }\n"
            + "  gl_Position = mMVP * vec4(iPosition + vec3(offset + float(x), offset + float(y), offset + float(z)), 1.0);\n"
            + "  vNormal = mNORMAL * iNormal;\n" + "}\n",
            "#version 330\n" + "\n" + "in vec3 vNormal;\n" + "\n" + "layout(location = 0) out vec4 oColor;\n"
                    + "\n" + "void main(void) {\n" + "  oColor = vec4(normalize(vNormal), 1.0);\n" + "}\n");
    uniformModelViewProjectionMatrix = glGetUniformLocation(programMesh, "mMVP");
    uniformNormalMatrix = glGetUniformLocation(programMesh, "mNORMAL");
    uniformCubeSize = glGetUniformLocation(programMesh, "cubeSize");

    programHUD = buildShaderProgram(
            "#version 330\n" + "\n" + "uniform mat4 mMVP;\n" + "\n"
                    + "layout(location = 0) in vec2 iPosition;\n" + "layout(location = 1) in vec4 iColor;\n"
                    + "\n" + "out vec4 vColor;\n" + "\n" + "void main(void) {\n"
                    + "  gl_Position = mMVP * vec4(iPosition, 0.0, 1.0);\n" + "  vColor = iColor;\n" + "}\n",
            "#version 330\n" + "\n" + "in vec4 vColor;\n" + "\n" + "layout(location = 0) out vec4 oColor;\n"
                    + "\n" + "void main(void) {\n" + "  oColor = vColor;\n" + "}\n");
    uniformModelViewProjectionMatrixHUD = glGetUniformLocation(programHUD, "mMVP");

    gpuGraph = new PerfGraph();
    gpuTimer = new GPUTimer();

    propertyStore = aiCreatePropertyStore();
    if (propertyStore == null) {
        throw new OutOfMemoryError();
    }
    aiSetImportPropertyInteger(propertyStore, AI_CONFIG_PP_PTV_NORMALIZE, 1);
}

From source file:tk.ivybits.engine.gl.GL.java

License:Open Source License

public static void glDebugMessageControl(int a, int b, int c, IntBuffer d, boolean e) {
    if (GLContext.getCapabilities().GL_KHR_debug)
        KHRDebug.glDebugMessageControl(a, b, c, d, e);
}