无法在OpenGL C++中渲染三角形

4
我曾尝试跟随this的OpenGL教程,但在第二个教程中,即应该在窗口上绘制三角形的教程中,我看不到任何东西。因此,我采用了创建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;
}

你也许需要加上glEnalbe(GL_DEPTH_TEST)。你缺失了很多,这可能有所帮助- http://www.videotutorialsrock.com/opengl_tutorial/basic_shapes/text.php - Irrational Person
1
www.opengl-tutorial.org 之所以坚持使用现代OpenGL(3.1+),是有原因的。我建议您在对OpenGL有更好的理解之前,仍然遵循教程中所呈现的内容。现代方式一开始可能会感觉更加困难,但一旦掌握了其工作模式,您会庆幸自己一直坚持下去。 - Lionel
1个回答

10

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代码有什么问题,而不是乱搞弃用的功能。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接