I can not draw a line using OpenGL

49 views Asked by At

I'm trying to make a raycaster:

#include <iostream>
#include <cmath>
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#define HOMESCREEN 0
#define GAMESCREEN 1
#define PAUSESCREEN 2
#define ESCAPE 0
#define SPACE_BAR 1
#define KEY_R 2
using namespace std;

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

void voProcessInput(GLFWwindow* window, bool inputs[])
{
    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    {
        inputs[ESCAPE] = true;
    }
    if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
    {
        inputs[SPACE_BAR] = true;
    }
    if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
    {
        inputs[KEY_R] = true;
    }
}


void voDrawMap2D(int a_intWidth, int a_intHight, int a_intVerticalWallsCounter, int a_intHorizontalBlocks)
{
    float l_flXvalue = 0.0f;
    float l_flYValue = 0.0f;
    glLineWidth(2.5);
    /*we tekenen lijnen*/
    glBegin(GL_LINES);
    glColor3f(1.0f, 1.0f, 1.0f);
    for (int i = 1; i < a_intVerticalWallsCounter; i++)
    {
        l_flXvalue = 2.0f / a_intVerticalWallsCounter * i;
        // Eerste punt van de lijn
        glVertex2f((l_flXvalue - 1.0f), 1.0f);
        // Tweede punt van de lijn
        glVertex2f((l_flXvalue - 1.0f), -1.0f);
    }
    glEnd();
}

int main()
{
    /*Initialisatie variabelen*/
    int current_screen = HOMESCREEN;
    // Initialisatie GLFW
    if (!glfwInit())
        return -1;

    // Configuratie GLFW, gebruik OpenGL 4.6
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // Maak een GLFW-venster in fullscreen-modus
    GLFWmonitor* monitor = glfwGetPrimaryMonitor();
    const GLFWvidmode* mode = glfwGetVideoMode(monitor);
    GLFWwindow* window = glfwCreateWindow(mode->width, mode->height, "GLFW en GLAD Voorbeeld", monitor, nullptr);

    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    // Maak het huidige venster de context
    glfwMakeContextCurrent(window);

    // Initialiseer GLAD
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        return -1;
    }

    

    // Hoofdloop
    while (!glfwWindowShouldClose(window))
    {
        // Invoer verwerken
        bool l_boInputs[3] = { 0,0,0 };
        voProcessInput(window, l_boInputs);

        // Renderen van verschillende menus
        switch (current_screen) 
        {
            case HOMESCREEN:
                /*we renderen hier de code voor de homescreen*/
                /*we geven het een blauwe achtergrond*/
                glClearColor(0.3f, 0.0f, 1.0f, 1.0f);
                if (l_boInputs[SPACE_BAR]) current_screen = GAMESCREEN;
                break;
            case GAMESCREEN:
                glClearColor(0.3f, 1.0f, 0.0f, 1.0f);
                glClear(GL_COLOR_BUFFER_BIT);
                glfwSwapBuffers(window);
                voDrawMap2D(mode->width, mode->height, 5, 5);
                glClear(GL_COLOR_BUFFER_BIT);
                glfwSwapBuffers(window);
                break;
            default:
                /*nog niks hier*/
                glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        }
        if (l_boInputs[ESCAPE]) glfwSetWindowShouldClose(window, true);
        // Wissel buffers en controleer gebeurtenissen
        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    // GLFW-resources opruimen
    glfwTerminate();
    return 0;
}

...but I seem to fail at drawing a line.

I'm drawing the lines in the "voDrawMap2D()" function. I use my function in my loop at the GAMESCREEN part.

1

There are 1 answers

0
genpfault On

You can't use deprecated functions like glBegin()/glEnd() & glVertex2f() on Core contexts.

Either switch to a compatibility context (GLFW_OPENGL_CORE_PROFILE -> GLFW_OPENGL_COMPAT_PROFILE) or port all your logic to use shaders and vertex buffers.