Home > Net >  OpenGL doesn't draw a point
OpenGL doesn't draw a point

Time:07-29

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 draw call 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;
}

CodePudding user response:

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);

    // [...]
}
  • Related