0
votes

OpenGL draws only the background, the yellow point does not appear. I want to draw it with glBegin and glEnd. The coordinates are variables, because I want to move that point later. Most of the code is just glfw initialization the function that worries me is the draw_player function since there the drawcall is contained. The Fix I stumbled upon, to use GL_POINTS instead of GL_POINT (in glBegin as argument), does not help (I continue to use it though).

#include <GLFW/glfw3.h>
//#include <stdio.h>

//coordinates
int px, py;

//My not working function
void draw_player()
{
    glColor3f(1.0f, 1.0f, 0);
    glPointSize(64.0f);
    glBegin(GL_POINTS);
    glVertex2i(px, py);
    glEnd();
}

int main(int argc, char* argv[])
{
    GLFWwindow* window;

    if (!glfwInit())
        return -1;

    window = glfwCreateWindow(910, 512, "Raycast", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    glClearColor(0.1f, 0.1f, 0.5f, 1.0f);
    //setting the coordinates
    px = 100;
    py = 10;


    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        draw_player();

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}
1
The coordinate (100, 10) is not in the window. You have not specified a projection matrix. Therefore, you must specify the coordinates of the point in the normalized device space (in the range [-1.0, 1.0]) - Rabbid76
Thx a lot, I use now glVertex2f with ranges from -1.0 and 1.0 and it works. I still have a question if it's not a problem, how is the glVertex2i to be used? - usernameisunavaible
Read the answer - Rabbid76

1 Answers

1
votes

The coordinate (100, 10) is not in the window. You have not specified a projection matrix. Therefore, you must specify the coordinates of the point in the normalized device space (in the range [-1.0, 1.0]).
If you want to specify the coordinates in pixel units, you must define a suitable orthographic projection with glOrtho:

int main(int argc, char* argv[])
{
    GLFWwindow* window;

    if (!glfwInit())
        return -1;

    window = glfwCreateWindow(910, 512, "Raycast", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);

    glMatrixMode(GL_PROJECTION);
    glOrtho(0, 910.0, 512.0, 0.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);

    // [...]
}