我無法讓 OpenGL(使用 GLFW)將內容渲染到螢屏上。我什至無法設定清晰的顏色并在我運行我的應用程式時顯示它,我只是一直顯示黑屏。
我已經在我的系統上安裝了必要的依賴項并設定了構建環境,以便我能夠成功編譯我的應用程式(和依賴項)而不會出錯。這是有問題的代碼片段...您會注意到大部分渲染代碼實際上已被注釋掉。現在只需要顯示我選擇的 Clear Color 來驗證一切設定是否正確:
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
//Include GLEW. Always include it before gl.h and glfw3.h, since it's a bit magic.
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
// Include GLM
#include <glm/glm.hpp>
#include <GL/glu.h>
#include<common/shader.h>
#include <iostream>
using namespace glm;
int main()
{
// Initialise GLFW
glewExperimental = true; // Needed for core profile
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global for simplicity)
window = glfwCreateWindow( 1024, 768, "Tutorial 02", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
//glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
//INIT VERTEX ARRAY OBJECT (VAO)...
//create Vertex Array Object (VAO)
GLuint VertexArrayID;
//Generate 1 buffer, put the resulting identifier in our Vertex array identifier.
glGenVertexArrays(1, &VertexArrayID);
//Bind the Vertex Array Object (VAO) associated with the specified identifier.
glBindVertexArray(VertexArrayID);
// Create an array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
//INIT VERTEX BUFFER OBJECT (VBO)...
// This will identify our vertex buffer
GLuint VertexBufferId;
// Generate 1 buffer, put the resulting identifier in VertexBufferId
glGenBuffers(1, &VertexBufferId);
//Bind the Vertex Buffer Object (VBO) associated with the specified identifier.
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferId);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
//Compile our Vertex and Fragment shaders into a shader program.
/**
GLuint programId = LoadShaders("../tutorial2-drawing-triangles/SimpleVertexShader.glsl","../tutorial2-drawing-triangles/SimpleFragmentShader.glsl");
if(programId == -1){
printf("An error occured whilst attempting to load one or more shaders. Exiting....");
exit(-1);
}
//glUseProgram(programId); //use our shader program
*/
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do{
// Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
glClearColor(8.0f, 0.0f, 0.0f, 0.3f);
//glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// DRAW OUR TRIANGE...
/**
glBindBuffer(GL_ARRAY_BUFFER, VertexBufferId);
glEnableVertexAttribArray(0); // 1st attribute buffer : vertices
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// plot the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0); //clean up attribute array
*/
// Swap buffers
glfwSwapBuffers(window);
//poll for and process events.
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
}
同樣,就 OpenGL 而言,非常直接,所有渲染邏輯、著色器加載等都已被注釋掉,我只是想設定一個清晰的顏色并顯示它以確保我的環境配置正確。為了構建應用程式,我使用帶有自定義 CMAKE 檔案的 QTCreator。如果您認為它可能有助于確定問題,我可以發布 make 檔案。
uj5u.com熱心網友回復:
所以我設法解決了這個問題。我將嘗試簡潔地概述問題的根源以及我如何得出解決方案,希望對遇到相同問題的其他人有用:
簡而言之,問題的根源是驅動程式問題,我忽略了我實際上是在 MacBook Pro(帶有專用顯卡)上的 Ubuntu Mate 18.0 VM(通過 Parallels 16)中運行 OpenGL,這就是問題所在;直到最近,Parallels 和 Ubuntu 都不支持更現代的 OpenGL 3.3 及更高版本。我通過將以下幾行添加到發布的代碼中以強制特定的 OpenGL 版本來發現這一點:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
執行此操作后,應用程式立即開始崩潰并glGetError()報告我需要降級到早期版本的 OpenGL,因為 3.3 與我的系統不兼容。
解決方案有兩個:
- 將 Parallels 更新到版本 17,它現在包含一個能夠運行 OpenGL 3.3 代碼的專用第三方虛擬 GPU ( virGL )。
- 更新 Ubuntu 或至少更新內核,因為 virGL 僅適用于 5.10 及更高版本的 linux 內核版本。(Ubuntu Mate 18 僅隨內核版本 5.04 提供。)
就是這樣,按照描述進行更改,使我能夠完全按照發布的方式運行代碼并成功地將基本三角形渲染到螢屏上。

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/381139.html
