c++ - 我需要为 OpenGL 3.3 Core 使用着色器吗?

标签 c++ opengl glfw glew glm-math

我正在尝试制作一个三角形,我正在使用 GLEW、GLFW 和 GLM 作为 OpenGL 的扩展。

这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <gl\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
using namespace glm;

int main()
{
    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);

    GLuint VertexArrayID;
    glGenVertexArrays(1, &VertexArrayID);
    glBindVertexArray(VertexArrayID);

    // An array of 3 vectors which represents 3 vertices
    static const GLfloat g_vertex_buffer_data[] = {
        -1.0f, -1.0f, 0.0f,
        1.0f, -1.0f, 0.0f,
        0.0f,  1.0f, 0.0f,
    };

    // This will identify our vertex buffer
    GLuint vertexbuffer;
    // Generate 1 buffer, put the resulting identifier in vertexbuffer
    glGenBuffers(1, &vertexbuffer);
    // The following commands will talk about our 'vertexbuffer' buffer
    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    // Give our vertices to OpenGL.
    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);


    do {
        // 1rst attribute buffer : vertices
        glEnableVertexAttribArray(0);
        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
        glVertexAttribPointer(
            0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
            3,                  // size
            GL_FLOAT,           // type
            GL_FALSE,           // normalized?
            0,                  // stride
            (void*)0            // array buffer offset
        );
        // Draw the triangle !
        glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
        glDisableVertexAttribArray(0);

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

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

编辑

代码不显示白色三角形,它只是打开窗口并显示空的黑色。 同样,起初我添加了我弄乱的代码,它缺少部分代码。但是它仍然不会产生白色三角形。

最佳答案

Do I need to use a shader for OpenGL 3.3?

您需要使用着色器,否则您不会看到任何东西。如果您确实看到了某些东西,那就是未指定的行为。这在很大程度上取决于每个单独的驱动程序。


您可以尝试摆弄清晰的颜色并清除屏幕:

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

教程能够在黑色背景上得到一个白色三角形。在我的电脑上情况正好相反。

教程还指出:

If you’re on lucky, you can see the result (don’t panic if you don’t)

所以请阅读本教程的其余部分,它还包括一个着色器。

关于c++ - 我需要为 OpenGL 3.3 Core 使用着色器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43169814/

相关文章:

c++ - 将由 header 和 cpp 文件组成的源文件“编译”为一个巨大的 cpp 文件

opengl - glSwapBuffers 在哪里定义?

c++ - 如何使用 OpenGL 将体积数据渲染成 3D 纹理?

c++ - 设置不对称截锥体

opengl - 为什么在调用 glfwWindowHint() 后,glewInit() 会导致 GL_INVALID_ENUM

c++ - View 或透视矩阵干扰剔除

c++ - 生成随机 boolean 值

依赖于编译器特定代理的 C++11 库功能

c++ - 使用 glOrtho() 时 gluLookAt() 不起作用?

c++ - 读取多个文件,并为每个文件保留一组数据。