gpt4 book ai didi

c++ - OpenGL程序没有显示三角形

转载 作者:行者123 更新时间:2023-12-02 09:53:47 25 4
gpt4 key购买 nike

我正在使用C++中的OpenGL教程,由于某种原因,完全相同的代码对我不起作用。
它应该显示一个三角形,但不显示任何内容。只是一个空白屏幕。
这是代码:

#include<iostream>

#define GLEW_STATIC
#include"GL/glew.h"

#include"GLFW/glfw3.h"

static unsigned int compileShader(unsigned int type, const std::string& shader)
{
unsigned int id = glCreateShader(type);
const char* src = shader.c_str();
glShaderSource(id, 1, &src, NULL);
glCompileShader(id);

int result;
glGetShaderiv(id, GL_COMPILE_STATUS, &result);
if (!result) {
char log[512];
glGetShaderInfoLog(id, 512, NULL, log);
std::cout << "Compiling shader error:\n" << log << std::endl;
}

return id;
}

static unsigned int createShader(const std::string& vertexShader, const std::string& fragmentShader)
{
unsigned int program = glCreateProgram();
unsigned int vs = compileShader(GL_VERTEX_SHADER, vertexShader);
unsigned int fs = compileShader(GL_FRAGMENT_SHADER, fragmentShader);

glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);

int result;
glGetProgramiv(program, GL_LINK_STATUS, &result);
if (!result) {
char log[512];
glGetProgramInfoLog(program, 512, NULL, log);
std::cout << "linking shader error:\n" << log << std::endl;
}

glValidateProgram(program);

glDeleteShader(vs);
glDeleteShader(fs);

return program;
}

void keyCallBack(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}

int main()
{
int width = 0, height = 0;

glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

GLFWwindow* window = glfwCreateWindow(500, 500, "Test", nullptr, nullptr);

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);

glfwSetKeyCallback(window, keyCallBack);

float triangle[] = {
-0.5f, -0.5f,
0.0f, 0.5f,
0.5f, -0.5f
};

GLuint buffer = 0;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
glEnableVertexAttribArray(0);

std::string vertexShader = "#version 330 core\n"
"layout (location = 0) in vec4 position;\n"
"void main()\n"
"{\n"
"gl_Position = position;\n"
"}\n";

std::string fragmentShader = "#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
"color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n";

unsigned int program = createShader(vertexShader, fragmentShader);
glUseProgram(program);

while(!glfwWindowShouldClose(window)) {

glfwPollEvents();

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

glDrawArrays(GL_TRIANGLES, 0, 3);

glfwSwapBuffers(window);
}

glfwTerminate();
return 0;
}

我真的不明白为什么它不起作用。三角形的位置看起来正确。 glVertexAttribPointer看起来也正确。我在着色器及其源代码的编译和链接中找不到任何错误。
编译和链接它们时,我也得到零错误。

最佳答案

该问题是由GLFW_OPENGL_CORE_PROFILE引起的。如果使用核心配置文件OpenGL Context,则必须使用命名的Vertex Array Object,因为默认的VAO(0)无效。

使用兼容性配置文件:

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

或在指定顶点属性数据数组之前创建VAO:

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

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

关于c++ - OpenGL程序没有显示三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62101117/

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