本文介绍了为什么在带有OpenGL核心配置文件的窗口上看不到我的红色三角形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码应在带有OpenGL兼容性配置文件的窗口上显示一个红色三角形,但除了空白的窗口核心配置文件外,我什么也看不到:

The following code should displays a red triangle on the window with OpenGL compatibility profile, but I can see nothing but a blank window core profile:

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

unsigned int shader_create(const char* src_vertex, const char* src_fragment) {
    ...
}

int main(void)
{
    GLFWwindow* window;

    /* Initialize the library */
    if (!glfwInit()) return -1;

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK) return -1;

    float coordinates[6] = {
        -0.5f, -0.5f,
        +0.0f, +0.5f,
        +0.5f, -0.5f
    };

    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

    const char* vertex_shader   = ... ;
    const char* fragment_shader = ... ;

    unsigned int shader = shader_create(vertex_shader, fragment_shader);
    glUseProgram(shader);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
        glClear(GL_COLOR_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

顶点着色器:

#version 330 core
layout(location = 0) in vec4 position;
void main() {
    gl_Position = position;
}

片段着色器:

#version 330 core
layout(location = 0) out vec4 color;
void main() {
    color = vec4(1.0, 0.0, 0.0, 1.0);
}

当我不使用着色器时(不是一个好主意),我可以完美地看到默认白色的三角形(至少在我的GPU上).同样,如果使用兼容性配置文件而不是核心配置文件,它也可以工作.

When I use no shaders (not a good idea), I can perfectly see the triangle in the default white color (at least on my GPU). Also if use compatibility profile instead of core profile, it works.

我试图同时使用C和C ++代码进行编译,但都无法显示三角形.

I tried to compile as both C and C++ code but both fails to show the triangle.

我在这里做错了什么?着色器的源代码不正确吗?

What am I doing wrong here? Is the source code for shader incorrect?

推荐答案

使用核心配置文件时 OpenGL上下文,您必须生成并命名为顶点数组对象,因为默认的顶点数组对象(0)"无效.

When you use a core profile OpenGL Context, you've to generate and named Vertex Array Object, because the default Vertex Array Object (0) is not valid.

切换到兼容性配置文件:

Either switch to a compatibility profile:

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

或为顶点规范创建一个命名的VAO:

or create a named VAO for the vertex specification:

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

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);

这篇关于为什么在带有OpenGL核心配置文件的窗口上看不到我的红色三角形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 15:57