gpt4 book ai didi

c - 为什么核心配置文件不起作用?

转载 作者:行者123 更新时间:2023-11-30 14:56:20 25 4
gpt4 key购买 nike

此代码在兼容性配置文件上下文版本 4.3 中运行良好,但当我切换到核心时,没有绘制任何内容,即使我使用的所有 GL 函数都包含在核心配置文件中。这是代码的主要部分

    glClearColor(0.f, 0.f, 0.f, 0.f);
glClearDepth(-1.d);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_GREATER);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
vs= glCreateShader(GL_VERTEX_SHADER);
fs= glCreateShader(GL_FRAGMENT_SHADER);
program= glCreateProgram();

const char* vertex_shader_text=
"#version 430 core\n"
"in vec2 vPos;"
"in vec3 vCol;"
"out vec3 colour;"
"void main() {"
"colour = vCol;"
"gl_Position = vec4(vPos, 1.0, 1.0);"
"}";
const char* fragment_shader_text=
"#version 430 core\n"
"in vec3 colour;"
"out vec4 frag_colour;"
"out vec4 frag_colour2;"
"void main() {"
"frag_colour = vec4(1.0, 1.0, 1.0, 0.0);"
"frag_colour2 = vec4(colour, 0.0);"
"}";
glShaderSource(vs, 1, &vertex_shader_text, NULL);
glShaderSource(fs, 1, &fragment_shader_text, NULL);
glCompileShader(vs);
glCompileShader(fs);
glAttachShader(program, vs);
glAttachShader(program, fs);

glBindAttribLocation(program,0,"vPos");
glBindAttribLocation(program,1,"vCol");
glBindFragDataLocation(program,0,"frag_colour2");

glLinkProgram(program);
glUseProgram(program);

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

float vertices[] =
{
-1.f, 1.f, 1.f, 0.f, 0.f,
3.f-2.f*(float)sqrt(3.d), -1.f, 0.f, 1.f, 0.f,
1.f, 2.f*(float)sqrt(3.d)-3.f, 0.f, 0.f, 1.f
};
GLuint vertex_buffer = 0;
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*) 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (void*) (sizeof(float) * 2));

A:
if (GetMessage(&msg, NULL, 0, 0) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else return TRUE;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES,0,3);
SwapBuffers(ghDC2);
goto A;

最佳答案

确保您创建并绑定(bind) vertex array object (VAO)在设置顶点属性之前:

A Vertex Array Object (VAO) is an OpenGL Object that stores all of the state needed to supply vertex data (with one minor exception noted below). It stores the format of the vertex data as well as the Buffer Objects (see below) providing the vertex data arrays. Note that VAOs do not copy, freeze or store the contents of the referenced buffers - if you change any of the data in the buffers referenced by an existing VAO, those changes will be seen by users of the VAO.

VAO 在核心环境中是必需,但在兼容性中是可选的。

关于c - 为什么核心配置文件不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44786351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com