You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OpenGL 4是否存在默认着色器?如何查看驱动默认行为?

未绑定着色器却能渲染三角形的OpenGL驱动行为疑问

使用环境

  • OpenGL 4
  • NVIDIA显卡(寻求通用解释/方法)

定义顶点属性数据后,未传入任何着色器,程序运行时仍能显示三角形,相关核心代码如下:

DK_Vector2 positions[3] =
{
    DK_Vector2 { -0.5f, -0.5f },
    DK_Vector2 { 0.0f, 0.5f },
    DK_Vector2 { 0.5f, -0.5f },
};

unsigned int buffer;

glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(DK_Vector2), DK_Vector2::offsetX());

我有以下疑问:

  • 这应该是显卡驱动的默认行为?有没有方法查看驱动的这类默认行为细节?哪怕普通开发者难以理解也没关系。
  • 看到有人说OpenGL不存在默认着色器,但实际运行效果却像是存在,对此感到困惑。

额外说明:项目还使用了

  • GLFW(负责窗口创建)
  • GLEW(管理OpenGL函数)

可复现示例(结果可能因环境不同而异)

Application.cpp

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "Vector2.h";

int main(void)
{
    GLFWwindow* window;

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

    /* 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);

    /* Initialize GLEW*/
    if (glewInit() != GLEW_OK)
        std::cout << "Main: GLEW Initialization failed!" << std::endl;

    std::cout << glGetString(GL_VERSION) << std::endl;

    DK_Vector2 positions[3] =
    {
        DK_Vector2 { -0.5f, -0.5f },
        DK_Vector2 { 0.0f, 0.5f },
        DK_Vector2 { 0.5f, -0.5f },
    };

    unsigned int buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), positions, GL_STATIC_DRAW);

    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(DK_Vector2), DK_Vector2::offsetX());


    /* 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;
}

Vector2.cpp

#include "Vector2.h";

void* DK_Vector2::offsetX(){
    return (void*) &((DK_Vector2*)  nullptr)->x;
}

void* DK_Vector2::offsetY(){
    return (void*) &((DK_Vector2*)  nullptr)->y;
}

Vector2.h

#pragma once
struct DK_Vector2
{
    float x, y;

    static void* offsetX();
    static void* offsetY();
};

核心问题

  1. 这种未传入着色器却能成功渲染的情况,是驱动的默认行为还是真的存在“默认着色器”?
  2. 如何查看这类驱动默认行为的具体细节?

内容的提问来源于stack exchange,提问作者Doku

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 16:09:52