HopefullyHelpful
HopefullyHelpful

Reputation: 1819

Can you mix shader and non shader code in opengl?

I'm currently trying to implement a minimap (basically just colored pixels next to each other) with the LWJGL opengl library.

So I have the problem when I try to use glBegin(GL_POINTS); for the following commands

glColor3i(stack.stack.get(z).minimapColor.getRed(),stack.stack.get(z).minimapColor.getRed(), stack.stack.get(z).minimapColor.getBlue());
glVertex2i(xStart-Board.map(x-x0), yStart-Board.map(y-y0));

It tells me that I don't have a context or the function is not allowed in the current context:

FATAL ERROR in native method: Thread[main,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
    at org.lwjgl.opengl.GL11.glBegin(Native Method)
    at UI.Minimap.draw(Minimap.java:34)
    at newUI.game.Renderer.render(Renderer.java:81)
    at newUI.game.Game.render(Game.java:52)
    at newUI.engine.GameEngine.gameLoop(GameEngine.java:63)
    at newUI.engine.GameEngine.run(GameEngine.java:33)
    at newUI.game.MainEcoGameLWJGL2_2021.main(MainEcoGameLWJGL2_2021.java:13)

Problem is I clearly have a context, I checked it in my code. I'm also on the main thread where my other stuff is drawn.

I tried moving the code around (putting it behind my shader unbind or before my shader bind)

public void render(final Window window, final BoardMode boardMode, final Player player, final Board board) {
    clear();
    
    if (window.isResized()) {
        glViewport(0, 0, window.getWidth(), window.getHeight());
        window.setResized(false);
    }

    shaderProgram.bind();

    shaderProgram.setUniform("texture_sampler", 0);
    // Render each gameItem
    switch(boardMode) {
        case STARTING_MENU:
            renderStartingMenu(window);
            break;
        case GAME:
            renderGame(window, player, board);
            break;
    }
    drawFPS(window);
    drawMode(window);

    shaderProgram.unbind();
}

Am I just missing something basic to use non-shader style opengl or is it not possible to mix shaders with non-shader opengl?

Most things I have found online in the last 7 hours are mostly shader based code, or atleast they bind the coordinates into an array and then use that array to draw.

I'm aware that more code would be necessary to debug my code, but I'm only asking for the general question, "is it possible to use very basic non-shader based opengl in mix with shader based code?"

PS: here the requested code part with calls to glfwWindowHint() and GL.createCapabilities() and glfwMakeContextCurrent():

public void init() {
    // Setup an error callback. The default implementation
    // will print the error message in System.err.
    GLFWErrorCallback.createPrint(System.err).set();

    // Initialize GLFW. Most GLFW functions will not work before doing this.
    if (!glfwInit()) {
        throw new IllegalStateException("Unable to initialize GLFW");
    }

    glfwDefaultWindowHints(); // optional, the current window hints are already the default
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);

    // Create the window
    windowHandle = glfwCreateWindow(width, height, title, NULL, NULL);
    if (windowHandle == NULL) {
        throw new RuntimeException("Failed to create the GLFW window");
    }

    // Setup resize callback
    glfwSetFramebufferSizeCallback(windowHandle, (window, width, height) -> {
        this.width = width;
        this.height = height;
        this.setResized(true);
    });
    
    glfwSetWindowCloseCallback(windowHandle, (window)->{
        saveAndQuit();
    });

    // Setup a key callback. It will be called every time a key is pressed, repeated or released.
    glfwSetKeyCallback(windowHandle, (window, key, scancode, action, mods) -> {
        KeyProcessor.generateKeyEvents(window, key, scancode, action, mods);
    });

    // Get the resolution of the primary monitor
    final GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());


    // Make the OpenGL context current
    glfwMakeContextCurrent(windowHandle);

    if (isvSync()) {
        // Enable v-sync
        glfwSwapInterval(1);
    }

    // Make the window visible
    glfwShowWindow(windowHandle);

    GL.createCapabilities();

    // Set the clear color
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    //glEnable(GL_DEPTH_TEST);
}

Upvotes: 0

Views: 179

Answers (1)

genpfault
genpfault

Reputation: 52164

Should work fine provided you request a Compatibility context & use program 0 for the fixed-function draws.

Upvotes: 1

Related Questions