gpt4 book ai didi

c++ - 如何在 debian 下正确链接 opengl 程序?

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:18 27 4
gpt4 key购买 nike

为了学习如何使用 opengl,我有一个极简构建系统和一个玩具程序。我的 makefile 生成这些命令:

g++ -O1 -std=c++17 -Wall -Wextra -pedantic   -c -o main.o main.cpp
g++ main.o -lglfw -lGLEW -lGL -o main

在该目录中,我有一个单独的 main.cpp 文件。与这里关于 SO 的许多问题相反,这确实有效。 编译和链接没有问题。但是,当我运行该程序时,它只显示我用 glClear 编写的背景颜色,而不是我的测试三角形

我已经验证这不是我的代码的问题,因为如果我使用我在教程中找到的(相当复杂和臃肿的)构建系统编译它似乎可以工作。但是,我想了解如何自己实际构建一个 opengl 程序。我怀疑我缺少一些库或其他东西,但我希望程序崩溃,而不是(几乎)什么都不做。

我正在运行最新的 debian。

怎么会编译链接完美,却不能正常运行呢?


免责声明:我对 opengl 和窗口应用程序完全陌生。我不是 100% 确定我为什么需要 glew。

代码:

#include <iostream>

#include <GL/glew.h>

#include <GLFW/glfw3.h>

bool install_shader(GLuint where, std::string const& code, GLuint* attach_to) {
GLuint shader_id = glCreateShader(where);
{
auto const p = code.c_str();
glShaderSource(shader_id,1,&p,nullptr); // nullptr indicates null-termination here
}
glCompileShader(shader_id);
GLint out;
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &out);
if (out == GL_FALSE) {
// something went wrong with the shader compilation
glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &out);
if (out == 0) {
std::cout << "Unknown Error during shader compilation\n";
} else {
std::string error;
error.resize(out-1);
glGetShaderInfoLog(shader_id, out, nullptr, error.data());
std::cout << "Shader Compilation failed with error: " << error;
}
return false;
} else {
std::cout << "shader(" << int(shader_id) << "@" << int(where) << ") compiled fine\n";

if (attach_to) {
glAttachShader(*attach_to, shader_id);
}

// XXX THE SHADERS SHOULD BE DELETED AFTER LINKING !!!!
// glDeleteShader(shader_id)

return true;
}
}

bool install_program(GLuint program_id) {
glLinkProgram(program_id);
GLint out;
glGetProgramiv(program_id, GL_LINK_STATUS, &out);
if (out == GL_FALSE) {
// something went wrong with the program linking
glGetProgramiv(program_id, GL_INFO_LOG_LENGTH, &out);
if (out == 0) {
std::cout << "Unknown link-Error in shader program\n";
} else {
std::string error;
error.resize(out-1);
glGetProgramInfoLog(program_id, out, nullptr, error.data());
std::cout << "Program linking failed with error: " << error;
}
return false;
} else {
std::cout << "program(" << int(program_id) << ") compiled fine\n";
return true;
}
}

int main() {
if (glfwInit()) {
std::cout << "Initialisation fine\n";
} else {
std::cout << "Initialisation failed\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);

if (GLFWwindow* window = glfwCreateWindow(800,600,"testwindow",nullptr,nullptr)) {
glfwMakeContextCurrent(window);

if (glewInit() == GLEW_OK) {
std::cout << "GLEW also fine\n";
} else {
std::cout << "GLEW says nope!\n";
goto die;
return 2;
}

//std::cout << "Ensure we can capture the escape key being pressed below...\n";
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
/*business*/
// (1) specify the triangle to draw
// An array of 3 vectors which represents 3 vertices
GLfloat g_vertex_buffer_data[] = {
-0.4f, -0.8f, 0.f,
0.4f, -0.8f, 0.f,
0.0f, 0.8f, 0.f,
};
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);

GLuint program_id = glCreateProgram();

// (3) vertex shader
std::string vshader_code = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\n";

if (!install_shader(GL_VERTEX_SHADER,vshader_code,&program_id)) {
goto die;
}

std::string fshader_code = "#version 330 core\n"
"out vec3 color;\n"
"\n"
"void main()\n"
"{\n"
" color = vec3(0.9,0.8,0.1);\n"
"}\n";

if (!install_shader(GL_FRAGMENT_SHADER,fshader_code,&program_id)) {
goto die;
}

if (!install_program(program_id)) {
goto die;
}

glClearColor(0.3, 0.5, 0.5, 1.0);

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

do {
glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(program_id);


glEnableVertexAttribArray(0); // this 0 also refes to 'layout (location = 0)' in the vertex shader

glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

glVertexAttribPointer(
0, // this refes to 'layout (location = 0)' in the vertex shader
3, // each vertex has 3 coordinates (2d data is also possible)
GL_FLOAT, // coordinates are of type GLfloat (GL_FLOAT)
GL_FALSE, // we don't need the input data to be nomalised,
0, //&g_vertex_buffer_data[3] - &g_vertex_buffer_data[0], // stride
nullptr // offset ptr
);

// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);

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

} else {
std::cout << "Could not create window\n";
}

die:
glfwTerminate();
std::cout << "Terminated\n";
}

最佳答案

您需要有一个 Vertex Array Object ,并绑定(bind)它来存储绘制三角形所需的状态:

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

// your remaining code
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
// ...

The compatibility OpenGL profile makes VAO object 0 a default object. The core OpenGL profile makes VAO object 0 not an object at all. So if VAO 0 is bound in the core profile, you should not call any function that modifies VAO state. This includes binding the GL_ELEMENT_ARRAY_BUFFER with glBindBuffer.

根据配置文件和/或驱动程序,可能会有一个事件的默认 VAO,这将导致渲染小三角。但是要有一个标准的提示设置,你需要为你想要渲染的对象创建一个 VAO。

这并不能真正解释为什么它与捆绑的 GLFW 和 GLEW 一起工作,但也许其中之一会创建一个默认的 VAO 来模仿兼容性配置文件行为或 NVIDIA 驱动程序的启动。

您可以使用该代码检查您是否处于核心配置文件或兼容性配置文件中:

GLint prof;
glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &prof);
std::cout << (prof&GL_CONTEXT_CORE_PROFILE_BIT) << " " << (prof&GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) << std::endl;

关于c++ - 如何在 debian 下正确链接 opengl 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59487230/

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