我尝试按照C语言在OpenGL中使用this教程,但是当涉及第二个教程时,该教程应该在窗口上绘制一个三角形,我什么也看不到。所以这就是我所做的,我使用了创建OpenGL上下文,窗口和内容的代码,并试图使其变得更简单:我尝试使用glBegin/glEnd而不是使用VAO。

我收到此错误:1282“无效操作”。我只是使用直接来自我的LWJGL项目的相同句子。主循环是如此简单,我无法理解它是如何工作的,并且1282错误没有提供任何信息。为什么仍然出现错误?

#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32s.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")

// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.
#define GLEW_STATIC
#include <GL/glew.h>

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

// Include GLM
#include <glm/glm.hpp>
using namespace glm;

void checkErrors() {
    int error = glGetError();
    if (error != GL_NO_ERROR) {
        printf("%s (%d)\n", gluErrorString(error), error);
    }
}

int main(void)
{
    // Initialise GLFW
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL

    // Open a window and create its OpenGL context
    GLFWwindow* window; // (In the accompanying source code, this variable is global)
    window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
    if (window == NULL){
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window); // Initialize GLEW
    glewExperimental = true; // Needed in core profile
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do{
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
            glVertex3f(-1.0f, -1.0f, 0.0f);
            glVertex3f( 1.0f, -1.0f, 0.0f);
            glVertex3f( 0.0f,  1.0f, 0.0f);
        glEnd();

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

        // Check for errors
        checkErrors();

    } // Check if the ESC key was pressed or the window was closed
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
    glfwWindowShouldClose(window) == 0);

    return 0;
}

最佳答案

不推荐使用glBegin/glEnd以及所有其他即时模式绘图功能以及更多功能,不能与OpenGL 3.1(及更高版本)内核和前向兼容上下文一起使用。

您可以尝试请求3.0兼容性上下文(包括所有不推荐使用的功能)。为此,请删除glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);行,并将次要版本提示更改为0。实际上,根据OpenGL wiki,无论如何,您都不应明确要求与3.1及更高版本兼容的前向兼容上下文。但是,最好的办法是弄清楚VAO代码出了什么问题,而不是混用已弃用的功能。

09-07 05:37