OpenGL不会绘制一个点

o3imoua4  于 2022-09-26  发布在  其他
关注(0)|答案(1)|浏览(195)

OpenGL仅绘制背景,不会显示黄点。我想用glBeginglEnd来画它。坐标是变量,因为我想稍后移动那个点。大部分代码只是GLFW初始化,我担心的是draw_player函数,因为那里包含了DRAW调用。我偶然发现的修复方法是使用GL_POINTS而不是GL_POINT(在glBegin中作为参数),但没有帮助(尽管我继续使用它)。


# 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;
}
11dmarpk

11dmarpk1#

坐标(100,10)不在窗口中。您尚未指定投影矩阵。因此,您必须在归一化设备空间中指定点的坐标(在范围[-1.0,1.0]内)。
如果要以像素单位指定坐标,则必须使用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);

    // [...]
}

相关问题