c++ - OpenGL 和 GLFW : Not drawing polygon

标签 c++ opengl glfw

我能够创建一个窗口并将其清除为所需的颜色。但是不能在左下角画一个正方形。

#include <iostream>

#define GLEW_STATIC

#include <GL/glew.h>
#include <GLFW/glfw3.h>

const GLint WIDTH = 720;
const GLint HEIGHT = 480;

int main()
{
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Attempts to set to opengl 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL", nullptr, nullptr);

    int screenWidth, screenHeight;

    glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

    if (nullptr == window)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();

        return EXIT_FAILURE;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;

    if (GLEW_OK != glewInit())
    {
        std::cout << "Failed to initialise GLEW" << std::endl;

        return EXIT_FAILURE;
    }

    glViewport(0, 0, screenWidth, screenHeight);

    while (!glfwWindowShouldClose(window))
    {
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glColor3f(0.0f, 0.0f, 1.0f);
        glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

        glBegin(GL_POLYGON);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.5, 0.0, 0.0);
            glVertex3f(0.5, 0.5, 0.0);
            glVertex3f(0.0, 0.5, 0.0);

        glEnd();
        glFlush();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();

    return EXIT_SUCCESS;
}

最佳答案

已弃用的固定函数功能(glBegin() 等和矩阵堆栈)在 Core 上下文 (GLFW_OPENGL_CORE_PROFILE) 中不起作用。

切换到兼容性上下文:

#include <iostream>

#include <GL/glew.h>
#include <GLFW/glfw3.h>

const GLint WIDTH = 720;
const GLint HEIGHT = 480;

int main()
{
    glfwInit();

    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Learn OpenGL", nullptr, nullptr);

    int screenWidth, screenHeight;

    glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

    if (nullptr == window)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();

        return EXIT_FAILURE;
    }

    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;

    if (GLEW_OK != glewInit())
    {
        std::cout << "Failed to initialise GLEW" << std::endl;

        return EXIT_FAILURE;
    }

    glViewport(0, 0, screenWidth, screenHeight);

    while (!glfwWindowShouldClose(window))
    {
        glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);

        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();

        glColor3f(0.0f, 0.0f, 1.0f);
        glBegin(GL_POLYGON);
            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(0.5, 0.0, 0.0);
            glVertex3f(0.5, 0.5, 0.0);
            glVertex3f(0.0, 0.5, 0.0);
        glEnd();

        glfwSwapBuffers(window);

        glfwPollEvents();
    }

    glfwTerminate();

    return EXIT_SUCCESS;
}

或者提供一些着色器并使用 VAO 和 VBO 上传您的几何图形。

关于c++ - OpenGL 和 GLFW : Not drawing polygon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41969843/

相关文章:

c++ - 从opencv中的每一行矩阵中减去一个 vector ?

opengl - 疯狂的闪烁窗口OpenGL GLFW

c++ - 在 GLFW 窗口标题中显示 FPS?

c++ - GLFW 输入状态

linux - 如何在 Linux 服务器上渲染图形

c++ - 检查外部应用程序是否可用的最佳方法是什么?

c++ - Visual Studio 中的 Qml

opengl - glVertexAttribPointer 内置顶点属性,如 gl_Vertex、gl_Normal

c++ 正在释放的指针未分配

C++/OpenGL : How does Tesselation work?